Qt serial port class Qserialport use notes __QT

Source: Internet
Author: User
Qt serial Port class Qserialport use notes

Although the RS232 interface is no longer available on most home PCs today. However, due to the simple operation of RS232 serial port and reliable communication, there are still a lot of applications in the industrial field. Qt Previous version, did not provide official support for the RS232 serial port, writing serial port program is inconvenient. Now, in Qt5.1, the Qtserialport module is provided to facilitate programmers to quickly develop applications that use serial ports. This article is simple to talk about the use of qtserialport modules.

The current Qtserialport module provides two C + + classes, Qserialport and Qserialportinfo respectively.

The Qserialport class provides a variety of interfaces for manipulating serial ports.

Qserialportinfo is a helper class that provides a variety of information about the serial ports available in your computer. How to use

First, introduce the use of qserialportinfo. Here is a simple example to enumerate all the serial devices on a computer.

First, you need to add the following in the pro file:

QT + + SerialPort

The Cpp file is as follows:

#include <QCoreApplication>
#include <QDebug>
 
#include <QSerialPort>
#include < qserialportinfo>
 
int main (int argc, char *argv[])
{
    qcoreapplication A (argc, argv);
    foreach (const qserialportinfo &info, Qserialportinfo::availableports ())
    {
        qdebug () << "Name:" < < Info.portname ();
        Qdebug () << "Description:" << info.description ();
        Qdebug () << "Manufacturer:" << info.manufacturer ();
        Qdebug () << "serial number:" << info.serialnumber ();
        Qdebug () << "System Location:" << info.systemlocation ();
    }
    return a.exec ();
}

The results displayed on my computer are as follows:

Name: "  COM1"
Description: "  communication port"
Manufacturer:  "(Standard port type)"
serial Number: "  "
System Location:  "\\.\com1"
Name:  "COM5"
Description:  "USB serial Port"
manufacturer:  "FTDI"
Serial number: "  a400g3uxa"
System Location:  "\\.\COM5"

Usually, we need to specify the program to use a certain serial port, this time can not only use the serial name, because the USB port each time inserted in a different USB port can be obtained by the serial name of the change. At this time can use the serial number of serial, this number is generally unique, such as the following code, first traverse all the serial port, to find the serial port we need to return.

#include <QCoreApplication>
#include <QDebug>
#include <QSerialPort>
#include < qserialportinfo>

int main (int argc, char *argv[])
{
    qcoreapplication A (argc, argv);
    Qserialportinfo Com_info;
    foreach (const qserialportinfo &info, Qserialportinfo::availableports ())
    {
        if (info.serialnumber () = = " A400g3uxa ")
        {
            com_info = info;
            break;
        }
    }
    Qdebug () << "Name:" << com_info.portname ();
    Qdebug () << "Description:" << com_info.description ();
    Qdebug () << "SerialNumber:" << com_info.serialnumber ();
    return a.exec ();
}

Qserialport is responsible for the specific serial operation. Select the serial port, you must first open the serial port to set the baud rate and other parameters. All of these parameters are set up and ready to use. The most basic operation is read () and write (). It should be noted that both operations are non-blocking.

Another important signal also needs to be used, that is void Qiodevice::readyread ()

Every time the serial port receives the data will send out this signal. We need to define a slot in our program and connect it to this signal. That way, each time new data arrives, we can read the data in the slot. At this time must be all the data in the serial port buffer read out, you can use ReadAll () to achieve.

The following code fragment gives an example of setting up a serial port.

    M_reader.setport (info);
    if (M_reader.open (qiodevice::readwrite))
    {
        qdebug () << "M_reader.open (Qiodevice::readwrite)";
        M_reader.setbaudrate (qserialport::baud9600);
        M_reader.setparity (qserialport::noparity);
        M_reader.setdatabits (qserialport::D ata8);
        M_reader.setstopbits (qserialport::onestop);
        M_reader.setflowcontrol (Qserialport::noflowcontrol);

        M_reader.clearerror ();
        M_reader.clear ();
        Connect (&m_reader, SIGNAL (Readyread ()), this, SLOT (Readyreadslot ()));


void Dialog::readyreadslot ()
{
    qdebug () << "x";
    Qbytearray arr = M_reader.readall ();
    Do_something (arr);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.