Makaroni
Makaroni

Reputation: 912

fprintf (serial communication) in matlab

I'm trying to communicate with differential drive mobile robot via matlab functions and .mex files. I can succesfully move the robot with command:

ref = serial('COM1');
set(ref,'BaudRate', 9600);
fopen(ref);

fprintf(ref,'C,1000,1000');
out = fscanf(ref)

fclose(ref)
delete(ref)

However, the function that I made which includes fprintf does not work:

function r = Move(ref,left,right)

fprintf(ref,'C,left,right');
out = fscanf(ref)

I'am aware that the problem is different string used in command fprintf (i.e. 'C,1000,1000' is not equal to 'C,left,right'), but I can't resolve this problem. Sorry if this is too trivial.

The ANSWER is (see comments below):

function r = Move(ref,left,right)

fprintf(ref,sprintf('C,%d,%d', left, right));
out = fscanf(ref);

Upvotes: 0

Views: 1454

Answers (1)

H.Muster
H.Muster

Reputation: 9317

You can try the following:

function r = Move(ref,left,right)

fprintf(ref,'C,%d,%d', left, right);
out = fscanf(ref)

Upvotes: 3

Related Questions