[英]fprintf (serial communication) in matlab
I'm trying to communicate with differential drive mobile robot via matlab functions and .mex files. 我正在尝试通过Matlab函数和.mex文件与差动驱动移动机器人进行通信。 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: 但是,我制作的包含
fprintf
的函数不起作用:
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
(ie 'C,1000,1000' is not equal to 'C,left,right'), but I can't resolve this problem. 我知道问题是在命令
fprintf
使用了不同的字符串(即'C,1000,1000'不等于'C,left,right'),但我无法解决此问题。 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);
You can try the following: 您可以尝试以下方法:
function r = Move(ref,left,right)
fprintf(ref,'C,%d,%d', left, right);
out = fscanf(ref)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.