The specified amount of data was not returned within the Timeout period or A timeout occurred before the Terminator was reached

조회 수: 5 (최근 30일)
Hello Matlab community,
I am having a problem with Matlab and I'm hoping someone can help me. I am trying to use serial port communication to read data from my device, but when I try it gives me back either no data with the error above or the correct data. Using fscanf will give me the "A timeout occurred before the Terminator was reached" error while using fread will give me the "The specified amount of data was not returned within the Timeout period" error. The device will give me back information possibly three out of every ten reads. I am a complete novice when it comes to Matlab so I would really appreciate help. The following is my code:
s = instrfind('Port','COM3');
if isempty(s)
s = serial('COM3','BaudRate',9600,'ByteOrder','littleEndian','DataBits',8,'Parity','none','StopBits',1,'Terminator','CR','Timeout', 1);
else
fclose(s);
delete(s);
s = s(1)
end;
fopen(s);
for i = 1:10
fprintf(s,'IR');
bytesava = s.BytesAvailable
T = fread(s)
pause(0.2)
end
end
fclose (s)
delete (s)
clear all
Best Regards,
Jin
  댓글 수: 1
Akina
Akina 2015년 4월 7일
Hi,
I know this thread is an old one, but I'm wondering if you had found solution to the problem you had. I'm having difficulty getting my device (hand-dynamometer) reads input via USB (CP2102) and getting exactly what you've gotten. It'd be great if you had any suggestion on this!
Best,
Akina

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

답변 (1개)

Walter Roberson
Walter Roberson 2013년 12월 2일
Your line
T = fread(s)
requests that data be read until BufferSize bytes of data have been read, or a terminator has been seen.
Have you been able to confirm on the receiving side that the IR\r being sent to the device is being received and replied to?
  댓글 수: 1
Jin
Jin 2013년 12월 2일
I am not sure if this answers that, but when I display the Read/Write State, it says ValuesSent: 3 and Values Received:0 when their is an error. I am not sure if this is referring to the device or Matlab. It does work sometimes which I think means it does indeed receive the command, but I don't know how to check whether the device reads the command properly or sends something back.
Thank you for your response. The following is the status of the serial port when it gives an error: 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: 0
ValuesReceived: 0
ValuesSent: 3

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

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by