0
votes

I want to control a Prior ProScan II controller and the fitting motorized stage with Matlab R2016b. Manual Tested with R2010b, got the same results. The relevant commands of the stage are VS(page 46), P(p.43), PS(p.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. 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. When used in a script in a separate .m file, it can only return 'R'. My code in main.m is as follows:

%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.

>> 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

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

ans = 

R

returns. Using fscanf() instead of fgets() yields the same results.

Is there any known bug of fprintf() or the ones mentioned above that could cause this? What could I do to succesfully and consistently read within a script? Thank you for any answers.

1
Solution was to force flush the serial input buffer, flushinput(s) before pause(1). I still don't know why it worked fine outside the script and not within. - navigator

1 Answers

0
votes

Solution was to force flush the serial input buffer, flushinput(s) before the line pause(1). For some reason, even fscanf() instead of fgets() didn't flush it. I still don't know why it worked fine outside the script but not within. Additionally, it also worked in a separate script.