MBED & MATLAB

05 Apr 2011

Hi ...

I´ve got problems connecting my mbed with Matlab (PC). I loaded my program into mbed. This is summarized in several lines like those shown below:

  1. include "mbed.h"

Serial pc(USBTX, USBRX);

AnalogIn Sensor_3(p17); DigitalOut LED_micro_1(LED1);

float mean_S1; int i;

int main() {

pc.baud(9600);

while(1) {

mean_S1 = 0;

LED_micro_1 = 1;

for(i=0; i<1000; i++){ mean_S1 = mean_S1 + 4.1*Sensor_3.read(); }

mean_S1 = mean_S1/i;

if (pc.readable()) { pc.printf("%f", mean_S1); }

LED_micro_1 = 0; wait (0.2); } }

First question ... if I want to connect my mbed with Matlab, is this program correct? Apparently, it is OK because checking with an oscilloscope the data is transferred via USB.

Now in Matlab, the commands are the following:

mbed = serial('COM9', ... 'BaudRate', 9600, ... 'Parity', 'none', ... 'DataBits', 8, ... 'StopBits', 1);

fopen(mbed); values = fscanf(mbed, '%f');

Nevertheless, when I check the variable 'values', no data appear. In fact, there is nothing in the Matlab buffer so I can not see the result.

Is there anything wrong?

Thanks for your help in advance.

Chema.

05 Apr 2011

  if (pc.readable()) { pc.printf("%f", mean_S1); }

shouldn't that be

  if (pc.writeable()) { pc.printf("%f", mean_S1); }
05 Apr 2011

Maybe, however I´ve checked your suggestion but it still doesn´t work. I provide more information ... my object information after "fscanf" is the following:

mbed

Serial Port Object : Serial-COM9

Communication Settings Port: COM9 BaudRate: 9600 Terminator: 'LF'

Communication State Status: open RecordStatus: off

Read/Write State TransferStatus: idle BytesAvailable: 0 ValuesReceived: 0 ValuesSent: 0

05 Apr 2011

The first thing you should try is ditch MathLab and just use a terminal program to actually verify that the Mbed is sending data down the comms.

Quote:

Apparently, it is OK because checking with an oscilloscope the data is transferred via USB

This is meaningless. You cannot use a scope to "look at data" on the USB bus. There's data banging back and forth on a USB bus all the time even when you are not sending anything. The only way to be sure is either use a terminal to actually read the comm port or you might like to flash an LED when you are sending data. For example:-

DigitalOut led4(LED4);

...

    if (pc.writeable()) { 
        led4 = !led4; // Toggle led4 when we send something.
        pc.printf("%f", mean_S1); 
    }


If led4 never toggles it's not sending.

05 Apr 2011

Of course ... I can confirm with no doubt that the mbed is sending the data. I´ve checked both ways suggested: the LED and the "hilgraeve" hyperterminal. So, the problem is Matlab. But where ... I don´t know.

05 Apr 2011

Then it would seem that your question is for a Matlab forum :)

06 Apr 2011

Hi Jose,

Just another point; your printf doesn't send a newline, so I don't think matlab would ever see a number (it is constantly reading one very long number!)

So, using "%f\n" should get you closer.

Simon

06 Apr 2011

Thanks Andy and Simon for your help and attention.

Best regards.

Jose.

PD: Including \n the problem has been solved ;)

12 Aug 2011

Would it possible to send me the full coding structure? I need test it with my mBed too since incorporating mBed with MATLAB. Appreciate ur help!!!