Serial Communication Timeout Warning

12 次查看(过去 30 天)
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by