航线读取状态机完成,无法采用定时器的方式,只能读取当前时间来做时间差判断超时
This commit is contained in:
+146
-146
@@ -1,146 +1,146 @@
|
||||
#include "dlink.h"
|
||||
#include <string.h>
|
||||
//#include "windows.h"
|
||||
#include "QDateTime"
|
||||
|
||||
|
||||
|
||||
DLink::DLink(QObject *parent) : QObject(parent)
|
||||
{
|
||||
qDebug() << "Dlink " << QThread::currentThreadId();
|
||||
|
||||
mavlinknode = new MavLinkNode();
|
||||
}
|
||||
DLink::~DLink()
|
||||
{
|
||||
mavlinknode->stop();
|
||||
delete mavlinknode;
|
||||
}
|
||||
|
||||
|
||||
int DLink::SendMessageTo(uint8_t, uint8_t *msg, uint16_t len)
|
||||
{
|
||||
if (DLink::Clientsock)
|
||||
{
|
||||
foreach(Node node,clientSockets)
|
||||
{
|
||||
DLink::Clientsock->writeDatagram((const char *)msg, len,
|
||||
node.addr, node.port);
|
||||
}
|
||||
}
|
||||
|
||||
if (DLink::serialPort)
|
||||
{
|
||||
DLink::serialPort->write((const char *)msg,len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DLink::setupPort(const QString port, qint32 baudrate, QSerialPort::Parity parity)
|
||||
{
|
||||
if (serialPort)
|
||||
{
|
||||
serialPort->close();
|
||||
delete serialPort;
|
||||
}
|
||||
serialPort = new QSerialPort(port,this);
|
||||
|
||||
if (serialPort->open(QIODevice::ReadWrite))
|
||||
{
|
||||
serialPort->setBaudRate(baudrate);
|
||||
serialPort->setParity(parity);
|
||||
serialPort->setDataBits(QSerialPort::Data8);
|
||||
serialPort->setStopBits(QSerialPort::OneStop);
|
||||
|
||||
mavlinknode->start();
|
||||
|
||||
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readPendingDatagramsSerialPort()));
|
||||
qDebug() << "Serial Port Open Success";
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "Serial Port Open Fail";
|
||||
delete serialPort;
|
||||
serialPort = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool DLink::statesPort()
|
||||
{
|
||||
return (serialPort != nullptr)?(true):(false);
|
||||
}
|
||||
|
||||
void DLink::stopPort()
|
||||
{
|
||||
serialPort->close();
|
||||
delete serialPort;
|
||||
serialPort = nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DLink::setupClient(const QHostAddress &remote_addr, int remote_port, int local_port)
|
||||
{
|
||||
Clientsock = new QUdpSocket(this);
|
||||
if (Clientsock->bind(QHostAddress::Any, local_port))
|
||||
{
|
||||
if(Clientsock->open(QIODevice::ReadWrite))
|
||||
{
|
||||
connect(Clientsock, SIGNAL(readyRead()),
|
||||
this, SLOT(readPendingDatagramsClient()));
|
||||
Node node;
|
||||
node.sock = Clientsock;
|
||||
node.addr = remote_addr;
|
||||
node.port = remote_port;
|
||||
|
||||
clientSockets.append(node);
|
||||
qDebug() << "UdpSocket open:"
|
||||
<< remote_addr
|
||||
<< local_port
|
||||
<< remote_port;
|
||||
|
||||
mavlinknode->start();
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "sock not open";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qWarning("sock is not binded.");
|
||||
delete Clientsock;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DLink::readPendingDatagramsSerialPort(void)
|
||||
{
|
||||
QByteArray datagram = serialPort->readAll();
|
||||
mavlinknode->setbuff(SourceType::s_port,datagram);
|
||||
}
|
||||
|
||||
void DLink::readPendingDatagramsClient(void)
|
||||
{
|
||||
//轮番查询
|
||||
while (Clientsock->hasPendingDatagrams()) {
|
||||
QByteArray datagram;
|
||||
datagram.resize(Clientsock->pendingDatagramSize());
|
||||
Clientsock->readDatagram(datagram.data(), datagram.size());
|
||||
mavlinknode->setbuff(SourceType::c_sock,datagram);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#include "dlink.h"
|
||||
#include <string.h>
|
||||
//#include "windows.h"
|
||||
#include "QDateTime"
|
||||
|
||||
|
||||
|
||||
DLink::DLink(QObject *parent) : QObject(parent)
|
||||
{
|
||||
qDebug() << "Dlink " << QThread::currentThreadId();
|
||||
|
||||
mavlinknode = new MavLinkNode(this);
|
||||
}
|
||||
DLink::~DLink()
|
||||
{
|
||||
mavlinknode->stop();
|
||||
delete mavlinknode;
|
||||
}
|
||||
|
||||
|
||||
int DLink::SendMessageTo(uint8_t, uint8_t *msg, uint16_t len)
|
||||
{
|
||||
if (DLink::Clientsock)
|
||||
{
|
||||
foreach(Node node,clientSockets)
|
||||
{
|
||||
DLink::Clientsock->writeDatagram((const char *)msg, len,
|
||||
node.addr, node.port);
|
||||
}
|
||||
}
|
||||
|
||||
if (DLink::serialPort)
|
||||
{
|
||||
DLink::serialPort->write((const char *)msg,len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DLink::setupPort(const QString port, qint32 baudrate, QSerialPort::Parity parity)
|
||||
{
|
||||
if (serialPort)
|
||||
{
|
||||
serialPort->close();
|
||||
delete serialPort;
|
||||
}
|
||||
serialPort = new QSerialPort(port,this);
|
||||
|
||||
if (serialPort->open(QIODevice::ReadWrite))
|
||||
{
|
||||
serialPort->setBaudRate(baudrate);
|
||||
serialPort->setParity(parity);
|
||||
serialPort->setDataBits(QSerialPort::Data8);
|
||||
serialPort->setStopBits(QSerialPort::OneStop);
|
||||
|
||||
mavlinknode->start();
|
||||
|
||||
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readPendingDatagramsSerialPort()));
|
||||
qDebug() << "Serial Port Open Success";
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "Serial Port Open Fail";
|
||||
delete serialPort;
|
||||
serialPort = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool DLink::statesPort()
|
||||
{
|
||||
return (serialPort != nullptr)?(true):(false);
|
||||
}
|
||||
|
||||
void DLink::stopPort()
|
||||
{
|
||||
serialPort->close();
|
||||
delete serialPort;
|
||||
serialPort = nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DLink::setupClient(const QHostAddress &remote_addr, int remote_port, int local_port)
|
||||
{
|
||||
Clientsock = new QUdpSocket(this);
|
||||
if (Clientsock->bind(QHostAddress::Any, local_port))
|
||||
{
|
||||
if(Clientsock->open(QIODevice::ReadWrite))
|
||||
{
|
||||
connect(Clientsock, SIGNAL(readyRead()),
|
||||
this, SLOT(readPendingDatagramsClient()));
|
||||
Node node;
|
||||
node.sock = Clientsock;
|
||||
node.addr = remote_addr;
|
||||
node.port = remote_port;
|
||||
|
||||
clientSockets.append(node);
|
||||
qDebug() << "UdpSocket open:"
|
||||
<< remote_addr
|
||||
<< local_port
|
||||
<< remote_port;
|
||||
|
||||
mavlinknode->start();
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "sock not open";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qWarning("sock is not binded.");
|
||||
delete Clientsock;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DLink::readPendingDatagramsSerialPort(void)
|
||||
{
|
||||
QByteArray datagram = serialPort->readAll();
|
||||
mavlinknode->setbuff(SourceType::s_port,datagram);
|
||||
}
|
||||
|
||||
void DLink::readPendingDatagramsClient(void)
|
||||
{
|
||||
//轮番查询
|
||||
while (Clientsock->hasPendingDatagrams()) {
|
||||
QByteArray datagram;
|
||||
datagram.resize(Clientsock->pendingDatagramSize());
|
||||
Clientsock->readDatagram(datagram.data(), datagram.size());
|
||||
mavlinknode->setbuff(SourceType::c_sock,datagram);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//在这里做一个检查缓冲区有没有数,有数据就往上发的一个函数
|
||||
//dlink底下的所有协议都可以往这个缓存发数据
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+73
-78
@@ -1,78 +1,73 @@
|
||||
#ifndef DLINK_H
|
||||
#define DLINK_H
|
||||
|
||||
#include <QObject>
|
||||
#include "QtSerialPort"
|
||||
#include <QHostAddress>
|
||||
#include <QUdpSocket>
|
||||
#include "QDebug"
|
||||
#include "mavlinknode.h"
|
||||
|
||||
#ifdef QtDlink
|
||||
#include <dlinkglobal.h>
|
||||
class DLINKSHARED_EXPORT DLink : public QObject {
|
||||
#else
|
||||
class DLink : public QObject
|
||||
{
|
||||
#endif
|
||||
Q_OBJECT
|
||||
|
||||
struct Node{
|
||||
QUdpSocket * sock;
|
||||
QHostAddress addr;
|
||||
quint16 port;
|
||||
};
|
||||
|
||||
|
||||
public:
|
||||
explicit DLink(QObject *parent = nullptr);
|
||||
~DLink();
|
||||
|
||||
void setupPort(const QString port, qint32 baudrate, QSerialPort::Parity parity);
|
||||
bool statesPort();
|
||||
void stopPort();
|
||||
|
||||
void setupClient(const QHostAddress &remote_addr, int remote_port, int local_port);
|
||||
|
||||
|
||||
|
||||
int SendMessageTo(uint8_t, uint8_t *msg, uint16_t len);
|
||||
|
||||
|
||||
MavLinkNode *mavlinknode = nullptr;
|
||||
|
||||
signals:
|
||||
|
||||
|
||||
|
||||
public slots:
|
||||
|
||||
void readPendingDatagramsSerialPort(void);
|
||||
|
||||
void readPendingDatagramsClient(void);
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
enum SourceType{
|
||||
c_sock = 0,
|
||||
s_port = 1
|
||||
};
|
||||
|
||||
|
||||
QSerialPort *serialPort = nullptr;
|
||||
QUdpSocket *Clientsock = nullptr;
|
||||
|
||||
QList<Node> clientSockets;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif // DLINK_H
|
||||
#ifndef DLINK_H
|
||||
#define DLINK_H
|
||||
|
||||
#include <QObject>
|
||||
#include "QtSerialPort"
|
||||
#include <QHostAddress>
|
||||
#include <QUdpSocket>
|
||||
#include "QDebug"
|
||||
#include "mavlinknode.h"
|
||||
|
||||
#ifdef QtDlink
|
||||
#include <dlinkglobal.h>
|
||||
class DLINKSHARED_EXPORT DLink : public QObject {
|
||||
#else
|
||||
class DLink : public QObject
|
||||
{
|
||||
#endif
|
||||
Q_OBJECT
|
||||
|
||||
struct Node{
|
||||
QUdpSocket * sock;
|
||||
QHostAddress addr;
|
||||
quint16 port;
|
||||
};
|
||||
|
||||
|
||||
public:
|
||||
explicit DLink(QObject *parent = nullptr);
|
||||
~DLink();
|
||||
|
||||
void setupPort(const QString port, qint32 baudrate, QSerialPort::Parity parity);
|
||||
bool statesPort();
|
||||
void stopPort();
|
||||
|
||||
void setupClient(const QHostAddress &remote_addr, int remote_port, int local_port);
|
||||
|
||||
|
||||
|
||||
int SendMessageTo(uint8_t, uint8_t *msg, uint16_t len);
|
||||
|
||||
|
||||
MavLinkNode *mavlinknode = nullptr;
|
||||
|
||||
signals:
|
||||
|
||||
|
||||
|
||||
public slots:
|
||||
|
||||
void readPendingDatagramsSerialPort(void);
|
||||
|
||||
void readPendingDatagramsClient(void);
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
enum SourceType{
|
||||
c_sock = 0,
|
||||
s_port = 1
|
||||
};
|
||||
|
||||
|
||||
QSerialPort *serialPort = nullptr;
|
||||
QUdpSocket *Clientsock = nullptr;
|
||||
|
||||
QList<Node> clientSockets;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif // DLINK_H
|
||||
|
||||
Reference in New Issue
Block a user