首页 > 编程知识 正文

modbus udp,qt串口在多个窗口使用

时间:2023-05-04 23:51:37 阅读:39576 作者:3294

串行端口接收、UDP传输在理解上可能会觉得多余,但在工作中遇到了这样的问题。 由于两个模块在工作量问题上没有分配给同一个人,因此对于两个模块的数据通信,在考虑后选择传输策略,所有数据传输都在后台执行,因此多接口操作不会受到影响

在上一个博客中写了使用多线程实现UDP数据发送,但链接是直达的

3359 blog.csdn.net/weixin _ 43552197/article/details/120882987? spm=1001.2014.3001.5501

正文中的一些代码复用了上次的代码。 感兴趣的人可以在上一篇博客发出的代码中自己推广。 博客上给出的实例比较简单,只作为学习案例,同时也是为了以后忘记的时候容易看到。 不用说,我直接坐代码

项目的目录结构如下

由于代码使用串行端口和网络,因此必须首先将以下代码添加到项目中

Qt=coreguinetworkserialportmythread.h代码如下

# ifndef my thread _ h # definem ythread _ h # includeqobject # includeqserialport # includeqserialportinfo # includeqtimer # iobject includeqstringlistclassmythread : publicqobject { q _ object public 3360 explicit my thread } void init port (; QString str; signals :语音发送msg (qstringlisttmp; 公共插槽: voidreadserialdata (; 语音写入数据(; void delay (; 隐私: q timer * mytimer; QThread *thread; QQ *端口; (; #endif //MYTHREAD_H mythread.cpp代码如下

# include ' my thread.h ' my thread 33603360 my thread (q object * parent ) :qobject(parent ) { thread=new QThread; myTimer=new QTimer (; port=new QSerialPort (; initPort (; this-movetothread(thread; 端口移动到萨德(thread; mytimer-movetothread(thread; thread-start (; }void myThread:initPort () port-setportname ) (TTYUSB0); 端口集基础(115200; 端口集数据(qserialport :3360数据8; 端口设置停止位(qserialport :3360 one stop; 端口集分区(qserialport :3360 no parity; 端口集流量控制(qserialport :3360否流量控制; if (端口打开(qiodevice :3360读写) { qDebug ) )“串行端口打开”(else ) qdebug )“串行端口打开失败” }连接(端口,信号)就绪读取)、this,插槽(延迟) ); 连接(mytimer,signal ) timeout )、this,slot (readserialdata ) ); } void my thread :3360 readserialdata () { myTimer-stop ); Qstring列表模板; q字节缓冲器; buf=端口读取全部(; if (! buf.isEmpty () ) { buf.simplified ); str=tr(buf; }inthead=str.indexof('# '; inttail=str.indexof('* ',head ); qDebug () head: ) head ) tail ) tail; wile (头!=-1 tail!=-1 ) temp.append(str.mid ) head,tail 1); str.remove(head,tail 1); head=str.indexof('# '; tail=str.indexof('* ',head ); //temp.append(str.mid ) head,tail 1); //str.remove(head,tail 1); //q调试() str; EMIT(sendmsg ) temp ); buf.clear (; } void my thread :3360 write data () port-write ) (#123456*#abcdef ) ); ) }void myThread:delay () mytimer-start ) 100; }构件. h代码如下

# ifndef构件_ h # define构件_ h # includeqwidget # include ' my thread.h ' # includequdpsocket # includeqhostaddresnsnnget ~小部件(); 隐私插槽:语音供应商(qstringlisttemp; 私有: ui :3360构件* ui; qudpsocket*插座; 我的thread *本地thread; q时间*时间; (; # endif//构件_ h构件. CPP代码如下:

# include '构件. h ' # include ' ui _构件. h ' # include ' my thread.h '构件33603360构件(q构件* parent ) ) socket=new QUdpSocket (; 本地thread=new my thread (; timer=new QTimer (; 套接字绑定(3080; 删除//注释后,按钮将控制数据发送,但在这种情况下,请使用带计时器的connet注释。 //connect(ui-pushbutton,signal(clicked ) )、localThread,slot (写入数据) ) ) ); 连接(timer,signal ) timeout )、localThread,slot (write data ) ); 连接(local thread,signal(sendmsg(qstringlist ),this,slot ) send to UDP (qstring list ) ) timer-start(500 ); }构件: ~构件() { delete ui; }语音构件:3360 send to UDP (qstringlisttemp ) { QString item; Foreach(item,temp ) { qDebug ) item; 用户接口(item; 套接字写入数据(item.to latin1 )、qhostaddress('127.0.0.1 ' )、3099 ); }} main.cpp代码如下

# include ' widget.h ' # includeqapplicationintmain (int argv,char *argv[] () qapplicationa ) argc,argv ); 小部件w; w.show (; return a.exec (; } UI的界面设计如下,很简单

如果复制代码有问题,可以通过以下连接下载此博客代码

3359 download.csdn.net/download/weixin _ 43552197/3488504

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。