0

Arduino に接続されている WIN7 上の qt5 アプリケーションから comport に char (つまり "R") を送信しようとすると問題が発生します。私はArduinoのLEDを点滅させるつもりで、Arduinoの部分は正常に動作します。

これが私のqtコードです:

#include <QTextStream>
#include <QCoreApplication>
#include <QtSerialPort/QSerialPortInfo>
#include <QSerialPort>
#include <iostream>
#include <QtCore>

QT_USE_NAMESPACE

using namespace std;
QSerialPort serial;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextStream out(stdout);
    QList<QSerialPortInfo> serialPortInfoList = QSerialPortInfo::availablePorts();

    out << QObject::tr("Total number of ports available: ") << serialPortInfoList.count() << endl;


    foreach (const QSerialPortInfo &serialPortInfo, serialPortInfoList) {
        out << endl
            << QObject::tr("Port: ") << serialPortInfo.portName() << endl
            << QObject::tr("Location: ") << serialPortInfo.systemLocation() << endl
            << QObject::tr("Description: ") << serialPortInfo.description() << endl
            << QObject::tr("Manufacturer: ") << serialPortInfo.manufacturer() << endl
            << QObject::tr("Vendor Identifier: ") << (serialPortInfo.hasVendorIdentifier() ? QByteArray::number(serialPortInfo.vendorIdentifier(), 16) : QByteArray()) << endl
            << QObject::tr("Product Identifier: ") << (serialPortInfo.hasProductIdentifier() ? QByteArray::number(serialPortInfo.productIdentifier(), 16) : QByteArray()) << endl
            << QObject::tr("Busy: ") << (serialPortInfo.isBusy() ? QObject::tr("Yes") : QObject::tr("No")) << endl;
    }

    serial.setPortName("COM5");
    serial.open(QIODevice::ReadWrite);
    serial.setBaudRate(QSerialPort::Baud9600);
    serial.setDataBits(QSerialPort::Data8);
    serial.setParity(QSerialPort::NoParity);
    serial.setStopBits(QSerialPort::OneStop);
    serial.setFlowControl(QSerialPort::NoFlowControl);




        if(!serial.isOpen())
        {
          std::cout<<"port is not open"<<endl;
          //serial.open(QIODevice::ReadWrite);
        }

    if(serial.isWritable()==true)
    {
        std::cout<<"port writable..."<<endl;
    }



    QByteArray data("R");

    serial.write(data);
    serial.flush();
    std::cout<<"value sent!!! "<<std::endl;
    serial.close();

    return 0;
}

私のソース コードは 2 つの部分で構成されています。

1- serialportinfolist .... 正常に動作します 2- データを開いて書き込む... コードを実行しても問題はなく、ディスプレイには何も問題がなかったかのように結果が表示されます。

ただし、このコードを実行してもボードの LED は点灯しません。

これをArduinoシリアルモニターでテストすると、オンになりますが、Qtからオンにできません。

4

2 に答える 2

0

Are you waiting for cr lf (0x0D 0x0A) in your arduino code?

QByteArray ba;
ba.resize(3);
ba[0] = 0x5c; //'R'
ba[1] = 0x0d;
ba[2] = 0x0a;

Or append it to your string with

QByteArray data("R\r\n");

Or

QByteArray data("R\n");
于 2014-01-25T01:46:34.817 に答える
0

部分的な解決策を見つけたと思いますが、まだ不完全です。

初めてデバッグを押すと、qt は Arduino に信号を送信しませんが、2 回目にデバッグを押すと、期待どおりに動作します。

それで、それを機能させるために2回実行しなければならないのはとても奇妙ではありませんか???

問題が別の場所にある場合はお知らせください。

何か助けて...

于 2014-02-01T00:57:35.113 に答える