필터 지우기
필터 지우기

Read full answer from serial device

조회 수: 1 (최근 30일)
Mark
Mark 2023년 10월 3일
편집: Alexander 2023년 10월 3일
Hi to all,
I have a serial device (9600, 8N1) that works in this way:
  • I send a request command, i.e., "OP\n"
  • it answers with "0.14\r\n52.32\r\n"
I need to always send a request command to obtain an answer from the device, so I should use a loop.
Now, I'm trying to use the serial functions under Matlab:
device = serialport("COM4",9600)
writeline(device,"OP")
readline(device)
it works, but I only get a part of the answer and not the full answer. I get the first double value and then I have to run it again to get the second one. Moreover, it seems that it is not able to handle the \r\n.
How can I continuously acquire from the device by correctly saving the values?
Thank you!

답변 (1개)

Alexander
Alexander 2023년 10월 3일
편집: Alexander 2023년 10월 3일
You are right, readline reads exactly to the first terminator, so it is handling the terminator correctly. Try "read" instead, e.g.:
data = read(device,17,"char");
But you have to parse the data afterwards to your needs.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by