-1

我的代码给出了一个错误,即索引超出了矩阵维度。它在 nsample=10 时运行得很好,但是当我使用 nsamples=other 值时它会给出错误。

错误从第 108 行开始,这意味着当 dataright(1:20) = output(1:20);但它在我使用 dataright(1:10) = output(1:10) 时运行。

function icall(block)
setup(block);
function setup(block)
block.NumInputPorts = 0;
block.NumOutputPorts = 4;
block.OutputPort(1).SamplingMode = 'sample';
%% Setup functional port to default
block.SetPreCompPortInfoToDefaults;
%% Setup output port

block.SampleTimes = [0 1];

block.SimStateCompliance = 'DefaultSimState';

block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('Outputs', @Outputs); % Required
block.RegBlockMethod('Update', @Update);
block.RegBlockMethod('Terminate', @Terminate); % Required
block.RegBlockMethod('SetInputPortSamplingMode', @SetInpPortFrameData);
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
function DoPostPropSetup(block)
% Setup Dwork
block.NumDworks = 4;
block.Dwork(1).Name = 'Nothing';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0;
block.Dwork(1).Complexity = 'Real';
block.Dwork(1).UsedAsDiscState = false;
block.Dwork(2).Name = 'Nothing1';
block.Dwork(2).Dimensions = 1;
block.Dwork(2).DatatypeID = 0;
block.Dwork(2).Complexity = 'Real';
block.Dwork(2).UsedAsDiscState = false;
block.Dwork(3).Name = 'Nothing2';
block.Dwork(3).Dimensions = 1;
block.Dwork(3).DatatypeID = 0;
block.Dwork(3).Complexity = 'Real';
block.Dwork(3).UsedAsDiscState = false;
block.Dwork(4).Name = 'Nothing3';
block.Dwork(4).Dimensions = 1;
block.Dwork(4).DatatypeID = 0;
block.Dwork(4).Complexity = 'Real';
block.Dwork(4).UsedAsDiscState = false;

%endfunction

%endfunction

function Start(block)

fullpathToDll = 'Z:\Farha\2015_02_26_photometer_Datenlogger\PC-Software\CGMultChan.dll';
fullpathToHeader = 'Z:\Farha\2015_02_26_photometer_Datenlogger\PC-Software\CGMultChan.h';
fullpathToHeader;
fullpathToDll;
loadlibrary(fullpathToDll, fullpathToHeader);
libfunctions ('CGMultChan');
delete(instrfindall);
serialPort = 'COM1';
IP = calllib('CGMultChan', 'CGMultChan_Connect', '192.168.100.158'); %Connect to Data Loger
if (IP == 0)
end
out = instrfind('Port', 'COM1');
%Open Serial COM Port
s = serial(serialPort);
set(s, 'BaudRate', 57600, 'DataBits', 8, 'Parity', 'none', 'StopBits', 1, 'FlowControl', 'none', 'Terminator', 'CR');
set(s, 'Timeout', 20);
fopen(s);
TI_ms = calllib('CGMultChan', 'CGMultChan_SetIntTime', .02 * 100);
tic

block.Dwork(1).Data = 1;
block.Dwork(2).Data = 2;
block.Dwork(3).Data = 3;
block.Dwork(4).Data = 5;

function Outputs(block)

block.OutputPort(1).Data = block.Dwork(1).Data; %+ %block.InputPort(1).Data;
block.OutputPort(2).Data = block.Dwork(2).Data;
block.OutputPort(3).Data = block.Dwork(3).Data;
block.OutputPort(4).Data = block.Dwork(4).Data;

count = 0;
function Update(block)
nsamples = 20
count = 0
while (count < nsamples)

    BufferSize = 16;
    pBuffer = libpointer('singlePtr', zeros(BufferSize, 1));
    data2 = calllib('CGMultChan', 'CGMultChan_MeasureAll', pBuffer); %Measurement
    output = pBuffer.Value

    count = count + 1;
    time(count) = toc; %Extract Elapsed Time
    dataright(count) = output(1)
    dataup(count) = output(2)
    databack(count) = output(3)
    datafront(count) = output(5)
end
dataright(1:20) = output(1:20);
dataup(1:20) = output(1:20);
databack(1:20) = output(1:20);
datafront(1:20) = output(1:20);
block.Dwork(1).Data = double(dataright(1));
block.Dwork(2).Data = double(dataup(2));
block.Dwork(3).Data = double(databack(3));
block.Dwork(4).Data = double(datafront(5));

%end Update
function SetInpPortFrameData(block, idx, fd)
block.InputPort(idx).SamplingMode = fd;
for i = 1:block.NumOutputPorts
    block.OutputPort(i).SamplingMode = fd;
end
%%
function Terminate(block)
serialPort = 'COM1';
s = serial(serialPort);

fclose(s);
clear pBuffer;
calllib('CGMultChan', 'CGMultChan_Disconnect'); %Disconnect from Data Logger
4

1 回答 1

1

这是生成 simulink 模块的 Level 2 S 功能代码。它将数据记录器与计算机连接起来。这意味着它是一个面向对象的代码。我解决了更改缓冲区大小 pbuffer=500 的问题。从而增加了数据输出的矩阵大小。

于 2016-06-30T07:50:24.040 回答