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
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
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
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!