没有收到整个消息Arduino的串行通信 [英] Arduino Serial Communication not receiving entire message

查看:187
本文介绍了没有收到整个消息Arduino的串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Arduino的通信问题。这是相当很难描述,所以我不能适合它的称号。反正这里有以下几点:

I have a problem with the Arduino communication. It's quite hard to describe so I cant fit it in the title. Anyway here are the following:

所以我有这个code为我的接收端:

So I have this code for my receiving end:

if(Serial1.available())
{
    while(Serial1.available())
    {
        uint8_t inByte = Serial1.read();

        inByte = inByte ^ k;

        Serial.write(inByte); 
    }

    Serial.println(" done");
}

它应该当它这样做在同一行和打印完成打印。在 Serial1.available()似乎跳过下 Serial1.available(),我不知道是怎么回事上。反正这里是我的电流,坏,输出:

It's supposed to print in one line and print done when it's done. The Serial1.available() seems to skip the next Serial1.available(), I don't know what's going on. Anyway here's my current, bad, output:

h done
e done
l done
l done
o done

done

当它应该是:

hello done

我很抱歉,如果这可以一直措辞更好,但仅此而已,现在我可以打字,我的脑子是有点痛苦。我从来没有在Windows的C ++控制台应用程序经历过这种行为。

I'm sorry if this could've been phrased better but that's all I can type now, my brain is kinda in pain. I've never experienced this behavior in a Windows c++ console application.

推荐答案

如果您呼叫的循环程序()然后是,它将从串行缓冲器读出并立即返回,因为你可能不发送数据速度不够快

If you are calling that routine in loop() then yes, it will read from the serial buffer and immediately return since you are probably not sending the data fast enough.

有一个更好的办法来处理这​​样的事情是使用控制字符,表示一个消息的末尾或者如果你有你期望接收特定的数据格式,然后继续已进来,直到字符计数达到数据格式限制。

A better way to handle this sort of thing is to use a control char which indicates the end of a message OR if you have a specific data format you expect to receive, then keep a count of the chars which have come in until the data format limit is reached.

有是讨论在这里,你可能会发现有用:使用Arduino的也有串行复式是与Arduino的IDE出货例如素描:菜单:例如:通讯:

There is discussion here which you may find useful: Serial Duplex using Arduino Also there are example sketches that ship with the Arduino IDE: Menu: Examples: Communication:

此外,阅读为Arduino的串行上市下的所有条目。好东西在那里。

Also, read all the entries under the Serial listing for Arduino. Good stuff there.

所以,你与串行输入合作开发常规真的取决于你的项目,您收到的数据类型。在上面的例子中,如果你使用控制字符,它可能是这样的:

So the routine you develop for working with Serial input really depends on your project and the kind of data you are receiving. In your example above, if you were to use a control char, it might look like this:

 while(Serial1.available()){
   char c = Serial1.read();

   if (c == '*'){
       Serial.println(" done");
   } else {
       Serial.write(c); 
   } 
 }

这篇关于没有收到整个消息Arduino的串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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