Serial callback in "byte" mode triggered even though no bytes are available to read
Show older comments
I am using a serial port object to control stepper motors like this:
sObject_motor = serialport('COM3',115200);
sObject_motor.Timeout = 2;
sObject_motor.ByteOrder = "little-endian";
configureCallback(sObject_motor,"byte",8,@FAS_serialReadCallbackFunction);
There should be always at least 8 bytes sent by the controller as a response to any command (assuming the communication is set up correctly).
According to the above the callback should be executed when there are at least 8 bytes in the buffer to read.
However I keep getting error like this (not always, but randomly):
Warning: Error occurred while executing the listener callback for event DataWritten defined for class asyncio.InputStream:
Error using FAS_serialReadCallbackFunction (line 5)
Expected input number 2, count, to be nonzero.
The line 5 in my callback function is just:
serialans = read(sObject,sObject.NumBytesAvailable,"uint8");
When I set up a break point at this line and peek at the NumBytesAvailable property indeed it shows that there are no bytes available to read.
But this function should be triggered only when there are at least 8 bytes availble to read!
This causes the communication to fail randomly.
I am using 'pause' command after issuing every command to the controler but still this does not seem right.
Any help or suggestions is much appreciated.
5 Comments
Walter Roberson
on 8 Feb 2021
is it possible that it was called when a timeout occurred?
SLV
on 9 Feb 2021
Humberto Fernández Álvarez
on 25 Feb 2021
I have the same problem. Any solution for it?
John
on 10 Aug 2026 at 20:11
I realize this is an old thread, but I may have an answer for you. The callback triggers are generated every time the triggering criteria are satisfied. So if you are triggering on 5 bytes and you receive 10, you get two invocations of your callback that get generated. If you read all 10 bytes in the first time through, then the NumBytesAvailable will be zero the next time around.
If you have fixed size packets, you won't have much of an issue. But if you get variable length packets, you may need to set the triggering numbe of bytes equal to the smallest packets you expect to see, which means you need to deal with the redundant callbacks when they occur.
To get around this I usually just do:
function my_serial_callback(serial_port, event)
%Serial callback for command serial port
if (~serial_port.NumBytesAvailable)
return; %Discard queued events handled earlier
end
%The rest of the code goes here.
Answers (0)
Categories
Find more on Serial and USB Communication 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!