MATLAB could not read multi-line serial port data using a loop
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm designing a firmware with Arduino C. In my code, I try to send multi-line data to the MATLAB.
I tried two methods on the Arduino side:
TOTAL_ROWS = 16;
for (MY_COUNTER = 0; MY_COUNTER < TOTAL_ROWS; MY_COUNTER++) {
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
}
Serial.println("Batch read complete");
or
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("40025, 40081, 40093, 40085, 40084, 40096, 40069, 40081");
Serial.println("Batch read complete");
You can see they should give exactly the same output to the MATLAB and I have verified it using the serial monitor embedded in mpide compiler.
On the MATLAB side (MATLAB version: 2015a) I try to use the code below to receive the data:
serConn = serial('COM1','BaudRate', 115200, 'InputBufferSize', 16784);
fopen(serConn);
readLines = 1;
Batch_DATA{readLines} = fscanf(serConn);
display(Batch_DATA{readLines});
while ~strcmp(Batch_DATA{readLines}(1:5),'Batch')
readLines = readLines + 1;
Batch_DATA{readLines} = fscanf(serConn);
display(Batch_DATA{readLines});
end
Surprisingly, with the first Arduino I can only get "Batch read complete" in Batch_DATA (1x1 cell), and only with the second Arduino code I get get all the data in Batch _DATA(1x17 cell).
I find it hard to explain and give me a lot of trouble in serial communication. Anyone encountered the same problem?
3 comentarios
Respuesta aceptada
Miao
el 3 de Mzo. de 2016
Editada: Miao
el 3 de Mzo. de 2016
2 comentarios
Walter Roberson
el 3 de Mzo. de 2016
The first code stops the loop as soon as Serial.available is false. But it can be false if there is no data available because it has not been received yet due to delays in transmission.
Contrawise of there was data queued after the parseFloat (perhaps because more was being received quickly) the first code would read it and overwrite V_BATCH_READ with the new value.
The second version of the code spins until data is available and then reads one group, and does not try again to see if there is more.
Más respuestas (0)
Ver también
Categorías
Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!