home.cpp 10.8 KB
Newer Older
代码海贼团船长's avatar
代码海贼团船长 已提交
1 2 3 4 5 6 7
//
// Created by 11518 on 2022/11/3.
//

// You may need to build the project (run Qt uic code generator) to get "ui_Home.h" resolved
#include <QTime>
#include <QMessageBox>
代码海贼团船长's avatar
代码海贼团船长 已提交
8
#include <QFileDialog>
代码海贼团船长's avatar
代码海贼团船长 已提交
9 10
#include <QComboBox>
#include <QMetaEnum>
代码海贼团船长's avatar
代码海贼团船长 已提交
11
#include <QtConcurrent/QtConcurrent>
代码海贼团船长's avatar
代码海贼团船长 已提交
12 13 14 15
#include "home.h"
#include "ui_Home.h"


16 17 18 19 20 21
Home::Home(QWidget *parent) : QWidget(parent),
                              ui(new Ui::Home),
                              mpZControl(ZControl::instance()),
                              m_SendNumber(0),
                              m_RecNumber(0),
                              mpRubberBand(new QRubberBand(QRubberBand::Rectangle, this)){
代码海贼团船长's avatar
代码海贼团船长 已提交
22 23 24 25 26 27
    ui->setupUi(this);

    m_TimerId = startTimer(1000);

    mpSingleSend = new SingleSend();
    ui->tabWidget->addTab(mpSingleSend, tr("单条发送"));
28
    connect(mpSingleSend,SIGNAL(signalSerialWrite(QByteArray,bool,bool)),this,SLOT(slotSerialWrite(QByteArray, bool,bool)));
代码海贼团船长's avatar
代码海贼团船长 已提交
29 30 31 32
    connect(mpSingleSend,&SingleSend::signalClearSendInfo,[=](){
        this->m_SendNumber = 0;
        ui->label_SendNumber->setText(tr("发送:%0").arg(m_SendNumber));
    });
代码海贼团船长's avatar
代码海贼团船长 已提交
33 34 35

    mpMultipleSend = new MultipleSend();
    ui->tabWidget->addTab(mpMultipleSend, tr("多条发送"));
36
    //connect(mpMultipleSend,SIGNAL(signalSerialWrite(QByteArray,bool,bool)),this,SLOT(slotSerialWrite(QByteArray, bool,bool)));
代码海贼团船长's avatar
代码海贼团船长 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    ui->cbBox_Tty->addItems(mpZControl->getMpSerialPort()->getTtyList());
    connect(ui->cbBox_Tty, SIGNAL(currentTextChanged(const QString &)),this,SLOT(slotTtyNameTextChanged(const QString &)));


    ui->cbBox_Baud->addItems(mpZControl->getMpSerialPort()->getBaudList());
    ui->cbBox_Baud->setCurrentIndex(mpZControl->getMpSettings()->getBaudRadioIndex());
    connect(ui->cbBox_Baud, SIGNAL(currentTextChanged(const QString &)),this,SLOT(slotBaudTextChanged(const QString &)));

    ui->cbBox_Stop->addItems(mpZControl->getMpSerialPort()->getStopList());
    ui->cbBox_Stop->setCurrentIndex(mpZControl->getMpSettings()->getStopBitIndex());
    connect(ui->cbBox_Stop, SIGNAL(currentTextChanged(const QString &)),this,SLOT(slotStopTextChanged(const QString &)));

    ui->cbBox_Parity->addItems(mpZControl->getMpSerialPort()->getParityList());
    ui->cbBox_Parity->setCurrentIndex(mpZControl->getMpSettings()->getParityBitIndex());
    connect(ui->cbBox_Parity, SIGNAL(currentTextChanged(const QString &)),this,SLOT(slotParityTextChanged(const QString &)));

    ui->cbBox_Data->addItems(mpZControl->getMpSerialPort()->getDataList());
    ui->cbBox_Data->setCurrentIndex(mpZControl->getMpSettings()->getDataBitIndex());
    connect(ui->cbBox_Data, SIGNAL(currentTextChanged(const QString &)),this,SLOT(slotDataTextChanged(const QString &)));

    ui->tEdit_Rec->setReadOnly(true);

    ui->ckBox_TimeLine->setChecked(mpZControl->getMpSettings()->getTimeLineState());
    connect(ui->ckBox_TimeLine,&QCheckBox::stateChanged,[=](int state){
        mpZControl->getMpSettings()->setTimeLineState(state);
    });

    connect(mpZControl->getMpSerialPort(), SIGNAL(readyRead()),this,SLOT(slotSerialRead()));
    connect(mpZControl->getMpSerialPort(), SIGNAL(error(QSerialPort::SerialPortError)),this,SLOT(slotSerialError(QSerialPort::SerialPortError)));
代码海贼团船长's avatar
代码海贼团船长 已提交
67
    connect(mpZControl->getMpSerialPort(), SIGNAL(signalSerialPortListChange(const QStringList &, QString)),this,SLOT(slotSerialPortListChange(const QStringList &,QString)));
代码海贼团船长's avatar
代码海贼团船长 已提交
68
    connect(ui->pBn_TtySet,SIGNAL(clicked()),this,SLOT(slotPBnTtySetClicked()));
代码海贼团船长's avatar
代码海贼团船长 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    connect(ui->pBn_Clear,&QPushButton::clicked,[=](){
        m_RecNumber = 0;
        ui->label_RecNumber->setText(tr("接收:%0").arg(m_RecNumber));
    });
    connect(ui->pBn_Save,&QPushButton::clicked,[=](){
        QString fileName = QFileDialog::getSaveFileName(this,tr("选择文件夹"),"./","文本(*.txt);;所有文件(*)");
        if(fileName.isEmpty())
        {
            return ;
        }
        QtConcurrent::run([=](){
            QFile file(fileName);
            if(!file.open(QIODevice::WriteOnly))
            {
                QMessageBox::warning(this, tr("提示"),tr("文件保存失败。"));
                return;
            }
            file.write(ui->tEdit_Rec->toPlainText().toLocal8Bit());
            file.close();
        });
    });
90 91
    ui->ckBox_HexShow->setChecked(mpZControl->getMpSettings()->getHexFormalRec());
    connect(ui->ckBox_HexShow,SIGNAL(stateChanged(int)),this, SLOT(slotHexShowStateChanged(int)));
92 93


代码海贼团船长's avatar
代码海贼团船长 已提交
94 95 96
}

Home::~Home() {
97
    mpZControl->getMpSettings()->setHexFormalRec(ui->ckBox_HexShow->isChecked());
代码海贼团船长's avatar
代码海贼团船长 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
    killTimer(m_TimerId);
    delete mpSingleSend;
    delete mpMultipleSend;
    delete ui;
}

void Home::timerEvent(QTimerEvent *event) {
    QObject::timerEvent(event);
    QString currentTimeStr = QTime::currentTime().toString("hh:mm:ss");
    ui->label_CurrentTime->setText(tr("当前时间 %0").arg(currentTimeStr));
}

void Home::slotPBnTtySetClicked() {
111

代码海贼团船长's avatar
代码海贼团船长 已提交
112 113
    if(mpZControl->getMpSerialPort()->isOpen()){
        ui->pBn_TtySet->setText(tr("打开串口"));
114
        qDebug()<<mpZControl->getMpSerialPort()->isBreakEnabled();
代码海贼团船长's avatar
代码海贼团船长 已提交
115 116 117 118 119 120
        return;
    }

    bool info = mpZControl->getMpSerialPort()->openTty(ui->cbBox_Tty->currentText(),ui->cbBox_Baud->currentText().toInt(),
                                                       ui->cbBox_Stop->currentText(),ui->cbBox_Data->currentText(),ui->cbBox_Parity->currentText());

121
//    mpZControl->getMpSerialPort()->setBreakEnabled(true);
代码海贼团船长's avatar
代码海贼团船长 已提交
122 123 124 125 126 127 128 129 130
    if(!info){
        QMessageBox::warning(this,tr("提示"),tr("串口打开失败。"));
    }else{
        ui->pBn_TtySet->setText(tr("关闭串口"));
    }

}

void Home::slotTtyNameTextChanged(const QString &ttyName) {
代码海贼团船长's avatar
代码海贼团船长 已提交
131
    if(mpZControl->getMpSerialPort()->isOpen()&&mpZControl->getMpSerialPort()->error()==QSerialPort::NoError) {
代码海贼团船长's avatar
代码海贼团船长 已提交
132
        mpZControl->getMpSerialPort()->close();
代码海贼团船长's avatar
代码海贼团船长 已提交
133 134
        ui->pBn_TtySet->setText(tr("打开串口"));
        QMessageBox::warning(this,tr("提示"),tr("串口设置失败,不支持的错误操作。"));
代码海贼团船长's avatar
代码海贼团船长 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    }
    qDebug()<<"串口名:" << ttyName;
}

void Home::slotBaudTextChanged(const QString &text) {
    QComboBox *sender = static_cast<QComboBox *>(this->sender());
    mpZControl->getMpSettings()->setBaudRadioIndex(sender->currentIndex());

    bool info = mpZControl->getMpSerialPort()->setBaudRate(text.toInt());
    if(!info){
        //return;
    }
    qDebug()<<"波特率:" << text;
}

void Home::slotStopTextChanged(const QString &text) {
    QComboBox *sender = static_cast<QComboBox *>(this->sender());
    mpZControl->getMpSettings()->setStopBitIndex(sender->currentIndex());

    QMetaEnum metaEnum = QMetaEnum::fromType<QSerialPort::StopBits>();
    int value = metaEnum.keyToValue(text.toStdString().c_str());
    bool info = mpZControl->getMpSerialPort()->setStopBits(static_cast<QSerialPort::StopBits>(value));
    if(!info){
        //return;
    }
    qDebug()<<"停止位:" << static_cast<QSerialPort::StopBits>(value);
}

void Home::slotParityTextChanged(const QString &text) {
    QComboBox *sender = static_cast<QComboBox *>(this->sender());
    mpZControl->getMpSettings()->setParityBitIndex(sender->currentIndex());

    QMetaEnum metaEnum = QMetaEnum::fromType<QSerialPort::Parity>();
    int value = metaEnum.keyToValue(text.toStdString().c_str());
    bool info = mpZControl->getMpSerialPort()->setParity(static_cast<QSerialPort::Parity>(value));
    if(!info){
        //return;
    }
    qDebug()<<"校验位:" << static_cast<QSerialPort::Parity>(value);
}

void Home::slotDataTextChanged(const QString &text) {
    QComboBox *sender = static_cast<QComboBox *>(this->sender());
    mpZControl->getMpSettings()->setDataBitIndex(sender->currentIndex());

    QMetaEnum metaEnum = QMetaEnum::fromType<QSerialPort::DataBits>();
    int value = metaEnum.keyToValue(text.toStdString().c_str());
    bool info = mpZControl->getMpSerialPort()->setDataBits(static_cast<QSerialPort::DataBits>(value));
    if(!info){
代码海贼团船长's avatar
代码海贼团船长 已提交
184

代码海贼团船长's avatar
代码海贼团船长 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
        //return;
    }
    qDebug()<<"数据位:" << static_cast<QSerialPort::DataBits>(value);
}

void Home::slotSerialRead() {
    QSerialPort *pSerialPort = static_cast<QSerialPort*>(sender());
    QByteArray byteArray;
    ui->tEdit_Rec->setTextColor(QColor(Qt::red));
    if(ui->ckBox_TimeLine->isChecked())
    {
        byteArray+=QDateTime::currentDateTime().toString("[yyyy/MM/dd hh:mm:ss.zzz]\nRX:").toLocal8Bit();
    }
198

代码海贼团船长's avatar
代码海贼团船长 已提交
199
    QByteArray recArray = pSerialPort->readAll();
200 201 202 203 204 205 206 207 208 209
    QString str;
    switch (ui->ckBox_HexShow->checkState()) {
        case Qt::Unchecked:
            str = QString::fromLocal8Bit(recArray.trimmed());
            break;
        case Qt::Checked:
            str = QString::fromLocal8Bit(recArray.toHex(' '));
            break;
    }
    str = QString::fromLocal8Bit(byteArray)+str;
代码海贼团船长's avatar
代码海贼团船长 已提交
210 211
    m_RecNumber+=recArray.size();
    ui->label_RecNumber->setText(tr("接收:%0").arg(m_RecNumber));
212
    ui->tEdit_Rec->append(str);
代码海贼团船长's avatar
代码海贼团船长 已提交
213 214
}

215
void Home::slotSerialWrite(QByteArray array,bool isHex,bool isEnter) {
代码海贼团船长's avatar
代码海贼团船长 已提交
216 217 218 219 220 221
    QByteArray byteArray;
    ui->tEdit_Rec->setTextColor(QColor(Qt::green));
    if(ui->ckBox_TimeLine->isChecked())
    {
        byteArray.append(QDateTime::currentDateTime().toString("[yyyy/MM/dd hh:mm:ss.zzz]\nRX:").toLocal8Bit());
    }
222 223 224 225 226 227 228 229 230 231 232 233
    if(isEnter){
        array.append("\r\n");
    }
    QString str;
    if(!isHex){
        str = QString::fromLocal8Bit(array.trimmed());
    }else{
        str = QString::fromLocal8Bit(array.toHex(' '));
    }


    str = QString::fromLocal8Bit(byteArray)+str;
代码海贼团船长's avatar
代码海贼团船长 已提交
234 235 236
    qint64 size = mpZControl->getMpSerialPort()->write(array);
    m_SendNumber+=size ;
    ui->label_SendNumber->setText(tr("发送:%0").arg(m_SendNumber));
237
    ui->tEdit_Rec->append(str);
代码海贼团船长's avatar
代码海贼团船长 已提交
238 239 240
}

void Home::slotSerialError(QSerialPort::SerialPortError error) {
代码海贼团船长's avatar
代码海贼团船长 已提交
241 242 243 244 245 246 247 248 249

    if(error!=QSerialPort::NoError)
    {
        mpZControl->getMpSerialPort()->close();
        ui->pBn_TtySet->setText(tr("打开串口"));
        QMetaEnum metaEnum = QMetaEnum::fromType<QSerialPort::SerialPortError>();
        QString errorStr = metaEnum.valueToKey(error);
        QMessageBox::warning(this,tr("提示"),tr("串口设置失败,不支持的错误操作,错误信息%0。").arg( errorStr));
    }
代码海贼团船长's avatar
代码海贼团船长 已提交
250
    qDebug()<<"串口错误信息:" << error;
代码海贼团船长's avatar
代码海贼团船长 已提交
251 252 253 254 255 256
}

void Home::slotSerialPortListChange(const QStringList &list,QString currentTtyName) {
    ui->cbBox_Tty->clear();
    ui->cbBox_Tty->addItems(list);
    ui->cbBox_Tty->setCurrentText(currentTtyName);
代码海贼团船长's avatar
代码海贼团船长 已提交
257
}
258 259 260 261 262 263 264 265 266 267 268 269

void Home::slotHexShowStateChanged(int state) {
    switch (state) {
        case Qt::Unchecked:

            break;
        case Qt::Checked:

            break;
    }
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
void Home::mousePressEvent(QMouseEvent *event) {
    m_Start = event->pos();
    mpRubberBand->setGeometry(QRect(m_Start, QSize()));
    mpRubberBand->show();
    QWidget::mousePressEvent(event);
}

void Home::mouseReleaseEvent(QMouseEvent *event) {
    if(mpRubberBand)
        mpRubberBand->hide();
    QWidget::mouseReleaseEvent(event);
}

void Home::mouseMoveEvent(QMouseEvent *event) {
    if(mpRubberBand)
        mpRubberBand->setGeometry(QRect(m_Start, event->pos()).normalized());
    QWidget::mouseMoveEvent(event);
}