Basic Communications with QSerialPort
the code looks a bit confuse. You open all port available and then you try to do something wrong.
NOTE: You use a GUI application like a shell application. It is wrong.
Try:
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QTextStream>
#include <QFile>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSerialPort serial;
serial.setPortName("COM19");
if(serial.open(QIODevice::ReadWrite)){
//Now the serial port is open try to set configuration
if(!serial.setBaudRate(QSerialPort::Baud57600))
qDebug()<<serial.errorString();
if(!serial.setDataBits(QSerialPort::Data8))
qDebug()<<serial.errorString();
if(!serial.setParity(QSerialPort::NoParity))
qDebug()<<serial.errorString();
if(!serial.setStopBits(QSerialPort::OneStop))
qDebug()<<serial.errorString();
if(!serial.setFlowControl(QSerialPort::NoFlowControl))
qDebug()<<serial.errorString();
//If any error was returned the serial il corrctly configured
serial.write("M114 \n");
//the serial must remain opened
if(serial.waitForReadyRead(100)){
//Data was returned
qDebug()<<"Response: "<<serial.readAll();
}else{
//No data
qDebug()<<"Time out";
}
//I have finish alla operation
serial.close();
}else{
qDebug()<<"Serial COM19 not opened. Error: "<<serial.errorString();
}
MainWindow w;
w.show();
return a.exec();
}
MrSynAckSter
Updated on June 04, 2022Comments
-
MrSynAckSter almost 2 years
I am trying to rig up some basic serial communications in QT I am getting the port COM19 from QSerialPortInfo, and I speaking successfully to the port via Arduino. However, I cannot get anything back via QT.
#include "mainwindow.h" #include <QApplication> #include <QDebug> #include <QTextStream> #include <QFile> #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> int main(int argc, char *argv[]) { QApplication a(argc, argv); foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){ qDebug() << "Name :" << info.portName(); qDebug() << "Description :" << info.description(); qDebug() << "Manufactuer :" << info.manufacturer(); QSerialPort serial; serial.setPort(info); if(serial.open(QIODevice::ReadWrite)) qDebug() << serial.errorString(); serial.write("M114 \n"); qDebug() << serial.readAll(); serial.close(); // Now we need to send and receive commands serial.setPortName("COM19"); serial.setBaudRate(QSerialPort::Baud57600); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); if(serial.open(QIODevice::ReadWrite)){ qDebug() << "opened"; }else{ qDebug() << "Not opened"; } qDebug() << serial.errorString(); serial.write("M114 \n"); qDebug() << serial.readAll(); serial.close(); } MainWindow w; w.show(); return a.exec(); }
As you can see, I am trying a simple connection along the lines of the documentation, and one where I write out all the baud rate information. They throw two different errors.
Like I said, I am connecting via arduino to this same port and having success. Any ideas what's wrong?
Name : "COM19" Description : "USB Serial (Communication Class, Abstract Control Model)" Manufactuer : "PJRC.COM, LLC." "Unknown error" "" opened "The handle is invalid." ""
Any ideas for what I am doing wrong?
My idea is to send commands to the device, and read them back to the console.