MATLAB: MATLAB could not read multi-line serial port data using a loop

arduinoserial

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 (1×1 cell), and only with the second Arduino code I get get all the data in Batch _DATA(1×17 cell).
I find it hard to explain and give me a lot of trouble in serial communication. Anyone encountered the same problem?

Best Answer

I have fixed the problem by changing the way of reading commands from MATLAB in firmware. Originally I was using some code as below from some online tutorials:
while(Serial.available()>0) {
V_BATCH_READ = Serial.parseFloat();
}
I change it to the code below and the problem is fixed:
while(Serial.available()==0) {}
V_BATCH_READ = Serial.parseFloat();
First I want to confirm both methods work with the mpide serial monitor. Why the first code cause problem with MATLAB?
I guess it's because when using MATLAB to send the command, it sends something extra and causes the data reading problem. I don't know why the mpide serial monitor would be different from the MATLAB's, but anyway here is the lesson I learned: don't use the first way to enter your command in firmware if you are communicating with MATLAB...
Related Question