彻底改版2.0
This commit is contained in:
8
tool/comtool/form/form.pri
Normal file
8
tool/comtool/form/form.pri
Normal file
@@ -0,0 +1,8 @@
|
||||
FORMS += \
|
||||
$$PWD/frmcomtool.ui
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/frmcomtool.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/frmcomtool.cpp
|
||||
588
tool/comtool/form/frmcomtool.cpp
Normal file
588
tool/comtool/form/frmcomtool.cpp
Normal file
@@ -0,0 +1,588 @@
|
||||
#include "frmcomtool.h"
|
||||
#include "ui_frmcomtool.h"
|
||||
#include "quihelper.h"
|
||||
#include "quihelperdata.h"
|
||||
|
||||
frmComTool::frmComTool(QWidget *parent) : QWidget(parent), ui(new Ui::frmComTool)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
this->initForm();
|
||||
this->initConfig();
|
||||
QUIHelper::setFormInCenter(this);
|
||||
}
|
||||
|
||||
frmComTool::~frmComTool()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void frmComTool::initForm()
|
||||
{
|
||||
comOk = false;
|
||||
com = 0;
|
||||
sleepTime = 10;
|
||||
receiveCount = 0;
|
||||
sendCount = 0;
|
||||
isShow = true;
|
||||
|
||||
ui->cboxSendInterval->addItems(AppData::Intervals);
|
||||
ui->cboxData->addItems(AppData::Datas);
|
||||
|
||||
//读取数据
|
||||
timerRead = new QTimer(this);
|
||||
timerRead->setInterval(100);
|
||||
connect(timerRead, SIGNAL(timeout()), this, SLOT(readData()));
|
||||
|
||||
//发送数据
|
||||
timerSend = new QTimer(this);
|
||||
connect(timerSend, SIGNAL(timeout()), this, SLOT(sendData()));
|
||||
connect(ui->btnSend, SIGNAL(clicked()), this, SLOT(sendData()));
|
||||
|
||||
//保存数据
|
||||
timerSave = new QTimer(this);
|
||||
connect(timerSave, SIGNAL(timeout()), this, SLOT(saveData()));
|
||||
connect(ui->btnSave, SIGNAL(clicked()), this, SLOT(saveData()));
|
||||
|
||||
ui->tabWidget->setCurrentIndex(0);
|
||||
changeEnable(false);
|
||||
|
||||
tcpOk = false;
|
||||
socket = new QTcpSocket(this);
|
||||
socket->abort();
|
||||
connect(socket, SIGNAL(readyRead()), this, SLOT(readDataNet()));
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
||||
connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)), this, SLOT(readErrorNet()));
|
||||
#else
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(readErrorNet()));
|
||||
#endif
|
||||
|
||||
timerConnect = new QTimer(this);
|
||||
connect(timerConnect, SIGNAL(timeout()), this, SLOT(connectNet()));
|
||||
timerConnect->setInterval(3000);
|
||||
timerConnect->start();
|
||||
|
||||
#ifdef __arm__
|
||||
ui->widgetRight->setFixedWidth(280);
|
||||
#endif
|
||||
}
|
||||
|
||||
void frmComTool::initConfig()
|
||||
{
|
||||
QStringList comList;
|
||||
for (int i = 1; i <= 20; i++) {
|
||||
comList << QString("COM%1").arg(i);
|
||||
}
|
||||
|
||||
comList << "ttyUSB0" << "ttyS0" << "ttyS1" << "ttyS2" << "ttyS3" << "ttyS4";
|
||||
comList << "ttymxc1" << "ttymxc2" << "ttymxc3" << "ttymxc4";
|
||||
comList << "ttySAC1" << "ttySAC2" << "ttySAC3" << "ttySAC4";
|
||||
ui->cboxPortName->addItems(comList);
|
||||
ui->cboxPortName->setCurrentIndex(ui->cboxPortName->findText(AppConfig::PortName));
|
||||
connect(ui->cboxPortName, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
QStringList baudList;
|
||||
baudList << "50" << "75" << "100" << "134" << "150" << "200" << "300" << "600" << "1200"
|
||||
<< "1800" << "2400" << "4800" << "9600" << "14400" << "19200" << "38400"
|
||||
<< "56000" << "57600" << "76800" << "115200" << "128000" << "256000";
|
||||
|
||||
ui->cboxBaudRate->addItems(baudList);
|
||||
ui->cboxBaudRate->setCurrentIndex(ui->cboxBaudRate->findText(QString::number(AppConfig::BaudRate)));
|
||||
connect(ui->cboxBaudRate, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
QStringList dataBitsList;
|
||||
dataBitsList << "5" << "6" << "7" << "8";
|
||||
|
||||
ui->cboxDataBit->addItems(dataBitsList);
|
||||
ui->cboxDataBit->setCurrentIndex(ui->cboxDataBit->findText(QString::number(AppConfig::DataBit)));
|
||||
connect(ui->cboxDataBit, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
QStringList parityList;
|
||||
parityList << "无" << "奇" << "偶";
|
||||
#ifdef Q_OS_WIN
|
||||
parityList << "标志";
|
||||
#endif
|
||||
parityList << "空格";
|
||||
|
||||
ui->cboxParity->addItems(parityList);
|
||||
ui->cboxParity->setCurrentIndex(ui->cboxParity->findText(AppConfig::Parity));
|
||||
connect(ui->cboxParity, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
QStringList stopBitsList;
|
||||
stopBitsList << "1";
|
||||
#ifdef Q_OS_WIN
|
||||
stopBitsList << "1.5";
|
||||
#endif
|
||||
stopBitsList << "2";
|
||||
|
||||
ui->cboxStopBit->addItems(stopBitsList);
|
||||
ui->cboxStopBit->setCurrentIndex(ui->cboxStopBit->findText(QString::number(AppConfig::StopBit)));
|
||||
connect(ui->cboxStopBit, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexSend->setChecked(AppConfig::HexSend);
|
||||
connect(ui->ckHexSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckHexReceive->setChecked(AppConfig::HexReceive);
|
||||
connect(ui->ckHexReceive, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckDebug->setChecked(AppConfig::Debug);
|
||||
connect(ui->ckDebug, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoClear->setChecked(AppConfig::AutoClear);
|
||||
connect(ui->ckAutoClear, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSend->setChecked(AppConfig::AutoSend);
|
||||
connect(ui->ckAutoSend, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoSave->setChecked(AppConfig::AutoSave);
|
||||
connect(ui->ckAutoSave, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
QStringList sendInterval;
|
||||
QStringList saveInterval;
|
||||
sendInterval << "100" << "300" << "500";
|
||||
|
||||
for (int i = 1000; i <= 10000; i = i + 1000) {
|
||||
sendInterval << QString::number(i);
|
||||
saveInterval << QString::number(i);
|
||||
}
|
||||
|
||||
ui->cboxSendInterval->addItems(sendInterval);
|
||||
ui->cboxSaveInterval->addItems(saveInterval);
|
||||
|
||||
ui->cboxSendInterval->setCurrentIndex(ui->cboxSendInterval->findText(QString::number(AppConfig::SendInterval)));
|
||||
connect(ui->cboxSendInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
ui->cboxSaveInterval->setCurrentIndex(ui->cboxSaveInterval->findText(QString::number(AppConfig::SaveInterval)));
|
||||
connect(ui->cboxSaveInterval, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
timerSend->setInterval(AppConfig::SendInterval);
|
||||
timerSave->setInterval(AppConfig::SaveInterval);
|
||||
|
||||
if (AppConfig::AutoSend) {
|
||||
timerSend->start();
|
||||
}
|
||||
|
||||
if (AppConfig::AutoSave) {
|
||||
timerSave->start();
|
||||
}
|
||||
|
||||
//串口转网络部分
|
||||
ui->cboxMode->setCurrentIndex(ui->cboxMode->findText(AppConfig::Mode));
|
||||
connect(ui->cboxMode, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerIP->setText(AppConfig::ServerIP);
|
||||
connect(ui->txtServerIP, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtServerPort->setText(QString::number(AppConfig::ServerPort));
|
||||
connect(ui->txtServerPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->txtListenPort->setText(QString::number(AppConfig::ListenPort));
|
||||
connect(ui->txtListenPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
|
||||
|
||||
QStringList values;
|
||||
values << "0" << "10" << "50";
|
||||
|
||||
for (int i = 100; i < 1000; i = i + 100) {
|
||||
values << QString("%1").arg(i);
|
||||
}
|
||||
|
||||
ui->cboxSleepTime->addItems(values);
|
||||
|
||||
ui->cboxSleepTime->setCurrentIndex(ui->cboxSleepTime->findText(QString::number(AppConfig::SleepTime)));
|
||||
connect(ui->cboxSleepTime, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
|
||||
|
||||
ui->ckAutoConnect->setChecked(AppConfig::AutoConnect);
|
||||
connect(ui->ckAutoConnect, SIGNAL(stateChanged(int)), this, SLOT(saveConfig()));
|
||||
}
|
||||
|
||||
void frmComTool::saveConfig()
|
||||
{
|
||||
AppConfig::PortName = ui->cboxPortName->currentText();
|
||||
AppConfig::BaudRate = ui->cboxBaudRate->currentText().toInt();
|
||||
AppConfig::DataBit = ui->cboxDataBit->currentText().toInt();
|
||||
AppConfig::Parity = ui->cboxParity->currentText();
|
||||
AppConfig::StopBit = ui->cboxStopBit->currentText().toDouble();
|
||||
|
||||
AppConfig::HexSend = ui->ckHexSend->isChecked();
|
||||
AppConfig::HexReceive = ui->ckHexReceive->isChecked();
|
||||
AppConfig::Debug = ui->ckDebug->isChecked();
|
||||
AppConfig::AutoClear = ui->ckAutoClear->isChecked();
|
||||
|
||||
AppConfig::AutoSend = ui->ckAutoSend->isChecked();
|
||||
AppConfig::AutoSave = ui->ckAutoSave->isChecked();
|
||||
|
||||
int sendInterval = ui->cboxSendInterval->currentText().toInt();
|
||||
if (sendInterval != AppConfig::SendInterval) {
|
||||
AppConfig::SendInterval = sendInterval;
|
||||
timerSend->setInterval(AppConfig::SendInterval);
|
||||
}
|
||||
|
||||
int saveInterval = ui->cboxSaveInterval->currentText().toInt();
|
||||
if (saveInterval != AppConfig::SaveInterval) {
|
||||
AppConfig::SaveInterval = saveInterval;
|
||||
timerSave->setInterval(AppConfig::SaveInterval);
|
||||
}
|
||||
|
||||
AppConfig::Mode = ui->cboxMode->currentText();
|
||||
AppConfig::ServerIP = ui->txtServerIP->text().trimmed();
|
||||
AppConfig::ServerPort = ui->txtServerPort->text().toInt();
|
||||
AppConfig::ListenPort = ui->txtListenPort->text().toInt();
|
||||
AppConfig::SleepTime = ui->cboxSleepTime->currentText().toInt();
|
||||
AppConfig::AutoConnect = ui->ckAutoConnect->isChecked();
|
||||
|
||||
AppConfig::writeConfig();
|
||||
}
|
||||
|
||||
void frmComTool::changeEnable(bool b)
|
||||
{
|
||||
ui->cboxBaudRate->setEnabled(!b);
|
||||
ui->cboxDataBit->setEnabled(!b);
|
||||
ui->cboxParity->setEnabled(!b);
|
||||
ui->cboxPortName->setEnabled(!b);
|
||||
ui->cboxStopBit->setEnabled(!b);
|
||||
ui->btnSend->setEnabled(b);
|
||||
ui->ckAutoSend->setEnabled(b);
|
||||
ui->ckAutoSave->setEnabled(b);
|
||||
}
|
||||
|
||||
void frmComTool::append(int type, const QString &data, bool clear)
|
||||
{
|
||||
static int currentCount = 0;
|
||||
static int maxCount = 100;
|
||||
|
||||
if (clear) {
|
||||
ui->txtMain->clear();
|
||||
currentCount = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentCount >= maxCount) {
|
||||
ui->txtMain->clear();
|
||||
currentCount = 0;
|
||||
}
|
||||
|
||||
if (!isShow) {
|
||||
return;
|
||||
}
|
||||
|
||||
//过滤回车换行符
|
||||
QString strData = data;
|
||||
strData = strData.replace("\r", "");
|
||||
strData = strData.replace("\n", "");
|
||||
|
||||
//不同类型不同颜色显示
|
||||
QString strType;
|
||||
if (type == 0) {
|
||||
strType = "串口发送 >>";
|
||||
ui->txtMain->setTextColor(QColor("dodgerblue"));
|
||||
} else if (type == 1) {
|
||||
strType = "串口接收 <<";
|
||||
ui->txtMain->setTextColor(QColor("red"));
|
||||
} else if (type == 2) {
|
||||
strType = "处理延时 >>";
|
||||
ui->txtMain->setTextColor(QColor("gray"));
|
||||
} else if (type == 3) {
|
||||
strType = "正在校验 >>";
|
||||
ui->txtMain->setTextColor(QColor("green"));
|
||||
} else if (type == 4) {
|
||||
strType = "网络发送 >>";
|
||||
ui->txtMain->setTextColor(QColor(24, 189, 155));
|
||||
} else if (type == 5) {
|
||||
strType = "网络接收 <<";
|
||||
ui->txtMain->setTextColor(QColor(255, 107, 107));
|
||||
} else if (type == 6) {
|
||||
strType = "提示信息 >>";
|
||||
ui->txtMain->setTextColor(QColor(100, 184, 255));
|
||||
}
|
||||
|
||||
strData = QString("时间[%1] %2 %3").arg(TIMEMS).arg(strType).arg(strData);
|
||||
ui->txtMain->append(strData);
|
||||
currentCount++;
|
||||
}
|
||||
|
||||
void frmComTool::readData()
|
||||
{
|
||||
if (com->bytesAvailable() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
QUIHelper::sleep(sleepTime);
|
||||
QByteArray data = com->readAll();
|
||||
int dataLen = data.length();
|
||||
if (dataLen <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isShow) {
|
||||
QString buffer;
|
||||
if (ui->ckHexReceive->isChecked()) {
|
||||
buffer = QUIHelperData::byteArrayToHexStr(data);
|
||||
} else {
|
||||
//buffer = QUIHelperData::byteArrayToAsciiStr(data);
|
||||
buffer = QString::fromLocal8Bit(data);
|
||||
}
|
||||
|
||||
//启用调试则模拟调试数据
|
||||
if (ui->ckDebug->isChecked()) {
|
||||
int count = AppData::Keys.count();
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (buffer.startsWith(AppData::Keys.at(i))) {
|
||||
sendData(AppData::Values.at(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
append(1, buffer);
|
||||
receiveCount = receiveCount + data.size();
|
||||
ui->btnReceiveCount->setText(QString("接收 : %1 字节").arg(receiveCount));
|
||||
|
||||
//启用网络转发则调用网络发送数据
|
||||
if (tcpOk) {
|
||||
socket->write(data);
|
||||
append(4, QString(buffer));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::sendData()
|
||||
{
|
||||
QString str = ui->cboxData->currentText();
|
||||
if (str.isEmpty()) {
|
||||
ui->cboxData->setFocus();
|
||||
return;
|
||||
}
|
||||
|
||||
sendData(str);
|
||||
|
||||
if (ui->ckAutoClear->isChecked()) {
|
||||
ui->cboxData->setCurrentIndex(-1);
|
||||
ui->cboxData->setFocus();
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::sendData(QString data)
|
||||
{
|
||||
if (com == 0 || !com->isOpen()) {
|
||||
return;
|
||||
}
|
||||
|
||||
//短信猫调试
|
||||
if (data.startsWith("AT")) {
|
||||
data += "\r";
|
||||
}
|
||||
|
||||
QByteArray buffer;
|
||||
if (ui->ckHexSend->isChecked()) {
|
||||
buffer = QUIHelperData::hexStrToByteArray(data);
|
||||
} else {
|
||||
buffer = QUIHelperData::asciiStrToByteArray(data);
|
||||
}
|
||||
|
||||
com->write(buffer);
|
||||
append(0, data);
|
||||
sendCount = sendCount + buffer.size();
|
||||
ui->btnSendCount->setText(QString("发送 : %1 字节").arg(sendCount));
|
||||
}
|
||||
|
||||
void frmComTool::saveData()
|
||||
{
|
||||
QString tempData = ui->txtMain->toPlainText();
|
||||
|
||||
if (tempData == "") {
|
||||
return;
|
||||
}
|
||||
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
QString name = now.toString("yyyy-MM-dd-HH-mm-ss");
|
||||
QString fileName = QString("%1/%2.txt").arg(QUIHelper::appPath()).arg(name);
|
||||
|
||||
QFile file(fileName);
|
||||
file.open(QFile::WriteOnly | QIODevice::Text);
|
||||
QTextStream out(&file);
|
||||
out << tempData;
|
||||
file.close();
|
||||
|
||||
on_btnClear_clicked();
|
||||
}
|
||||
|
||||
void frmComTool::on_btnOpen_clicked()
|
||||
{
|
||||
if (ui->btnOpen->text() == "打开串口") {
|
||||
com = new QextSerialPort(ui->cboxPortName->currentText(), QextSerialPort::Polling);
|
||||
comOk = com->open(QIODevice::ReadWrite);
|
||||
|
||||
if (comOk) {
|
||||
//清空缓冲区
|
||||
com->flush();
|
||||
//设置波特率
|
||||
com->setBaudRate((BaudRateType)ui->cboxBaudRate->currentText().toInt());
|
||||
//设置数据位
|
||||
com->setDataBits((DataBitsType)ui->cboxDataBit->currentText().toInt());
|
||||
//设置校验位
|
||||
com->setParity((ParityType)ui->cboxParity->currentIndex());
|
||||
//设置停止位
|
||||
com->setStopBits((StopBitsType)ui->cboxStopBit->currentIndex());
|
||||
com->setFlowControl(FLOW_OFF);
|
||||
com->setTimeout(10);
|
||||
|
||||
changeEnable(true);
|
||||
ui->btnOpen->setText("关闭串口");
|
||||
timerRead->start();
|
||||
}
|
||||
} else {
|
||||
timerRead->stop();
|
||||
com->close();
|
||||
com->deleteLater();
|
||||
|
||||
changeEnable(false);
|
||||
ui->btnOpen->setText("打开串口");
|
||||
on_btnClear_clicked();
|
||||
comOk = false;
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::on_btnSendCount_clicked()
|
||||
{
|
||||
sendCount = 0;
|
||||
ui->btnSendCount->setText("发送 : 0 字节");
|
||||
}
|
||||
|
||||
void frmComTool::on_btnReceiveCount_clicked()
|
||||
{
|
||||
receiveCount = 0;
|
||||
ui->btnReceiveCount->setText("接收 : 0 字节");
|
||||
}
|
||||
|
||||
void frmComTool::on_btnStopShow_clicked()
|
||||
{
|
||||
if (ui->btnStopShow->text() == "停止显示") {
|
||||
isShow = false;
|
||||
ui->btnStopShow->setText("开始显示");
|
||||
} else {
|
||||
isShow = true;
|
||||
ui->btnStopShow->setText("停止显示");
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::on_btnData_clicked()
|
||||
{
|
||||
QString fileName = QString("%1/%2").arg(QUIHelper::appPath()).arg("send.txt");
|
||||
QFile file(fileName);
|
||||
if (!file.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ui->btnData->text() == "管理数据") {
|
||||
ui->txtMain->setReadOnly(false);
|
||||
ui->txtMain->clear();
|
||||
file.open(QFile::ReadOnly | QIODevice::Text);
|
||||
QTextStream in(&file);
|
||||
ui->txtMain->setText(in.readAll());
|
||||
file.close();
|
||||
ui->btnData->setText("保存数据");
|
||||
} else {
|
||||
ui->txtMain->setReadOnly(true);
|
||||
file.open(QFile::WriteOnly | QIODevice::Text);
|
||||
QTextStream out(&file);
|
||||
out << ui->txtMain->toPlainText();
|
||||
file.close();
|
||||
ui->txtMain->clear();
|
||||
ui->btnData->setText("管理数据");
|
||||
AppData::readSendData();
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::on_btnClear_clicked()
|
||||
{
|
||||
append(0, "", true);
|
||||
}
|
||||
|
||||
void frmComTool::on_btnStart_clicked()
|
||||
{
|
||||
if (ui->btnStart->text() == "启动") {
|
||||
if (AppConfig::ServerIP == "" || AppConfig::ServerPort == 0) {
|
||||
append(6, "IP地址和远程端口不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
socket->connectToHost(AppConfig::ServerIP, AppConfig::ServerPort);
|
||||
if (socket->waitForConnected(100)) {
|
||||
ui->btnStart->setText("停止");
|
||||
append(6, "连接服务器成功");
|
||||
tcpOk = true;
|
||||
}
|
||||
} else {
|
||||
socket->disconnectFromHost();
|
||||
if (socket->state() == QAbstractSocket::UnconnectedState || socket->waitForDisconnected(100)) {
|
||||
ui->btnStart->setText("启动");
|
||||
append(6, "断开服务器成功");
|
||||
tcpOk = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::on_ckAutoSend_stateChanged(int arg1)
|
||||
{
|
||||
if (arg1 == 0) {
|
||||
ui->cboxSendInterval->setEnabled(false);
|
||||
timerSend->stop();
|
||||
} else {
|
||||
ui->cboxSendInterval->setEnabled(true);
|
||||
timerSend->start();
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::on_ckAutoSave_stateChanged(int arg1)
|
||||
{
|
||||
if (arg1 == 0) {
|
||||
ui->cboxSaveInterval->setEnabled(false);
|
||||
timerSave->stop();
|
||||
} else {
|
||||
ui->cboxSaveInterval->setEnabled(true);
|
||||
timerSave->start();
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::connectNet()
|
||||
{
|
||||
if (!tcpOk && AppConfig::AutoConnect && ui->btnStart->text() == "启动") {
|
||||
if (AppConfig::ServerIP != "" && AppConfig::ServerPort != 0) {
|
||||
socket->connectToHost(AppConfig::ServerIP, AppConfig::ServerPort);
|
||||
if (socket->waitForConnected(100)) {
|
||||
ui->btnStart->setText("停止");
|
||||
append(6, "连接服务器成功");
|
||||
tcpOk = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::readDataNet()
|
||||
{
|
||||
if (socket->bytesAvailable() > 0) {
|
||||
QUIHelper::sleep(AppConfig::SleepTime);
|
||||
QByteArray data = socket->readAll();
|
||||
|
||||
QString buffer;
|
||||
if (ui->ckHexReceive->isChecked()) {
|
||||
buffer = QUIHelperData::byteArrayToHexStr(data);
|
||||
} else {
|
||||
buffer = QUIHelperData::byteArrayToAsciiStr(data);
|
||||
}
|
||||
|
||||
append(5, buffer);
|
||||
|
||||
//将收到的网络数据转发给串口
|
||||
if (comOk) {
|
||||
com->write(data);
|
||||
append(0, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void frmComTool::readErrorNet()
|
||||
{
|
||||
ui->btnStart->setText("启动");
|
||||
append(6, QString("连接服务器失败,%1").arg(socket->errorString()));
|
||||
socket->disconnectFromHost();
|
||||
tcpOk = false;
|
||||
}
|
||||
68
tool/comtool/form/frmcomtool.h
Normal file
68
tool/comtool/form/frmcomtool.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef FRMCOMTOOL_H
|
||||
#define FRMCOMTOOL_H
|
||||
|
||||
#include <QWidget>
|
||||
#include "qtcpsocket.h"
|
||||
#include "qextserialport.h"
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class frmComTool;
|
||||
}
|
||||
|
||||
class frmComTool : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmComTool(QWidget *parent = 0);
|
||||
~frmComTool();
|
||||
|
||||
private:
|
||||
Ui::frmComTool *ui;
|
||||
|
||||
bool comOk; //串口是否打开
|
||||
QextSerialPort *com; //串口通信对象
|
||||
QTimer *timerRead; //定时读取串口数据
|
||||
QTimer *timerSend; //定时发送串口数据
|
||||
QTimer *timerSave; //定时保存串口数据
|
||||
|
||||
int sleepTime; //接收延时时间
|
||||
int sendCount; //发送数据计数
|
||||
int receiveCount; //接收数据计数
|
||||
bool isShow; //是否显示数据
|
||||
|
||||
bool tcpOk; //网络是否正常
|
||||
QTcpSocket *socket; //网络连接对象
|
||||
QTimer *timerConnect; //定时器重连
|
||||
|
||||
private slots:
|
||||
void initForm(); //初始化窗体数据
|
||||
void initConfig(); //初始化配置文件
|
||||
void saveConfig(); //保存配置文件
|
||||
void readData(); //读取串口数据
|
||||
void sendData(); //发送串口数据
|
||||
void sendData(QString data);//发送串口数据带参数
|
||||
void saveData(); //保存串口数据
|
||||
|
||||
void changeEnable(bool b); //改变状态
|
||||
void append(int type, const QString &data, bool clear = false);
|
||||
|
||||
private slots:
|
||||
void connectNet();
|
||||
void readDataNet();
|
||||
void readErrorNet();
|
||||
|
||||
private slots:
|
||||
void on_btnOpen_clicked();
|
||||
void on_btnStopShow_clicked();
|
||||
void on_btnSendCount_clicked();
|
||||
void on_btnReceiveCount_clicked();
|
||||
void on_btnClear_clicked();
|
||||
void on_btnData_clicked();
|
||||
void on_btnStart_clicked();
|
||||
void on_ckAutoSend_stateChanged(int arg1);
|
||||
void on_ckAutoSave_stateChanged(int arg1);
|
||||
};
|
||||
|
||||
#endif // FRMCOMTOOL_H
|
||||
533
tool/comtool/form/frmcomtool.ui
Normal file
533
tool/comtool/form/frmcomtool.ui
Normal file
@@ -0,0 +1,533 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmComTool</class>
|
||||
<widget class="QWidget" name="frmComTool">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_6">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QTextEdit" name="txtMain">
|
||||
<property name="enabled">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::StyledPanel</enum>
|
||||
</property>
|
||||
<property name="verticalScrollBarPolicy">
|
||||
<enum>Qt::ScrollBarAsNeeded</enum>
|
||||
</property>
|
||||
<property name="horizontalScrollBarPolicy">
|
||||
<enum>Qt::ScrollBarAsNeeded</enum>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" rowspan="2">
|
||||
<widget class="QWidget" name="widgetRight" native="true">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>180</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>180</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QFrame" name="frameTop">
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::Box</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="labPortName">
|
||||
<property name="text">
|
||||
<string>串口号</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="cboxPortName">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
||||
<horstretch>1</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="editable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="labBaudRate">
|
||||
<property name="text">
|
||||
<string>波特率</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QComboBox" name="cboxBaudRate">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
||||
<horstretch>1</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="editable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="labDataBit">
|
||||
<property name="text">
|
||||
<string>数据位</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QComboBox" name="cboxDataBit">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
||||
<horstretch>1</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="labParity">
|
||||
<property name="text">
|
||||
<string>校验位</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QComboBox" name="cboxParity">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
||||
<horstretch>1</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0">
|
||||
<widget class="QLabel" name="labStopBit">
|
||||
<property name="text">
|
||||
<string>停止位</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QComboBox" name="cboxStopBit">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
||||
<horstretch>1</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="btnOpen">
|
||||
<property name="text">
|
||||
<string>打开串口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTabWidget" name="tabWidget">
|
||||
<property name="tabPosition">
|
||||
<enum>QTabWidget::South</enum>
|
||||
</property>
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="tab">
|
||||
<attribute name="title">
|
||||
<string>串口配置</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_5">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QCheckBox" name="ckHexSend">
|
||||
<property name="text">
|
||||
<string>Hex发送</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QCheckBox" name="ckHexReceive">
|
||||
<property name="text">
|
||||
<string>Hex接收</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QCheckBox" name="ckDebug">
|
||||
<property name="text">
|
||||
<string>模拟设备</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QCheckBox" name="ckAutoClear">
|
||||
<property name="text">
|
||||
<string>自动清空</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="2">
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QCheckBox" name="ckAutoSend">
|
||||
<property name="text">
|
||||
<string>自动发送</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="cboxSendInterval">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QCheckBox" name="ckAutoSave">
|
||||
<property name="text">
|
||||
<string>自动保存</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QComboBox" name="cboxSaveInterval">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="btnSendCount">
|
||||
<property name="text">
|
||||
<string>发送 : 0 字节</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="btnReceiveCount">
|
||||
<property name="text">
|
||||
<string>接收 : 0 字节</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="btnStopShow">
|
||||
<property name="text">
|
||||
<string>停止显示</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="6" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="btnSave">
|
||||
<property name="text">
|
||||
<string>保存数据</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="7" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="btnData">
|
||||
<property name="text">
|
||||
<string>管理数据</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="8" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="btnClear">
|
||||
<property name="text">
|
||||
<string>清空数据</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="9" column="0" colspan="2">
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>2</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab_2">
|
||||
<attribute name="title">
|
||||
<string>网络配置</string>
|
||||
</attribute>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<property name="leftMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>6</number>
|
||||
</property>
|
||||
<item row="0" column="0" colspan="2">
|
||||
<layout class="QGridLayout" name="gridLayout_4">
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="labListenPort">
|
||||
<property name="text">
|
||||
<string>监听端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="labServerIP">
|
||||
<property name="text">
|
||||
<string>远程地址</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="1">
|
||||
<widget class="QComboBox" name="cboxSleepTime"/>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QLineEdit" name="txtServerIP">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="labServerPort">
|
||||
<property name="text">
|
||||
<string>远程端口</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QLineEdit" name="txtServerPort">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="4" column="0">
|
||||
<widget class="QLabel" name="labSleepTime">
|
||||
<property name="text">
|
||||
<string>延时时间</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1">
|
||||
<widget class="QLineEdit" name="txtListenPort"/>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="labMode">
|
||||
<property name="text">
|
||||
<string>转换模式</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QComboBox" name="cboxMode">
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Tcp_Client</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Tcp_Server</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Udp_Client</string>
|
||||
</property>
|
||||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Udp_Server</string>
|
||||
</property>
|
||||
</item>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="2">
|
||||
<widget class="QPushButton" name="btnStart">
|
||||
<property name="text">
|
||||
<string>启动</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="2">
|
||||
<spacer name="verticalSpacer_2">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>59</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="2">
|
||||
<widget class="QCheckBox" name="ckAutoConnect">
|
||||
<property name="text">
|
||||
<string>自动重连网络</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<property name="leftMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QComboBox" name="cboxData">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="editable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="duplicatesEnabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnSend">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>80</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>80</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>发送</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user