2118 views|2 replies

164

Posts

0

Resources
The OP
 

itop4412 development board Qt serial port programming-realizing serial port functions [Copy link]

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


2. Automatically obtain the serial port
Use QSerialPortInfo:::availablePorts() to obtain the current serial port. This function returns the container class Qlist. Use the Qt-defined keyword foreach to traverse the serial port information in the container Qlist, and put the serial port information into the class object serialNamePort of QStringList, and display it in the serial port component of the ui.
{
{
ui->setupUi(this);
QStringList serialNamePort;
//Traverse the serial port information returned by: availablePorts()
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){
serialNamePort << info.portName();
}
ui->serialCb->addItems(serialNamePort);
}
After compiling, click the serial port selection box, and the connected serial port will appear.

3. Open the serial port function and property settings
Step 1: Instantiate the serial port class QSerialPort object serialPort. The operation of the serial port is the operation of the serialPort object. The serial port can be controlled by calling the member variables (properties) and member functions (functions) encapsulated by QSerialPort.
class Example : public QMainWindow
{
public:
.......... QSerialPort * serialPort;
.......... };
ui(new Ui::Example)
{
ui->setupUi(this);
......
serialPort = new QSerialPort;
...... }
Step 2: Fill in the baud rate, data bits, stop bits, check bits and other properties. Get the serial port information passed by the ui component and fill the serial port properties into the serialPort object.

Step 3: Open the serial port and determine whether it is opened successfully.
/*Open button*/
void Example:n_openCb_clicked()
{
QSerialPort::BaudRate bauRate; //Baud rate
QSerialPort:ataBits dataBits; //Data bits
QSerialPort::StopBits stopBits; //Stop bits
QSerialPort:arity checkBits; //Check bit
//Set baud rateif
(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 bitsif
(ui->dataCb->currentText() == "5") { dataBits = QSerialPort: ata5;}
else if(ui->dataCb->currentText() == "6") { dataBits = QSerialPort: ata6;}
else if(ui->dataCb->currentText() == "7") { dataBits = QSerialPort: ata7;}
else if(ui->dataCb->currentText() == "8") { dataBits = QSerialPort: ata8;}
//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 the check bit
if(ui->check Cb->currentText() == "none" ) { checkBits = QSerialPort::NoParity; }
//Fill in the property 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. Serial port data sending and receiving functions
Reading data: Every time the data stream arrives at the system from the serial port, it will be transmitted to the Qt application once, and the readyRead signal will be triggered once, so the signal and slot mechanism described in the previous chapter can be used to bind the readyRead signal and the slot function, and then the serial port data can be read in the slot function. In the slot function, readAll() is used to read the data, and appendPlainText() with line break function is used to display it to the receiving window of the ui.
//Declare slot function in class
private slots:
void serialPortReadyRead_Solt(void);
//Binding readyRead signal and slot function
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(), use QSerialPort's member function write to write data to
the serial port.
void Widget:n_sendBt_clicked()
{
serilaPort->write(ui->sendEdit->text().toLocal8Bit().data());
}
5. Close serial port function
Use QSerialPort's member function close() to close the serial port.
void Widget:n_closeBt_clicked()
{
serilaPort->close();
}
6. Clear the data in the send column
Call the member function clear of the ui component lineEdit to clear the data.
void Widget:n_clearBt_clicked()
{
ui->recvEdit->clear();
}
Compile and test, the results are as shown:

This post is from ARM Technology

Latest reply

Thanks for sharing! Very good!!!   Details Published on 2021-4-16 10:52

赞赏

1

查看全部赞赏

 

7462

Posts

2

Resources
2
 

Qt is indeed very convenient. It encapsulates the functions of the operating system and is very easy to use.

This post is from ARM Technology
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

1942

Posts

2

Resources
3
 

Thanks for sharing! Very good!!!

This post is from ARM Technology
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list