简体   繁体   English

无法使用qtserial将数据写入Arduino

[英]Not able to write data to Arduino using qtserial

I am fairly new to qt programming. 我对qt编程还很陌生。 I am not able to send/write data to Arduino. 我无法向Arduino发送/写入数据。

Reading data from Arduino works expected. 从Arduino读取数据的工作预期。

I am trying to turn-on pin13 led on when Serial.available() > 0 . Serial.available() > 0时,我试图打开引脚Serial.available() > 0 It works expected when I send data using Arduino serial monitor. 当我使用Arduino串行监视器发送数据时,它可以正常工作。 But I am not able to do the same using qt-serial. 但是我不能使用qt-serial来做同样的事情。 I do not get any error messages. 我没有收到任何错误消息。

what approach one should take to debug such issues? 应该采取什么方法来调试此类问题?

serialCom.h serialCom.h

#ifndef SERIALCOM_H
#define SERIALCOM_H

#include <QObject>
#include <QSerialPort>

class SerialCom: public QObject
{
    Q_OBJECT
public:
    SerialCom(QObject *parent = 0);
    ~SerialCom();
    void openSerialPort();
    void closeSerialPort();
    void writeData(const QByteArray &data);    
public slots:
    void handleError(QSerialPort::SerialPortError error);
    void readData();
private:
    QSerialPort *serial;
};

#endif // SERIALCOM_H

serialCom.cpp serialCom.cpp

#include "serialcom.h"
#include <QDebug>
#include <QSerialPortInfo>

SerialCom::SerialCom(QObject *parent):
    QObject(parent)
{
    serial = new QSerialPort(this);

    connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
            SLOT(handleError(QSerialPort::SerialPortError)));
    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}

SerialCom::~SerialCom()
{

}

void SerialCom::openSerialPort()
{
    QSerialPortInfo portToUse;
    foreach (const QSerialPortInfo &port, QSerialPortInfo::availablePorts()) {

        if(port.isNull() || !port.isValid())
        {
            qDebug() << "port is not valid:" << port.portName();
            return;
        }
        if(!port.isBusy() && port.manufacturer().contains("Arduino")){
            portToUse = port;
            qDebug() << tr("Port: %1 || manufacurer: %2").arg(port.portName()).arg(port.manufacturer());
        } else {
            qDebug() << "either port is busy or its not arduino";
        }
    }//forEach

    serial->setPortName(portToUse.portName());
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    if (serial->open(QIODevice::ReadWrite)) {
        qDebug() << "Connected to" << portToUse.description() << "on" << portToUse.portName();
    } else {
        qCritical() << "Serial Port error:" << serial->errorString();

        qDebug() << tr("Open error");
    }
}

void SerialCom::writeData(const QByteArray &data)
{
    if(serial->isOpen() && serial->isWritable()){
        serial->write(data);
        serial->flush();
        qDebug() << "Writing: " << data;
    }else{
        qDebug() << "port not ready to write:" << serial->isWritable();
    }
}

void SerialCom::readData()
{
    QByteArray data = serial->readAll();
    qDebug() << "UART:" << data;
}

void SerialCom::handleError(QSerialPort::SerialPortError error)
{
    qDebug() << error;
}

void SerialCom::closeSerialPort()
{
    serial->close();
    qDebug() << tr("Disconnected");
}

main.cpp main.cpp中

#include <QCoreApplication>
#include "serialcom.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    SerialCom serialCom;

    //open port
    serialCom.openSerialPort();


    //make some stupid data to write to the serial port for testing
    const QByteArray stupid_data = "Hello World";


    //write @stupid_data to serial port
    serialCom.writeData(stupid_data);

    //closing port
//    serialCom.closeSerialPort();

    return a.exec();
}

simpleSerialCom.pro simpleSerialCom.pro

QT += core serialport
QT -= gui

TARGET = simpleSerialCom
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    serialcom.cpp

HEADERS += \
    serialcom.h

I have two relevant Qt projects on github: 我在github上有两个相关的Qt项目:

https://github.com/peteristhegreat/qt-serialport-arduino https://github.com/peteristhegreat/qt-serialport-arduino

https://github.com/peteristhegreat/qt-serial-port-terminal https://github.com/peteristhegreat/qt-serial-port-terminal

See if you can get either one of those working, and then compare to how its done there, with how you are doing it. 看看您能否让其中任何一个正常工作,然后将其与那里的工作方式进行比较。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM