学QT时,写的一个简单得不能再简单的串口上位机,用来控制单片机上的2个LED。假设一个是只有开和关的状态。一个可以调节亮度。上位机的界面如下图:
其中,波特率,数据位,停止位下拉值在设计师里面添加。剩下的功能,基本由代码实现。通信使用的协议也是随便写的。很简单和随意。图片是老弟手绘的。
下面贴代码 (*^__^*)
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QtSerialPort> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); Pic = tr("://Resource/关灯.jpg"); //放置一张图片 ui->lightLabel->setPixmap(Pic); ui->RTextEdit->setReadOnly(false); ui->STextEdit->setReadOnly(false); ui->onButton->setEnabled(true); //默认关灯 ui->offButton->setEnabled(false); ui->lightDial->setEnabled(false); serial = new QSerialPort(this); OpenFlag = false; //串口未打开 connect(ui->lightDial, SIGNAL(valueChanged(int)), this, SLOT(on_light_chance())); UsartUi(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::UsartUi() //完善串口配置UI { ui->parityBox->addItem(QStringLiteral("None"),QSerialPort::NoParity); ui->parityBox->addItem(QStringLiteral("偶校验"),QSerialPort::EvenParity); ui->parityBox->addItem(QStringLiteral("奇校验"),QSerialPort::OddParity); ui->parityBox->addItem(QStringLiteral("Mark 1"),QSerialPort::MarkParity); ui->parityBox->addItem(QStringLiteral("Space 0"),QSerialPort::SpaceParity); ui->baundRateBox->setItemData(0,QSerialPort::Baud4800); ui->baundRateBox->setItemData(1,QSerialPort::Baud9600); ui->baundRateBox->setItemData(2,QSerialPort::Baud19200); ui->baundRateBox->setItemData(3,QSerialPort::Baud38400); ui->baundRateBox->setItemData(4,QSerialPort::Baud57600); ui->baundRateBox->setItemData(5,QSerialPort::Baud115200); ui->dataBitBox->setItemData(0,QSerialPort::Data5); ui->dataBitBox->setItemData(1,QSerialPort::Data6); ui->dataBitBox->setItemData(2,QSerialPort::Data7); ui->dataBitBox->setItemData(3,QSerialPort::Data8); ui->stopBitBox->setItemData(0,QSerialPort::OneStop); ui->stopBitBox->setItemData(1,QSerialPort::OneAndHalfStop); ui->stopBitBox->setItemData(2,QSerialPort::TwoStop); PortInfo(); } void MainWindow::PortInfo() { ui->uNameBox->clear(); foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { QStringList list; list << info.portName() << info.description() << info.manufacturer() << info.systemLocation() << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString()) << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString()); ui->uNameBox->addItem(list.first(), list); } } //槽函数 void MainWindow::on_startButton_clicked() //打开串口 { if(!OpenFlag)//执行打开串口 { currentConfig.name = ui->uNameBox->currentText(); currentConfig.baudRate = static_cast<QSerialPort::BaudRate> (ui->baundRateBox->itemData(ui->baundRateBox->currentIndex()).toInt()); currentConfig.dataBits = static_cast<QSerialPort::DataBits> (ui->dataBitBox->itemData(ui->dataBitBox->currentIndex()).toInt()); currentConfig.stopBits = static_cast<QSerialPort::StopBits> (ui->stopBitBox->itemData(ui->stopBitBox->currentIndex()).toInt()); currentConfig.parity = static_cast<QSerialPort::Parity> (ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt()); serial->setPortName(currentConfig.name); if(serial->open(QIODevice::ReadWrite)) { if( serial->setBaudRate(currentConfig.baudRate) && serial->setDataBits(currentConfig.dataBits) && serial->setStopBits(currentConfig.stopBits) && serial->setParity(currentConfig.parity) ) { OpenFlag = true; ui->startButton->setText(tr("关闭串口")); } else { serial->close(); OpenFlag = false; ui->startButton->setText(tr("打开串口")); } } else { OpenFlag = false; ui->startButton->setText(tr("打开串口")); } } else { OpenFlag = false; ui->startButton->setText(tr("打开串口")); serial->close(); } /* ui->STextEdit->setText(tr("校验位:%1 波特率:%2 数据位:%3 停止位:%4") .arg(ui->parityBox->itemData(ui->parityBox->currentIndex()).toString()) .arg(ui->baundRateBox->itemData(ui->baundRateBox->currentIndex()).toString()) .arg(ui->dataBitBox->itemData(ui->dataBitBox->currentIndex()).toString()) .arg(ui->stopBitBox->itemData(ui->stopBitBox->currentIndex()).toString())); */ } void MainWindow::on_offButton_clicked() //关灯 { QByteArray data; QString Pdata; data.resize(3); data[0] = 0xF0; data[1] = 0X0F; data[2] = 0x80; ui->offButton->setEnabled(false); ui->onButton->setEnabled(true); ui->lightDial->setValue(1); ui->lightDial->setEnabled(false); Pdata = data.toHex().toUpper(); int Size = Pdata.size(); for(int i = Size; i > 0; i--) { Pdata.insert(i * 2, " "); } ui->STextEdit->insertPlainText(Pdata); //发送数据 serial->write(data); } void MainWindow::on_onButton_clicked() //开灯 0XF0 0X0F 0X81 { QByteArray data; QString Pdata; data.resize(3); data[0] = 0xF0; data[1] = 0X0F; data[2] = 0X81; ui->onButton->setEnabled(false); ui->offButton->setEnabled(true); ui->lightDial->setEnabled(true); Pdata = data.toHex().toUpper(); int Size = Pdata.size() / 2; for(int i = Size; i > 0; i--) { Pdata.insert(i * 2, " "); } ui->STextEdit->insertPlainText(Pdata); //发送数据 serial->write(data); } void MainWindow::on_light_chance() { QByteArray data; QString Pdata; data.resize(5); data[0] = 0xF0; data[1] = 0x0F; data[2] = 0xC0; data[3] = 0x01; data[4] = ui->lightDial->value(); Pdata = data.toHex().toUpper(); int Size = Pdata.size() / 2; for(int i = Size; i > 0; i--) { Pdata.insert(i * 2, " "); } ui->STextEdit->insertPlainText(Pdata); serial->write(data); } void MainWindow::on_freshButton_clicked() //刷新状态 { QByteArray Rdata = serial->readAll(); QString Pdata = Rdata.toHex().toUpper(); //转换成HEX格式且大写 int Size = Pdata.size()/2; for(int i = Size; i > 0; i--) //插入空格 使格式如 xx xx xx { Pdata.insert(i * 2, " "); } ui->RTextEdit->insertPlainText(Pdata); int i = 0; if(Rdata.at(i) && 0xF0 == 0xF0) { i++; if(Rdata.at(i) && 0x0F == 0x0F) { while(Rdata.at(i) != ' ') //解析指令 { if(Rdata.at(i) && 0x80 == 0x80)currentStatus.lightOnOff = false; if(Rdata.at(i) && 0x81 == 0x81)currentStatus.lightOnOff = true; if(Rdata.at(i) && 0xc0 == 0xc0) { i = i + 2; currentStatus.lightValue = Rdata.at(i); } i++; } } } if(currentStatus.lightOnOff)Pic = tr("://Resource/开灯.jpg"); else Pic = tr("://Resource/关灯.jpg"); ui->lightLabel->setPixmap(Pic); ui->lightLcdNumber->display(currentStatus.lightValue); }