简体   繁体   English

Matlab串口通讯不一致

[英]Matlab serial communication inconsistency

I want to control a Prior ProScan II controller and the fitting motorized stage with Matlab R2016b. 我想使用Matlab R2016b控制Prior ProScan II控制器和配件电动平台。 Manual Tested with R2010b, got the same results. 使用R2010b进行手动测试,得到相同的结果。 The relevant commands of the stage are VS(page 46), P(p.43), PS(p.44). 该阶段的相关命令为VS(第46页),P(第43页),PS(第44页)。 In a plain terminal, immediately after the stage halts I can issue the P or PS command, returning the current position of the X and Y axes. 在普通终端中,在平台停止后,我可以立即发出P或PS命令,返回X和Y轴的当前位置。 If done in Matlab prompt, it MIGHT need a second or two to return the proper value, before that it returns 'R' - Probably not the ACK of the previous command as it is returned after init, without any R-ACKed commands issued before. 如果在Matlab提示符下完成,则可能需要一两秒钟才能返回正确的值,然后再返回“ R”-可能不是初始化后返回的前一个命令的ACK,因为之前没有发出任何R-ACKed命令。 When used in a script in a separate .m file, it can only return 'R'. 在单独的.m文件的脚本中使用时,它只能返回'R'。 My code in main.m is as follows: 我在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 the segment in if..end is invoked from the Matlab prompt, it works fine in most cases. 如果从Matlab提示中调用了if..end中的段,则在大多数情况下它可以正常工作。

>> s = openserial()
%properties as before, skipped to save space
>> fprintf(s,'ps');
>> fgets(s)

ans =

100000,100000

or 要么

>> fprintf(s,'p');
>> fgets(s)

ans =

100000,100000,0

If I Ctrl+C out of the infinite loop but leave the serial open and issue 如果我按Ctrl + C退出无限循环,但保持串行打开状态并发出

>> fprintf(s,'p');
>> fgets(s)

ans = 

R

returns. 回报。 Using fscanf() instead of fgets() yields the same results. 使用fscanf()而不是fgets()会产生相同的结果。

Is there any known bug of fprintf() or the ones mentioned above that could cause this? 是否存在fprintf()的任何已知错误或可能导致此错误的上述错误? What could I do to succesfully and consistently read within a script? 如何成功并一致地在脚本中阅读? Thank you for any answers. 谢谢您的回答。

Solution was to force flush the serial input buffer, flushinput(s) before the line pause(1). 解决方法是在行暂停(1)之前强制刷新串行输入缓冲区flushinput。 For some reason, even fscanf() instead of fgets() didn't flush it. 由于某些原因,即使是fscanf()而不是fgets()都没有刷新它。 I still don't know why it worked fine outside the script but not within. 我仍然不知道为什么在脚本外部却不能正常工作。 Additionally, it also worked in a separate script. 此外,它还可以在单​​独的脚本中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM