Timeout occurred when try serial communication

조회 수: 7 (최근 30일)
Nayeong Gi
Nayeong Gi 2019년 5월 17일
답변: Koundinya 2019년 5월 28일
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

답변 (1개)

Koundinya
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

카테고리

Help CenterFile Exchange에서 Licensing on Cloud Platforms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by