data format error with serial communication

6 Ansichten (letzte 30 Tage)
squall141
squall141 am 23 Okt. 2016
Kommentiert: Walter Roberson am 23 Okt. 2016
Hello everybody, i have realized a script who send some comands to a serial devide. The device is a Mitsubishi Rv-m1 robot and it communicates with matlab on RS232. Here is the script.
%%Inizializzazione
clear all
clc
delete(instrfind)
cam = webcam('Trust Webcam');
s = serial('COM4');
s.StopBits = 2;
s.Parity = 'even';
s.DataBits = 7;
s.BaudRate = 9600;
fopen(s);
t2 = timer('TimerFcn', 'stat=false; disp(''Timer'')','StartDelay',2);
t5 = timer('TimerFcn', 'stat=false; disp(''Timer'')','StartDelay',5);
t8 = timer('TimerFcn', 'stat=false; disp(''Timer'')','StartDelay',8);
fprintf(s,'SP 5');
fprintf(s, 'NT');
pause
%%Definizione posizioni
% Posizione webcam
fprintf(s,'PD 1,+32.5,+250.3,+318.4,-93.8,+179.9');
start(t2)
waitfor(t2)
% Origine piano dei cilindri
fprintf(s,'PD 2,-122.4,+365.8,+33.8,-86.8,+179.9');
start(t2)
% Vettore contenente le coordinate dell'origine
origin = [-122.4 365.8 33.8 -86.8 179.9];
waitfor(t2)
%%Posizionamento per scattare la foto
fprintf(s,'MO 1');
%%Snapshot - ridimensionamento - localizzazione posizioni
new_circle_recognition;
%%Posizionamento nell'origine
fprintf(s,'MO 2');
%%Movimento dall'origine alla posizione dell'oggetto
% Vettore contenente le coordinate del centro del cilindro
% Ottenuto modificando il vettore origin con le coordinate dei centri.
cylinder = [origin(1)+centrimm(1),origin(2)-centrimm(2),origin(3),origin(4),origin(5)];
% Conversione in formato char con 2 cifre decimali
movechar = sprintf('MP %0.1f,%0.1f,%0.1f,%0.1f,%0.1f',cylinder(1),cylinder(2),cylinder(3),cylinder(4),cylinder(5));
% Conversione in formato adatto per il comando seriale
% Esempio di comando : 'MP +28.1,311.9,40,-88.8,179.9'
movestr = cellstr(movechar);
% Movimento robot
fprintf(s,movestr);
In the first section i have setted all the correct parameters for the communication infact the firsts commands (fprintf(s,'SP 5'); fprintf(s, 'NT'); fprintf(s,'PD 1,+32.5,+250.3,+318.4,-93.8,+179.9');) work correctly. "new_circle_recognition" is a script who makes a snapshot of the webcam (matlab object cam) and returns a vector 1x2 named centrimm with the position in mm of the center of a cylindric object. I want to use the information inside centrimm for send a serial command to serial object "s" with fprintf. The string should be like this: 'MP +28,311,40,-88,179' so i have used sprintf for create a char variable with the number in float format without decimal digits.
If i use char variable "movechar" in fprintf
fprintf(s,'movechar')
the device returns an error sound instead using cell variable "movestr"
fprintf(s,movestr)
matlab returns "cmd must be a string".
At this time i don't have at home the robot. I believe the problem is in the wrong data format of the command sent with fprintf. I'm thinking to use something like this
fprintf(s,'%s',movestr)
or i can use fwrite instead fprintf. does someone have a suggestion? Thanks a lot guys.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 23 Okt. 2016
fprintf(s, '%s\n', movestr{:});
  4 Kommentare
squall141
squall141 am 23 Okt. 2016
I have used cellstr to obtain a value which includes character '. In this way I have 'my generic string' instead my generic string. If I remove
movestr = cellstr(movechar);
I have only movechar which is an 1xn char like this
movechar =
MP 12.5,52.7,256.3,659.4,179.5
Furthermore in your answer you say to use
fprintf(s, '%s\n', movestr{:});
on movestr 1x1 cell obtained with cellstr (that you suggest to remove). Anyway i can't use movestr{2}, movestr{3} etc because it's only 1x1 cell. Only movestr{1} and movestr{:} works obviously.
Walter Roberson
Walter Roberson am 23 Okt. 2016
The inclusion of the ' is only for display purposes in cell strings; it is not stored along with the string. If you need to actually transfer ' (which I doubt) then you should
fprintf(s, '''%s''\n', movestr)
where movestr is not a cellstr
movestr{2} would be beyond movestr{end} in the case of a 1 x 1 cellstr. I am illustrating what cell array expansion means.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by