Timeout occurred when try serial communication
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Koundinya
2019-5-28
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Licensing on Cloud Platforms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!