Hi all, I met a problem using serial communication between MATLAB and Arduino.
My MATLAB code is computing variables in real-time and sending the result to Arduino via Serial Port. It works well when there is only one variable to write into the serial port. But I have three variables to write into the serial pot and to be received by the Arduino controller. I want to send an array to Arduino so that three variables are ordered well so that it can be taken easily by Arduino but failed.
The code is used to autonomously compute desired motor actuation and send the command to different motors for actuation via Serial Communication.
The method I used to receive data on the Arduino side is like this:
float parameters[3];
void setup(){
Serial.begin(9600);
while(!Serial || Serial.available() <= 0);
for (int i=0; i<3; i++){
parameters[i] = Serial.parseFloat();
}
}
void loop(){
for(size_t i=0; i<3; ++i){
Serial.print(parameters[i]);
Serial.print(i<2 ? '\t' : '\n');
}
}
The MATLAB side to send the data is like this:
measurement_interval = 5.0;ec_ref_thickness = 2.0;e_ref_thickness = 3.0;
parameters = [measurement_interval ec_ref_thickness e_ref_thickness];
arduino = serialport("COM11",9600);
fprintf(arduino, '%f', parameters);
fprintf(arduino, '\n');
output = fread(arduino,3,'float');
Based on some online tutorials, seems I successfully passed the data between MATLAB and Arduino. However, what I get on MATLAB is 3x1 double array
[6.33692931462093e-10;
4.00259270172398e-11;
4.99087882133154e-10]
There is some error about datatype, which is updated as above. Are there any suggestions? Appreciate a lot!