Matlab serial command to read binary data

조회 수: 7 (최근 30일)
Jason Chen
Jason Chen 2014년 10월 17일
댓글: Jason Chen 2014년 10월 17일
I want to communicate with an Arduino board using Matlab through a serial port. The Arduino board will transmit 8 bytes of BINARY data every 20 ms once it receives a 1. The transmission will stop if the Arduino receives a 0.
The following commands in an m-file work as expected when I step through them in the debug mode. However, the transmission stopped immediately when I ran the m-file. The s.BytesAvailable is zero. It looks like the pause command has not properly executed. Why was wrong? How can I fix it? Thanks.
s = serial('COM15','BaudRate',115200);
fopen(s);
fwrite(s,1); %ask Arduino to send data
tic;pause(3);toc %wait for several data to arrive
fwrite(s,0); %ask Arduino to stop sending data
fprintf(1,'%i\n\n',s.BytesAvailable)
fclose(s)
  댓글 수: 2
Pritesh Shah
Pritesh Shah 2014년 10월 17일
Check following thing. 1. Did u connect to hardware to com15? (Go to device manager and click on port)
2. set(s, ’BaudRate’, 4800); % Set this Baud Rate
Go through following link may be useful: http://home.iitb.ac.in/~rahul./ITSP/serial_comm_matlab.pdf
Jason Chen
Jason Chen 2014년 10월 17일
The code worked when I stepped through them in the debug mode. The com port and baud rate are not the problem. I think it has to do with the execution timing.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sean de Wolski
Sean de Wolski 2014년 10월 17일
You might be interested in doing this with the support package directly rather than using the serial port yourself
  댓글 수: 1
Jason Chen
Jason Chen 2014년 10월 17일
Mathworks states that the Matlab support package is not real time and the loop time can't be less than 40 ms (25 Hz). I'm using an Arduino for a real time application and like to transfer data to the computer to display later. I hope the serial data buffer can catch the data at faster rate.
I tried the Simulink Arduino support package. It doesn't work well in external mode either, especially for Due.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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!

Translated by