#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); serialPort = new QSerialPort(this); // 修改窗口标题 this->setWindowTitle("Design by JRNitre - Serial Port Debug Tools"); // 读取系统可用串口 // 发送到下拉框中 ui->config_port_comboBox->clear(); foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){ ui->config_port_comboBox->addItem(info.portName()); } // 串口配置数据初始化 // 默认接收&发送模式 // TRUE - Hex // FALSE - Bool if (recive_config_select){ ui->receive_hexmode->setChecked(1); }else{ ui->receivetextmode->setChecked(1); } if (transmission_config_select){ ui->transmission_hexmode->setChecked(1); }else{ ui->transmission_textmode->setChecked(1); } // 链接信号与槽函数 // 发送模式配置 -> hex模式被选择 -> Widget::receiveHex_selectSlot() connect(ui->receive_hexmode, &QRadioButton::pressed, this, &Widget::receiveHex_selectSlot); // 发送模式配置 -> text模式被选择 -> Widget::receiveText_selectSlot() connect(ui->receivetextmode, &QRadioButton::pressed, this, &Widget::receiveText_selectSlot); // 接收模式配置 -> hex模式被选择 -> Widget::transmissionHex_selectSlot() connect(ui->transmission_hexmode, &QRadioButton::pressed, this, &Widget::transmissionHex_selectSlot); // 接收模式配置 -> text模式被选择 -> Widget::transmissionText_selectSlot() connect(ui->transmission_textmode, &QRadioButton::pressed, this, &Widget::transmissionText_selectSlot); // 读取默认波特率并赋值 baud_rate = ui->config_baud_comboBox->currentText().toInt(); // 读取默认数据位长度并赋值 data_bit = ui->config_databit_comboBox->currentText().toInt(); // 读取默认停止位长度并赋值 stop_bit = ui->config_stopbit_comboBox->currentText().toDouble() * 10; // 读取默认校验模式 if (ui->config_check_comboBox->currentText() != "no"){ if (ui->config_check_comboBox->currentText() == "odd"){ check = 1; } if (ui->config_check_comboBox->currentText() == "even"){ check = 2; } }else{ check = 0; } } Widget::~Widget() { delete ui; } // Button: Clear void Widget::on_transmission_clear_Button_clicked() { ui->transmission_Edit->clear(); } void Widget::on_receive_clear_Button_clicked() { ui->receive_Edit->clear(); } // 手动更新串口列表 void Widget::on_updateport_Button_clicked() { // 列出可用串口 ui->config_port_comboBox->clear(); foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()){ ui->config_port_comboBox->addItem(info.portName()); } } // 发送模式 - Hex 模式被选中 void Widget::receiveHex_selectSlot(){ ui->receive_Edit->insertPlainText("Receive Hex select\n"); } // 发送模式 - Text 模式被选中 void Widget::receiveText_selectSlot(){ ui->receive_Edit->insertPlainText("Receive Text select\n"); } // 接收模式 - Hex 模式被选中 void Widget::transmissionHex_selectSlot(){ ui->receive_Edit->insertPlainText("Transmission Hex select\n"); } // 接收模式 - Text 模式被选中 void Widget::transmissionText_selectSlot(){ ui->receive_Edit->insertPlainText("Transmission Text select\n"); } // 波特率选择 void Widget::on_config_baud_comboBox_currentIndexChanged(int index) { baud_rate = ui->config_baud_comboBox->itemText(index).toInt(); ui->receive_Edit->insertPlainText("baud rate to " + QString::number(baud_rate) + "\n"); } // 数据位长度选择 void Widget::on_config_databit_comboBox_currentIndexChanged(int index) { data_bit = ui->config_databit_comboBox->itemText(index).toInt(); ui->receive_Edit->insertPlainText("data bit to " + QString::number(data_bit) + "\n"); } // 停止位长度选择 void Widget::on_config_stopbit_comboBox_currentIndexChanged(int index) { stop_bit = ui->config_stopbit_comboBox->itemText(index).toDouble() * 10; ui->receive_Edit->insertPlainText("stop bit to " + QString::number(stop_bit) + "\n"); } void Widget::on_config_check_comboBox_currentIndexChanged(int index) { if (ui->config_check_comboBox->itemText(index) != "no"){ if (ui->config_check_comboBox->itemText(index) == "odd"){ check = 1; } if (ui->config_check_comboBox->itemText(index) == "even"){ check = 2; } }else{ check = 0; } ui->receive_Edit->insertPlainText("check to " + QString::number(check) + "\n"); } // 打开串口 void Widget::on_openserial_button_clicked() { // 串口初始化 // 串口号 serialPort->setPortName(ui->config_port_comboBox->currentText()); // 波特率 switch (baud_rate) { case 4800: serialPort->setBaudRate(QSerialPort::Baud4800); break; case 9600: serialPort->setBaudRate(QSerialPort::Baud9600); break; case 38400: serialPort->setBaudRate(QSerialPort::Baud38400); break; case 115200: serialPort->setBaudRate(QSerialPort::Baud115200); break; } // 数据位 switch (data_bit) { 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; } // 停止位 switch (stop_bit) { case 10: serialPort->setStopBits(QSerialPort::OneStop); break; case 15: serialPort->setStopBits(QSerialPort::OneAndHalfStop); break; case 20: serialPort->setStopBits(QSerialPort::TwoStop); break; } // 奇偶校验 switch (check) { case 0: serialPort->setParity(QSerialPort::NoParity); break; case 1: serialPort->setParity(QSerialPort::OddParity); break; case 2: serialPort->setParity(QSerialPort::EvenParity); break; } // 按下后是否需要执行打开端口功能 if (ui->openserial_button->text() == "Open Serial"){ // 提示正在连接 ui->receive_Edit->insertPlainText("Connect to Serial " + ui->config_port_comboBox->currentText() + " ...\n"); // 端口开启成功 if (serialPort->open(QIODevice::ReadWrite) == true){ // 改变按钮名 ui->openserial_button->setText("Close Serial"); // 让端口下拉框不可选 ui->config_port_comboBox->setEnabled(false); // 更新提示 ui->status->setText("Connect to " + ui->config_port_comboBox->currentText()); ui->status->setStyleSheet("color:green"); // 提示用户已连接 ui->receive_Edit->insertPlainText("Connect Succesufully! \n"); }else{ // 连接失败 ui->receive_Edit->insertPlainText("Connect Faild\n"); } }else{ // 关闭串口 serialPort->close(); // 改变按钮名 ui->openserial_button->setText("Open Serial"); // 让端口下拉框可选 ui->config_port_comboBox->setEnabled(true); ui->status->setText("Disconnect"); ui->status->setStyleSheet("color:black"); } } // Button: About void Widget::on_about_button_clicked() { QMessageBox::information(this, "Info", "null"); }