MATLAB与Arduino的串行接口的速度很慢 [英] Matlab serial interface with Arduino is very slow

查看:2845
本文介绍了MATLAB与Arduino的串行接口的速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立在Matlab串行链路与Arduino板。从板上读取数据顺利。不过,写数据到板大约需要第二次为我送的信息每个块。

I am trying to establish a serial link in Matlab with an Arduino board. Reading data from the board goes well. However, writing data to the board takes about a second for each block of information I send.

在code我正在写数据:

The code I am running to write data:

S =串行(comprt,波特率,9600,数据位,8); 结果
的fopen(S); 结果
fprintf中(S,'%C','C'); 结果
fprintf中(S,'%U%U%U%U \\ N',[A B C D]); 结果
暂停(1); 结果
fprintf中(S,'%C','A'); 结果
暂停(1);

s = serial(comprt,'BaudRate',9600,'DataBits',8);
fopen(s);
fprintf(s, '%c', 'c');
fprintf(s, '%u %u %u %u \n', [A B C D]);
pause(1);
fprintf(s, '%c', 'a');
pause(1);

A,B,C,D为0 8位数字的任何地方 - 255,'C'和'一'是做在Arduino板的东西,挖掘到主板上的固件字符的命令。

A, B, C, D are 8-bit numbers anywhere from 0 - 255, 'c' and 'a' are characters commands that do stuff on the Arduino board and tap into the firmware on the board.

如果我不包括暂停(1)命令,所以当我不从至少第二执行下一个命令停止MATLAB,串行信息没有打通。

If I do not include the pause(1) commands, so when I do not stop Matlab from executing the next command for at least a second, the serial information doesn't get through.

谁能帮我加快写东西串口?我与Arduino的编辑检查,当我通过他们的界面输入等效命令,一切都很好。因此,延迟不相关的Arduino板或设备驱动程序,这绝对是对事物的Matlab的一面。

Can anyone help me to speed up writing stuff to the serial port? I checked with the Arduino editor, and when I enter equivalent commands via their interface, everything is fine. So the delays are not related to the Arduino board or device drivers, it's definitely on the Matlab side of things.

推荐答案

我用MATLAB颇有几分与Arduino的。例如:在这里看到(的http://www.instructables.com/id/Arduino-to-MATLAB-GUI-Live-Data-Acquisition-Plotti/) [见造说明链接的GitHub我的Arduino和MATLAB code]和这里( https://开头www.youtube.com/watch?v=wY3oh2GIfCI )。

I have used MATLAB quite a bit with Arduino. Ex: see here (http://www.instructables.com/id/Arduino-to-MATLAB-GUI-Live-Data-Acquisition-Plotti/) [see link in instructable for my GitHub Arduino and MATLAB code] and here (https://www.youtube.com/watch?v=wY3oh2GIfCI).

我相信,在事你身边的Arduino的问题。

I believe your problem IS on your Arduino side of things.

这行添加到您的设置()函数:

Add this line to your setup() function:

Serial.setTimeout(100); //this will make the Arduino wait a max of only 100ms per incoming set of serial data, before moving on

在这里阅读: http://arduino.cc/en/Serial/SetTimeout

然后,逐步减少超时,直到你得到不好的结果,最大限度地减少浪费的等待时间。然后,再次增加了一点,以确保它设置得足够高。

Then, decrease the timeout progressively until you get bad results, to minimize wasted waiting time. Then increase it a bit again to ensure it's set high enough.

这是一个快速和肮脏的方法。基本上,你的Arduino是设置为等待默认1秒持续,一旦传入的数据在读取之前。

This is a quick and dirty method. Basically, your Arduino is set to wait 1 sec by default before continuing on, once incoming data is read in.

有一个更好的方法是使用一个终止符。例如:有MATLAB发送终止换行符,并使用Arduino的功能Serial.readBytesUntil读取到终止字符。然后,串行输入超时永远不会达到,并且可以设置超时时间长再次(例如:1秒),而无需实际等待时延

A better method is to use a terminating character. Ex: have the MATLAB send a terminating Newline character, and use the Arduino function Serial.readBytesUntil to read up to the terminating character. Then, the serial input timeout will never be reached, and you can set the timeout to be long again (Ex: 1 sec), without actually having to wait for that delay.

这是否解决问题了吗?如果是的话,请用旁边的投票按钮,复选框,以纪念这是正确的答案。谢谢你。

Does this solve your problem? If so, please use the check box next to the voting buttons to mark this as the correct answer. Thanks.

这篇关于MATLAB与Arduino的串行接口的速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆