QT C ++与Arduino Leonardo的沟通

时间:2015-02-02 11:19:38

标签: c++ arduino qt-creator

所以基本上我一直在努力解决这个问题,我可以用qt c ++向leonardo发送信息,但是当我尝试发送另一条消息时,由于某种原因它没有到达,使用autoit(所以我100%确信我在leonardo上使用的代码完美无瑕,而且我在c ++中仍然是一个大菜鸟)

void _sendcomm(QByteArray _send2comm) {
if (serial.isOpen() && serial.isWritable())
{
        serial.write(_send2comm);
        serial.flush();
}
else {
    qDebug() << "fail ;/"; //however even when calling this the second time this doesn't set off...
     }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    _start();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
            //qDebug() << "Name        : " << info.portName();
            //qDebug() << "Description : " << info.description();
            //qDebug() << "Manufacturer: " << info.manufacturer();
            if(info.description() == "Arduino Leonardo") {
                _aport = info.portName();
            }
            serial.setPort(info);
        }
    if(_aport.isEmpty()) {
        qDebug() << "none found";
    }
    else {
        serial.setPortName(_aport);
        serial.open(QIODevice::ReadWrite);
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::TwoStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);
                _sendcomm("light;1\r\n"); // this one arrived on the arduino
                Sleep(1500);
                _sendcomm("light;120\r\n"); // this one never arrives, I created the input on the leonardo so when it receives light;num it set's the brightness of the lcd keypadshield.

                serial.close();
    }
    w->show();

    return a.exec();
}

是否有人可以指出我犯了错误和/或如何解决这个问题?

0 个答案:

没有答案