Why do I not see changes in readings from "serialport" objects in MATLAB despite verifying the data changed using other software?

3 views (last 30 days)
I have two devices that are connected to COM ports, which I am accessing in MATLAB using "serialport". I have confirmed that I can read and write commands to these devices, and have a script that plots the output readings of these devices. 
The first device reads the voltage of a battery using a DATAQ DI 1100. I noticed that when unplugging the battery from power, my plots seem to stay at the same value as the last one that was read. The expected behavior here is for the battery to output 0V, which my script does not seem to reflect.
The second device reads the pH of a system. When changing the pH of the system, the resulting changes in pH are not demonstrated in MATLAB.
I can confirm that the devices actually change readings because I have DATAQ software that demonstrates the changes, so this seems to be an issue related to MATLAB.
Why do the readings from my MATLAB script not update when my devices' readings update?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 3 Nov 2022
The issue here is that the devices read much more data than what is expected. For example, if the device reads 2000 data points, and its value changes after reading 1500 data points, but the code has only iterated through 200 data points, it might not appear that the device is updating properly. This can be confirmed by checking the number of bytes available from the device using the following command (with "s" as your "serialport" object):
s.BytesAvailable
To read the changes more quickly, you can flush the data read buffer using the "flush" command (again, with "s" as your "serialport" object):
flush(s)
The next read will correspond to the newest data point read to the device.
Alternatives for ensuring that you read updated data is to read a certain amount of bytes of data from the device using code similar to the following:
numBytesToRead = 1000; % the number of bytes you would like to read and remove from the buffer
read(s, numBytesToRead, "uint8");
This code will remove 1000 bytes of data from the buffer. In this case, some consideration should be given to the number of bytes included in each data read and how much data would be read between each iteration of the for loop so as not to read too much data.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!