Timeout occurred when try serial communication
7 views (last 30 days)
Show older comments
Hello, I try to control my temperature controller using serial communicate via matlab.
But it was really hard because i don't know very well about the serial communicate and protocol.
I used PC LINK protocol in the manual.
The command I want to test is DRS Command
Frame Format is
STX+Address DRS, amount, D-resister number CR LF
The example of that is
STX+"01DRS,02,0001"+CR+LF
and respond frame format is
STX+Address DRS, 0K, Data-1, ... Data-n CR LF
Ex)
STX+"01DRS,0K,04D2,0929"+CR+LF
So I tried to communicate with my temperature controller using MATLAB, always there is occured errors...
s=serial('COM6');
set(s, 'BaudRate', 9600, 'DataBits', 8, 'parity', 'none', 'stopbits', 1, 'timeout', 5);
fopen(s);
S=sprintf('\x02"01DRS,02,0001"\r\n');
fwrite(s, S);
out=fscanf(s);
The error was Timeout occured ... when I add the pause(2); between fwrite and fscanf the result was same.
s=serial('COM6');
set(s, 'BaudRate', 9600, 'DataBits', 8, 'parity', 'none', 'stopbits', 1, 'timeout', 1);
fopen(s);
S=sprintf('%x[01DRS,02,0001]\r\n', 002);
fwrite(s, S);
out=fscanf(s);
And, also, I tried this code but result was same...
I think the code that I used to send the command was wrong, but i don't know how to fix it to right form.
STX=0x02(hex)
LF=0x0A
CR=0x0D
I suffers from this for 2 weeks...
How I can fix the code form????
plz help me
0 Comments
Answers (1)
Koundinya
on 28 May 2019
It seems to me like the timeout occurs due to fscanf not returning any serial data. This may be due to fscanf waiting on the terminator character in the response frame. Because you mentioned that the response frame ends with a CR LF as the terminating character, try adding the following line to your script :
s=serial('COM6');
s.Terminator='CR/LF'; % or 'LF/CR' if this doesn't work
0 Comments
See Also
Categories
Find more on Licensing on Cloud Platforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!