| #include "widget.h" |
| #include "ui_widget.h" |
| #include <QDebug> |
| #include <QMessageBox> |
| |
| Widget::Widget(QWidget *parent) : |
| QWidget(parent), |
| ui(new Ui::Widget) |
| { |
| ui->setupUi(this); |
| |
| |
| resize(800, 600); |
| setWindowTitle("串口调试助手"); |
| |
| |
| timer = new QTimer(this); |
| |
| timer->start(500); |
| |
| connect(timer, &QTimer::timeout, this, &Widget::TimerEvent); |
| |
| |
| serial = new QSerialPort(this); |
| |
| |
| ui->comboBox_2->setCurrentIndex(5); |
| ui->comboBox_3->setCurrentIndex(3); |
| ui->comboBox_4->setCurrentIndex(2); |
| ui->comboBox_5->setCurrentIndex(0); |
| |
| |
| Receive_Byte = 0; |
| Send_Byte = 0; |
| |
| |
| |
| connect(serial, &QSerialPort::readyRead, this, &Widget::serialPort_readyRead); |
| |
| ui->checkBox->setCheckState(Qt::Checked); |
| } |
| |
| Widget::~Widget() |
| { |
| delete ui; |
| } |
| |
| void Widget::TimerEvent() |
| { |
| |
| QStringList newPortStringList; |
| newPortStringList.clear(); |
| |
| |
| foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) |
| newPortStringList += info.portName(); |
| |
| |
| if(newPortStringList.size() != portStringList.size()) |
| { |
| portStringList = newPortStringList; |
| ui->comboBox->clear(); |
| ui->comboBox->addItems(portStringList); |
| } |
| } |
| |
| void Widget::on_pushButton_clicked() |
| { |
| if(ui->pushButton->text() == QString("打开串口")) |
| { |
| |
| serial->setPortName(ui->comboBox->currentText()); |
| serial->setBaudRate(ui->comboBox_2->currentText().toInt()); |
| switch(ui->comboBox_3->currentText().toInt()) |
| { |
| case 5:serial->setDataBits(QSerialPort::Data5);break; |
| case 6:serial->setDataBits(QSerialPort::Data6);break; |
| case 7:serial->setDataBits(QSerialPort::Data7);break; |
| case 8:serial->setDataBits(QSerialPort::Data8);break; |
| default:serial->setDataBits(QSerialPort::UnknownDataBits); |
| } |
| switch(ui->comboBox_4->currentIndex()) |
| { |
| case 0:serial->setParity(QSerialPort::EvenParity);break; |
| case 1:serial->setParity(QSerialPort::OddParity);break; |
| case 2:serial->setParity(QSerialPort::NoParity);break; |
| default:serial->setParity(QSerialPort::UnknownParity); |
| } |
| switch(ui->comboBox_5->currentIndex()) |
| { |
| case 0:serial->setStopBits(QSerialPort::OneStop);break; |
| case 1:serial->setStopBits(QSerialPort::OneAndHalfStop);break; |
| case 2:serial->setStopBits(QSerialPort::TwoStop);break; |
| default:serial->setStopBits(QSerialPort::UnknownStopBits); |
| } |
| |
| serial->setFlowControl(QSerialPort::NoFlowControl); |
| |
| if(!serial->open(QIODevice::ReadWrite)) |
| { |
| QMessageBox::information(this, "错误提示", "无法打开串口", QMessageBox::Ok); |
| return; |
| } |
| |
| ui->comboBox->setEnabled(false); |
| ui->comboBox_2->setEnabled(false); |
| ui->comboBox_3->setEnabled(false); |
| ui->comboBox_4->setEnabled(false); |
| ui->comboBox_5->setEnabled(false); |
| |
| ui->pushButton->setText("关闭串口"); |
| } |
| else |
| { |
| |
| serial->close(); |
| ui->comboBox->setEnabled(true); |
| ui->comboBox_2->setEnabled(true); |
| ui->comboBox_3->setEnabled(true); |
| ui->comboBox_4->setEnabled(true); |
| ui->comboBox_5->setEnabled(true); |
| |
| ui->pushButton->setText("打开串口"); |
| } |
| } |
| |
| void Widget::serialPort_readyRead() |
| { |
| QString last_text; |
| int length; |
| int i; |
| if(ui->checkBox_3->checkState() != Qt::Checked) |
| { |
| last_text = ui->textEdit->toPlainText(); |
| Receivetext = serial->readAll(); |
| Receive_Byte += Receivetext.length(); |
| ui->label_9->setText(QString::number(Receive_Byte)); |
| |
| if(ui->checkBox_2->checkState() == Qt::Checked) |
| { |
| Receivetext = Receivetext.toLatin1().toHex(); |
| length = Receivetext.length(); |
| for(i = 0; i <= length/2; i++) |
| { |
| |
| Receivetext.insert(2+3*i, ' '); |
| } |
| } |
| else |
| { |
| Receivetext = Receivetext.toLatin1(); |
| } |
| last_text = last_text.append(Receivetext); |
| ui->textEdit->setText(last_text); |
| |
| } |
| } |
| |
| |
| |
| void Widget::on_checkBox_clicked() |
| { |
| ui->checkBox->setCheckState(Qt::Checked); |
| ui->checkBox_2->setCheckState(Qt::Unchecked); |
| ui->checkBox_3->setCheckState(Qt::Unchecked); |
| } |
| |
| void Widget::on_checkBox_2_clicked() |
| { |
| ui->checkBox->setCheckState(Qt::Unchecked); |
| ui->checkBox_2->setCheckState(Qt::Checked); |
| ui->checkBox_3->setCheckState(Qt::Unchecked); |
| } |
| |
| void Widget::on_checkBox_3_clicked() |
| { |
| ui->checkBox->setCheckState(Qt::Unchecked); |
| ui->checkBox_2->setCheckState(Qt::Unchecked); |
| ui->checkBox_3->setCheckState(Qt::Checked); |
| } |
| |
| |
| void Widget::on_pushButton_2_clicked() |
| { |
| QByteArray bytearray; |
| |
| Sendtext = ui->textEdit_2->toPlainText(); |
| bytearray = Sendtext.toLatin1(); |
| serial->write(bytearray); |
| Receive_Byte += Sendtext.length(); |
| ui->label_11->setText(QString::number(Receive_Byte)); |
| } |
| |
| void Widget::on_pushButton_3_clicked() |
| { |
| ui->textEdit->clear(); |
| } |
| |
| void Widget::on_pushButton_4_clicked() |
| { |
| ui->textEdit_2->clear(); |
| } |