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

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

问题描述

我有以下代码:

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;

以及 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');  
}

首先我向 Arduino 发送一个a",而 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:您已经设置了波特率(大概所有其他设置)打开端口之后.

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.

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

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. 使用 ASCII 演示加载 Arduino(在我的情况下为 Uno;Leonardo 的行为可能非常不同).拔下并重新插入 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.

使用 Putty 或 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 程序连接到它.这一次,尽管端口打开正常,但 TX 灯永远不会亮起并且 readyRead() 永远不会被触发.另请注意,Arduino 不会重置,因为默认情况下 QSerialPort 不会更改 DTR.如果你执行 QSerialPort::setDataTerminalReady(false); 然后暂停 10ms 然后设置它 true按预期重置 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 示例停止),如果您使用 putty 打开端口以使其开始传输,然后 然后 使用 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.

这让我怀疑 putty 正在设置一些 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.

据我所知,以下是 Putty 使用的设置:

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 = ...;

通过 :

And by 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 认为它没有连接的未更改值之一.来自 DCB 文档 我怀疑 fTxContinueOnXoff.

So I think it must be one of those unchanged values which makes the Arduino think that it isn't connected. From the DCB documentation I suspect fTxContinueOnXoff.

好的,我要写一个小程序来读取这些设置,看看有什么变化.

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

好的,我写了我的程序并做出了以下发现.运行 putty 和我的 Qt 程序后的区别是:

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

  • 波特率:这不是由 QT 设置的!!!!!!!事实证明,您只能在打开端口后设置波特率..否则,它会保留在您第一次插入电缆时的先前值为 0 的值.
  • fDtrControl:Putty 设置为 1,Qt 设置为 0.
  • fOutX 和 fInX:两者也被 Putty 设置为 1,而被 Qt 设置为 0.

在打开后移动我所有的 set...() 函数调用后,它运行良好.我不必摆弄 DtrControl 或 Out/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天全站免登陆