QTcpSocket
一、
二、
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTcpServer> #include <QTcpSocket> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void slotNewConnection(); void slotReadyRead(); void on_btn_listen_clicked(); void on_btn_close_clicked(); void on_btn_send_clicked(); void on_btn_open_lock_clicked(); void on_btn_clean_clicked(); private: Ui::MainWindow *ui; private: uint8_t mac_addr[6]; QTcpServer *tcp_server_; QTcpSocket *tcp_socket_; void init(); void send(const QByteArray &ba); void showMsg(bool isSend,const QByteArray &ba); void showMsg(const QString &msg); }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QDateTime> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); ui->le_port->setText("8080"); init(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::slotNewConnection() { showMsg("设备连接进来"); tcp_socket_ = tcp_server_->nextPendingConnection(); //获取已经连接的客户端套接字 connect(tcp_socket_,SIGNAL(readyRead()),this,SLOT(slotReadyRead())); } void MainWindow::slotReadyRead() { QByteArray data=tcp_socket_->readAll(); //qDebug()<<data.toHex(); showMsg(false,data); QString rec=data.toHex(); QString mark_str=""; QString data1=rec.mid(2,2); if(data1.toInt(nullptr,16)==0xB4) { QString mac_addr=rec.mid(4,12); QString mac_temp=""; for(int i=0;i<6;i++) { mac_temp+=mac_addr.mid(i*2,2)+" "; } mark_str="B4设备状态上报 "; mark_str+=" mac addr:"+mac_temp; mark_str+=" 低电报警:"+rec.mid(16,2); mark_str+=" 开关状态(00关锁 01开锁):"+rec.mid(18,2); mark_str+=" 锁长时间未锁报警(00正常 01报警):"+rec.mid(20,2); } else if(data1.toInt(nullptr,16)==0xA3) { mark_str="A3设备刷卡数据上报"; } showMsg(mark_str); } void MainWindow::init() { tcp_server_ = new QTcpServer(this); tcp_socket_ = new QTcpSocket(this); connect(tcp_server_,SIGNAL(newConnection()),this,SLOT(slotNewConnection())); } void MainWindow::send(const QByteArray &ba) { int ret=tcp_socket_->write(ba); if(ret==-1) { showMsg("发送失败!IODevice::write (QTcpSocket): device not open"); } else { showMsg(true,ba); } } void MainWindow::showMsg(bool isSend, const QByteArray &ba) { QString rec=ba.toHex(); int count = rec.size()/2; QString temp=""; for(int i=0;i<count;i++) { temp += rec.mid(i*2,2)+" "; } QString now_time="["+QDateTime::currentDateTime ().toString ("hh:mm:ss zzz")+"]"; if(isSend) { ui->te_info->append(now_time+"[发] "+temp.toUpper()); } else { ui->te_info->append(now_time+"[收] "+temp.toUpper()); } } void MainWindow::showMsg(const QString &msg) { QString now_time="["+QDateTime::currentDateTime ().toString ("hh:mm:ss zzz")+"] "; ui->te_info->append(now_time+msg); } void MainWindow::on_btn_listen_clicked() { ui->btn_listen->setEnabled(false); ui->le_port->setEnabled(false); if(tcp_server_->listen(QHostAddress::Any,ui->le_port->text().toUInt())) //监听端口号 { showMsg("开启服务器成功!"); } else { showMsg("开启服务器失败!"); ui->btn_listen->setEnabled(true); ui->le_port->setEnabled(true); } } void MainWindow::on_btn_close_clicked() { tcp_server_->close(); tcp_socket_->close(); ui->btn_listen->setEnabled(true); ui->le_port->setEnabled(true); } void MainWindow::on_btn_send_clicked() { //qint64 write(const QByteArray &data); QByteArray ba; ba.resize(4); ba[0]=0x3c; ba[1]=0x3d; ba[2]=0x3e; ba[3]=0xff; send(ba); } void MainWindow::on_btn_open_lock_clicked() { QByteArray ba; ba.resize(13); ba[0]=0x0c; ba[1]=0xb3; ba[2]=0x01; ba[3]=0x02; ba[4]=0x03; ba[5]=0x04; ba[6]=0x05; ba[7]=0x06; ba[8]=0x07; ba[9]=0x11; ba[10]=0x22; ba[11]=0xa5; ba[12]=0x3e; send(ba); } void MainWindow::on_btn_clean_clicked() { ui->te_info->clear(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2021-09-26 linux 安装gtk