本練習適合剛接觸QT的小伙伴,大佬略過。
先上成果圖:
首先使用串口模擬工具模擬出兩個虛擬串口出來:
本機com1虛擬機com2,將虛擬機的串口打開設置為com2:
然后進入Linux終端中查看目前插入設備的信息:
dmesg |grep ttys*
接着將設備權限改為777(任何用戶可讀可寫可執行,我這邊是已知ttyS1是com2口,如果不知道自己挨個試一下即可。)
sudo chmod 777 /dev/ttyS1
最后附上部分qt代碼:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); SerialPort = new QSerialPort(); serialInit(); connect(SerialPort,SIGNAL(readyRead()),this,SLOT(serialRead())); } MainWindow::~MainWindow() { delete ui; } //界面加載時刷新界面相應控件信息 void MainWindow::serialInit() { ui->cboPort->clear(); //讀取串口信息 foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { // if(!info.portName().contains("USB")) // continue; qDebug() << info.portName(); //自動讀取串口號添加到端口下拉框中 QSerialPort serial; serial.setPort(info); if(serial.open(QIODevice::ReadWrite)) { qDebug() << "open success"; ui->cboPort->addItem(info.portName()); // serial.close(); } else { qDebug() << "open fail"; } QStringList baudList;//波特率 QStringList parityList;//奇偶校驗 QStringList dataBitList;//數據位 QStringList stopBitList;//停止位 baudList<<"1200"<<"2400"<<"4800"<<"9600" <<"14400"<<"19200"<<"38400"<<"56000" <<"57600"<<"115200"; ui->cboBaud->addItems(baudList); ui->cboBaud->setCurrentIndex(3); parityList<<("None")<<("Odd")<<("Even"); ui->cboVerify->addItems(parityList); } } //收到界面信號時執行的方法 void MainWindow::serialRead() { QString strBuffer; //從緩沖區中讀取數據 QByteArray buffer = SerialPort->readAll(); if(!buffer.isEmpty())//非空說明有數據接受 { //轉換成16進制大寫 QString str = buffer.toHex().data(); str = str.toUpper(); //一個16進制占4位,8位為一字節,所以每兩位16進制空一格 for(int i=0;i<str.length();i+=2) { qDebug() << "str.Length"+str.length(); qDebug() << "str"+str; QString strTem = str.mid(i,2); strBuffer +=strTem; strBuffer +=" "; } //讀取之前顯示數據 QString receive = ui->txtRec->toPlainText(); //清空顯示 ui->txtRec->clear(); receive += QString(strBuffer); ui->txtRec->appendPlainText(receive); } buffer.clear(); } void MainWindow::on_btnOpenSerial_clicked() { //打開串口 if(ui->btnOpenSerial->text() == "打開串口") { //設置串口號 SerialPort->setPortName(ui->cboPort->currentText()); //打開串口 if(SerialPort->open(QIODevice::ReadWrite)) { //設置波特率 SerialPort->setBaudRate(ui->cboBaud->currentText().toInt()); //設置數據位數 switch(ui->cboNumBit->currentIndex()) { case 5:SerialPort->setDataBits(QSerialPort::Data5);break; case 6:SerialPort->setDataBits(QSerialPort::Data6);break; case 7:SerialPort->setDataBits(QSerialPort::Data7);break; case 8:SerialPort->setDataBits(QSerialPort::Data8);break; default:break; } //設置奇偶 switch(ui->cboVerify->currentIndex()) { case 0:SerialPort->setParity(QSerialPort::NoParity);break; case 1:SerialPort->setParity(QSerialPort::OddParity);break; case 2:SerialPort->setParity(QSerialPort::EvenParity);break; default:break; } //設置流控制 SerialPort->setFlowControl(QSerialPort::NoFlowControl); //設置停止位 switch(ui->cboStop->currentIndex()) { case 1:SerialPort->setStopBits(QSerialPort::OneStop); case 2:SerialPort->setStopBits(QSerialPort::TwoStop); default:break; } } //打不開串口提示報錯 else { QMessageBox::about(NULL,"提示","串口無法打開\r\n不存在或已被占用"); return ; } ui->btnOpenSerial->setText("關閉串口"); //下拉菜單控件使能 ui->cboBaud->setEnabled(false); ui->cboNumBit->setEnabled(false); ui->cboPort->setEnabled(false); ui->cboStop->setEnabled(false); ui->cboVerify->setEnabled(false); //發送按鍵使能 ui->btnSend->setEnabled(true); } else { SerialPort->close(); ui->btnOpenSerial->setText("打開串口"); //下拉按鍵使能 ui->cboBaud->setEnabled(true); ui->cboNumBit->setEnabled(true); ui->cboPort->setEnabled(true); ui->cboStop->setEnabled(true); ui->cboVerify->setEnabled(true); //發送按鍵使能 ui->btnSend->setEnabled(false); } } void MainWindow::on_btnSend_clicked() { QByteArray data; //獲取輸入窗口sendData的數據 QString strData = ui->txtSend->toPlainText(); data = QByteArray::fromHex(strData.toLatin1().data()); //寫入發送緩沖區 SerialPort->write(data); }