attaching callback function to a serialdev object.

3 views (last 30 days)
I've got a raspberry pi, that control a CNC machine. this machine is running on a version of GRBL for arduino mega.
I can connect to the pi with no issues, and can connect to the GRBL to write\read data to\from.
but, I can't figure out how to attach a callback function that will read in real time data coming from the serial port.
if I'm defining the serial as 'serialdev' object, using the "configureCallback" function returns with: Undefined function 'configureCallback' for input arguments of type 'raspi.internal.SerialDev'.
and I can't define it as 'serialport' object, because the port I'm supposed to enter as an argument, is on a different device...
any help would be very much appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 30 May 2023
Callbacks are not supported for serialdev() objects.
Note that this serial port lives on the pi and is for connecting an external device to the pi, not for connecting the pi to the MATLAB host. The request to read from the serialdev() using read() is relayed to the pi and the pi handles the reading into a buffer and then forwards the content to the MATLAB session; likewise when you write(), the buffer-full is sent from the MATLAB host to the pi and the monitor program on the pi is responsible for sending the data out.
Therefore, you cannot use this mechanism to read from the serialdev port in real time -- because it is all getting buffered at the pi.
This is a read-on-demand mechanism, not "reads continually in the background relaying bytes to a buffer on the MATLAB side."
It pretty much follows that if you need high speed processing that you should be writing code that execute on the Pi .
The trade-offs probably change if you use MATLAB Coder to deploy the serialdev() code to the Pi itself; in that case there would be no need of relaying back to the MATLAB host.
  1 Comment
Golan Ramati
Golan Ramati on 30 May 2023
thanks for the reply!
unfortunately it makes sense that it's working this way. I guess my best option is to use timers to make pull request. that will make the program more complicated, but I don't really see another way.
if you have an alternative solution I'll be happy to hear about it.

Sign in to comment.

More Answers (0)


Find more on Simulink Support Package for Raspberry Pi Hardware 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!