QT Development (50)--the basis ofQT serial ProgrammingFirst,
Qtserialport
Introduction
1, serial Communication Foundation
at present, the most widely used serial port is The DB9 interface is suitable for communication at a relatively close distance. Generally less than 10 meters. The DB9 interface has 9 pins.
The main parameters of serial communication are as follows:
A, baud rate: measure the speed of communication parameters, indicating the number of bits transmitted per second. For example, 9600 baud indicates that 9,600 bits are sent per second.
B, data bits: measure the actual data bits in the communication parameters, when the computer sends a packet, actually contains the number of valid data bits.
C, stop bit: Used to represent the last one in a single package. Typical values are 1 and 2 bits.
D, parity check bit: Serial communication in a method of error detection. Common error detection methods are: even, odd check.
2.
Qtserialport
Module Introduction
The Qtserialport module is a module of the add-on module in the QT5 , providing a unified interface for both hardware and virtual serial ports.
Because of its simplicity and reliability, the serial port is still used in many industries such as embedded systems and robots. With the Qtserialport module, developers can significantly shorten the cycle of developing serial-related applications.
Qt SerialPort provides basic functionality, including configuration ,I/O operations , acquisition and setting of RS-232 pin signals.
The Qt SerialPort module does not support the following features:
A, the characteristics of the terminal, such as Echo, control cr/lf and so on
B, text mode
C, read or write operation timeout and delay configuration
D, when the RS-232 pin signal Change notification
to use in the application Qtserialport, you need to include the following statements:
#include <QtSerialPort/QtSerialPort>
to link Qtserialport module, you need to add the following in the. Pro file:
QT + = SerialPort
Second,
Qserialport
1.
Qserialport
Introduction
The Qserialport provides an interface function to access the serial port. Use the auxiliary class Qserialportinfo to obtain the available serial port information. the Qserialportinfo Helper class object is used as a parameter, and the Setport () or setportname () function allows you to set the serial device to access.
after you have set up a port, you can use the open () function in a read-only, write-only, or read-write mode.
Note that the serial port is opened in exclusive mode.
use the Close () function to close the serial port and cancel the IO operation.
after the serial port is successfully opened, Qserialport will attempt to determine the current configuration of the serial port and initialize it. You can use setbaudrate (),setdatabits (),setparity (),setstopbits (), and The Setflowcontrol () function configures the port settings again.
There is a pair named Qserialport: Properties of the :d ataterminalready,qserialport::requesttosend
Qserialport provides a series of functions that abort the calling thread until the signal is triggered. These functions are used to block the serial port.
Waitforreadyread (): Block call until new data is readable
Waitforbyteswritten (): block call until data and write to serial port
blocking serial programming is completely different from non-blocking serial programming. Blocking the serial port does not require a time loop and usually simplifies the code. However, in GUI programs, in order to avoid freezing the user interface, blocking serial programming can only be used for non-GUI threads.
Qserialport can also use the Qtextstream and qdatastream flow operators. When trying to use stream operators >> reads, you need to make sure that there is enough data available.
2.
Qserialport
member functions
Qserialport::qserialport (Qobject *parent = q_nullptr)
Qserialport::qserialport (const QString &name, Qobject *parent = q_nullptr)
Qserialport::qserialport (const qserialportinfo &serialportinfo, Qobject *parent = q_nullptr)
constructor function
[Virtual] bool Qserialport::atend () const
returns True if no data is currently readable
[Signal] void qserialport::baudratechanged (Qint32 baudrate, Qserialport::D irections directions)
After the baud rate is changed, the signal is triggered
[Virtual] Qint64 qserialport::bytesavailable () const
Returns the number of bytes of readable data
[Virtual] Qint64 qserialport::bytestowrite () const
Returns the number of bytes of writable data
[Virtual] void Qserialport::close ()
Close the serial port
void Qserialport::setport (const qserialportinfo &serialportinfo)
set the serial port information to Serialportinfo
void Qserialport::setportname (const QString &name)
set the serial port name to name
Third,
Qserialportinfo
1.
Qserialportinfo
Introduction
the Qserialportinfo class provides information about existing serial devices. Use the static member function of the Qserialportinfo class to generate a linked list of Qserialportinfo objects. Each Qserialportinfo object in the list represents a serial port, each of which can be used with the name, system location, description, and manufacturer query. the Qserialportinfo class object can also be used as a parameter to the Setport () member function of the Qserialport class .
2.
Qserialportinfo
member functions
Qserialportinfo::qserialportinfo (const qserialport &port)
Qserialportinfo::qserialportinfo (const QString &name)
Qserialportinfo::qserialportinfo (const qserialportinfo &other)
constructor function
[Static] Qlist<qserialportinfo> Qserialportinfo::availableports ()
Returns the list of available serial ports for the current system
QString qserialportinfo::d escription () const
If the serial port is available, return the description of the serial port
BOOL Qserialportinfo::hasproductidentifier () const
Returns True if there is a valid 16-bit Production Code
BOOL Qserialportinfo::hasvendoridentifier () const
If there is a valid 16-bit manufacturer code, return True
BOOL Qserialportinfo::isbusy () const
returns True if the serial port is currently busy
QString qserialportinfo::manufacturer () const
If the serial port is available, return the manufacturer's name
QString qserialportinfo::p ortname () const
Returns the name of the serial port
Quint16 qserialportinfo::p roductidentifier () const
If the serial port is available, return the serial 16-bit Production Code
QString Qserialportinfo::serialnumber () const
If the serial port is available, return serial number
[Static] Qlist<qint32> qserialportinfo::standardbaudrates ()
Returns the list of available standard baud rates supported by the target platform
void Qserialportinfo::swap (Qserialportinfo &other)
Exchanging Qserialportinfo objects with other
QString qserialportinfo::systemlocation () const
Return to the system location of the serial port
Quint16 Qserialportinfo::vendoridentifier () const
If the serial port is available, the 16-bit manufacturer code is returned
3.
Qserialportinfo
#include <QCoreApplication> #include <QtSerialPort/QtSerialPort> #include <qlist > #include <qdebug> int main (int argc, char *argv[]) { qcoreapplication a (ARGC,&NBSP;ARGV); qlist<qserialportinfo> list = qserialportinfo::availableports (); qdebug () << "total number of availiable ports: " << list.count (); foreach (const qserialportinfo &serialportinfo, list) { qdebug () << "port: " << Serialportinfo.portname (); qdebug () << " location: " << serialportinfo.systemlocation (); qdebug () << "Description: " << serialportinfo.description (); Qdebug () << "manufactutor: " << serialportinfo.manufacturer (); qdebug () << "vendor indentifier: " << serialportinfo.vendoridentifier (); qdebug () << "busy: " << serialportinfo.isbusy (); } return a.exec ();}
This article from "Life is endless, struggle not only" blog, declined reprint!
QT Development (50)--QT Serial Programming Basics