带有 Arduino 的 Matlab 串行接口非常慢 [英] Matlab serial interface with Arduino is very slow

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

问题描述

我正在尝试使用 Arduino 板在 Matlab 中建立串行链接.从板上读取数据很顺利.但是,对于我发送的每个信息块,将数据写入电路板大约需要一秒钟.

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.

我正在运行的用于写入数据的代码:

The code I am running to write data:

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

A、B、C、D 是 0 - 255 之间的 8 位数字,c"和a"是字符命令,用于在 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.

如果我不包括 pause(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 代码的 instructable 链接] 和这里(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.

将此行添加到您的 setup() 函数中:

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.

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

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