Send array from MATLAB to Arduino
6 views (last 30 days)
Show older comments
I want to send an array of three floating point numbers to Arduino from MATLAB. I'm trying to check if these values have been received by Arduino by sending these values back from the Arduino to MATLAB. It seems that Arduino only reads the first element in the array correctly.
My array, 'parameters' is:
measurement_interval = 5.0;
ec_ref_thickness = 2.0;
e_ref_thickness = 3.0;
parameters = [measurement_interval ec_ref_thickness e_ref_thickness];
I established communication with Arduino as:
arduino = serial('COM4');
set(arduino,'DataBits',8);
set(arduino,'StopBits',1);
set(arduino,'BaudRate',9600);
set(arduino,'Parity','none');
fopen(arduino);
I send it to Arduino using:
fprintf(arduino, '%d', parameters);
fprintf(arduino, '\n');
And in Arduino I have:
char parameters[3] = { '0','0','0'};
void setup()
{
Serial.begin(9600);
while (Serial.available() == 0)
{
}
if (Serial.available() > 0)
{
for (int i=0; i < 3 ; i++)
{
parameters[i] = Serial.read();
}
Serial.flush();
}
I send back from the Arduino over the serial port as:
void loop()
{
Serial.print(parameters[0])
Serial.print(" ");
Serial.print(parameters[1]);
Serial.print(" ");
Serial.print(parameters[2]);
}
And read in MATLAB as:
output = fscanf(arduino);
'output' should be [5.0 2.0 1.0]. However, what I get is [5 ÿ ÿ]
So only the first element '5.0' is returned correctly.
0 Comments
Answers (3)
german rodriguez
on 31 Oct 2018
amigo pudiste solucionar el problema ???. ya que tengo el mismo problema. No logro enviar un vector desde matlab a arduino de ningun modo
0 Comments
Walter Roberson
on 1 Nov 2018
Serial.read returns -1 if no bytes are available.
When you check for serial available the way you do, you are testing for 1 or more bytes available, not for 3 or more.
That said, sending to the arduino over usb will not happen until a timeout or until the buffer is full, because the usb driver assumes that you might be sending more data that it should buffer all together for efficiency. So you should be sending groups of 4 bytes.
Hmmm, but you are flushing whatever else might be in the same usb packet instead of eating the newline
0 Comments
german rodriguez
on 1 Nov 2018
Edited: german rodriguez
on 1 Nov 2018
Lo estoy enviando de esta manera MATLAB
dato=[5 11 33 23 14 65 33 11 10 6];
fprintf(s,dato);
En arduino lo recibo
float lectura[10];
int N=10;
if (Serial.available()>5)
{
for(int r=0;r<N;r++)
{
lectura[r]=Serial.read();
}
}
1 Comment
Walter Roberson
on 4 Nov 2018
MATLAB's documentation does not define what happens if you have a serial() object, s, and you use
fprintf(s, NumericArray)
Probably it would be treated the same as
fprintf(s, char(uint8(NumericArray)) )
which in turn would be the same as
fprintf(s, '%s\n', char(uint8(NumericArray)) )
Notice the line terminator would be sent, so a minimum of 11 bytes would be sent for a 10 entry buffer.
Your code is not expecting anything after the N items, so you could have trouble with the unexpected newline.
You should probably code
fprintf(s, '%s', NumericArray)
or better yet,
fwrite(s, data, 'uint8')
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware 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!