Qt写的串口调试助手

 

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

int displayCount=0;
qint64 numAll=0,numError=0;
qint64 RX=0,TX=0;
//QTextStream *

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
    ui->setupUi(this);
    ui->pushButton_2->setEnabled(false);   
    timer = new QTimer(NULL);       
    myCom = new Win_QextSerialPort();
    out=new QTextStream();
    ui->comboBox_5->clear();
    for(int loop=1;loop<=10;loop++)
    {
        portName=QObject::tr("COM%1").arg(loop);
        myCom->setPortName(portName);
        myCom->setQueryMode(QextSerialBase::EventDriven);
        if(myCom ->open(QIODevice::ReadWrite))
            ui->comboBox_5->insertItem(loop,portName);
        myCom->close();
    }
    connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent()));
}

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

void Widget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);       
        break;       
    default:
        break;
    }
}

void Widget::on_pushButton_clicked()
{   
    ui->pushButton->setEnabled(false);
    ui->pushButton_2->setEnabled(true);
    ui->textBrowser->clear();
    ui->label_11->setText("0");
    ui->label_12->setText("0");
    portName=ui->comboBox_5->currentText();
    myCom->setPortName(portName);
    myCom->setQueryMode(QextSerialBase::EventDriven);

    if(myCom->isOpen())
        myCom->close();
    myCom ->open(QIODevice::ReadWrite);

    if(ui->comboBox->currentText()==tr("BAUD50"))
        myCom->setBaudRate(BAUD50);
    else if(ui->comboBox->currentText()==tr("BAUD75"))
        myCom->setBaudRate(BAUD75);
    else if(ui->comboBox->currentText()==tr("BAUD110"))
        myCom->setBaudRate(BAUD110);
    else if(ui->comboBox->currentText()==tr("BAUD110"))
        myCom->setBaudRate(BAUD110);
    else if(ui->comboBox->currentText()==tr("BAUD134"))
        myCom->setBaudRate(BAUD134);
    else if(ui->comboBox->currentText()==tr("BAUD150"))
        myCom->setBaudRate(BAUD150);
    else if(ui->comboBox->currentText()==tr("BAUD200"))
        myCom->setBaudRate(BAUD200);
    else if(ui->comboBox->currentText()==tr("BAUD300"))
        myCom->setBaudRate(BAUD300);
    else if(ui->comboBox->currentText()==tr("BAUD600"))
        myCom->setBaudRate(BAUD600);
    else if(ui->comboBox->currentText()==tr("BAUD1200"))
        myCom->setBaudRate(BAUD1200);
    else if(ui->comboBox->currentText()==tr("BAUD1800"))
        myCom->setBaudRate(BAUD1800);
    else if(ui->comboBox->currentText()==tr("BAUD2400"))
        myCom->setBaudRate(BAUD2400);
    else if(ui->comboBox->currentText()==tr("BAUD4800"))
        myCom->setBaudRate(BAUD4800);
    else if(ui->comboBox->currentText()==tr("BAUD9600"))
        myCom->setBaudRate(BAUD9600);
    else if(ui->comboBox->currentText()==tr("BAUD14400"))
        myCom->setBaudRate(BAUD14400);
    else if(ui->comboBox->currentText()==tr("BAUD19200"))
        myCom->setBaudRate(BAUD19200);
    else if(ui->comboBox->currentText()==tr("BAUD38400"))
        myCom->setBaudRate(BAUD38400);
    else if(ui->comboBox->currentText()==tr("BAUD56000"))
        myCom->setBaudRate(BAUD56000);
    else if(ui->comboBox->currentText()==tr("BAUD57600"))
        myCom->setBaudRate(BAUD57600);
    else if(ui->comboBox->currentText()==tr("BAUD76800"))
        myCom->setBaudRate(BAUD76800);
    else if(ui->comboBox->currentText()==tr("BAUD115200"))
        myCom->setBaudRate(BAUD115200);
    else if(ui->comboBox->currentText()==tr("BAUD128000"))
        myCom->setBaudRate(BAUD128000);
    else if(ui->comboBox->currentText()==tr("BAUD256000"))
        myCom->setBaudRate(BAUD256000);

    if(ui->comboBox_2->currentText()==tr("PAR_NONE"))
        myCom->setParity(PAR_NONE);
    else if(ui->comboBox_2->currentText()==tr("PAR_ODD"))
        myCom->setParity(PAR_ODD);
    else if(ui->comboBox_2->currentText()==tr("PAR_EVEN"))
        myCom->setParity(PAR_EVEN);
    else if(ui->comboBox_2->currentText()==tr("PAR_MARK"))
        myCom->setParity(PAR_MARK);
    else if(ui->comboBox_2->currentText()==tr("PAR_SPACE"))
        myCom->setParity(PAR_SPACE);

    if(ui->comboBox_3->currentText()==tr("DATA_5"))
        myCom->setDataBits(DATA_5);
    else if(ui->comboBox_3->currentText()==tr("DATA_6"))
        myCom->setDataBits(DATA_6);
    else if(ui->comboBox_3->currentText()==tr("DATA_7"))
        myCom->setDataBits(DATA_7);
    else if(ui->comboBox_3->currentText()==tr("DATA_8"))
        myCom->setDataBits(DATA_8);

    if(ui->comboBox_4->currentText()==tr("STOP_1"))
        myCom->setStopBits(STOP_1);
    else if(ui->comboBox_4->currentText()==tr("STOP_2"))
        myCom->setStopBits(STOP_2);
    myCom->setTimeout(50);
    connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
}

void Widget::readMyCom()
{
    temp = myCom->readAll();
    if(ui->checkBox->checkState()==Qt::Checked)
        ui->textBrowser->insertPlainText(temp.toHex());
    else
        ui->textBrowser->insertPlainText(temp);
    ui->textBrowser->moveCursor(QTextCursor::End);
    displayCount++;
    RX++;
    ui->label_13->setText(QObject::tr("RX:%1").arg(RX));
    if(displayCount>=1000)
    {
        displayCount=0;
        ui->textBrowser->clear();
    }
}

void Widget::on_pushButton_3_clicked()
{
    ui->textBrowser->clear();
}

void Widget::on_pushButton_2_clicked()
{
    disconnect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
    if(myCom->isOpen())
        myCom->close();
    displayCount=0;
    timer->stop();
    numAll=0;
    numError=0;
    ui->pushButton->setEnabled(true);
    ui->pushButton_2->setEnabled(false);
}

void Widget::on_pushButton_6_clicked()
{
    if(myCom->isOpen())
        myCom->close();   
    if(timer->isActive())
        timer->stop();
    Widget::close();
}

void Widget::on_pushButton_5_clicked()
{
    if(myCom->isOpen())
    {
        QByteArray ba=ui->lineEdit->document()->toPlainText().toAscii();
        int bl=ba.size();
        if(ba[bl-1]=='\n')
        {
            ba.resize(bl+1);
            ba[bl]='\r';
        }
        if(ui->checkBox_3->checkState()==Qt::Checked)
        {
            int hlong=0,hloop=0;
            QByteArray outarray;
            outarray.resize(0);
            for(hloop=0;hloop             {
                if(((ba[hloop].operator <=(0x39))&&(ba[hloop].operator >=(0x30)))||((ba[hloop].operator<=(0x47))&&(ba[hloop].operator>=(0x41)))||((ba[hloop].operator<=(0x67))&&(ba[hloop].operator>=(0x61))))
                {
                    hlong++;
                    outarray.resize(hlong);
                    if((ba[hloop].operator<=(0x39))&&(ba[hloop].operator>=(0x30)))
                        outarray[hlong-1]=(ba[hloop]-48)<<4;
                    else if((ba[hloop].operator<=(0x47))&&(ba[hloop].operator>=(0x41)))
                        outarray[hlong-1]=(ba[hloop]-55)<<4;
                    else if((ba[hloop].operator<=(0x67))&&(ba[hloop].operator>=(0x61)))
                        outarray[hlong-1]=(ba[hloop]-87)<<4;

                    if((ba[hloop+1].operator<=(0x39))&&(ba[hloop+1].operator>=(0x30)))
                        outarray[hlong-1]=outarray[hlong-1]+ba[hloop+1]-48;
                    else if((ba[hloop+1].operator<=(0x47))&&(ba[hloop+1].operator>=(0x41)))
                        outarray[hlong-1]=outarray[hlong-1]+ba[hloop+1]-55;
                    else if((ba[hloop+1].operator<=(0x67))&&(ba[hloop+1].operator>=(0x61)))
                        outarray[hlong-1]=outarray[hlong-1]+ba[hloop+1]-87;
                }
                else if(ba[hloop].operator==(0x0A))
                {
                    outarray[hlong-1]=ba[hloop];
                    hlong++;
                    outarray.resize(hlong);
                    outarray[hlong-1]='\r';
                }
            }           
            myCom->write(outarray);
        }
        else
            myCom->write(ba);
        TX++;
        ui->label_14->setText(QObject::tr("TX:%1").arg(TX));
        ui->textBrowser->document()->toPlainText().toAscii().toHex();
    }
}

void Widget::timerEvent()
{       
    if(myCom->isOpen())
    {
        QByteArray ba=ui->lineEdit->document()->toPlainText().toAscii();
        int bl=ba.size();
        if(ba[bl-1]=='\n')
        {
            ba.resize(bl+1);
            ba[bl]='\r';
        }
        if(ui->checkBox_3->checkState()==Qt::Checked)
        {
            int hlong=0,hloop=0;
            QByteArray outarray;
            outarray.resize(0);
            for(hloop=0;hloop             {
                if(((ba[hloop].operator <=(0x39))&&(ba[hloop].operator >=(0x30)))||((ba[hloop].operator<=(0x5A))&&(ba[hloop].operator>=(0x41)))||((ba[hloop].operator<=(0x7A))&&(ba[hloop].operator>=(0x61))))
                {
                    hlong++;
                    outarray.resize(hlong);
                    if((ba[hloop].operator<=(0x39))&&(ba[hloop].operator>=(0x30)))
                        outarray[hlong-1]=(ba[hloop]-48)<<4;
                    else if((ba[hloop].operator<=(0x5A))&&(ba[hloop].operator>=(0x41)))
                        outarray[hlong-1]=(ba[hloop]-55)<<4;
                    else if((ba[hloop].operator<=(0x7A))&&(ba[hloop].operator>=(0x61)))
                        outarray[hlong-1]=(ba[hloop]-87)<<4;

                    if((ba[hloop+1].operator<=(0x39))&&(ba[hloop+1].operator>=(0x30)))
                        outarray[hlong-1]=outarray[hlong-1]+ba[hloop+1]-48;
                    else if((ba[hloop+1].operator<=(0x5A))&&(ba[hloop+1].operator>=(0x41)))
                        outarray[hlong-1]=outarray[hlong-1]+ba[hloop+1]-55;
                    else if((ba[hloop+1].operator<=(0x7A))&&(ba[hloop+1].operator>=(0x61)))
                        outarray[hlong-1]=outarray[hlong-1]+ba[hloop+1]-87;
                }
                else if(ba[hloop].operator==(0x0A))
                {
                    outarray[hlong-1]=ba[hloop];
                    hlong++;
                    outarray.resize(hlong);
                    outarray[hlong-1]='\r';
                }
            }
            myCom->write(outarray);
        }
        else
            myCom->write(ba);
        TX++;
        ui->label_14->setText(QObject::tr("TX:%1").arg(TX));
    }
}

void Widget::on_checkBox_2_clicked(bool checked)
{
    if(checked)
        timer->start(ui->spinBox_3->value());
    else
    {
        if(timer->isActive())
            timer->stop();
    }
}

void Widget::on_pushButton_4_clicked()
{
    ui->pushButton_4->setEnabled(false);
    ui->pushButton_7->setEnabled(true);
    if(ui->checkBox_5->checkState()==Qt::Checked)
    {
        path = QFileDialog::getSaveFileName(NULL, QObject::tr("保存"), ".", QObject::tr("保存类型(*.txt)"));
        file.setFileName(path);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
            return;
        out->setDevice(&file);
    }
    connect(myCom,SIGNAL(readyRead()),this,SLOT(beginRec()));
}

void Widget::beginRec()
{   
    if(myCom->isOpen())
    {
        QByteArray temp2=ui->lineEdit_2->toPlainText().toAscii();
        int blong=temp2.size();
        if(temp2[blong-1]=='\n')
        {
            temp2.resize(blong+1);
            temp2[blong]='\r';
        }
        numAll++;
        if(!temp2.contains(temp))
            numError++;
        if((ui->checkBox_5->checkState()==Qt::Checked)&&(!temp2.contains(temp)))
        {
            *out << QString::number(numError) << ":" << QString::number(numAll);
            *out<<"    ";
            if(ui->checkBox->checkState()==Qt::Checked)
                *out<< temp.toHex();
            else
                *out<< temp;
            *out<<"    ";
            *out<< QDateTime::currentDateTime().date().toString("yyyy-MM-dd");
            *out<<"    ";
            *out<< QDateTime::currentDateTime().time().toString("hh:mm:ss") <<"\n";
            out->flush();
        }
        ui->label_11->setText(QString::number(numAll));
        ui->label_12->setText(QString::number(numError));       
    }
}

void Widget::on_pushButton_7_clicked()
{
    ui->pushButton_4->setEnabled(true);
    ui->pushButton_7->setEnabled(false);
    file.close();
    disconnect(myCom,SIGNAL(readyRead()),this,SLOT(beginRec()));
    numAll=0;
    numError=0;
}

void Widget::on_pushButton_8_clicked()
{
    ui->comboBox_5->clear();
    myCom->close();
    for(int loop=1;loop<=10;loop++)
    {
        portName=QObject::tr("COM%1").arg(loop);
        myCom->setPortName(portName);
        myCom->setQueryMode(QextSerialBase::EventDriven);
        if(myCom ->open(QIODevice::ReadWrite))
            ui->comboBox_5->insertItem(loop,portName);
        myCom->close();
    }
}

void Widget::on_pushButton_9_clicked()
{
    RX=0;
    TX=0;
    ui->label_13->setText(QObject::tr("RX:%1").arg(RX));
    ui->label_14->setText(QObject::tr("TX:%1").arg(TX));
}

你可能感兴趣的:(qt,signal,timer,ui,path,include)