필터 지우기
필터 지우기

Reading serial port data in MATLAB background

조회 수: 8 (최근 30일)
Nikan Fakhari
Nikan Fakhari 2022년 5월 10일
답변: Manas 2023년 10월 4일
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