935 lines
25 KiB
C++
935 lines
25 KiB
C++
#include "dlink.h"
|
|
#include <string.h>
|
|
#include "QDateTime"
|
|
|
|
|
|
DLink::DLink(QObject *parent) : QObject(parent)
|
|
{
|
|
qDebug() << "Dlink " << QThread::currentThreadId();
|
|
|
|
|
|
//采用插件导入的方式(plugin)
|
|
|
|
//mavlink协议节点
|
|
mavlinknode = new MavLinkNode();//不允许带参数,因为这是单独的线程
|
|
|
|
//在当前线程运行
|
|
connect(mavlinknode,SIGNAL(SendMessageTo(quint8,quint8*,quint16)),
|
|
this,SLOT(SendMessageTo(quint8,quint8*,quint16)),Qt::BlockingQueuedConnection);//BlockingQueuedConnection);//信号和槽必须在不同的线程中,否则就产生死锁
|
|
|
|
connect(mavlinknode,SIGNAL(SendMessageToExport(quint8,quint8*,quint16)),
|
|
this,SLOT(SendMessageToExport(quint8,quint8*,quint16)),Qt::BlockingQueuedConnection);//BlockingQueuedConnection);//信号和槽必须在不同的线程中,否则就产生死锁
|
|
|
|
|
|
//在当前线程运行
|
|
connect(mavlinknode,SIGNAL(showMessage(QString,int)),
|
|
this,SIGNAL(showMessage(QString,int)));
|
|
|
|
//在当前线程运行
|
|
connect(this,SIGNAL(recieveMessage(quint32,QByteArray)),
|
|
mavlinknode,SLOT(setbuff(quint32,QByteArray)),Qt::DirectConnection);
|
|
|
|
|
|
connect(this,SIGNAL(getRemote(quint32,QByteArray)),
|
|
mavlinknode->RC,SLOT(setbuff(quint32,QByteArray)),Qt::DirectConnection);
|
|
|
|
|
|
connect(this,SIGNAL(getRTK(quint32,QByteArray)),
|
|
mavlinknode->rtk,SLOT(setbuff(quint32,QByteArray)),Qt::DirectConnection);
|
|
|
|
//其他协议节点。。。
|
|
|
|
|
|
bitTimer = new QTimer(this);
|
|
bitTimer->setInterval(1000);
|
|
|
|
connect(bitTimer,SIGNAL(timeout()),
|
|
this,SLOT(bitTimerout()));
|
|
|
|
|
|
bitTimer->start();
|
|
|
|
|
|
Exportsock = new QUdpSocket(this);
|
|
int rst = Exportsock->bind(QHostAddress::AnyIPv4,10000);
|
|
if(rst)
|
|
{
|
|
if(Exportsock->open(QIODevice::ReadWrite))
|
|
{
|
|
qDebug() << "信息输出端口打开正常";
|
|
}
|
|
}
|
|
|
|
|
|
sendRTCM = false;
|
|
|
|
RecieveRCtime = QDateTime::currentMSecsSinceEpoch();
|
|
RecieveRTCMtime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
}
|
|
DLink::~DLink()
|
|
{
|
|
foreach (serialNode node, serials) {
|
|
if(node.port)
|
|
{
|
|
if(node.port->isOpen())
|
|
{
|
|
node.port->close();
|
|
}
|
|
delete node.port;
|
|
node.port = nullptr;
|
|
}
|
|
}
|
|
|
|
foreach(clientNode node,clientSockets)
|
|
{
|
|
if(node.sock)
|
|
{
|
|
node.sock->leaveMulticastGroup(node.addr);
|
|
node.sock->abort();
|
|
qWarning("leave multicast Group");
|
|
delete node.sock;
|
|
node.sock = nullptr;
|
|
}
|
|
}
|
|
|
|
disconnect(this, nullptr, nullptr, nullptr);
|
|
|
|
delete mavlinknode;
|
|
mavlinknode = nullptr;
|
|
|
|
if(bitTimer)
|
|
{
|
|
bitTimer->stop();
|
|
delete bitTimer;
|
|
}
|
|
|
|
if(Exportsock)
|
|
{
|
|
Exportsock->abort();
|
|
delete Exportsock;
|
|
Exportsock = nullptr;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void DLink::bitTimerout(void)
|
|
{
|
|
|
|
static qint64 last = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
qint64 time = QDateTime::currentMSecsSinceEpoch() - last;
|
|
|
|
|
|
if(time >= 2000)
|
|
{
|
|
out_bits = (double)outCount / (time * 0.001);
|
|
in_bits = (double)inCount / (time * 0.001);
|
|
|
|
if(frameCount > 0)
|
|
{
|
|
rssi_bits = (double)errCount / frameCount * 100;
|
|
}
|
|
else
|
|
{
|
|
rssi_bits = 0;
|
|
}
|
|
|
|
|
|
outCount = 0;
|
|
inCount = 0;
|
|
|
|
last = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
emit info(rssi_bits,in_bits,out_bits);
|
|
|
|
|
|
|
|
|
|
|
|
double RecieveRCByte = 0;
|
|
double RecieveRTCMByte = 0;
|
|
|
|
RecieveRCByte = (double)RecieveRCcount / (time * 0.001);
|
|
RecieveRTCMByte = (double)RecieveRTCMcount / (time * 0.001);
|
|
|
|
|
|
|
|
if((QDateTime::currentMSecsSinceEpoch() - RecieveRCtime) > 2000)
|
|
{
|
|
emit RC_Byte(0);
|
|
}
|
|
else
|
|
{
|
|
emit RC_Byte(RecieveRCByte);
|
|
}
|
|
|
|
if((QDateTime::currentMSecsSinceEpoch() - RecieveRTCMtime) > 2000)
|
|
{
|
|
emit RTCM_Byte(0);
|
|
}
|
|
else
|
|
{
|
|
emit RTCM_Byte(RecieveRTCMByte);
|
|
}
|
|
|
|
RecieveRCcount = 0;
|
|
RecieveRTCMcount = 0;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void DLink::setExportInfo(QVariant value,QVariant id)
|
|
{
|
|
isExportInfo = value.toBool();
|
|
|
|
if(mavlinknode)
|
|
{
|
|
mavlinknode->infoExportID = id.toInt();
|
|
}
|
|
|
|
qDebug() << "isExportInfo" << value;
|
|
}
|
|
|
|
int DLink::SendMessageToExport(quint8 ch, quint8 *msg, quint16 len)
|
|
{
|
|
Q_UNUSED(ch)
|
|
|
|
if(isExportInfo)//使能输出再输出
|
|
{
|
|
if(Exportsock)
|
|
{
|
|
qint64 flag = Exportsock->writeDatagram((const char *)msg,len,QHostAddress("192.168.5.8"), 10038);
|
|
if(flag != -1)
|
|
{
|
|
//qDebug() << "Client Send Msg";
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return len;
|
|
}
|
|
|
|
int DLink::SendMessageTo(quint8 ch, quint8 *msg, quint16 len)
|
|
{
|
|
//让这个函数在其他线程运行
|
|
outCount += len;
|
|
//qDebug() << "Send Msg";
|
|
//更加ch选择
|
|
|
|
foreach(clientNode node,clientSockets)
|
|
{
|
|
if(node.sock->isOpen()||node.sock->isWritable()||node.sock->isValid())
|
|
{
|
|
if(node.type == devtype::datalink)
|
|
{
|
|
qint64 flag = node.sock->writeDatagram((const char *)msg,len,node.addr, node.port);
|
|
if(flag != -1)
|
|
{
|
|
//qDebug() << "Client Send Msg";
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "udp Send Msg failure";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (serialNode node, serials) {
|
|
if(node.port)
|
|
{
|
|
if(node.port->isOpen()||node.port->isWritable())
|
|
{
|
|
if(node.type == devtype::datalink)
|
|
{
|
|
qint64 flag = node.port->write((const char *)msg,len);
|
|
if(flag != -1)
|
|
{
|
|
//qDebug() << "serialPort Send Msg";
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "serialPort Send Msg failure";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (multicast_sock)
|
|
{
|
|
if(multicast_sock->isOpen()||multicast_sock->isWritable())
|
|
{
|
|
qint64 flag = multicast_sock->writeDatagram((const char *)msg, len,multicast_transmit_addr, multicast_transmit_port);
|
|
if(flag != -1)
|
|
{
|
|
//qDebug() << "multicast Send Msg";
|
|
}
|
|
}
|
|
}
|
|
|
|
return outCount;
|
|
}
|
|
|
|
//这个函数就在本线程内运行
|
|
bool DLink::setupPort(const QString port, qint32 baudrate, QSerialPort::Parity parity)
|
|
{
|
|
bool isSuccess = false;
|
|
|
|
foreach (serialNode node, serials) {
|
|
if(node.port)
|
|
{
|
|
if(node.name == port)
|
|
{
|
|
if(node.port->isOpen())
|
|
{
|
|
node.port->close();
|
|
}
|
|
|
|
//断掉所有的连接
|
|
disconnect(node.port,nullptr,nullptr,nullptr);
|
|
|
|
delete node.port;
|
|
node.port = nullptr;
|
|
|
|
|
|
serials.removeAt(serials.indexOf(node));
|
|
}
|
|
}
|
|
}
|
|
|
|
QSerialPort *serialPort = new QSerialPort(port,this);
|
|
|
|
if (serialPort->open(QIODevice::ReadWrite))
|
|
{
|
|
serialPort->setBaudRate(baudrate);
|
|
serialPort->setParity(parity);
|
|
serialPort->setDataBits(QSerialPort::Data8);
|
|
serialPort->setStopBits(QSerialPort::OneStop);
|
|
|
|
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readPendingDatagramsSerialPort()));//本线程内调用
|
|
isSuccess = true;
|
|
|
|
serialNode node;
|
|
|
|
node.port = serialPort;
|
|
node.baud = baudrate;
|
|
node.parity = parity;
|
|
node.name = port;
|
|
node.type = devtype::datalink;
|
|
|
|
serials.append(node);
|
|
|
|
emit showMessage(tr("Serial Port Open Success"));
|
|
qWarning() << "Serial Port Open Success";
|
|
}
|
|
else
|
|
{
|
|
isSuccess = false;
|
|
emit showMessage(tr("Serial Port Open Fail"));
|
|
qWarning() << "Serial Port Open Fail";
|
|
delete serialPort;
|
|
serialPort = nullptr;
|
|
}
|
|
|
|
return isSuccess;
|
|
}
|
|
|
|
|
|
/* 新想法
|
|
* dlink 当作一个线程一直循环,一旦某一个串口连接,就会把事件循环加入其中,得到事件驱动
|
|
* 指令按下会把数据存到缓存,等待发送出去,线程就会结束
|
|
* 发起任务读写,新建一个线程,航线读取发送结束,航线线程结束
|
|
* 发起参数读写,新建一个线程,参数读取发送结束,参数线程结束
|
|
*/
|
|
bool DLink::PortSetup(devtype type,const QString port, qint32 baud, QSerialPort::Parity parity, const char * method)
|
|
{
|
|
bool isSuccess = false;
|
|
QSerialPort *serialPort = new QSerialPort(port, this);
|
|
|
|
if (serialPort->open(QIODevice::ReadWrite))
|
|
{
|
|
serialPort->setBaudRate(baud);
|
|
serialPort->setStopBits(QSerialPort::OneStop);
|
|
serialPort->setParity(parity);
|
|
serialPort->setDataBits(QSerialPort::Data8);
|
|
connect(serialPort, SIGNAL(readyRead()), this, method);
|
|
|
|
isSuccess = true;
|
|
|
|
serialNode node;
|
|
|
|
node.port = serialPort;
|
|
node.baud = baud;
|
|
node.parity = parity;
|
|
node.name = port;
|
|
node.type = type;
|
|
|
|
serials.insert(serials.size(),node);
|
|
}
|
|
else
|
|
{
|
|
delete serialPort;
|
|
serialPort = NULL;
|
|
}
|
|
return isSuccess;
|
|
}
|
|
|
|
bool DLink::PortState(const QString name)
|
|
{
|
|
bool flag = false;
|
|
|
|
foreach (serialNode node, serials) {
|
|
if(node.port)
|
|
{
|
|
if(node.name == name)
|
|
{
|
|
flag = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return flag;
|
|
}
|
|
|
|
bool DLink::PortStop(const QString name)
|
|
{
|
|
bool flag = false;
|
|
|
|
foreach (serialNode node, serials) {
|
|
if(node.port)
|
|
{
|
|
if(node.name == name)
|
|
{
|
|
if(node.port->isOpen())
|
|
{
|
|
emit showMessage(tr("%1 close").arg(node.name));
|
|
node.port->close();
|
|
}
|
|
|
|
//断掉所有的连接
|
|
disconnect(node.port,nullptr,nullptr,nullptr);
|
|
|
|
delete node.port;
|
|
node.port = nullptr;
|
|
|
|
serials.removeOne(node);
|
|
|
|
flag = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return flag;
|
|
}
|
|
|
|
void DLink::connectSignal(QVariant m_state,
|
|
QVariant m_usrName,QVariant m_Type,
|
|
QVariant m_Param1,QVariant m_Param2,
|
|
QVariant m_Param3,QVariant m_Param4,
|
|
QVariant m_Param5)
|
|
{
|
|
qDebug() << m_Type.toString() << m_usrName.toString();
|
|
|
|
if(m_state.toBool() == true)
|
|
{
|
|
if(m_Type.toString() == "SerialPort")
|
|
{
|
|
QSerialPort::Parity parity;
|
|
if(m_Param4.toString() == "NONE")
|
|
{
|
|
parity = QSerialPort::Parity::NoParity;
|
|
}
|
|
if(m_Param4.toString() == "ODD")
|
|
{
|
|
parity = QSerialPort::Parity::OddParity;
|
|
}
|
|
if(m_Param4.toString() == "EVEN")
|
|
{
|
|
parity = QSerialPort::Parity::EvenParity;
|
|
}
|
|
|
|
|
|
qWarning() << m_Type << m_usrName.toString() << m_Param1 << m_Param2.toString().toInt() << parity;
|
|
|
|
|
|
if(m_usrName.toString() == "DataLink")
|
|
{
|
|
if(setupPort(m_Param1.toString(),m_Param2.toString().toInt(),parity))
|
|
{
|
|
emit PortConnected(true,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
else
|
|
{
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
}
|
|
else if(m_usrName.toString() == "RTK")
|
|
{
|
|
if(setup_rtk(m_Param1.toString(),m_Param2.toString().toInt(),parity))
|
|
{
|
|
emit PortConnected(true,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
else
|
|
{
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
}
|
|
else if(m_usrName.toString() == "Emergecy")
|
|
{
|
|
qDebug() << "Emergecy";
|
|
}
|
|
else if(m_usrName.toString() == "Remote")
|
|
{
|
|
if(setup_remote(m_Param1.toString(),m_Param2.toString().toInt(),parity))
|
|
{
|
|
emit PortConnected(true,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
else
|
|
{
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
}
|
|
|
|
}
|
|
else if(m_Type.toString() == "UDP")
|
|
{
|
|
if(m_usrName.toString() == "Emergecy")
|
|
{
|
|
qDebug() << "Emergecy";
|
|
}
|
|
else
|
|
{
|
|
qDebug() << "connet to udp socket";
|
|
if(setupClient(QHostAddress(m_Param1.toString()),m_Param2.toString().toInt(),QHostAddress(m_Param3.toString()),m_Param4.toString().toInt()))
|
|
{
|
|
emit PortConnected(true,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
else
|
|
{
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
}
|
|
}
|
|
else if(m_Type.toString() == "Multicast")
|
|
{
|
|
qDebug() << "connet to multicast socket";
|
|
if(setup_multicast(m_Param1.toString(),m_Param2.toString().toInt(),m_Param3.toString(),m_Param4.toString().toInt()))
|
|
{
|
|
emit PortConnected(true,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
else
|
|
{
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
qWarning() << "disconnect";
|
|
if(m_Type.toString() == "SerialPort")
|
|
{
|
|
PortStop(m_Param1.toString());
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
else if(m_Type.toString() == "UDP")
|
|
{
|
|
//断开连接
|
|
for(int i = 0;i < clientSockets.count();i++)
|
|
{
|
|
if((clientSockets.at(i).addr == QHostAddress(m_Param3.toString()))&&(clientSockets.at(i).port == m_Param4.toInt()))
|
|
{
|
|
|
|
clientSockets.at(i).sock->leaveMulticastGroup(QHostAddress(m_Param3.toString()));
|
|
clientSockets.at(i).sock->close();
|
|
qWarning("leave multicast Group");
|
|
clientSockets.at(i).sock->deleteLater();
|
|
|
|
clientSockets.removeAt(i);
|
|
}
|
|
|
|
}
|
|
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
else if(m_Type.toString() == "Multicast")
|
|
{
|
|
//断开连接
|
|
if(multicast_sock)
|
|
{
|
|
qWarning("leave multicast Group");
|
|
delete multicast_sock;
|
|
multicast_sock = nullptr;
|
|
}
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHostAddress &remote_addr,int remote_port)
|
|
{
|
|
bool isSuccess = false;
|
|
|
|
|
|
foreach (clientNode node, clientSockets) {
|
|
if(node.sock)
|
|
{
|
|
if((node.addr == remote_addr)&&(node.port == remote_port))
|
|
{
|
|
if(node.sock->isOpen())
|
|
{
|
|
node.sock->leaveMulticastGroup(node.addr);
|
|
node.sock->close();
|
|
}
|
|
//断掉所有的连接
|
|
disconnect(node.sock,nullptr,nullptr,nullptr);
|
|
|
|
node.sock->deleteLater();
|
|
//node.sock = nullptr;
|
|
|
|
clientSockets.removeAt(clientSockets.indexOf(node));
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
QUdpSocket *clientsock = new QUdpSocket(this);
|
|
int rst = clientsock->open(QIODevice::ReadWrite);
|
|
if(rst)
|
|
{
|
|
clientsock->setProxy(QNetworkProxy::NoProxy);
|
|
|
|
if(clientsock->bind(local_addr,local_port,QAbstractSocket::ShareAddress))
|
|
{
|
|
connect(clientsock, SIGNAL(readyRead()),
|
|
this, SLOT(readPendingDatagramsClient()));
|
|
|
|
clientNode node;
|
|
node.sock = clientsock;
|
|
node.addr = remote_addr;
|
|
node.port = remote_port;
|
|
node.type = devtype::datalink;
|
|
|
|
clientSockets.append(node);
|
|
qWarning() << "UdpSocket open:"
|
|
<< remote_addr
|
|
<< local_port
|
|
<< remote_port;
|
|
|
|
isSuccess = true;
|
|
emit showMessage(tr("UdpSocket open"));
|
|
}
|
|
else
|
|
{
|
|
clientsock->close();
|
|
isSuccess = false;
|
|
qWarning() << "sock open failure" << clientsock->state();
|
|
delete clientsock;
|
|
clientsock = nullptr;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isSuccess = false;
|
|
clientsock->close();
|
|
qWarning("sock is not binded.");
|
|
delete clientsock;
|
|
clientsock = nullptr;
|
|
}
|
|
|
|
return isSuccess;
|
|
|
|
}
|
|
|
|
bool DLink::setup_multicast(const QString &recieveIP, qint32 recievePort,
|
|
const QString &transmitIP, qint32 transmitPort)
|
|
{
|
|
bool isSuccess = false;
|
|
multicast_transmit_port = transmitPort;
|
|
multicast_transmit_addr.setAddress(transmitIP);
|
|
|
|
if (multicast_sock)
|
|
delete multicast_sock;
|
|
|
|
multicast_recieve_addr.setAddress(recieveIP);
|
|
multicast_sock = new QUdpSocket(this);
|
|
if (multicast_sock->bind(QHostAddress::AnyIPv4, recievePort, QUdpSocket::ShareAddress))
|
|
{
|
|
multicast_sock->setProxy(QNetworkProxy::NoProxy);
|
|
|
|
if (multicast_sock->joinMulticastGroup(multicast_recieve_addr))
|
|
{
|
|
connect(multicast_sock,&QUdpSocket::readyRead,
|
|
this,&DLink::processPendingMulticastDatagrams);
|
|
multicast_recieve_port = recievePort;
|
|
multicast_sock->setSocketOption(QAbstractSocket::MulticastTtlOption, 3);
|
|
|
|
isSuccess = true;
|
|
emit showMessage(tr("Bind and join the gdt multicast group"));
|
|
}
|
|
else
|
|
{
|
|
isSuccess = false;
|
|
delete multicast_sock;
|
|
multicast_sock = NULL;
|
|
|
|
emit showMessage(tr("Fail to join gdt multicast group."));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isSuccess = false;
|
|
emit showMessage(tr("Fail to bind gdt multicast socket."));
|
|
delete multicast_sock;
|
|
multicast_sock = NULL;
|
|
}
|
|
return isSuccess;
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DLink::setup_remote(const QString port, qint32 baud, QSerialPort::Parity parity)
|
|
{
|
|
bool isSuccess = false;
|
|
|
|
foreach (serialNode node, serials) {
|
|
if(node.port)
|
|
{
|
|
if(node.name == port)
|
|
{
|
|
if(node.port->isOpen())
|
|
{
|
|
node.port->close();
|
|
}
|
|
|
|
//断掉所有的连接
|
|
disconnect(node.port,nullptr,nullptr,nullptr);
|
|
|
|
delete node.port;
|
|
node.port = nullptr;
|
|
|
|
serials.removeAt(serials.indexOf(node));
|
|
}
|
|
}
|
|
}
|
|
|
|
QSerialPort *serialPort = new QSerialPort(port, this);
|
|
|
|
if (serialPort->open(QIODevice::ReadWrite))
|
|
{
|
|
serialPort->setBaudRate(baud);
|
|
serialPort->setStopBits(QSerialPort::OneStop);
|
|
serialPort->setParity(parity);
|
|
serialPort->setDataBits(QSerialPort::Data8);
|
|
connect(serialPort, &QSerialPort::readyRead, this, &DLink::readRemotePendingDatagrams);
|
|
isSuccess = true;
|
|
|
|
serialNode node;
|
|
|
|
node.port = serialPort;
|
|
node.baud = baud;
|
|
node.parity = parity;
|
|
node.name = port;
|
|
node.type = devtype::remote;
|
|
|
|
serials.insert(serials.size(),node);
|
|
}
|
|
else
|
|
{
|
|
delete serialPort;
|
|
serialPort = NULL;
|
|
}
|
|
return isSuccess;
|
|
}
|
|
|
|
bool DLink::setup_rtk(const QString port, qint32 baudrate, QSerialPort::Parity parity)
|
|
{
|
|
bool isSuccess = false;
|
|
|
|
foreach (serialNode node, serials) {
|
|
if(node.port)
|
|
{
|
|
if(node.name == port)
|
|
{
|
|
if(node.port->isOpen())
|
|
{
|
|
node.port->close();
|
|
}
|
|
|
|
//断掉所有的连接
|
|
disconnect(node.port,nullptr,nullptr,nullptr);
|
|
|
|
delete node.port;
|
|
node.port = nullptr;
|
|
|
|
serials.removeAt(serials.indexOf(node));
|
|
}
|
|
}
|
|
}
|
|
|
|
QSerialPort *serialPort = new QSerialPort(port, this);
|
|
|
|
if (serialPort->open(QIODevice::ReadWrite))
|
|
{
|
|
serialPort->setBaudRate(baudrate);
|
|
serialPort->setStopBits(QSerialPort::OneStop);
|
|
serialPort->setParity(parity);
|
|
serialPort->setDataBits(QSerialPort::Data8);
|
|
connect(serialPort, &QSerialPort::readyRead, this, &DLink::readRTKPendingDatagrams);
|
|
isSuccess = true;
|
|
|
|
serialNode node;
|
|
|
|
node.port = serialPort;
|
|
node.baud = baudrate;
|
|
node.parity = parity;
|
|
node.name = port;
|
|
node.type = devtype::rtk;
|
|
|
|
serials.insert(serials.size(),node);
|
|
}
|
|
else
|
|
{
|
|
delete serialPort;
|
|
serialPort = NULL;
|
|
}
|
|
return isSuccess;
|
|
}
|
|
|
|
|
|
|
|
void DLink::readPendingDatagramsSerialPort(void)
|
|
{
|
|
QObject *obj = sender();
|
|
|
|
if(obj)
|
|
{
|
|
QSerialPort *serialport = qobject_cast<QSerialPort *>(obj);
|
|
if(serialport)
|
|
{
|
|
QByteArray datagram = serialport->readAll();
|
|
|
|
inCount += datagram.size();
|
|
|
|
emit recieveMessage(SourceType::s_port,datagram);
|
|
}
|
|
}
|
|
}
|
|
|
|
void DLink::readPendingDatagramsClient(void)
|
|
{
|
|
QObject *obj = sender();
|
|
|
|
if(obj)
|
|
{
|
|
QUdpSocket *clientsock = qobject_cast<QUdpSocket *>(obj);
|
|
if(clientsock)
|
|
{
|
|
while (clientsock->hasPendingDatagrams()) {
|
|
QByteArray datagram;
|
|
datagram.resize(clientsock->pendingDatagramSize());
|
|
clientsock->readDatagram(datagram.data(), datagram.size());
|
|
|
|
inCount += datagram.size();
|
|
|
|
emit recieveMessage(SourceType::c_sock,datagram);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void DLink::processPendingMulticastDatagrams(void)
|
|
{
|
|
QObject *obj = sender();
|
|
|
|
if(obj)
|
|
{
|
|
QUdpSocket *clientsock = qobject_cast<QUdpSocket *>(obj);
|
|
if(clientsock)
|
|
{
|
|
while (clientsock->hasPendingDatagrams()) {
|
|
QByteArray datagram;
|
|
datagram.resize(clientsock->pendingDatagramSize());
|
|
clientsock->readDatagram(datagram.data(), datagram.size());
|
|
|
|
inCount += datagram.size();
|
|
|
|
emit recieveMessage(SourceType::c_sock,datagram);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void DLink::readRTKPendingDatagrams(void)
|
|
{
|
|
QObject *obj = sender();
|
|
|
|
if(obj)
|
|
{
|
|
QSerialPort *serialport = qobject_cast<QSerialPort *>(obj);
|
|
if(serialport)
|
|
{
|
|
QByteArray datagram = serialport->readAll();
|
|
|
|
if(datagram.size() > 0)
|
|
{
|
|
//emit RTCM_Byte(datagram.size());
|
|
|
|
RecieveRTCMcount += datagram.size();
|
|
|
|
RecieveRTCMtime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
isRecieveRTCM = true;
|
|
if(sendRTCM == true)
|
|
{
|
|
emit getRTK(SourceType::s_port,datagram);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void DLink::readRemotePendingDatagrams(void)
|
|
{
|
|
QObject *obj = sender();
|
|
|
|
if(obj)
|
|
{
|
|
QSerialPort *serialport = qobject_cast<QSerialPort *>(obj);
|
|
if(serialport)
|
|
{
|
|
QByteArray datagram = serialport->readAll();
|
|
|
|
if(datagram.size() > 0)
|
|
{
|
|
//emit RC_Byte(datagram.size());
|
|
|
|
RecieveRCcount += datagram.size();
|
|
|
|
RecieveRCtime = QDateTime::currentMSecsSinceEpoch();
|
|
|
|
isRecieveRC = true;
|
|
emit getRemote(SourceType::s_port,datagram);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|