Timeout problem in matlab

조회 수: 4 (최근 30일)
Jim
Jim 2013년 5월 24일
Hi,
I am trying to read data through serial communication in matlab
I am reading 2 bytes of data
First byte is zero for most of the times and 2nd byte contains information.
I am getting timeout error Is there any command to read data starting with zeros Warning: Unsuccessful read: A timeout occurred.
Code:
nexys = serial('COM1', 'BaudRate', 9600, 'Parity', 'odd', 'Terminator', '', 'Timeout', 1);
fopen(nexys);
myNum = 4563;
%fprintf(nexys, 'd1539'); %Fungerar
%fprintf(nexys, 'd%4d', myNum); %Fungerar
for i=1:2
fprintf(nexys, 's');
scan_s1 = fscanf(nexys,'%s')-0;
fprintf(nexys, 't');
scan_s2 = fscanf(nexys,'%s')-0;
end
Can anybody suggest something about this
Thanks Jim
  댓글 수: 2
Matt Kindig
Matt Kindig 2013년 5월 24일
On what line is the timeout error thrown? Have you verified that the scan_s1 and scan_s2 are incorrect?
It looks like you are setting the 'Terminator' to empty. In my experience, a timeout error can arise when the device does not send a specified terminator string that Matlab recognizes. I'm not sure how it would respond with an empty terminator specified.
Jim
Jim 2013년 5월 24일
Hi Matt kindig,
Thanks for ur reply. Even when I am using LF as terminator I am getting the same error: Yes, I think matlab does not recognizes the data I am sending Is this because 1st byte contains 00000000? Is there any command to recognize zeros in matlab
Thanks in advance

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

채택된 답변

Jan
Jan 2013년 5월 25일
You try to read a string but do not define a terminator. Even the the LF terminator, the string might not be terminated, when no LF appears in the data stream. So are you sure, that FSCANF for a string is what you want?
FREAD read all values, while the import of a string might be impeded by a zero byte, because they terminate strings in C.
  댓글 수: 2
Jim
Jim 2013년 5월 25일
Hi Jan,
Thanks for your reply. In the command fscanf
scan_s1 = fscanf(nexys,'%s')-0;
What does "-0" will do exactly?
What is the need of substracting 0 from a string
Thanks Jim
Jan
Jan 2013년 5월 26일
@Jim: Subtracting 0 from a CHAR array converts the type to DOUBLE:
c = '123'
d = c + 0
class(d)
Alternative, which is less funky:
d = double(c);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by