串口接收,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_H
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);
}
//temp.append(str.mid(head,tail+1));
//str.remove(head,tail+1);
//qDebug()<<str;
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_H
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);
//洗掉注釋后,是由按鈕控制資料發送,此時需要將帶定時器的connet注釋,
// connect(ui->pushButton,SIGNAL(clicked()),localThread,SLOT(writeData()));
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340695.html
標籤:其他
上一篇:Nginx資料結構學習
下一篇:華為網路配置(路由配置)
