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

10 vues (au cours des 30 derniers jours)
teguh
teguh le 16 Juin 2011
[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

Réponses (3)

Jan
Jan le 16 Juin 2011
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 commentaires
Chirag Gupta
Chirag Gupta le 16 Juin 2011
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 le 16 Juin 2011
I admit, my advice is not congruent with this doc. But does it work?

Connectez-vous pour commenter.


Chirag Gupta
Chirag Gupta le 16 Juin 2011
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 le 29 Juin 2011
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