Not able to write data to Arduino using qtserial

173 Views Asked by At

I am fairly new to qt programming. I am not able to send/write data to Arduino.

Reading data from Arduino works expected.

I am trying to turn-on pin13 led on when Serial.available() > 0. It works expected when I send data using Arduino serial monitor. But I am not able to do the same using qt-serial. I do not get any error messages.

what approach one should take to debug such issues?

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

#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

#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

QT += core serialport
QT -= gui

TARGET = simpleSerialCom
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp \
    serialcom.cpp

HEADERS += \
    serialcom.h
1

There are 1 best solutions below

1
On

I have two relevant Qt projects on github:

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

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.