新增控制指令发送功能

chen
ChenZhuo
parent 530b18113c
commit ef0c77fd96

@ -27,9 +27,15 @@ void CommandWidget::on_settingToolBtn_clicked() {
// 连接
void CommandWidget::on_startConnectionToolBtn_clicked() {
ui->startConnectionToolBtn->setDisabled(true);
if (!g_networkSettingInfo) return;
if (!g_networkSettingInfo) {
qWarning() << "Network settings are not initialized!";
return;
}
int dataSourceType = g_networkSettingInfo->value("DataSource").toInt();
QString groupName = "";
switch (dataSourceType) {
case 0:
groupName = "LLink";
@ -41,27 +47,31 @@ void CommandWidget::on_startConnectionToolBtn_clicked() {
groupName = "SATCOM2";
break;
default:
break;
qWarning() << "Unknown data source type:" << dataSourceType;
return;
}
m_remoteIP =
g_networkSettingInfo->value(groupName + "/remoteIP").toString();
m_remotePort =
g_networkSettingInfo->value(groupName + "/remotePort").toInt();
int localPort =
g_networkSettingInfo->value(groupName + "/localPort").toInt();
QString localIP =
g_networkSettingInfo->value(groupName + "/localIP").toString();
m_remoteIP = g_networkSettingInfo->value(groupName + "/remoteIP").toString();
m_remotePort = g_networkSettingInfo->value(groupName + "/remotePort").toInt();
int localPort = g_networkSettingInfo->value(groupName + "/localPort").toInt();
QString localIP = g_networkSettingInfo->value(groupName + "/localIP").toString();
// 校验 IP 和端口
if (m_remoteIP.isEmpty() || m_remotePort <= 0 || localIP.isEmpty() || localPort <= 0) {
qWarning() << "Invalid IP or port configuration!";
return;
}
if (dataSourceType == 0) { // 组播
qDebug() << "Starting multicast connection to" << m_remoteIP << ":" << m_remotePort;
emit startConnectionSignal(m_remoteIP, m_remotePort);
} else { // 单播
qDebug() << "Starting unicast connection on" << localIP << ":" << localPort;
emit startConnectionSignal(localIP, localPort);
}
ui->stopConnectionToolBtn->setDisabled(false);
}
// 断开
void CommandWidget::on_stopConnectionToolBtn_clicked() {
ui->stopConnectionToolBtn->setDisabled(true);
@ -75,3 +85,176 @@ void CommandWidget::on_stopConnectionToolBtn_clicked() {
void CommandWidget::receiveMessageSlots(QString message, int type) {
emit sendErrorMessage(message, type);
}
unsigned char CommandWidget::getCrc(quint8* data, quint8 length)
{
quint16 crc = 0;
int i=0;
while (length-- > 0)
{
crc = CRC8TAB[crc ^ (data[i]&0x00FF)];
i++;
}
return crc&0x00ff;
}
ProtocalKB CommandWidget::EncodeCommandCmd(quint8 cmd)
{
std::unique_ptr<ProtocalKB> pBuff = std::make_unique<ProtocalKB>();
pBuff->head[0] = PROTOCOL_HEAD_0;
pBuff->head[1] = PROTOCOL_HEAD_1;
pBuff->reserved = PROTOCOL_RESERVED;
pBuff->data[0] = cmd;
pBuff->data[1] = cmd;
pBuff->data[2] = cmd;
pBuff->curPage = PROTOCOL_CUR_PAGE;
quint8 *ptr = reinterpret_cast<quint8*>(&pBuff);
pBuff->CHKSUM = getCrc(ptr, sizeof(pBuff) - sizeof(pBuff->CHKSUM));
return *pBuff;
}
bool CommandWidget::writeBufferToClient(const QByteArray &data)
{
// 目标地址和端口
QHostAddress destination("172.10.1.216"); // 目标 IP 地址
quint16 port = 9003; // 目标端口
// 发送数据
qint64 bytesSent = g_CommandudpSocket->writeDatagram(data, destination, port);
if (bytesSent == -1) {
qDebug() << "发送失败:" << g_CommandudpSocket->errorString();
return false;
} else {
qDebug() << "发送成功,字节数:" << bytesSent;
return true;
}
}
void CommandWidget::buttonResponse(quint8 cmd)
{
ProtocalKB packet = EncodeCommandCmd(cmd);
// 将 ProtocalKB 结构体转换为 QByteArray
QByteArray data;
data.append(reinterpret_cast<const char*>(&packet), sizeof(ProtocalKB));
for(int i=0;i<3;i++)
{
writeBufferToClient(data);
}
}
void CommandWidget::on_pushButton_clicked()
{
buttonResponse(0x75);
}
void CommandWidget::on_pushButton_2_clicked()
{
buttonResponse(0x76);
}
void CommandWidget::on_pushButton_5_clicked()
{
buttonResponse(0x7D);
}
void CommandWidget::on_pushButton_6_clicked()
{
buttonResponse(0x7C);
}
void CommandWidget::on_pushButton_10_clicked()
{
buttonResponse(0x69);
}
void CommandWidget::on_pushButton_9_clicked()
{
buttonResponse(0x6A);
}
void CommandWidget::on_pushButton_4_clicked()
{
buttonResponse(0x77);
}
void CommandWidget::on_pushButton_3_clicked()
{
buttonResponse(0x78);
}
void CommandWidget::on_pushButton_14_clicked()
{
buttonResponse(0x79);
}
void CommandWidget::on_pushButton_13_clicked()
{
buttonResponse(0xC6);
}
void CommandWidget::on_pushButton_17_clicked()
{
buttonResponse(0x66);
}
void CommandWidget::on_pushButton_8_clicked()
{
buttonResponse(0x67);
}
void CommandWidget::on_pushButton_7_clicked()
{
buttonResponse(0x68);
}
void CommandWidget::on_pushButton_12_clicked()
{
buttonResponse(0x7B);
}
void CommandWidget::on_pushButton_11_clicked()
{
buttonResponse(0x7A);
}
void CommandWidget::on_pushButton_16_clicked()
{
buttonResponse(0xC8);
}
void CommandWidget::on_pushButton_15_clicked()
{
buttonResponse(0xC9);
}

@ -6,6 +6,74 @@
#include "communicationsettingdlg.h"
typedef struct struProtocalKB {
quint8 head[2]; //帧头0x
quint8 reserved; //预留
quint8 data[3]; //开关指令
quint8 curPage; //指令页
quint8 CHKSUM; //校验字节
}ProtocalKB;
// 定义常量
const unsigned char PROTOCOL_HEAD_0 = 0xEB;
const unsigned char PROTOCOL_HEAD_1 = 0x98;
const unsigned char PROTOCOL_RESERVED = 0x00;
const unsigned char PROTOCOL_CUR_PAGE = 0x01;
const quint16 CRC8TAB[256] =
{
//0
0x0000, 0x0031, 0x0062, 0x0053, 0x00C4, 0x00F5, 0x00A6, 0x0097,
0x00B9, 0x0088, 0x00DB, 0x00EA, 0x007D, 0x004C, 0x001F, 0x002E,
//1
0x0043, 0x0072, 0x0021, 0x0010, 0x0087, 0x00B6, 0x00E5, 0x00D4,
0x00FA, 0x00CB, 0x0098, 0x00A9, 0x003E, 0x000F, 0x005C, 0x006D,
//2
0x0086, 0x00B7, 0x00E4, 0x00D5, 0x0042, 0x0073, 0x0020, 0x0011,
0x003F, 0x000E, 0x005D, 0x006C, 0x00FB, 0x00CA, 0x0099, 0x00A8,
//3
0x00C5, 0x00F4, 0x00A7, 0x0096, 0x0001, 0x0030, 0x0063, 0x0052,
0x007C, 0x004D, 0x001E, 0x002F, 0x00B8, 0x0089, 0x00DA, 0x00EB,
//4
0x003D, 0x000C, 0x005F, 0x006E, 0x00F9, 0x00C8, 0x009B, 0x00AA,
0x0084, 0x00B5, 0x00E6, 0x00D7, 0x0040, 0x0071, 0x0022, 0x0013,
//5
0x007E, 0x004F, 0x001C, 0x002D, 0x00BA, 0x008B, 0x00D8, 0x00E9,
0x00C7, 0x00F6, 0x00A5, 0x0094, 0x0003, 0x0032, 0x0061, 0x0050,
//6
0x00BB, 0x008A, 0x00D9, 0x00E8, 0x007F, 0x004E, 0x001D, 0x002C,
0x0002, 0x0033, 0x0060, 0x0051, 0x00C6, 0x00F7, 0x00A4, 0x0095,
//7
0x00F8, 0x00C9, 0x009A, 0x00AB, 0x003C, 0x000D, 0x005E, 0x006F,
0x0041, 0x0070, 0x0023, 0x0012, 0x0085, 0x00B4, 0x00E7, 0x00D6,
//8
0x007A, 0x004B, 0x0018, 0x0029, 0x00BE, 0x008F, 0x00DC, 0x00ED,
0x00C3, 0x00F2, 0x00A1, 0x0090, 0x0007, 0x0036, 0x0065, 0x0054,
//9
0x0039, 0x0008, 0x005B, 0x006A, 0x00FD, 0x00CC, 0x009F, 0x00AE,
0x0080, 0x00B1, 0x00E2, 0x00D3, 0x0044, 0x0075, 0x0026, 0x0017,
//A
0x00FC, 0x00CD, 0x009E, 0x00AF, 0x0038, 0x0009, 0x005A, 0x006B,
0x0045, 0x0074, 0x0027, 0x0016, 0x0081, 0x00B0, 0x00E3, 0x00D2,
//B
0x00BF, 0x008E, 0x00DD, 0x00EC, 0x007B, 0x004A, 0x0019, 0x0028,
0x0006, 0x0037, 0x0064, 0x0055, 0x00C2, 0x00F3, 0x00A0, 0x0091,
//C
0x0047, 0x0076, 0x0025, 0x0014, 0x0083, 0x00B2, 0x00E1, 0x00D0,
0x00FE, 0x00CF, 0x009C, 0x00AD, 0x003A, 0x000B, 0x0058, 0x0069,
//D
0x0004, 0x0035, 0x0066, 0x0057, 0x00C0, 0x00F1, 0x00A2, 0x0093,
0x00BD, 0x008C, 0x00DF, 0x00FE, 0x0079, 0x0048, 0x001B, 0x002A,
//E
0x00C1, 0x00F0, 0x00A3, 0x0092, 0x0005, 0x0034, 0x0067, 0x0056,
0x0078, 0x0049, 0x001A, 0x002B, 0x00BC, 0x008D, 0x00DE, 0x00EF,
//F
0x0082, 0x00B3, 0x00E0, 0x00D1, 0x0046, 0x0077, 0x0024, 0x0015,
0x003B, 0x000A, 0x0059, 0x0068, 0x00FF, 0x00CE, 0x009D, 0x00AC
};
namespace Ui {
class CommandWidget;
}
@ -31,12 +99,55 @@ private slots:
void receiveMessageSlots(QString message, int type);
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_10_clicked();
void on_pushButton_9_clicked();
void on_pushButton_4_clicked();
void on_pushButton_3_clicked();
void on_pushButton_14_clicked();
void on_pushButton_13_clicked();
void on_pushButton_17_clicked();
void on_pushButton_8_clicked();
void on_pushButton_7_clicked();
void on_pushButton_12_clicked();
void on_pushButton_11_clicked();
void on_pushButton_16_clicked();
void on_pushButton_15_clicked();
private:
Ui::CommandWidget *ui;
CommunicationSettingDlg settingDlg;
QString m_remoteIP;
int m_remotePort;
unsigned char getCrc(quint8* data, quint8 length);
ProtocalKB EncodeCommandCmd(quint8 cmd);
bool writeBufferToClient(const QByteArray &data);
void buttonResponse(quint8 cmd);
};
#endif // COMMANDWIDGET_H

@ -1,6 +1,10 @@
#include "global.h"
QSettings* g_networkSettingInfo;
QUdpSocket* g_CommandudpSocket;
void getLocalIP(QStringList& localIPList) {
QHostInfo hostInfo = QHostInfo::fromName(QHostInfo::localHostName());

@ -8,8 +8,9 @@
#include <QSerialPortInfo>
#include <QSettings>
#include <QStringList>
#include <QUdpSocket>
extern QSettings* g_networkSettingInfo;
extern QUdpSocket* g_CommandudpSocket;
extern void getLocalIP(QStringList& localIPList);

@ -89,6 +89,7 @@ MainWindow::MainWindow(QWidget *parent)
initSignalConnection();
initNotifyManager();
initNotifyMessageConnection();
initCommunitions();
}
MainWindow::~MainWindow() {
@ -110,6 +111,7 @@ void MainWindow::initNotifyMessageConnection() {
&MainWindow::showMessageSlots);
}
void MainWindow::initNotifyManager() {
m_notifyManager = new NotifyManager(this, this);
m_notifyManager->setMaxCount(5);
@ -117,6 +119,21 @@ void MainWindow::initNotifyManager() {
m_notifyManager->setNotifyWndSize(400, 60);
}
void MainWindow::initCommunitions()
{
g_CommandudpSocket = new QUdpSocket(this);
// 将套接字绑定到指定端口
if (g_CommandudpSocket->bind(QHostAddress::Any, 1200)) {
qDebug() << "UDP 套接字已绑定到端口 1200";
} else {
qDebug() << "绑定 UDP 套接字失败";
}
}
void MainWindow::showMessageSlots(QString message, int type) {
if (m_notifyManager) {
m_notifyManager->notify(message, "", type, 3000);

@ -32,6 +32,7 @@ private:
void initNotifyManager();
void showMessageSlots(QString message, int type);
void initCommunitions();
// void installWindowAgent();

Loading…
Cancel
Save