2021a Serial Port not reading correctly

3 vues (au cours des 30 derniers jours)
Felicity Freeman
Felicity Freeman le 21 Mai 2021
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) %allow device to boot
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) %allows the device to boot up after connection
flush(DataFlow);
pause(1) %allow the buffer to get sufficient data
A=DataFlow.NumBytesAvailable
X=read(DataFlow,10,"uint16")
Can you advise what I am doing wrong in the 2021a set up please?
Thanks,
Felicity
  1 commentaire
Felicity Freeman
Felicity Freeman le 24 Mai 2021
Hi,
Can anyone help me on this please? I can't progress with this project unless I can resolve this issue.
Thanks,
Felicity

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Data Acquisition Toolbox Supported Hardware dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by