Question

I am trying to connect arduino with the qserialport of qt 5.1 but the method open always return false value if somebody knows another way to do it or other library or suggestion to make it work please let me know XD i don't know what is wrong here the source code

#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->serial = new QSerialPort(this);
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
           qDebug() << "Name        : " << info.portName();
           qDebug() << "Description : " << info.description();
           qDebug() << "Manufacturer: " << info.manufacturer();

           this->serial->setPort(info);

    }
       if(this->serial->open(QIODevice::ReadWrite)){

        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        QObject::connect(&serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    }else{
           qDebug() << "Couldn't open port";

    }

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::serialReceived(){
    serial->readAll();
}
Était-ce utile?

La solution

Try this:

main.cpp

#include <QtSerialPort/QtSerialPort>
#include <iostream>

int main()
{
    QSerialPort serialPort;
    serialPort.setPortName("yourportpath"); // Hard code it for a test
    if (!serialPort.open(QIODevice::ReadOnly)) {
        std::cout << "Error: " << serialPort.error() << std::endl;
        return 1;
    }

    return 0;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top