Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

Serial communication between Arduino and Matlab is losing data

I am now trying to establish the serial communication between Arduino and Matlab. The script is very simple:

  1. Matlab send a number named as i to Arduino;

  2. Arduino receive this i, then send it back to Matlab;

Repeat step 1&2 for 10 times, i.e., Matlab sends 1,2,...,10 to Arduino, then receive the 1,2,...,10 from Arduino. However, Matlab only get back 3,4,...,10, while the first i=1 and i=2 are lost (I have already made the inputbuffersize=200 now, still not right).

Hereby is the code from Matlab:

clc,clear;
s=serial('COM16','BaudRate',9600); 
s.InputBufferSize = 200;    
fopen(s);
a=10;
rx = zeros(1, a); % rx is used to store the data send back by Arduino
ry = zeros(1, a); % ry is just helping me to see what happens in Serial
for i = 1:1:a
  fwrite(s, i); % Start to write the value "i" through serial to Arduino
  pause(0.5) % if no pause, the result is worse than now
  ry(i) = s.BytesAvailable; % check how many data are there in the Buffer
  if s.BytesAvailable>0
      rx(i) = fread(s, s.BytesAvailable); % Record the data send by Arduino
  end
end
fclose(s);

And the Arduino Code:

char ibyte;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available()>0)
  {
    ibyte=Serial.read();
    Serial.write(ibyte);
  }
}

My reference link is: http://robocv.blogspot.com/2012/01/serial-communication-between-arduino.html

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The link you provide clearly notes that delay(2500); needs to be added to the Arduino setup() code to allow time for rebooting, during which the device may "behave unpredictably" (like dropping sent data?). You may need to pause the MATLAB code to accommodate for that as well, such as adding a pause(2.5) to your code after you open the serial connection (unless the fopen step already waits for the Arduino setup() to finish).

Regarding your MATLAB code, one problem is that your code is set up so that it could possibly move ahead with sending more data before it has a chance to get the prior response. In addition, you should specify the precision of the data you're sending, like 'uint8' for an unsigned 8-bit integer or 'double' for a double-precision number (as is your case).

What you probably want to do is just call fread without checking BytesAvailable first, since fread will block until it receives all of the data specified by the size argument (1 in this case). Only after receiving will your loop move on to the next iteration/message:

for i = 1:1:a
  fwrite(s, i, 'double');
  rx(i) = fread(s, 1, 'double');
end

You could also try using only uint8 data:

rx = zeros(1, a, 'uint8');
for i = 1:1:a
  fwrite(s, uint8(i), 'uint8');
  rx(i) = fread(s, 1, 'uint8');
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...