MATLAB could not read multi-line serial port data using a loop
조회 수: 11 (최근 30일)
이전 댓글 표시
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
채택된 답변
Miao
2016년 3월 3일
편집: Miao
2016년 3월 3일
댓글 수: 2
Walter Roberson
2016년 3월 3일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!