MATLAB could not read multi-line serial port data using a loop
이전 댓글 표시
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
Walter Roberson
2016년 3월 2일
As an experiment, try adding a small sleep() after each line is emitted.
Miao
2016년 3월 3일
Walter Roberson
2016년 3월 3일
That's pretty weird.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 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!