- read: Read data from serial port - MATLAB read
- strsplit: Split string or character vector at specified delimiter - MATLAB strsplit
- strtrim: Remove leading and trailing whitespace from strings - MATLAB strtrim
- serialport.NumBytesAvailable: NumBytesAvailable property of serialport object
Serialport terminator callback function not triggering
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
%connect
app.s = serialport(app.CommPortDropDown.Value,115200);
%GRBL sends CR/LF as terminator, expects CR in return
configureTerminator(app.s,"CR/LF","CR");
%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;
disp(grbl_out)
end
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
%triggering
K>> app.s.NumBytesAvailable
ans =
185
K>> readline(app.s)
ans =
"$31=0"
K>> readline(app.s)
ans =
"$32=0"
0 Kommentare
Antworten (1)
Nivedita
am 8 Sep. 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
disp(line);
end
end
end
end
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:
I hope this helps!
Regards,
Nivedita.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!