How to read serial port data without no terminator?

8 views (last 30 days)
I am trying to send commands to a serial port device and read its response using "writeread". However, the response from the device does not have terminators, which results in a timeout error. How can I let the function return whatever it reads before timeout and ignore the timeout error?
  1 Comment
chrisw23
chrisw23 on 27 Sep 2022
You have to know the min count of response bytes at least to
Set the Ports property ReceivedBytesThreshold and register for the DataReceived event. Read the buffer in the callback function. (some example lines, copied from a class)
% obj.thisPort = System.IO.Ports.SerialPort(portName,baudRate)
obj.thisPort.ReceivedBytesThreshold = <n> % event trigger count
addlistener(obj.thisPort,'DataReceived',@obj.DataReceived_Callback)
function DataReceived_Callback(obj,~,event)
bytesToRead = obj.thisPort.BytesToRead; % get real byte count to read
...
end

Sign in to comment.

Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!