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.
10 years ago.
Communicating between MATLAB and mBED.
Hallo everyone, I seem to be finding myself in a bit of a pickle with the USB communication between MATLAB and mBED LPC1768 microcontroller. Here is a bit of code that behaves rather unexpectedly (explanation follows):
Receiving data from MATLAB and confirming correct reception
//Receive parameter data from MATLAB. pc.scanf("%f",&aux_var); Readbatch = aux_var; //Capture Readbatch (read after Readbatch write pulses). pc.scanf("%f",&aux_var); Databatch = aux_var; //Capture Databatch (process data in Databatch-sized chunks). //Declare new array to hold voltage and timing data on the time series. static float *BatchT = new float[int(Databatch)]; //Timing array. static float *BatchV = new float[int(Databatch)]; //Voltage array. //NeurorecCapture(Databatch, BatchV, BatchT); pc.scanf("%f", &aux_var); pc.scanf("%f", &aux_var); printf("%f\n", Databatch); //Confirm data is being passed.
MATLAB is set up to pass 2 numbers to mBED via the standard "try-fprintf-end" scheme, which the microcontroller captures through scanf statements (lines 2-5). Following this the microcontroller is instructed to create a couple of arrays whose size depends on the value in received varfiable 'Databatch' (lines 8-9). Then, the mBED receives another couple of variables from MATLAB (lines 12-13) and finally replies to MATLAB by showing it that is has correctly captured the value of Databatch (line 15).
The interesting thing is this: If I comment lines 8 & 9, where the new arrays are declared all goes well and the program runs completely clean of any errors or warnings. MATLAB sends the data quite happily and receives the correct reply at the end through its own scanf statement; the coutnerpart to line 15 of the uC code above. However, as soon as lines 8 & 9 are back in the loop I start getting issues of "Unsuccessful read: Timeout before reaching the terminator"!
This I find quite baffling. Why would declaring a couple of arrays in between the scanf statements cause such dramatic change in behaviour?
Attempted fix: I tried moving line 15 in between lines 9 and 12 too and changed the corresponding code in MATLAB so that MATLAB can stay on stand-by until the mBED is done creating the new arrays, instead of trying to force another couple of floats in. Alas to no avail: the system experiences the same problem and once again the problem vanishes if the array declaration is commented out.
Any possible insights into why this is happening and how it could be fixed would be much appreciated.
Many thanks!
1 Answer
10 years ago.
Can you try moving line 15 up to line 6? So that it directly echos the size of Databatch? Most likely explanation seems to be that it is trying to create a way too large array, crashing the whole thing (actually that should not crash, but new does crash if there is no memory, malloc doesn't).
How large is it supposed to be? Or something else you can try: Set a fixed size for the two batches, and see if that works.
Btw bit sad that apparantly the try-printf-end method is still required with matlab.
Preallocating the arrays and declaring them as static worked! Thanks for the advice. I think the static requirement stems from the fact that I'm trying to fill the array from within a function...
It is indeed sad that to this point working with the try-printf-end method seems to still be the best way of communicating between MATLAB and mBED. I wonder whether there is any better way of carrying out communications...
posted by 08 Dec 2014