Qt-serial debugging assistant UHelper

Preface

Introduced in front of the serial class QSerialPortand auxiliary class QSerialPortInfo
to write a bit of a comprehensive project - serial debugging assistant
program source code has been uploaded to GitHub
UI part not carried out, it is placed directly Photo
Insert picture description here

Function and realization

Both data sending and receiving support ASCII and Hex

In fact, it is data conversion, and basically you don’t need to implement it yourself. QtSome interfaces have been provided

void MainWindow::on_buttom_sendout_clicked()
{
    
    
    if(uart_state != s_connect){
    
    
        setNewsColor(Qt::red);
        ui->label_news->setText("SerialPort is't Connect.");
        return;
    }

    if(mBGsendoutmode->checkedId() == SENDOUTMODE_ASCII){
    
    
        //支持中文需使用toLocal8Bit()
        mSerial->write(ui->textEdit_output->toPlainText().toLocal8Bit().data());
    }
    else if(mBGsendoutmode->checkedId() == SENDOUTMODE_HEX){
    
    
        QByteArray bytehex = QByteArray::fromHex(
                    ui->textEdit_output->toPlainText().toLatin1()).data();
         mSerial->write(bytehex);
    }
}

void MainWindow::slot_uartReadData(){
    
    

    if(DISPLAYMODE_ASCII == mBGdisplaymode->checkedId()){
    
    

        ui->textBrowser_intput->insertPlainText(QString::fromLocal8Bit(mSerial->readAll()));

    }

    else if(DISPLAYMODE_HEX == mBGdisplaymode->checkedId()){
    
    

        QString re = "";

        QByteArray bytearray = mSerial->readAll();

        //hex char[] 转QString

        for(int i = 0; i < bytearray.length(); i++){
    
    
            if((unsigned char)bytearray[i] > 255)
                re.append("Error ");
            re.append("0x" + QString::number((unsigned char)bytearray[i],16) + " ");
        }

        ui->textBrowser_intput->insertPlainText(re);
    }

}

Support automatic line wrapping display

The code in the received data plus the slot function, when in Linuxthe platform should be as"\n"

    if(ui->checkBox_displayNewline->isChecked()){
    
    
        if(ui->textBrowser_intput->document()->toPlainText() != "")
            ui->textBrowser_intput->insertPlainText("\r\n");
    }

Support display receiving time

Add this code

    if(ui->checkBox_displayTime->isChecked()){
    
    
        ui->textBrowser_intput->insertPlainText(QDateTime::currentDateTime().toString("[hh:mm:ss:zzz] "));
    }

Support timing data transmission

Use the timer to complete, check its selection status in the multi-select box and complete the timer on and off operations

    mAutosendoutTimer = new QTimer(this);
    connect(mAutosendoutTimer,SIGNAL(timeout()),this,SLOT(slot_Timeout_Uartsendout()));

	void MainWindow::slot_Timeout_Uartsendout(){
    
    
    on_buttom_sendout_clicked();

	void MainWindow::on_checkBox_sendoutAutoResend_clicked(bool checked)
{
    
    
    if(checked){
    
    

        if(s_connect == uart_state){
    
    
            mAutosendoutTimer->start(ui->lineEdit_AutoResend->text().toInt());
        }
        else{
    
    
            setNewsColor(Qt::red);
            ui->label_news->setText("SerialPort is't Connect.");
            return;
        }
    }
    else{
    
    
        if(mAutosendoutTimer->isActive())
            mAutosendoutTimer->stop();
    }
}
}

Errors during serial port use will be displayed in the bottom message bar

In addition to opening and closing the serial port of the results will show in the end part message bar, also bound QSerialPortthe errorsignal

        connect(mSerial,SIGNAL(errorOccurred(QSerialPort::SerialPortError)),
                this,SLOT(slot_uartError(QSerialPort::SerialPortError)));

	void MainWindow::slot_uartError(QSerialPort::SerialPortError error){
    
    

    	QMetaEnum metaError = QMetaEnum::fromType<QSerialPort::SerialPortError>();
    	ui->label_news->setText(metaError.valueToKey(error));
    	setNewsColor(Qt::red);
	}

Support custom baud rate

Our baud rate option bar is QComboBox, if you want to implement a custom baud rate, you need to change it to Edit
here setLineEdit(), and use it to enter it now

void MainWindow::on_comBox_uartDps_currentIndexChanged(int index)
{
    
    
    //自定义波特率
    if(8 == index){
    
    
        QLineEdit *lineEdit = new QLineEdit(this);
        ui->comBox_uartDps->setLineEdit(lineEdit);

        lineEdit->clear();
        lineEdit->setFocus();

        //正则限制输入
        QRegExp rx("[0-9]+$");
        QRegExpValidator *validator = new QRegExpValidator(rx, this);
        lineEdit->setValidator(validator);
    }
    else{
    
    
        ui->comBox_uartDps->setEditable(false);
    }
}

The serial port parameters can still be modified after opening the serial port

With StopBitan example:

void MainWindow::on_comBox_uartStopBit_currentIndexChanged(int index)
{
    
    
    if(s_connect == uart_state){
    
    

         QSerialPort::StopBits mStopBits = (QSerialPort::StopBits)(index + 1);


        if(mSerial->setStopBits(mStopBits)){
    
    
            setNewsColor(Qt::black);
            ui->label_news->setText("SerialPort setStopBits is OK.");
        }
        else{
    
    
            setNewsColor(Qt::red);
            ui->label_news->setText("SerialPort setStopBits is Error.");
        }
    }
}

Click on the port to refresh

void MainWindow::Init_UartPort(){
    
    

    ui->comBox_uartPort->clear();

    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
    
    
        ui->comBox_uartPort->addItem(info.portName());
    }

}

void MainWindow::on_comBox_uartPort_clicked(){
    
    
    Init_UartPort();
}

effect

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_40774605/article/details/105869293