Hi,
I am using Matlab to read data from a device using a serial port connection. The device is sending data continuously, and has an LCD screen showing the current value, updated every 0.5 seconds. The values are typically in the range 12000-13000. I have one function which connects to the device, and a separate function which reads data from the device into a variable, although the example code I've shown here is a subset of the overall program.
I was using v2019a originally, and was able to connect to the device successfully and read data from it. However, I was not able to flush the device buffer, so it always read from the start rather than taking the most recent value. I've attached some example code and output below, showing values in the range expected.
DataFlow=serial("COM3",'BaudRate',115200,'DataBits',8,'StopBits',1);
fopen(DataFlow);
pause(6)
for i=1:20
X=fscanf(DataFlow)
Z(i)=str2double(X)
end
I upgraded to v2021a so that I could flush the buffer before reading data. I am updating my code to reflect changes between the versions, but have noticed that it is no longer reading the serial data correctly. There are repeated values, and values outside the expected range. I've attached the example code and two sets of output below. The device was both digitally and physically (USB) disconnected between the two runs that generated the output data. Many of the output values are identical between the two runs, and some are outside the device output range.
DataFlow=serialport("COM3",115200,'DataBits',8,'StopBits',1);
pause(6)
flush(DataFlow);
pause(1)
A=DataFlow.NumBytesAvailable
X=read(DataFlow,10,"uint16")
Can you advise what I am doing wrong in the 2021a set up please?
Thanks,
Felicity