This uses two classes provided by the QSerialPort module: the QSerialPort class and the QSerialPortInfo class, the QSerialPort class provides operations on the serial port, and the QSerialPortInfo class provides access to the serial port information.The following is the main code, which contains a simple use of the serial class.
First, be sure to add in the.pro file: QT += serialport
The serial port is initialized as follows:
- /****************************Serial port initialization*****************************************/
- void MainWindow::initPort()
- {
- //Read Serial Port Information
- foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
- {
- qDebug()<<"Name:"<<info.portName();
- qDebug()<<"Description:"<<info.description();
- qDebug()<<"Manufacturer:"<<info.manufacturer();
- //This is equivalent to adding cmb after recognizing serial number automatically, if you want to select manually you can add it in the following list
- QSerialPort serial;
- serial.setPort(info);
- if(serial.open(QIODevice::ReadWrite))
- {
- //Add serial number to cmb
- ui->cmbPortName->addItem(info.portName());
- //Close the serial port and wait for it to be opened manually (open the serial button)
- serial.close();
- }
- }
- QStringList baudList;//baud rate
- QStringList parityList;//Check bits
- QStringList dataBitsList;//Data bits
- QStringList stopBitsList;//Stop Bit
- baudList<<"50"<<"75"<<"100"<<"134"<<"150"<<"200"<<"300"
- <<"600"<<"1200"<<"1800"<<"2400"<<"4800"<<"9600"
- <<"14400"<<"19200"<<"38400"<<"56000"<<"57600"
- <<"76800"<<"115200"<<"128000"<<"256000";
- ui->cmbBaudRate->addItems(baudList);
- ui->cmbBaudRate->setCurrentIndex(12);
- parityList<<"nothing"<<"odd"<<"even";
- parityList<<"sign";
- parityList<<"Spaces";
- ui->cmbParity->addItems(parityList);
- ui->cmbParity->setCurrentIndex(0);
- dataBitsList<<"5"<<"6"<<"7"<<"8";
- ui->cmbDataBits->addItems(dataBitsList);
- ui->cmbDataBits->setCurrentIndex(3);
- stopBitsList<<"1";
- stopBitsList<<"1.5";
- stopBitsList<<"2";
- ui->cmbStopBits->addItems(stopBitsList);
- ui->cmbStopBits->setCurrentIndex(0);
- //Settings button can be pressed
- ui->btnOpen->setCheckable(true);
- }
The serial port settings are as follows:
- /****************************Serial Port Settings******************************/
- void MainWindow::on_btnOpen_clicked()
- {
- if(ui->btnOpen->text() == "Open Serial Port")
- {
- my_serialport = new QSerialPort(this);
- //Set serial number
- my_serialport->setPortName(ui->cmbPortName->currentText());
- //Open Serial Port Read-Write
- if(my_serialport->open(QIODevice::ReadWrite))
- {
- //set baud rate
- my_serialport->setBaudRate(ui->cmbBaudRate->currentText().toInt());
- //set data bit
- my_serialport->setDataBits(QSerialPort::Data8);
- //Set Check Bit
- my_serialport->setParity(QSerialPort::NoParity);
- //set flow control
- my_serialport->setFlowControl(QSerialPort::NoFlowControl);
- //Set stop bits
- my_serialport->setStopBits(QSerialPort::OneStop);
- //Read once per second
- timer = new QTimer(this);
- connect(timer, SIGNAL(timeout()), this, SLOT(readComDataSlot()));
- timer->start(1000);
- setNonSelectable();
- }
- else
- {
- QMessageBox::about(NULL, "Tips", "Serial port not open!");
- return;
- }
- }
- else
- {
- timer->stop();
- setSelectable();
- my_serialport->close();
- }
- }
The data is sent and received as follows:
- /****************************Data Transfer************************************/
- void MainWindow::readComDataSlot()
- {
- //Reading Serial Port Data
- QByteArray readComData = my_serialport->readAll();
- //Display read data in te of data receiving area
- if(readComData != NULL)
- {
- ui->teReceiveData->append(readComData);
- }
- //Clear Buffer
- readComData.clear();
- }
- void MainWindow::on_btnSend_clicked()
- {
- //Get data from the sending area
- QString sendData = ui->teSendData->toPlainText();
- QByteArray sendData_2 = sendData.toLatin1();
- //Write Buffer
- my_serialport->write(sendData_2);
- }