Serialport terminator callback function not triggering

9 visualizaciones (últimos 30 días)
Tasman de Pury
Tasman de Pury el 16 de Oct. de 2022
Respondida: Nivedita el 8 de Sept. de 2023
I have a callback function setup to process incoming serialport data coming from an arduino running GRBL. This is all running inside an app designer GUI.
app.s = serialport(app.CommPortDropDown.Value,115200);
%GRBL sends CR/LF as terminator, expects CR in return
%setup callback to receive data
configureCallback(app.s, "terminator", @(src, evt) receiveSerial(app, src, evt));
%callback function
function receiveSerial(app, src, evt)
grbl_out = readline(app.s).strip;
This setup seems to work 99% of the time and receives all the data that I expect. But sometimes there are bytes available to read in the input buffer (shown by s.NumBytesAvailable>0) and when checking the data manually via the command line, they are full lines with correct terminators. Yet for some reason the callback function is not triggered.
I have noticed this occurs more often when lots of data is being sent to MATLAB in a short period of time. (Specifically when the $$ command is sent to GRBL to receive a print out of all the settings.)
%command line output of manually checking serialport once callback stopped
K>> app.s.NumBytesAvailable
ans =
K>> readline(app.s)
ans =
K>> readline(app.s)
ans =

Respuestas (1)

Nivedita el 8 de Sept. de 2023
Hi Tasman,
I understand that you are looking for a way to read all the incoming serialport data.
A possible solution is to modify your callback function to handle multiple lines of data at once by using the “read” function, rather than relying on the “readline” function to read one line at a time.
You can try modifying your code to accommodate your requirements by referring to the below pseudo code:
%updated callback function
function receiveSerial(app, src, evt)
% Check if there are bytes available in the input buffer
if app.s.NumBytesAvailable > 0
% Read all available bytes
data = read(app.s, app.s.NumBytesAvailable, "char");
% Split the data into separate lines
lines = strsplit(data, ["\r", "\n"], 'CollapseDelimiters', true);
% Process each line separately
for i = 1:numel(lines)
line = strtrim(lines{i});
if ~isempty(line)
% Process the line of data
We are first reading all the available bytes from the input buffer as a character array. Then using the “strsplit” function the data is being split into different lines. Each line is then processed individually, and any leading or trailing whitespace is removed using “strtrim”.
For more information on the different functions used in the code, you can refer to the following documentation links provided below:
  1. read: Read data from serial port - MATLAB read
  2. strsplit: Split string or character vector at specified delimiter - MATLAB strsplit
  3. strtrim: Remove leading and trailing whitespace from strings - MATLAB strtrim
  4. serialport.NumBytesAvailable: NumBytesAvailable property of serialport object
I hope this helps!




Community Treasure Hunt

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

Start Hunting!

Translated by