matlab通讯丢失,Arduino和Matlab之间的串行通信丢失d

我现在正在尝试建立Arduino和Matlab之间的串行通信。脚本非常简单:Matlab将一个名为i的数字发送给Arduino;

Arduino接收到这个i,然后将其发送回Matlab;

重复步骤1&2 10次,即Matlab向Arduino发送1,2,…,10,然后从Arduino接收1,2,…,10。但是,Matlab只返回3,4,…,10,而第一个i=1和i=2丢失(我已经将inputbuffersize设置为200,仍然不对)。在

下面是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);

以及Arduino代码:

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值