Reading serial port data in MATLAB background

12 views (last 30 days)
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

Answers (1)

Manas
Manas on 4 Oct 2023
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!

Categories

Find more on MATLAB Support Package for USB Webcams in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!