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:
|