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