MATLAB: BytesAvailable doesnt empty Buffer completely

bufferbytesavailablefcnserial

Hello,
I have Problems reading all Data from the Buffer with BytesAvailableFcn. When Data is put in the buffer very fast, I have Data left in the Buffer but the BytesAvailableFcn doesnt trigger.
I programmed a Gui in Matlab which reads data froma an Arduino. The Arduino sends blocks of 8 Bytes. The BytesAvailableFcn is configured this way:
handles.serialIn = serial(COM3,'BaudRate',115200);
set(handles.serialIn,'BytesAvailableFcnMode','byte');
set(handles.serialIn,'BytesAvailableFcnCount',8);
fopen(handles.serialIn);
set(handles.serialIn,'BytesAvailableFcn', {@mycallback,hObject});
When 8 Bytes are available the function mycallback reads 8 Bytes and processes them.
function mycallback(obj,event,hObject)
inByte = fread(obj,8,'char');
% process inByte
When the Arduino stops sending Data and I controll the Buffer manually, most of the time there ist still Data in there (more than 8 Byte).
It would be nice if someone could help me here.
Thanks,
Alex

Best Answer

Your callback can look at the BytesAvailable property to find out how many bytes are present, possibly reading full groups of 8 bytes in one go, or looping in your callback until less than 8 bytes are available.