You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
VideoClient98/src/commandwidget.cpp

148 lines
3.6 KiB
C++

#include "commandwidget.h"
#include "ui_commandwidget.h"
CommandWidget::CommandWidget(QWidget *parent)
: QWidget(parent), ui(new Ui::CommandWidget) {
ui->setupUi(this);
m_commandUdpSocket = new QUdpSocket(this);
// 将套接字绑定到指定端口
if (m_commandUdpSocket->bind(QHostAddress::Any, 1200)) {
qDebug() << "UDP 套接字已绑定到端口 1200";
} else {
qDebug() << "绑定 UDP 套接字失败";
}
}
CommandWidget::~CommandWidget() {
delete ui;
if (m_commandUdpSocket) m_commandUdpSocket->deleteLater();
}
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 =
m_commandUdpSocket->writeDatagram(data, destination, port);
if (bytesSent == -1) {
qDebug() << "发送失败:" << m_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);
}