Qt serial

QtSerialPort

https://qt-project.org/wiki/QtSerialPort

http://qt-project.org/doc/qt-5/qserialport.html

Currently, the module API contains two classes: QSerialPort and QSerialPortInfo

QSerialPort is the base class of the module and provides a set of basic methods and properties to access resources on serial ports.

QSerialPortInfo is a helper class. It provides information on the available serial ports on the system.

 

 

1

Below is a simple example of main.cpp :

  1. #include <QtCore/QCoreApplication>
  2. #include <QtCore/QDebug>
  3.  
  4. #include <QtSerialPort/QSerialPort>
  5. #include <QtSerialPort/QSerialPortInfo>
  6. QT_USE_NAMESPACE
  7. int main(int argc, char *argv[])
  8. {
  9.     QCoreApplication a(argc, argv);
  10.     // Example use QSerialPortInfo
  11.     foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
  12.         qDebug() << “Name        : “ << info.portName();
  13.         qDebug() << “Description : “ << info.description();
  14.         qDebug() << “Manufacturer: “ << info.manufacturer();
  15.         // Example use QSerialPort
  16.         QSerialPort serial;
  17.         serial.setPort(info);
  18.         if (serial.open(QIODevice::ReadWrite))
  19.             serial.close();
  20.     }
  21.     return a.exec();
  22. }

Note:
CONFIG += serial port
QT += serialport
must be the first or second line in your .pro file.

.

QextSerialPort *port = new QextSerialPort(<portname>, <Querymode>);

to add the setting like databit, stopbit, etc syntax is as follows:

port->setBaudRate(BAUD_9600);

port->setStopBit();

port->setDataBit(); etc..

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Leave a comment