Trouble using serial.scanf

3 views (last 30 days)
aenrich
aenrich on 27 Feb 2017
Commented: Sachin Kumar on 9 Mar 2017
Hi, I'm using Matlab in order to manage the serial ports of my PC. The setup is the following. I have and arduino UNO and a NUCLEO L053R8 connected to the PC, and I want to synchronize them.
Let me explain better.
With the Arduino, I'm controlling two steppers motors. With the NUCLEO board, I'm just taking measures (I need to use this board because the ADC resolution is higher). Then the flow of the system should be the following:
  1. Arduino moving motors. NUCLEO STOPPED.
  2. Arduino STOPPED. Nucleo measuring.
  3. Arduino moving motors. NUCLEO STOPPED.
  4. Arduino STOPPED. Nucleo measuring.
I have no problem with the Arduino, but with the NUCLEO I'm having trouble.
My code on the NUCLEO is basically a while (1) { ... } that inside has a
while(1){
while(c=='0'){//it is supposed that I will send a 1 for example
c=pc.getc();
}
//Code to measure
pc.printf("Measures finished");
c="0";
}
My code on Matlab is the following:
clear all;
%Inicializo el puerto serie
delete(instrfind({'Port'},{'usbmodem1411'}));
puerto_serial_ard=serial('/dev/cu.usbmodem1411');
puerto_serial_ard.BaudRate=9600;
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
delete(instrfind({'Port'},{'usbmodem1423'}));
puerto_serial_stm=serial('/dev/tty.usbmodem1423');
puerto_serial_stm.BaudRate=9600;
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
%Abro el puerto serie
fopen(puerto_serial_ard);
fopen(puerto_serial_stm);
while(1)
fprintf(puerto_serial_stm,'Arduino moving');
ard=fscanf(puerto_serial_ard,'%d');%Read from arduino
if(strfind(ard,'Ready to measure')>0) %If we have moved to position, connect to STM32
fprintf(puerto_serial_stm,'1');%Change variable c from STM32 to jump out of the while
stm=fgetl(puerto_serial_stm);%Read from STM32
while(isempty(strfind(stm,'Measures finished')))%Wait until Measuring has finished
stm=fgetl(puerto_serial_stm);%Read from STM32
end
fprintf(puerto_serial_ard,'1');%Send something to arduino, it can move again
end
end
fclose(puerto_serial_ard);
fclose(puerto_serial_stm);
As you can see, I first read from the arduino until I get "Ready to measure". Once i have gotten this string, it means the arduino has moved the motors correctly and I'm ready to take the measures. That's why i send a '1' as toy can see, in order to make the NUCLEO out of the while (c=='0').
It works, and here comes the PROBLEM. As you can see in my Matlab code, I read from the NUCLEO until I find a coincidence with the string 'Measures finished'. After that, I would send a '1' to the Arduino to make it move again and restart the process.
Problem is: The first time Matlab will read from the NUCLEO it stm variable, the output from scanf/getl/gets (I've tried them all) will be "Measures finishedMeasures finished" as it had done twice the Nucleo while. It will find a coincidence, and the arduino will move the motors again, but when matlab tries to find another time the Measures finished, it won't work. Just like if the NUCLEO wasn't running. I cannot understand why this strange behaviour.
I have tried with other serial managers and introducing manually the values in the serial ports, and the NUCLEO will loop infinitly, not only twice.
  1 Comment
Sachin Kumar
Sachin Kumar on 9 Mar 2017
Insert delay in Nucleo code while loop while(c=='0'), after pc.printf("Measures finished"); and try. Try to debug just by sending "Measures finished" from nucleo and checking whether you are receiving in MATLAB.

Sign in to comment.

Answers (0)

Categories

Find more on Filter Banks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!