How to call function with BytesAvailableFcn?
Show older comments
How to call function with BytesAvailableFcn
I’m testing a serial loopback function using a COM bridge (COM5 and 6).
I want to write to COM5 and receive at COM 6. I would like to display the COM 6 data as soon it is ready.
I would like to use some sort of interrupt function to get the data ASAP.
Setting up the receiver serial object with:
set(S2, 'Terminator', 'CR');
set(S2, 'BytesAvailableFcnMode','Terminator');
set(S2, 'BytesAvailableFcn', @instrcallback);
This should call a function ”instrcallback” when CR is received!
My instrcallback function has only instrcallback(S2) as a input parameter and will not call this function.
? How do I declare my function properly?
Acc Mathworks helpsite “Events and Callbacks” they state the different event parameters to the callback function.
?How do I use them with respect to my task – all of them refer to a timestamp?
S1 = serial('COM5'); % create serial object
set(S1, 'BaudRate', 9600); % set BaudRate to 9600
set(S1, 'Parity', 'none'); % set Parity Bit to None
set(S1, 'DataBits', 8); % set DataBits to 8
set(S1, 'StopBit', 1); % set StopBit to 1
set(S1, 'Terminator', 'CR');
set(S1, 'BytesAvailableFcnCount',10);
set(S1,'InputBufferSize',128);
set(S1,'OutputBufferSize',128);
fopen(S1); % open serial
%
%----------------------------------------------------------
%
S2 = serial('COM6'); % create serial object
set(S2, 'BaudRate', 9600); % set BaudRate to 9600
set(S2, 'Parity', 'none'); % set Parity Bit to None
set(S2, 'DataBits', 8); % set DataBits to 8
set(S2, 'StopBit', 1); % set StopBit to 1
set(S2, 'Terminator', 'CR');
set(S2,'InputBufferSize',128);
set(S2,'OutputBufferSize',128);
set(S2, 'BytesAvailableFcnCount',32);
set(S2, 'BytesAvailableFcnMode','Terminator');
set(S2, 'BytesAvailableFcn', @instrcallback);
fopen(S2); % open serial
Y=5;
z=1024;
out=1000;
a=1;
while Y~=0 %sending var z 5 times
fprintf(S1,'%4.0f \n',z); % send command to serial
pause(0.2); % delay to wait for COM6
z=z-1;
Y=Y-1;
%S2.BytesAvailable % test that inputbuffer is incrementing OK
end
fclose(S1);
fclose(S2);
%---------------------------------------------------------
%fCallback function ------------- display var z on screen
function[]= instrcallback(S2)
out = fscanf(S2,'4.0%f');
disp('out')
%fprintf('Test Var: %4.0f \r\n',out);
%flushinput(S2) test
end
% -----------
BR. Benjamin
Answers (0)
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!