QT串口调试助手,接收和发送功能的实现

QT串口调试助手_发送和接收功能的实现

通过CheckBox的Qt::checked来看选中的是哪种接收,是Hex还是ASCLL码还是暂停。上面说的只有一个可以被选中,不能同时选中多个。所以可以将checkBox提升到为信号槽,并定义以下事件来让其只能同时执行一个。
QT串口调试助手,接收和发送功能的实现_第1张图片
Serial接收到的数据是字节数组,也就是字符串,所以可以定义一个字符串来储存当前接收到的数据,但注意也只是接收到的是当前数据,我们要想保存历史数据,可以通过将TextEdit中的字符串保留下来,然后再用append函数将当前接收到的字符串追加到历史字符串在显示出来:如下
QT串口调试助手,接收和发送功能的实现_第2张图片
Widget.cpp如下:

#include "widget.h"
#include "ui_widget.h"
#include 
#include 
#include 

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::Timer_event);

    serial = new QSerialPort(this);
    ui->comboBox_2->setCurrentIndex(3);
    ui->comboBox_3->setCurrentIndex(3);
    ui->comboBox_4->setCurrentIndex(2);
    ui->comboBox_5->setCurrentIndex(0);

    connect(serial,&QSerialPort::readyRead,this,&Widget::serialPort_readyRead);
    ui->checkBox->setCheckState(Qt::Checked);//设置初始选中的checkBox
}

Widget::~Widget()
{
    delete ui;
}

void Widget::Timer_event()
{
   QStringList newPortList;
   newPortList.clear();
   foreach(const QSerialPortInfo &Info, QSerialPortInfo::availablePorts())
       newPortList += Info.portName();
   //foreach是QT里特有的迭代赋值
   //将QSerialPortInfo::availablePorts()全部给Info

   if(newPortList.size() != portStringList.size())
   //看一下这一轮有多少串口,和上一轮串口数比较,如果不相等才执行
   {
      portStringList = newPortList;//用于保存本轮的串口,和下一轮比较即为(newPortList)
      ui->comboBox->clear();
      ui->comboBox->addItems(newPortList);
   }
}
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())//如果字符串直接转int不可以的话,那么可以用序号来switch
        {
            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);break;
        }
        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);break;
        }
        serial->setFlowControl(QSerialPort::NoFlowControl);
        //设置一下当前的流控
        if(!serial->open(QIODevice::ReadWrite))
        //初始化完毕之后,就可以打开串口了
        {
            QMessageBox::information(this,"错误提示","无法打开串口",QMessageBox::Ok);
            return;//如果没有返回值简单的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 lastText;
    int length;
    if(ui->checkBox_3->checkState() != Qt::Checked)
    {
        lastText = ui->textEdit_2->toPlainText();//通过读取text_edit来读取历史数据
        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();
        }
        else
        {
           receiveText = receiveText.toLatin1().toHex();
           length = receiveText.length();
           for(int i = 0; i <= length/2; i++)
           {
               receiveText.insert(2+3*i,' ');
           }
        }
        lastText = lastText.append(receiveText);
        ui->textEdit_2->setText(lastText);
    }
    else
    {

    }
}

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_3->toPlainText();
    bytearray = sendText.toLatin1();
    serial->write(bytearray);//发送数据
    Send_Byte += sendText.length();
    ui->label_9->setText(QString::number(Send_Byte));
}

Widget.h如下:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void Timer_event();
    void on_pushButton_clicked();
    void serialPort_readyRead();

    void on_checkBox_clicked();

    void on_checkBox_2_clicked();

    void on_checkBox_3_clicked();

    void on_pushButton_2_clicked();
    
private:
    Ui::Widget *ui;
    QTimer *timer;
    QStringList portStringList;
    QSerialPort *serial;
    QString receiveText,sendText;
    long Receive_Byte = 0;
    long Send_Byte = 0;
};

#endif // WIDGET_H

下载链接https://download.csdn.net/download/misikamikirto/87862058?spm=1001.2014.3001.5501

你可能感兴趣的:(qt,ui,开发语言)