通讯Arduino的C ++不读的Arduino [英] Communication Arduino-C++ do not read Arduino

查看:263
本文介绍了通讯Arduino的C ++不读的Arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

QSerialPort arduPort("COM5");
arduPort.setBaudRate(QSerialPort::Baud9600);
arduPort.setDataBits(QSerialPort::Data8);
arduPort.setParity(QSerialPort::NoParity);
arduPort.setStopBits(QSerialPort::OneStop);
arduPort.setFlowControl(QSerialPort::NoFlowControl);
arduPort.open(QSerialPort::ReadWrite);
cout<<arduPort.isReadable()<<endl;
cout<<arduPort.isWritable()<<endl;
arduPort.write("a");
QByteArray s=arduPort.readAll();

cout<<QString(s).toStdString()<<endl;

和下一个code在Arduino的:

And the next code in Arduino:

int inByte = 0;

void setup()
{
    Serial.begin(9600);
    while(!Serial){;}
    int i=0;
}

void loop()
{
     if(Serial.read()=='a')
         Serial.write('b');  
}

首先我发送一个A到Arduino和Arduino的必须使用B作出回应。但是,当我读了Arduino的港口,我只收到''。

First I send an 'a' to the Arduino, and the ARduino must respond with 'b'. But when I read the port of the Arduino, I recieve '' only.

任何人都知道,为什么我收到''而不是'B'?感谢您的时间。

Anyone knows why I recieve '' instead of 'b'? Thanks for your time.

推荐答案

更新:看到这个答案的答案底部 TL; DR:你有这么设置波特率(和presumably所有其他设置)的之后的打开端口。

Update: See bottom of this answer for the answer. TL;DR: You have so set the baud rate (and presumably all the other settings) after you open the port.

我相信这是Windows实现QSerialPort的错误。我没能还缩小的原因,但我有以下症状:

I believe this is a bug in the Windows implementation of QSerialPort. I haven't been able to narrow down the cause yet but I have the following symptoms:


  1. 装入的Arduino(Uno在我的情况下,莱昂纳多可能表现非常不同)与ASCII演示。拔下并重新插入Arduino的。需要注意的是TX灯不亮。

  1. Load the Arduino (Uno in my case; Leonardo may behave very differently) with the ASCII demo. Unplug and replug the Arduino. Note that the TX light doesn't come on.

连接到它用腻子或Arduino的串口监视器。这将重置Arduino的,然后打印的ASCII表。该TX灯亮持续符合市场预期。

Connect to it with Putty or the Arduino serial port monitor. This resets the Arduino and then prints the ASCII table. The TX light is on continuously as expected.

拔出/重新插入Arduino的,这一次连接到它与QSerialPort程序。这一次,尽管端口被打开ok了TX光永远不会到来的和 readyRead()永远不会触发。还要注意的是阿尔杜伊诺不会因为重置缺省QSerialPort不改变DTR。如果你这样做 QSerialPort :: setDataTerminalReady(假); 然后暂停10毫秒然后将其设置真正它的的重置Arduino的预期,但它仍然不发送。

Unplug/replug the Arduino and this time connect to it with a QSerialPort program. This time despite the port being opened ok the TX light never comes on and readyRead() is never triggered. Also note that the Arduino is not reset because by default QSerialPort does not change DTR. If you do QSerialPort::setDataTerminalReady(false); then pause for 10ms then set it true it will reset the Arduino as expected but it still doesn't transmit.

请注意,如果您有连续发送数据一个Arduino程序(ASCII例如停止),如果你打开​​的端口用腻子,使其开始传输和然后的有QSerialPort打开,除非拔出电缆,将工作!但是只要你拔掉/插上再次停止工作的电缆。

Note that if you have an Arduino program that transmits data continuously (ASCII example stops), if you open the port with putty so that it starts transmitting and then open it with QSerialPort without unplugging the cable it will work! However as soon as you unplug/plug the cable it stops working again.

这让我怀疑腻子设置由Arduino的需要和复位,当你重新插入电缆一些串口选项。 QSerialPort显然不更改此值。

This makes me suspect that putty is setting some serial port option that is required by the arduino and reset when you replug the cable. QSerialPort obviously doesn't change this value.

下面是使用腻子,据我可以告诉设置:

Here are the settings used by Putty as far as I can tell:

dcb.fBinary = TRUE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = FALSE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fAbortOnError = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.BaudRate = ...;
dcb.ByteSize = ...;

和由<一个href=\"https://qt.gitorious.org/qt/qtserialport/source/e08753474802607611532590b58816f0eee30b5f%3asrc/serialport/qserialport_win.cpp#L248-271\"相对=nofollow> QSerialPort :

dcb.fBinary = TRUE;
dcb.fDtrControl = unchanged!
dcb.fDsrSensitivity = unchanged!
dcb.fTXContinueOnXoff = unchanged!
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = unchanged!
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.BaudRate = ...;
dcb.ByteSize = ...;

所以我觉得一定是这使得Arduino的认为这是不连接这些不变的价值观之一。从href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa363214%28v=vs.85%29.aspx\" rel=\"nofollow\"> DCB文件我怀疑 fTxContinueOnXoff

好吧,我会写一个小程序来读取这些设置,并看看有什么变化。

Ok I am going to write a little program to read these settings and see what changes.

好吧,我写我的程序,并提出了以下发现。运行腻子,只是我的Qt程序后的差异:

Ok I wrote my program and made the following discovery. The differences after running putty and just my Qt program were:


  • 波特率:这不是组由QT !!!!!!! 原来你只能设定后,的波特率的打开端口。。否则,它是在previous值留下了0当你在第一线插头。

  • fDtrControl:被Qt用腻子设置为1,保留为0

  • fOutX和fInX:二者均腻子设置为1,被Qt保留为0

  • BaudRate: THIS WASN'T SET BY QT!!!!!!! It turns out you can only set the baud rate after you open the port.. Otherwise it is left at the previous value which is 0 when you first plug in the cable.
  • fDtrControl: Set to 1 by Putty, left at 0 by Qt.
  • fOutX and fInX: Both also set to 1 by Putty and left at 0 by Qt.

动我所有的集后...()函数它完美地工作开后调用。我没有与DtrControl或输出/ INX捣鼓。 (虽然我还设置DTR高手动)。

After moving all my set...() function calls after the open it worked perfectly. I didn't have to fiddle with DtrControl or Out/InX. (Although I have also set DTR high manually.)

在设定我认为这将是设置错误政策跳过一个好主意的所有参数。千万不要这么做!离开它忽略!否则,一切都乱搞了,并增加了怪异的延迟,所有通信。

While setting all the parameters I thought it would be a good idea to set the error policy to 'skip'. DON'T DO THIS! LEAVE IT ON IGNORE! Otherwise it fucks everything up and adds weird delays to all your communications.

这篇关于通讯Arduino的C ++不读的Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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