How do I write to move the line in sending serial?

조회 수: 15 (최근 30일)
teguh
teguh 2011년 6월 16일
[EDIT: 20110616 09:33 CDT - reformat - WDR]
I want to send serial data using the CR (Cariage Return) I've tried to change the terminator to CR but still belom succeed. I sent this data later to SSC-32 (Servo Motor Driver).
clc; disp('BEGIN')
Serial = serial('COM5');
set(Serial,'Terminator','CR');
set(Serial,'BaudRate',115200);
set(Serial,'DataBits',8);
set(Serial,'Parity','none');
set(Serial,'StopBits',1);
set(Serial,'FlowControl','none');
fopen(Serial);
get(Serial);
%sending data
fprintf(Serial,'%s\n','#1 P1500 T1000');pause(0.2);
fprintf(Serial,'%s\n', '#2 P1500 T1000');pause(0.2);
%close the serial
fclose(serial);
delete(Serial)
clear Serial
disp('STOP')
is not successful,. data I can not send new line breaks. so the servo can not move.
Thanks for help

답변 (3개)

Jan
Jan 2011년 6월 16일
This defines CR = CHAR(13) as terminator:
set(Serial,'Terminator','CR');
But:
fprintf(Serial, '%s\n', ...);
Writes \n = LF = CHAR(10) as line break. So either use this:
set(Serial, 'Terminator', 'LF');
or this:
fprintf(Serial, '%s\r', ...);
which is equivalent to
fprintf(Serial, ['%s', char(13)], ...);
  댓글 수: 2
Chirag Gupta
Chirag Gupta 2011년 6월 16일
Actually the <http://www.mathworks.com/help/techdoc/matlab_external/f62852.html#br8pqg6-1 documentation> does mention that \n will be interpreted as the Terminator property, so it is strange.
Jan
Jan 2011년 6월 16일
I admit, my advice is not congruent with this doc. But does it work?

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


Chirag Gupta
Chirag Gupta 2011년 6월 16일
Can you also try to send the carriage return directly:
fprintf(serial,'%s\r',...);
Also can you check with a serial monitor that the correct data is being sent out. (You are getting a CR (13) for terminator).

Ankit Desai
Ankit Desai 2011년 6월 29일
When using serial object (and any Instrument Control Toolbox object) using fprintf will automatically append the 'Terminator' character.
So you can avoid appending \n to your command string once you setup your terminator property. For the example above, the following should work:
fprintf(Serial,'#1 P1500 T1000');

Community Treasure Hunt

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

Start Hunting!

Translated by