282 lines
8.9 KiB
C++
282 lines
8.9 KiB
C++
#include "formserialportsettingdialog.h"
|
|
#include "ui_formserialportsettingdialog.h"
|
|
|
|
#include <QListView>
|
|
#include <QStandardItemModel>
|
|
#include <QPushButton>
|
|
#include <QMessageBox>
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
#include <QByteArray>
|
|
#include <QDebug>
|
|
|
|
#include <hv/hlog.h>
|
|
|
|
#include "frame_define.h"
|
|
#include "globalparameters.h"
|
|
#include "kutilities.h"
|
|
|
|
FormSerialPortSettingDialog::FormSerialPortSettingDialog(QWidget *parent)
|
|
: QDialog(parent)
|
|
, ui(new Ui::FormSerialPortSettingDialog)
|
|
{
|
|
ui->setupUi(this);
|
|
InitializeUi();
|
|
}
|
|
|
|
FormSerialPortSettingDialog::~FormSerialPortSettingDialog()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void FormSerialPortSettingDialog::InitializeUi()
|
|
{
|
|
ui->delay_timeout->setText("1000");
|
|
ui->loop_time->setText("5000");
|
|
ui->slave_id->setText("1");
|
|
|
|
//设置奇偶校验
|
|
ui->cb_parity->setView(new QListView(this));
|
|
|
|
QStandardItemModel* model_parity = new QStandardItemModel(this);
|
|
|
|
vecParityItemData vparity;
|
|
vparity.push_back(std::make_pair(EMS_PARITY_NONE,tr("None")));
|
|
vparity.push_back(std::make_pair(EMS_PARITY_ODD,tr("Odd")));
|
|
vparity.push_back(std::make_pair(EMS_PARITY_EVEN,tr("Even")));
|
|
|
|
for (auto it = vparity.begin(); it != vparity.end(); it++)
|
|
{
|
|
int id = it->first;
|
|
QString name = it->second;
|
|
|
|
QStandardItem* item = new QStandardItem(name);
|
|
//QVariant var = QVariant::fromValue(data);
|
|
item->setData(id);
|
|
model_parity->appendRow(item);
|
|
}
|
|
|
|
ui->cb_parity->setModel(model_parity);
|
|
|
|
//设置数据位
|
|
ui->cb_data->setView(new QListView(this));
|
|
QStandardItemModel* model_databits = new QStandardItemModel(this);
|
|
vecDataBitsItemData vdatabits;
|
|
vdatabits.push_back(std::make_pair(EMS_DATA_8_BITS,tr("8 Data bits")));
|
|
vdatabits.push_back(std::make_pair(EMS_DATA_7_BITS,tr("7 Data bits")));
|
|
|
|
for (auto it = vdatabits.begin(); it != vdatabits.end(); it++)
|
|
{
|
|
int id = it->first;
|
|
QString name = it->second;
|
|
|
|
QStandardItem* item = new QStandardItem(name);
|
|
item->setData(id);
|
|
model_databits->appendRow(item);
|
|
}
|
|
|
|
ui->cb_data->setModel(model_databits);
|
|
|
|
//设置数据位
|
|
ui->cb_stop->setView(new QListView(this));
|
|
QStandardItemModel* model_stopbits = new QStandardItemModel(this);
|
|
vecDataBitsItemData vstopbits;
|
|
vstopbits.push_back(std::make_pair(EMS_1_STOP_BITS,tr("1 Stop bit")));
|
|
vstopbits.push_back(std::make_pair(EMS_2_STOP_BITS,tr("2 Stop bits")));
|
|
|
|
for (auto it = vstopbits.begin(); it != vstopbits.end(); it++)
|
|
{
|
|
int id = it->first;
|
|
QString name = it->second;
|
|
|
|
QStandardItem* item = new QStandardItem(name);
|
|
item->setData(id);
|
|
model_stopbits->appendRow(item);
|
|
}
|
|
|
|
ui->cb_stop->setModel(model_stopbits);
|
|
|
|
//设置串口模式
|
|
ui->cb_mode->setView(new QListView(this));
|
|
QStandardItemModel* model_mode = new QStandardItemModel(this);
|
|
vecDataBitsItemData vportmode;
|
|
vportmode.push_back(std::make_pair(EMS_RTU_MODE,tr("RTU")));
|
|
vportmode.push_back(std::make_pair(EMS_ASCII_MODE,tr("ASCII")));
|
|
|
|
for (auto it = vportmode.begin(); it != vportmode.end(); it++)
|
|
{
|
|
int id = it->first;
|
|
QString name = it->second;
|
|
|
|
QStandardItem* item = new QStandardItem(name);
|
|
item->setData(id);
|
|
model_mode->appendRow(item);
|
|
}
|
|
|
|
ui->cb_mode->setModel(model_mode);
|
|
|
|
//设置串口模式
|
|
ui->cb_baund->setView(new QListView(this));
|
|
QStandardItemModel* model_baund = new QStandardItemModel(this);
|
|
vecBaundItemData vbaund;
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("2400")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("4800")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("9600")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("19200")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("38400")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("57600")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("115200")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("128000")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("153600")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("230400")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("460800")));
|
|
vbaund.push_back(std::make_pair(EMS_BAUND_2400,tr("921600")));
|
|
|
|
for (auto it = vbaund.begin(); it != vbaund.end(); it++)
|
|
{
|
|
int id = it->first;
|
|
QString name = it->second;
|
|
|
|
QStandardItem* item = new QStandardItem(name);
|
|
item->setData(id);
|
|
model_baund->appendRow(item);
|
|
}
|
|
|
|
ui->cb_baund->setModel(model_baund);
|
|
ui->cb_baund->setCurrentIndex(2);
|
|
|
|
//设置串口类型
|
|
ui->cb_type->setView(new QListView(this));
|
|
QStandardItemModel* model_type = new QStandardItemModel(this);
|
|
vecPortTypeItemData vporttype;
|
|
vporttype.push_back(std::make_pair(EMS_SERIALPORT_TYPE,tr("Serial Port")));
|
|
|
|
for (auto it = vporttype.begin(); it != vporttype.end(); it++)
|
|
{
|
|
int id = it->first;
|
|
QString name = it->second;
|
|
|
|
QStandardItem* item = new QStandardItem(name);
|
|
item->setData(id);
|
|
model_type->appendRow(item);
|
|
}
|
|
|
|
ui->cb_type->setModel(model_type);
|
|
|
|
ui->toolButton->setFixedSize(40, 30);
|
|
|
|
ui->buttonBox->clear();
|
|
QPushButton *okButton = ui->buttonBox->addButton(QDialogButtonBox::Ok);
|
|
QPushButton *cancelButton = ui->buttonBox->addButton(QDialogButtonBox::Cancel);
|
|
okButton->setFixedSize(100, 30);
|
|
cancelButton->setFixedSize(100, 30);
|
|
|
|
//设置串口号
|
|
ui->cb_serialport->setView(new QListView(this));
|
|
|
|
QStandardItemModel* model_serialport = new QStandardItemModel(this);
|
|
|
|
for (int i=0; i < AppData::getInstance()->SerialPortCount ; i++)
|
|
{
|
|
QString name = AppData::getInstance()->SerialPortPrefix + QString("%1").arg(i);
|
|
|
|
QStandardItem* item = new QStandardItem(name);
|
|
//QVariant var = QVariant::fromValue(data);
|
|
item->setData(name);
|
|
model_serialport->appendRow(item);
|
|
}
|
|
|
|
ui->cb_serialport->setModel(model_serialport);
|
|
|
|
connect(okButton, &QPushButton::clicked, this, &FormSerialPortSettingDialog::onOkClicked);
|
|
connect(cancelButton, &QPushButton::clicked, this, &FormSerialPortSettingDialog::onCancelClicked);
|
|
connect(ui->toolButton, &QPushButton::clicked, this, &FormSerialPortSettingDialog::onToolButtonClicked);
|
|
}
|
|
|
|
void FormSerialPortSettingDialog::onOkClicked()
|
|
{
|
|
QMessageBox::information(this, "onOkClicked", "Refresh");
|
|
}
|
|
|
|
void FormSerialPortSettingDialog::onCancelClicked()
|
|
{
|
|
QMessageBox::information(this, "onCancelClicked", "Refresh");
|
|
}
|
|
|
|
void FormSerialPortSettingDialog::onToolButtonClicked()
|
|
{
|
|
QString filePath("d:\\test_base641.zip");
|
|
|
|
// 打开文件进行读取
|
|
QFile file(filePath);
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
{
|
|
QString msg = QString(tr("Failed to open file: %1")).arg(filePath);
|
|
QMessageBox::critical(this, msg, tr("Critical"));
|
|
return;
|
|
}
|
|
|
|
// 读取文件内容
|
|
QByteArray fileData = file.readAll();
|
|
int fileLen = fileData.length();
|
|
|
|
// 关闭文件
|
|
file.close();
|
|
|
|
//构筑消息包
|
|
int msgLen = FRAME_HEADER_LENGTH + fileLen + 4;
|
|
unsigned char* pMsg = new unsigned char[msgLen];
|
|
memset(pMsg, 0, msgLen);
|
|
|
|
HJ::MessageFrame* pFrame = (HJ::MessageFrame*)pMsg;
|
|
pFrame->frame_type = HJ::Frame_Configure_DB_Request;
|
|
pFrame->frame_len = fileLen;
|
|
memcpy(pMsg+FRAME_HEADER_LENGTH,(void*)fileData.data(),fileLen);
|
|
pMsg[FRAME_HEADER_LENGTH+fileLen] = 0xEE;
|
|
pMsg[FRAME_HEADER_LENGTH+fileLen+1] = 0xFF;
|
|
pMsg[FRAME_HEADER_LENGTH+fileLen+2] = 0xEE;
|
|
pMsg[FRAME_HEADER_LENGTH+fileLen+3] = 0xFF;
|
|
|
|
int ret = -1;
|
|
|
|
AppTcpClient::getInstance()->m_tcpclient->onMessage = [&ret](const hv::SocketChannelPtr& channel, hv::Buffer* buf)
|
|
{
|
|
hlogd("FormSerialportSettingDialog <==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
|
|
HJ::MessageFrame* pRespMsg = (HJ::MessageFrame*)buf->data();
|
|
ret = (int)(*pRespMsg->frame_content);
|
|
};
|
|
|
|
while(1)
|
|
{
|
|
int cnt = AppTcpClient::getInstance()->m_tcpclient->send((void*)pMsg, msgLen);
|
|
hlogd("send %d bytes configure database ...", cnt);
|
|
|
|
{
|
|
CWaitorCursor waits;
|
|
|
|
hv_sleep(2);
|
|
}
|
|
|
|
hlogd("FormSerialportSettingDialog <== response from service %d ", ret);
|
|
|
|
if (ret == HJ::ErrorCode::ERR_OK)
|
|
{
|
|
break;
|
|
}
|
|
else if (ret == HJ::ErrorCode::ERR_PERMISSION_DENIED)
|
|
{
|
|
QMessageBox::critical(this, "Critical", tr("Service responsed: Permission denyed\r\nContact Developement for supporting!"));
|
|
break;
|
|
}
|
|
else if (ret == HJ::ErrorCode::ERR_INVALID_CFG_CONTENT || ret == HJ::ErrorCode::ERR_RETRANS_CONTENT)
|
|
{
|
|
if (QMessageBox::No == QMessageBox::critical(this, "Alert",tr("Upload file failed, Press Yes to upload again!"), QMessageBox::Yes | QMessageBox::No))
|
|
break;
|
|
}
|
|
}
|
|
|
|
delete[] pMsg;
|
|
}
|
|
|
|
|