ui控件:
串口控制:
void serialpro::on_open_Button_clicked()
{
if(ui->open_Button->text() == QString("打开串口"))
{
Serial.setPortName(ui->comBox->currentText());
Serial.setBaudRate(ui->bandRateBox->currentText().toInt());
switch (ui->dataBitBox->currentText().toInt())
{
case 8: Serial.setDataBits(QSerialPort::Data8); break;
case 7: Serial.setDataBits(QSerialPort::Data7); break;
case 6: Serial.setDataBits(QSerialPort::Data6); break;
case 5: Serial.setDataBits(QSerialPort::Data5); break;
default: break;
}
switch (ui->stopBitBox->currentText().toInt())
{
case 1: Serial.setStopBits(QSerialPort::OneStop);break;
case 2: Serial.setStopBits(QSerialPort::TwoStop);break;
default:break;
}
switch (ui->checkoutBox->currentIndex())
{
case 0: Serial.setParity(QSerialPort::NoParity);break;
default:break;
}
Serial.setFlowControl(QSerialPort::NoFlowControl);
if(Serial.open(QIODevice::ReadWrite)==false)
{
QMessageBox::warning(NULL , "提示", "串口打开失败!");
return;
}
ui->comBox->setEnabled(false);
ui->checkoutBox->setEnabled(false);
ui->bandRateBox->setEnabled(false);
ui->dataBitBox->setEnabled(false);
ui->stopBitBox->setEnabled(false);
ui->search_Button->setEnabled(false);
ui->open_Button->setText(QString("关闭串口"));
}
else
{
Serial.close();
ui->comBox->setEnabled(true);
ui->checkoutBox->setEnabled(true);
ui->bandRateBox->setEnabled(true);
ui->dataBitBox->setEnabled(true);
ui->stopBitBox->setEnabled(true);
ui->search_Button->setEnabled(true);
ui->open_Button->setText(QString("打开串口"));
}
}
发送数据:
void serialpro::on_send_Button_clicked()
{
if(Serial.isOpen()==false)
{
QMessageBox::warning(NULL , "提示", "请打开串口!");
return;
}
QByteArray senddata = ui->SendEdit->toPlainText().toUtf8();
if(ui->send16Box->isChecked()==true)
{
int cnt = senddata.size();
char *data = senddata.data();
for(int i=0;i<cnt;i++)
{
if(data[i]>='0' && (data[i]<='9'))
continue;
else if(data[i]>='a' && (data[i]<='f'))
continue;
else if(data[i]>='A' && (data[i]<='F'))
continue;
else if(data[i] == ' ')
continue;
else
{
QMessageBox::warning(NULL , "提示", "输入非16进制字符!");
return;
}
}
senddata = senddata.fromHex(senddata);
}
if(ui->newlineBox->isChecked()==true)
{
int cnt = senddata.size();
senddata = senddata.insert(cnt,"\r\n");
}
Serial.write(senddata);
}
显示数据:
void serialpro::Serial_read()
{
QByteArray buffer = Serial.readAll();
if(ui->show16Box->isChecked()==true)
{
buffer = buffer.toHex() ;
}
QString receive = QString(buffer);
ui->receiveEdit->insertPlainText(receive);
}
整个文件:
#include "serialpro.h"
#include "ui_serialpro.h"
#include <QMessageBox>
#include <QDataStream>
serialpro::serialpro(QWidget *parent) :
QWidget(parent),
ui(new Ui::serialpro)
{
ui->setupUi(this);
this->setWindowTitle("串口助手");
connect(&Serial, &QSerialPort::readyRead , this , &serialpro::Serial_read);
ui->bandRateBox->setCurrentIndex(0);
}
serialpro::~serialpro()
{
delete ui;
}
void serialpro::on_open_Button_clicked()
{
if(ui->open_Button->text() == QString("打开串口"))
{
Serial.setPortName(ui->comBox->currentText());
Serial.setBaudRate(ui->bandRateBox->currentText().toInt());
switch (ui->dataBitBox->currentText().toInt())
{
case 8: Serial.setDataBits(QSerialPort::Data8); break;
case 7: Serial.setDataBits(QSerialPort::Data7); break;
case 6: Serial.setDataBits(QSerialPort::Data6); break;
case 5: Serial.setDataBits(QSerialPort::Data5); break;
default: break;
}
switch (ui->stopBitBox->currentText().toInt())
{
case 1: Serial.setStopBits(QSerialPort::OneStop);break;
case 2: Serial.setStopBits(QSerialPort::TwoStop);break;
default:break;
}
switch (ui->checkoutBox->currentIndex())
{
case 0: Serial.setParity(QSerialPort::NoParity);break;
default:break;
}
Serial.setFlowControl(QSerialPort::NoFlowControl);
if(Serial.open(QIODevice::ReadWrite)==false)
{
QMessageBox::warning(NULL , "提示", "串口打开失败!");
return;
}
ui->comBox->setEnabled(false);
ui->checkoutBox->setEnabled(false);
ui->bandRateBox->setEnabled(false);
ui->dataBitBox->setEnabled(false);
ui->stopBitBox->setEnabled(false);
ui->search_Button->setEnabled(false);
ui->open_Button->setText(QString("关闭串口"));
}
else
{
Serial.close();
ui->comBox->setEnabled(true);
ui->checkoutBox->setEnabled(true);
ui->bandRateBox->setEnabled(true);
ui->dataBitBox->setEnabled(true);
ui->stopBitBox->setEnabled(true);
ui->search_Button->setEnabled(true);
ui->open_Button->setText(QString("打开串口"));
}
}
void serialpro::on_search_Button_clicked()
{
ui->comBox->clear();
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->comBox->addItem(info.portName());
}
}
void serialpro::on_clear_r_Button_clicked()
{
ui->receiveEdit->clear();
}
void serialpro::on_send_Button_clicked()
{
if(Serial.isOpen()==false)
{
QMessageBox::warning(NULL , "提示", "请打开串口!");
return;
}
QByteArray senddata = ui->SendEdit->toPlainText().toUtf8();
if(ui->send16Box->isChecked()==true)
{
int cnt = senddata.size();
char *data = senddata.data();
for(int i=0;i<cnt;i++)
{
if(data[i]>='0' && (data[i]<='9'))
continue;
else if(data[i]>='a' && (data[i]<='f'))
continue;
else if(data[i]>='A' && (data[i]<='F'))
continue;
else if(data[i] == ' ')
continue;
else
{
QMessageBox::warning(NULL , "提示", "输入非16进制字符!");
return;
}
}
senddata = senddata.fromHex(senddata);
}
if(ui->newlineBox->isChecked()==true)
{
int cnt = senddata.size();
senddata = senddata.insert(cnt,"\r\n");
}
Serial.write(senddata);
}
void serialpro::Serial_read()
{
QByteArray buffer = Serial.readAll();
if(ui->show16Box->isChecked()==true)
{
buffer = buffer.toHex() ;
}
QString receive = QString(buffer);
ui->receiveEdit->insertPlainText(receive);
}
码云代码
|