110VCGQ/485SWJ/CGQ485V2/CGQ485/dialogsetport.cpp
2024-11-18 10:09:39 +08:00

199 lines
4.8 KiB
C++

#include "dialogsetport.h"
#include "ui_dialogsetport.h"
#include "mycombobox.h"
#include <QMessageBox>
DialogSetPort::DialogSetPort(QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogSetPort)
{
ui->setupUi(this);
setFixedSize(300,300);
ui->cboxSerialPort->clear();
QList<QSerialPortInfo> serialPortInfo = QSerialPortInfo::availablePorts();
int count=serialPortInfo.count();
for(int i = 0; i < count; i++)
{
ui->cboxSerialPort->addItem( serialPortInfo.at(i).portName()+" "+serialPortInfo.at(i).description());
}
//串口中途退出时的提示
connect(&mSerialPort,&QSerialPort::errorOccurred,this,[=](QSerialPort::SerialPortError error){
if(error == QSerialPort::PermissionError)
{
ui->rbtnOpen->setChecked(false);
QMessageBox::information(this,"提示","串口已退出或不存在");
ui->rbtnOpen->setText("打开串口");
ui->cboxSerialPort->setEnabled(true);
ui->cboxBaudrate->setEnabled(true);
ui->cboxDataBites->setEnabled(true);
ui->cboxParity->setEnabled(true);
ui->cboxStop->setEnabled(true);
mSerialPort.close();
on_cboxSerialPort_clicked();
}
});
}
DialogSetPort::~DialogSetPort()
{
// QMessageBox::information(this,"","设置串口对话框已退出");
delete ui;
}
bool DialogSetPort::getSerialPortConfig()
{
//获取串口配置
mPortName=ui->cboxSerialPort->currentText();
mBaudRate=ui->cboxBaudrate->currentText();
mParity=ui->cboxParity->currentText();
mDataBits=ui->cboxDataBites->currentText();
mStopBits=ui->cboxStop->currentText();
//设置串口
//串口号
if(mPortName.isEmpty())
{
return false;
}
if(mPortName[4]>='0' && mPortName[4] <= '9')
{
mSerialPort.setPortName(mPortName.mid(0,5));
}
else
{
mSerialPort.setPortName(mPortName.mid(0,4));
}
//波特率
mSerialPort.setBaudRate(mBaudRate.toInt());
// if("9600" == mBaudRate)
// {
// mSerialPort.setBaudRate(QSerialPort::Baud9600);
// }
// else if("19200" == mBaudRate)
// {
// mSerialPort.setBaudRate(QSerialPort::Baud19200);
// }
// else
// {
// mSerialPort.setBaudRate(QSerialPort::Baud115200);
// }
//校验位
if("Even" == mParity)
{
mSerialPort.setParity(QSerialPort::EvenParity);
}
else if("Odd" == mParity)
{
mSerialPort.setParity(QSerialPort::OddParity);
}
else
{
mSerialPort.setParity(QSerialPort::NoParity);
}
//数据位
if("5" == mDataBits)
{
mSerialPort.setDataBits(QSerialPort::Data5);
}
else if("6" == mDataBits)
{
mSerialPort.setDataBits(QSerialPort::Data6);
}
else if("7" == mDataBits)
{
mSerialPort.setDataBits(QSerialPort::Data7);
}
else
{
mSerialPort.setDataBits(QSerialPort::Data8);
}
//停止位
if("1.5" == mStopBits)
{
mSerialPort.setStopBits(QSerialPort::OneAndHalfStop);
}
else if("2" == mStopBits)
{
mSerialPort.setStopBits(QSerialPort::TwoStop);
}
else
{
mSerialPort.setStopBits(QSerialPort::OneStop);
}
return mSerialPort.open(QSerialPort::ReadWrite);
}
//显示串口信息
void DialogSetPort::on_cboxSerialPort_clicked()
{
ui->cboxSerialPort->clear();
QList<QSerialPortInfo> serialPortInfo = QSerialPortInfo::availablePorts();
int count=serialPortInfo.count();
for(int i = 0; i < count; i++)
{
ui->cboxSerialPort->addItem( serialPortInfo.at(i).portName()+" "+serialPortInfo.at(i).description());
}
// ui->cboxSerialPort->setFixedWidth(300);
}
//打开关闭串口
void DialogSetPort::on_rbtnOpen_clicked(bool checked)
{
//打开串口
if(true == checked)
{
if(true == getSerialPortConfig())
{
ui->rbtnOpen->setText("关闭串口");
ui->cboxSerialPort->setEnabled(false);
ui->cboxBaudrate->setEnabled(false);
ui->cboxDataBites->setEnabled(false);
ui->cboxParity->setEnabled(false);
ui->cboxStop->setEnabled(false);
}
else
{
ui->rbtnOpen->setChecked(false);
QMessageBox::information(this,"提示","串口不存在或串口被占用");
}
}
//关闭串口
if(false == checked)
{
mSerialPort.close();
ui->rbtnOpen->setText("打开串口");
ui->cboxSerialPort->setEnabled(true);
ui->cboxBaudrate->setEnabled(true);
ui->cboxDataBites->setEnabled(true);
ui->cboxParity->setEnabled(true);
ui->cboxStop->setEnabled(true);
}
}
void DialogSetPort::on_buttonOK_clicked()
{
this->close();
}