Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Itop4412 Development Board Qt Serial Port programming to realize Serial Port function

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "itop4412 development board Qt serial port programming to achieve serial port function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "itop4412 development board Qt serial port programming to achieve serial port function" bar!

1. Edit the project file (a file with the suffix .pro) add serialport after QT + = core gui.

two。 Obtain serial port automatically

Use QSerialPortInfo:::availablePorts () to get the current serial port. This function returns the container class Qlist, which is defined by Qt.

The foreach keyword traverses the serial port information in the container Qlist and puts the serial port information into the class object serialNamePort of QStringList

Display the serial port components to the ui.

{

{

Ui- > setupUi (this)

QStringList serialNamePort

/ / traversal: serial port information returned by availablePorts ()

Foreach (const QSerialPortInfo & info, QSerialPortInfo::availablePorts ()) {

SerialNamePort serialCb- > addItems (serialNamePort)

}

Click the serial port selection box after compilation, and the connected serial port will appear.

3. Open serial port function and property settings

Step 1: instantiate the serial port class QSerialPort object serialPort. The operation on the serial port is the operation on the serialPort object. Call the

The member variables (attributes) and member functions (functions) encapsulated by QSerialPort can control the serial port.

Class Example: public QMainWindow

{

Public:

. QSerialPort * serialPort

. }

Ui (new Ui::Example)

{

Ui- > setupUi (this)

.

SerialPort = new QSerialPort

. }

Step 2: fill in the attributes such as baud rate, data bit, stop bit, check bit, etc. Get the serial port information passed by the ui component, and change the serial port to

Property is populated into the serialPort object.

Step 3: open the serial port and judge whether it is successful or not.

/ * Open button * /

Void Example::on_openCb_clicked ()

{

QSerialPort::BaudRate bauRate; / / Porter rate

QSerialPort::DataBits dataBits; / / data bit

QSerialPort::StopBits stopBits; / / stop bit

QSerialPort::Parity checkBits; / / check bit

/ / set baud rate

If (ui- > baudCb- > currentText () = = "4800") {bauRate = QSerialPort::Baud4800;}

Else if (ui- > baudCb- > currentText () = = "9600") {bauRate = QSerialPort::Baud9600;}

Else if (ui- > baudCb- > currentText () = = "115200") {bauRate = QSerialPort::Baud115200;}

/ / set data bits

If (ui- > dataCb- > currentText () = = "5") {dataBits = QSerialPort::Data5;}

Else if (ui- > dataCb- > currentText () = = "6") {dataBits = QSerialPort::Data6;}

Else if (ui- > dataCb- > currentText () = = "7") {dataBits = QSerialPort::Data7;}

Else if (ui- > dataCb- > currentText () = "8") {dataBits = QSerialPort::Data8;}

/ / set the stop bit

If (ui- > stopCb- > currentText () = = "1") {stopBits = QSerialPort::OneStop;}

Else if (ui- > stopCb- > currentText () = "1.5") {stopBits = QSerialPort::OneAndHalfStop;}

Else if (ui- > stopCb- > currentText () = = "2") {stopBits = QSerialPort::TwoStop;}

/ / set check bit

If (ui- > checkCb- > currentText () = = "none") {checkBits = QSerialPort::NoParity;}

/ / populate the attribute values of the serial port object

SerialPort- > setPortName (ui- > serialCb- > currentText ())

SerialPort- > setBaudRate (bauRate)

SerialPort- > setDataBits (dataBits)

SerialPort- > setStopBits (stopBits)

SerialPort- > setParity (checkBits)

/ / Open the serial port after setting the properties

If (serialPort- > open (QIODevice::ReadWrite) = = true) {

QMessageBox::information (this, "prompt", "success")

} else {

QMessageBox::critical (this, "prompt", "failure")

}

}

4. The function of sending and receiving serial data

Read data: every time the data stream arrives at the system from the serial port, it will be transmitted to the Qt application, and the readyRead signal will trigger a

So you can use the signal and slot mechanism described in the previous section to bind the readyRead signal to the slot function, which can then be read in the slot function

Serial port data. ReadAll () is used in the slot function to read data, and appendPlainText () with newline function is used to display to the receive window of ui.

/ / declare slot functions in the class

Private slots:

Void serialPortReadyRead_Solt (void)

/ / readyRead signal and slot function binding

Connect (serialPort,SIGNAL (readyRead ()), this,SLOT (serialPortReadyRead_Solt ()

/ / read serial port

Void Example::serialPortReadyRead_Solt (void)

{

QString buf

Buf = QString (serilaPort- > readAll ())

Ui- > recvEdit- > appendPlainText (buf)

}

Write data: get the information filled in the ui interface, ui- > sendEdit- > text (), and use the QSerialPort member function write to write the data to

serial port.

Void Widget::on_sendBt_clicked ()

{

SerilaPort- > write (ui- > sendEdit- > text (). ToLocal8Bit (). Data ())

}

5. Turn off serial port function

Use the QSerialPort member function close () to close the serial port.

Void Widget::on_closeBt_clicked ()

{

SerilaPort- > close ()

}

6. Clear the send bar data

The data can be cleared by calling the member function clear of the ui component lineEdit.

Void Widget::on_clearBt_clicked ()

{

Ui- > recvEdit- > clear ()

}

Compile the test, and the result is shown in the figure:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report