Reading serial port data in MATLAB background

8 次查看(过去 30 天)
Hi There,
I have wrote a program in MATLAB that countiously read data through the serial port (from Arduino) in the background. To perform this im using the bytesavailable function in MATLAB 2019.
Essentially I need to run this in MATLAB background so I can run other codes.
When I Press the RUN button, Its looks like that it runs in background, but as soon I run other codes, it will get stuck in a long pause and it never excecutes.
Im thinking the problem might be related to fscanf function that I use to read the data.
I would appreciate it if someone can provide me with suggestions. Maybe I could use other functions in MATLAB 2019?
Best,
Nikan

回答(1 个)

Manas
Manas 2023-10-4
Hey Nikan Fakhari,
I understand that you are code is getting paused indefinitely when you run it along with other codes.
Based on your assumption that it depends on “fscanf” function, consider the following workarounds to see if it resolves the issue,
  • Asynchronous reading: Instead of “fscanf” consider using “BytesAvailableFcn” property of the serial port object.
  • Non-blocking read: Use the “BytesAvailable” property of the serial port object to check if the data is available before attempting to read it.
I suggest you refer to this documentation to learn more about the serial port object: https://in.mathworks.com/help/instrument/serial.html
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by