Multiple Serial Callback executions

5 views (last 30 days)
P. Chatz
P. Chatz on 28 Apr 2015
Hello,
I have been working on a UI for some serial devices. The protocol is packet based.Every packet i receive consists of a header( 3 bytes) in which the length of the packet is defined. depending on this i read the rest bytes.
For this i use the BytesAvailable callback with a callback function in a separate mfile. Here is a snippet
Inside the callback of a button
handles.s = serial('COM14');
set(handles.s,'BaudRate',115200,'BytesAvailableFcnMode','byte',...
'BytesAvailableFcnCount',3);
set(handles.s,'BytesAvailableFcn', {@UI_DataCallback_Monitor,handles});
Inside the callback function
function UI_DataCallback_Monitor(obj,event,handles)
header = fread(obj,3,'uint8');
length=Uint8toUint16(header(2),header(3),0);
data = fread(obj,length,'uint8');
checksum=fread(obj,1,'uint8');
The problem is that as it seems multiple callbacks are being executed for every packet and results in timeout and errors.. I only want the callback to be executed once for each packet. Any suggestions?
Thank you

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!