问题
I am now trying to establish the serial communication between Arduino and Matlab. The script is very simple:
Matlab send a number named as
i
to Arduino;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
回答1:
When you open the serial connection, the Arduino resets itself and the bootloader waits for potential sketch upload. This adds some delay before the sketch actually runs.
Add a pause after you open the serial connection.
fopen(s);
pause(3);
There are other options:
Make the Arduino to announce when it's booted up, by sending something to the PC. For example you wait until the Arduino sends an
S
.I am not sure if this is possible with MATLAB, but if you disable the serial's DTR pin, the Arduino won't auto reset.
Burn the code directly with the programmer, so the there is no bootloader which waits at boot. But this also prevents you from uploading the sketch via the USB.
A hardware solution to prevent auto reset. But this prevents the necessary reboot during the sketch uploading, so you have to time the reset manually.
回答2:
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
来源:https://stackoverflow.com/questions/51728813/serial-communication-between-arduino-and-matlab-is-losing-data