#include "commandwidget.h" #include "ui_commandwidget.h" CommandWidget::CommandWidget(QWidget *parent) : QWidget(parent), ui(new Ui::CommandWidget) { ui->setupUi(this); udpSocket = new QUdpSocket(this); ui->videoLayout1TBtn->setIcon(QIcon(":/images/videolayout1.png")); ui->videoLayout4TBtn->setIcon(QIcon(":/images/videolayout4.png")); } CommandWidget::~CommandWidget() { delete ui; if (udpSocket) udpSocket->deleteLater(); } // 连接 void CommandWidget::on_startConnectionToolBtn_clicked() { ui->startConnectionToolBtn->setDisabled(true); 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"; break; case 1: groupName = "SATCOM1"; break; case 2: groupName = "SATCOM2"; break; default: 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(); // 校验 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); } } 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 pBuff = std::make_unique(); 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(&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(&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); } void CommandWidget::on_videoLayout1TBtn_clicked() { ui->videoLayout1TBtn->setDisabled(true); emit changeVideoLayout(0); ui->videoLayout1TBtn->setDisabled(false); } void CommandWidget::on_videoLayout4TBtn_clicked() { ui->videoLayout4TBtn->setDisabled(true); emit changeVideoLayout(1); ui->videoLayout4TBtn->setDisabled(false); }