Reading reply from serial port returns empty when read/write commands run separately

I have a serial port that is configured as such: Serial Port Object : Serial-COM3
Communication Settings
Port: COM3
BaudRate: 9600
Terminator: 'CR'
Communication State
Status: open
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 4
ValuesReceived: 306
ValuesSent: 200
(BytesAvailable shows 4 because the snapshot is right after I had written to the port).
If I run the following commands in a function without entering debugger or if I simply enter them simultaneously in the matlab console, the read function returns an expected result, almost instantly
fprintf(this.serial_, '%s', command); fread(this.serial_, nBytes);
IF, however, I print the command, and then afterwards try to read it, the read results in the following:
Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.
ans =
Empty string: 0-by-1
Any thoughts on why the serial reply is different?
Thanks!

댓글 수: 2

Hey did you ever figure this out? I'm having the same issue where I send a command just as you did, and upon reading the response, I get an emtpy 0x1 matrix.
I have used this exact method before to communicate and it worked every time, but now for some reason I am having issues... is there something you did that made it start working again?
It is not clear to me that a terminator is being sent?

댓글을 달려면 로그인하십시오.

답변 (1개)

riverCN
riverCN 2020년 10월 9일
편집: riverCN 2020년 10월 9일
I got the same problem. Just add a pause between writing and reading. This is called the port latency. A pause(0.05) should do the trick.

카테고리

도움말 센터File Exchange에서 Data Acquisition Toolbox에 대해 자세히 알아보기

제품

릴리스

R2014a

질문:

2018년 10월 23일

편집:

2020년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by