Serial Communication Timeout Warning

조회 수: 13 (최근 30일)
Sebastian Irby
Sebastian Irby 2015년 6월 15일
편집: Abhijit Deka 2017년 4월 24일
I have been using the following code to simply send data through a usb to an oscilloscope and although I have been able to send data just fine, I am receiving the the following timeout warning. Can someone help me figure out why this is showing up and how to fix it?
Code:
clc;
clear;
string1=[200,200,200,200,200]
obj1 = instrfind('Type', 'serial', 'Port', 'COM5', 'Tag', '');
set(obj1, 'timeout',.5);
if isempty(obj1)
obj1 = serial('COM5');
else
fclose(obj1);
obj1 = obj1(1)
end
fopen(obj1);
fwrite(obj1,string1,'uint8')
A=fread(obj1)
fclose(obj1);
Warning:
Warning: Unsuccessful read: The specified amount of data was not returned
within the Timeout period.
  댓글 수: 1
Daniel Rausch
Daniel Rausch 2016년 2월 22일
I assume you want to read the Bytes available in the Buffer. Try A=fread(obj1,get(obj1,'BytesAvailable'))

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

답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 15일
"A = fread(obj) and A = fread(obj,size) read binary data from the device connected to the serial port object, obj, and returns the data to A. The maximum number of values to read is specified by size. If size is not specified, the maximum number of values to read is determined by the object's InputBufferSize property."
"Rules for Completing a Binary Read Operation
A read operation with fread blocks access to the MATLAB® command line until:
The specified number of values are read.
The time specified by the Timeout property passes.
Note The Terminator property is not used for binary read operations."
You did not specify a size in your fread() code, so it is going to try to read an entire InputBufferSize worth of bytes, ending only if it gets that many or if the timeout occurs. If your sending system is not filling the buffer then you are going to get a timeout.
I would suggest that you specify the count specifically.
  댓글 수: 1
Abhijit  Deka
Abhijit Deka 2017년 4월 24일
편집: Abhijit Deka 2017년 4월 24일
thanks, after this, the slow reception of data was solved too. Communication is now fast as I wanted. and no warning regarding unsuccessful read.

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

카테고리

Help CenterFile Exchange에서 Serial and USB Communication에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by