【发布时间】:2017-03-28 15:49:00
【问题描述】:
我想用 Matlab R2016b 控制 Prior ProScan II 控制器和合适的电动载物台。 Manual 用 R2010b 测试,得到相同的结果。该阶段的相关命令是VS(第46页),P(第43页),PS(第44页)。在普通终端中,在舞台停止后我可以立即发出 P 或 PS 命令,返回 X 和 Y 轴的当前位置。如果在 Matlab 提示符下完成,它可能需要一两秒才能返回正确的值,然后才返回 'R' - 可能不是前一个命令的 ACK,因为它在 init 之后返回,之前没有发出任何 R-ACKed 命令.在单独的 .m 文件中的脚本中使用时,它只能返回“R”。我在 main.m 中的代码如下:
%Opening serial port
s = serial('COM9');
set(s,'BaudRate',9600,'Terminator','CR'); %Note that CR will be concatenated to all commands
fopen(s);
s %print for debugging
t=1 %loop index
while true
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Here is code to mess with a joystick that works fine, using VS command
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if button(joy, 3) == 1 %Button3 of joystick should trigger position reading
fprintf(s,'vs,0,0'); %Halt joystick movement
pause(1) %Debouncing
fprintf(s,'p'); %Axe-pos reading command
fgets(s) %Reading the answer
end
%This way to increment follows the skipped parts and is required for timing
if mod(t, 100)==0
fprintf(s,'%s',Command);
t=1;
else
t=t+1;
end
如果 if..end 中的段是从 Matlab 提示符调用的,它在大多数情况下都可以正常工作。
>> s = openserial()
%properties as before, skipped to save space
>> fprintf(s,'ps');
>> fgets(s)
ans =
100000,100000
或
>> fprintf(s,'p');
>> fgets(s)
ans =
100000,100000,0
如果我 Ctrl+C 退出无限循环但保持串行打开并发出
>> fprintf(s,'p');
>> fgets(s)
ans =
R
返回。使用 fscanf() 而不是 fgets() 会产生相同的结果。
是否有任何已知的 fprintf() 错误或上述错误可能导致此问题?我该怎么做才能在脚本中成功且一致地阅读?感谢您的任何回答。
【问题讨论】:
-
解决方案是在 pause(1) 之前强制刷新串行输入缓冲区、flushinput(s)。我仍然不知道为什么它在脚本之外而不是在脚本内部运行良好。
标签: matlab