串口接收,UDP转发
虽然在理解上可能会觉得多此一举,但是在工作中确实遇到了这样的问题。两个模块因为工作量问题并没有分配给同一人,因此,当面对两个模块的数据通信时,经过商讨选用了转发的策略,由于所有的数据转发均在后台执行,为了不影响UI界面的操作,故采用多线程进行。 前面的博客中我已经写过使用多线程实现UDP数据发送,链接直达 https://blog.csdn.net/weixin_43552197/article/details/120882987?spm=1001.2014.3001.5501 本文有一部分代码复用了上次的代码,有兴趣的可以在上篇博客给出的代码中自己拓展,博客给出的实例相对较为简单,仅做学习范例使用,同时也是为了后面忘记时方便查看。话不多说,直接上代码 项目的目录结构如下
由于代码中用到串口和网络,因此首先需要在项目中加入以下代码
QT += core gui network serialport
mythread.h代码如下
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QTimer>
#include <QTime>
#include <QDebug>
#include <QString>
#include <QThread>
#include <QStringList>
class myThread : public QObject
{
Q_OBJECT
public:
explicit myThread(QObject *parent = nullptr);
void initPort();
QString str;
signals:
void sendMsg(QStringList tmp);
public slots:
void readSerialData();
void writeData();
void delay();
private:
QTimer *myTimer;
QThread *thread;
QSerialPort *port;
};
#endif
mythread.cpp代码如下
#include "mythread.h"
myThread::myThread(QObject *parent) : QObject(parent)
{
thread = new QThread();
myTimer = new QTimer();
port = new QSerialPort();
initPort();
this->moveToThread(thread);
port->moveToThread(thread);
myTimer->moveToThread(thread);
thread->start();
}
void myThread::initPort()
{
port->setPortName("ttyUSB0");
port->setBaudRate(115200);
port->setDataBits(QSerialPort::Data8);
port->setStopBits(QSerialPort::OneStop);
port->setParity(QSerialPort::NoParity);
port->setFlowControl(QSerialPort::NoFlowControl);
if(port->open(QIODevice::ReadWrite)){
qDebug()<<"串口已打开";
}
else{
qDebug()<<"串口打开失败";
}
connect(port,SIGNAL(readyRead()),this,SLOT(delay()));
connect(myTimer,SIGNAL(timeout()),this,SLOT(readSerialData()));
}
void myThread::readSerialData()
{
myTimer->stop();
QStringList temp;
QByteArray buf;
buf = port->readAll();
if(!buf.isEmpty())
{
buf.simplified();
str += tr(buf);
}
int head = str.indexOf("#");
int tail = str.indexOf("*",head);
qDebug()<<"head:"<<head<<"tail"<<tail;
while(head != -1 && tail != -1)
{
temp.append(str.mid(head,tail+1));
str.remove(head,tail+1);
head = str.indexOf("#");
tail = str.indexOf("*",head);
}
emit(sendMsg(temp));
buf.clear();
}
void myThread::writeData()
{
port->write("#123456*#abcdef*");
}
void myThread::delay()
{
myTimer->start(100);
}
widget.h代码如下
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "mythread.h"
#include <QUdpSocket>
#include <QHostAddress>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void sendToUDP(QStringList temp);
private:
Ui::Widget *ui;
QUdpSocket *socket;
myThread *localThread;
QTimer *timer;
};
#endif
widget.cpp代码如下:
#include "widget.h"
#include "ui_widget.h"
#include "mythread.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
socket = new QUdpSocket();
localThread = new myThread();
timer = new QTimer();
socket->bind(3080);
connect(timer,SIGNAL(timeout()),localThread,SLOT(writeData()));
connect(localThread,SIGNAL(sendMsg(QStringList)),this,SLOT(sendToUDP(QStringList)));
timer->start(500);
}
Widget::~Widget()
{
delete ui;
}
void Widget::sendToUDP(QStringList temp)
{
QString item;
foreach (item, temp)
{
qDebug()<<item;
ui->textEdit->append(item);
socket->writeDatagram(item.toLatin1(),QHostAddress("127.0.0.1"),3099);
}
}
main.cpp代码如下
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
UI的界面设计如下,很简单 如果复制代码出现问题的话,可以在下面的连接中下载本博客代码 https://download.csdn.net/download/weixin_43552197/34888504
|