1 Star 3 Fork 0

pengpeng5800/Qt 串口 网络 多线程

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
home.cpp 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
pengpeng5800 提交于 2021-09-22 05:51 . v1.0.0
#include "home.h"
#include "ui_home.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
start();
}
Widget::~Widget()
{
stop();
delete ui;
}
void Widget::start()
{
if(2==LYP_DEBUG)
{
m_scom = new SerialCom();
m_scom->ScanPort();
m_scom->Connect();
m_icom = m_scom;
}
else
{
m_ucom = new UdpCom();
m_ucom->Connect();
m_icom = m_ucom;
}
if(m_icom->IsConnected())
{
connect(m_icom,SIGNAL(recvMsg()),this,SLOT(msgSwitch()));//msgSwitch()为自定义槽
//QMessageBox::information(NULL, "成功", "连接成功!\n"+m_icom->GetStatus());
m_is->SetInterface(m_icom, ui->tabWidget_home->currentIndex());
m_is->ipfetch();
}
else
{
QMessageBox::critical(NULL, "失败", "连接失败,端口被占用!\n"+m_icom->GetStatus());
QTimer::singleShot(200, this, SLOT(jumpQuit()));
}
}
int Widget::stop() const
{
if(2!=LYP_DEBUG)
{
if (m_ucom != nullptr)
{
m_ucom->Disconnect();
delete m_ucom;
}
}
else
{
if (m_scom != nullptr)
{
m_scom->Disconnect();
delete m_scom;
}
}
return 0;
}
void Widget::msgSwitch()
{
ComMessage msg;
QByteArray datagram;
if (!(m_icom->GetPacket(msg, 15)))
{
QStringList target = msg.m_target.split(':');
if (target.size() == 3)
{
// 获取到包
datagram = msg.getData();
qDebug() << msg.getData().toHex() << "Recv data lenth:" << msg.size();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/pengpeng5800/Qt-SerialPort-Socket.git
git@gitee.com:pengpeng5800/Qt-SerialPort-Socket.git
pengpeng5800
Qt-SerialPort-Socket
Qt 串口 网络 多线程
master

搜索帮助