Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 10 months ago.
Serial data from Matlab
Has anyone tried sending data from Matlab to F401RE over serial interface? If possible, is there any method to receive data using serial ISRs?
I am trying to get some floating point numbers from Matlab and could not succeed with the following code:
F401RE CODE:
#include <mbed.h>
Serial pc(USBTX, USBRX);
int cycle = 0;
int main()
{
float counter = 0;
float number;
while (1) {
counter = counter + 0.5f;
if (pc.readable() > 0) {
pc.scanf("%f",&number);
pc.printf("%f, %f\n", counter, number);
}
if (counter ==5.0f) counter = -5.0f;
wait_ms(2000);
}
}
Matlab Code
try
answer=3; % this is where we'll store the user's answer
mbed = serial('COM4','BaudRate', 9600, 'Parity', 'none','DataBits', 8,'StopBits', 1);
TIMEOUT = 5;
set(mbed,'Timeout',TIMEOUT);
%set(mbed,'Terminator','LF');
fopen(mbed); % initiate communication
fprintf(mbed,'%f\n',answer); % send
%pause(1);
values = fscanf(mbed, '%f,%f')
fclose(mbed);
catch
%in case of error or mbed being disconnected
disp('Failed!');
fclose(mbed); %close connection to prevent COM port being lokced open
end