|
| 1 | +#include <QPushButton> |
| 2 | +#include <QAbstractEventDispatcher> |
| 3 | +#include "dialogmsgparser.h" |
| 4 | +#include "ui_dialogmsgparser.h" |
| 5 | + |
| 6 | +/// |
| 7 | +/// \brief DialogMsgParser::DialogMsgParser |
| 8 | +/// \param mode |
| 9 | +/// \param parent |
| 10 | +/// |
| 11 | +DialogMsgParser::DialogMsgParser(DataDisplayMode mode, QWidget *parent) |
| 12 | + : QDialog(parent) |
| 13 | + , ui(new Ui::DialogMsgParser) |
| 14 | + ,_mm(nullptr) |
| 15 | +{ |
| 16 | + ui->setupUi(this); |
| 17 | + |
| 18 | + setWindowFlags(Qt::Dialog | |
| 19 | + Qt::CustomizeWindowHint | |
| 20 | + Qt::WindowTitleHint); |
| 21 | + |
| 22 | + ui->info->setShowTimestamp(false); |
| 23 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Parse")); |
| 24 | + ui->hexView->setCheckState(mode == DataDisplayMode::Hex ? Qt::Checked : Qt::Unchecked); |
| 25 | + |
| 26 | + auto dispatcher = QAbstractEventDispatcher::instance(); |
| 27 | + connect(dispatcher, &QAbstractEventDispatcher::awake, this, &DialogMsgParser::on_awake); |
| 28 | +} |
| 29 | + |
| 30 | +/// |
| 31 | +/// \brief DialogMsgParser::~DialogMsgParser |
| 32 | +/// |
| 33 | +DialogMsgParser::~DialogMsgParser() |
| 34 | +{ |
| 35 | + delete ui; |
| 36 | + if(_mm) delete _mm; |
| 37 | +} |
| 38 | + |
| 39 | +/// |
| 40 | +/// \brief DialogMsgParser::on_awake |
| 41 | +/// |
| 42 | +void DialogMsgParser::on_awake() |
| 43 | +{ |
| 44 | + ui->deviceIdIncluded->setVisible(ui->buttonPdu->isChecked()); |
| 45 | + ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!ui->bytesData->isEmpty()); |
| 46 | +} |
| 47 | + |
| 48 | +/// |
| 49 | +/// \brief DialogMsgParser::on_hexView_toggled |
| 50 | +/// \param checked |
| 51 | +/// |
| 52 | +void DialogMsgParser::on_hexView_toggled(bool checked) |
| 53 | +{ |
| 54 | + ui->bytesData->setInputMode(checked ? ByteListTextEdit::HexMode : ByteListTextEdit::DecMode); |
| 55 | + ui->info->setDataDisplayMode(checked ? DataDisplayMode::Hex : DataDisplayMode::Decimal); |
| 56 | +} |
| 57 | + |
| 58 | +/// |
| 59 | +/// \brief DialogMsgParser::on_bytesData_valueChanged |
| 60 | +/// \param value |
| 61 | +/// |
| 62 | +void DialogMsgParser::on_bytesData_valueChanged(const QByteArray& value) |
| 63 | +{ |
| 64 | + if(value.size() < 3) |
| 65 | + { |
| 66 | + if(value.isEmpty()) |
| 67 | + ui->info->clear(); |
| 68 | + |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const auto data = value.left(value.size() - 2); |
| 73 | + const int checksum = makeWord(value[value.size() - 1], value[value.size() - 2], ByteOrder::LittleEndian); |
| 74 | + ui->checksumIncluded->setChecked(QModbusAdu::calculateCRC(data, data.size()) == checksum); |
| 75 | +} |
| 76 | + |
| 77 | +/// |
| 78 | +/// \brief DialogMsgParser::accept |
| 79 | +/// |
| 80 | +void DialogMsgParser::accept() |
| 81 | +{ |
| 82 | + auto data = ui->bytesData->value(); |
| 83 | + if(data.isEmpty()) return; |
| 84 | + |
| 85 | + ModbusMessage::Type type = ModbusMessage::Adu; |
| 86 | + auto protocol = QModbusAdu::Tcp; |
| 87 | + int checksum = 0; |
| 88 | + |
| 89 | + if(ui->buttonPdu->isChecked()) |
| 90 | + { |
| 91 | + type = ModbusMessage::Pdu; |
| 92 | + } |
| 93 | + |
| 94 | + if(!ui->deviceIdIncluded->isChecked()) |
| 95 | + { |
| 96 | + data.push_front('\0'); |
| 97 | + } |
| 98 | + |
| 99 | + if(ui->checksumIncluded->isChecked()) |
| 100 | + { |
| 101 | + protocol = QModbusAdu::Rtu; |
| 102 | + checksum = makeWord(data[data.size() - 1], data[data.size() - 2], ByteOrder::LittleEndian); |
| 103 | + data = data.left(data.size() - 2); |
| 104 | + } |
| 105 | + |
| 106 | + if(_mm) delete _mm; |
| 107 | + _mm = ModbusMessage::parse(data, type, protocol, ui->request->isChecked(), checksum); |
| 108 | + ui->info->setModbusMessage(_mm); |
| 109 | +} |
0 commit comments