Qt automatically searches the serial port number list

Source: Internet
Author: User

@ Function:
Serialportlist class enables real-time scanning of currently available serial ports.
Or when the existing serial port disappears, the serialportlist class will issue a qstringlist type
The onnewserialport and qstringlist objects store the currently available serial devices.

@ Usage:
1. the QT version must be later than 5.1;

2. Add the qserialport module when creating the QT project file. That is, in the XXX. Pro File
Qt + = SerialPort

3. Define a Cao in the application to respond to the serialportlist signal and obtain the serial port list.
Void onnewportlist (qstringlist portname ){
UI-> comboboxcomlist-> clear ();
UI-> comboboxcomlist-> additems (portname );
}

4. Construct the serialprot object in the application and connect the onnewserialport signal:
Portlist = new serialportlist (200 );
Connect (portlist, signal (onnewserialport (qstringlist )),\
This, slot (onnewportlist (qstringlist )));

5. Enable Scanning
Portlist-> scanstart ();

 

 

Serialportlist. h

# Ifndef serialportlist_h # define serialportlist_h # include <qtimer> # include <qdebug> # include <qserialport> # include <qstringlist> # include <qserialportinfo>/* @ communication: * worldsing.cnblogs.com ** @ function: * serialportlist class scans the currently available serial ports in real time. When a new serial port * or an existing serial port disappears, the serialportlist class sends out a * signal of the qstringlist type onnewserialport and stores the currently available serial devices in the qstringlist object. ** @ Usage: * 1. The QT version must be later than 5.1. ** 2. Add the qserialport module when creating the QT project file. That is, in XXX. add QT + = SerialPort ** to the Pro file *. Define a Cao in the application to respond to the serialportlist signal and obtain the serial port list * void onnewportlist (qstringlist portname) {* UI-> comboboxcomlist-> clear (); * UI-> comboboxcomlist-> additems (portname); *} ** 4. Build the serialprot object in the application, signal of onnewserialport connection: * portlist = new serialportlist (200); * connect (portlist, signal (onnewserialport (Q Stringlist), * This, slot (onnewportlist (qstringlist); ** 5. enable scanning * portlist-> scanstart (); */# define debug_infor_en 0 class serialportlist: public qobject {q_objectpublic: serialportlist (); serialportlist (quint16 );~ Serialportlist (); quint16 scancyclems; qtimer * timer; qstringlist oldportstringlist; void scanstart (); void scanstop (); Public slots: void ontimeout (); signals: void onnewserialport (qstringlist) ;};# endif // serialportlist_h

 

Serialportlist. cpp

# Include "serialportlist. H "// Default Construction serialportlist: serialportlist () {timer = new qtimer; scancyclems = 200; oldportstringlist. clear (); Connect (timer, signal (timeout (), this, slot (ontimeout () ;}// custom scan cycle to construct serialportlist :: serialportlist (quint16 cyclems) {timer = new qtimer; scancyclems = cyclems; oldportstringlist. clear (); Connect (timer, signal (timeout (), this, slot (ontimeout ();} serialportlist ::~ Serialportlist () {Delete timer;} // start scanning void serialportlist: scanstart () {timer-> stop (); timer-> Start (scancyclems );} // stop scanning void serialportlist: scanstop () {timer-> stop ();} // periodic scanning service void serialportlist: ontimeout () {qstringlist newportstringlist; // search for the serial foreach (const qserialportinfo & info, qserialportinfo: availableports () {# If debug_infor_en qdebug () <"name:" <info. portname (); qdebug () <"descr Iption: "<info. description (); qdebug () <"Manufacturer:" <info. manufacturer (); # endif newportstringlist + = info. portname ();} // update the old serial port list if (newportstringlist. size ()! = Oldportstringlist. Size () {oldportstringlist = newportstringlist; emit onnewserialport (oldportstringlist );}}

Qt automatically searches the serial port number list

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.