299 lines
8.2 KiB
C++
299 lines
8.2 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);//采用直连的方式,因为是多线程
|
|
|
|
//在当前线程运行
|
|
connect(mavlinknode,SIGNAL(showMessage(QString,int)),
|
|
this,SIGNAL(showMessage(QString,int)),Qt::DirectConnection);
|
|
|
|
//在当前线程运行
|
|
connect(this,SIGNAL(recieveMessage(quint32,QByteArray)),
|
|
mavlinknode,SLOT(setbuff(quint32,QByteArray)),Qt::DirectConnection);
|
|
|
|
mavlinknode->start();
|
|
//其他协议节点。。。
|
|
}
|
|
DLink::~DLink()
|
|
{
|
|
stopPort();
|
|
|
|
mavlinknode->stop();
|
|
delete mavlinknode;
|
|
}
|
|
|
|
|
|
int DLink::SendMessageTo(quint8 ch, quint8 *msg, quint16 len)
|
|
{
|
|
//让这个函数在其他线程运行
|
|
/*
|
|
QString num;
|
|
for (int i = 0; i < len; ++i) {
|
|
num.append(QString::number(msg[i],16).toUpper());
|
|
num.append(" ");
|
|
}
|
|
qDebug() << num;
|
|
*/
|
|
//Q_UNUSED(ch);
|
|
|
|
//更加ch选择
|
|
if (DLink::Clientsock)
|
|
{
|
|
foreach(Node node,clientSockets)
|
|
{
|
|
qint64 flag = DLink::Clientsock->writeDatagram((const char *)msg,len,node.addr, node.port);
|
|
if(flag != -1)
|
|
{
|
|
qDebug() << "Client Send Msg";
|
|
}
|
|
}
|
|
}
|
|
|
|
if (DLink::serialPort)
|
|
{
|
|
qint64 flag = DLink::serialPort->write((const char *)msg,len);
|
|
if(flag != -1)
|
|
{
|
|
qDebug() << "serialPort Send Msg";
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
//这个函数就在本线程内运行
|
|
bool DLink::setupPort(const QString port, qint32 baudrate, QSerialPort::Parity parity)
|
|
{
|
|
|
|
qWarning() << port
|
|
<< baudrate
|
|
<< parity;
|
|
|
|
bool isSuccess = false;
|
|
|
|
if (serialPort)//多串口接入
|
|
{
|
|
serialPort->close();
|
|
delete serialPort;
|
|
}
|
|
serialPort = new QSerialPort(this);
|
|
|
|
serialPort->setPortName(port);
|
|
|
|
if (serialPort->open(QIODevice::ReadWrite))
|
|
{
|
|
serialPort->setBaudRate(baudrate);
|
|
serialPort->setParity(parity);
|
|
serialPort->setDataBits(QSerialPort::Data8);
|
|
serialPort->setStopBits(QSerialPort::OneStop);
|
|
|
|
DLink::serialPort->write("serial connet ok",16);
|
|
|
|
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readPendingDatagramsSerialPort()));//本线程内调用
|
|
isSuccess = true;
|
|
|
|
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;
|
|
}
|
|
|
|
bool DLink::statesPort()
|
|
{
|
|
if(serialPort)
|
|
{
|
|
return (serialPort != nullptr)?(true):(false);
|
|
}
|
|
}
|
|
|
|
void DLink::stopPort()
|
|
{
|
|
if(serialPort)
|
|
{
|
|
emit showMessage(tr("Serial Port close"));
|
|
if(serialPort->isOpen())
|
|
{
|
|
serialPort->close();
|
|
}
|
|
delete serialPort;
|
|
serialPort = nullptr;
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
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_Param1 << m_Param2.toString().toInt() << parity;
|
|
|
|
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_Type.toString() == "UDP")
|
|
{
|
|
//qDebug() << m_Type << m_Param1 << m_Param2 << m_Param4;
|
|
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
|
|
{
|
|
qWarning() << "disconnect";
|
|
if(m_Type.toString() == "SerialPort")
|
|
{
|
|
stopPort();
|
|
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
|
}
|
|
else if(m_Type.toString() == "UDP")
|
|
{
|
|
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)
|
|
{
|
|
if(Clientsock)
|
|
{
|
|
Clientsock->disconnect();
|
|
delete Clientsock;
|
|
Clientsock = nullptr;
|
|
}
|
|
|
|
bool isSuccess = false;
|
|
Clientsock = new QUdpSocket(this);
|
|
if (Clientsock->bind(QHostAddress::AnyIPv4, local_port, QUdpSocket::ShareAddress|QUdpSocket::ReuseAddressHint))
|
|
{
|
|
Clientsock->joinMulticastGroup(remote_addr);
|
|
Clientsock->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption,1024*1024*8);
|
|
Clientsock->setSocketOption(QAbstractSocket::MulticastTtlOption, 3);
|
|
|
|
if(Clientsock->open(QIODevice::ReadWrite))
|
|
//if(Clientsock->joinMulticastGroup(remote_addr))
|
|
{
|
|
connect(Clientsock, SIGNAL(readyRead()),
|
|
this, SLOT(readPendingDatagramsClient()));
|
|
|
|
Node node;
|
|
node.sock = Clientsock;
|
|
node.addr = remote_addr;
|
|
node.port = remote_port;
|
|
|
|
clientSockets.append(node);
|
|
qWarning() << "UdpSocket open:"
|
|
<< remote_addr
|
|
<< local_port
|
|
<< remote_port;
|
|
|
|
isSuccess = true;
|
|
emit showMessage(tr("UdpSocket open"));
|
|
}
|
|
else
|
|
{
|
|
Clientsock->leaveMulticastGroup(remote_addr);
|
|
Clientsock->close();
|
|
isSuccess = false;
|
|
qWarning() << "sock not open";
|
|
delete Clientsock;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isSuccess = false;
|
|
Clientsock->leaveMulticastGroup(remote_addr);
|
|
Clientsock->close();
|
|
qWarning("sock is not binded.");
|
|
delete Clientsock;
|
|
}
|
|
|
|
return isSuccess;
|
|
|
|
}
|
|
|
|
void DLink::readPendingDatagramsSerialPort(void)
|
|
{
|
|
if(serialPort)
|
|
{
|
|
QByteArray datagram = serialPort->readAll();
|
|
emit recieveMessage(SourceType::s_port,datagram);
|
|
}
|
|
}
|
|
|
|
void DLink::readPendingDatagramsClient(void)
|
|
{
|
|
//轮番查询
|
|
if(Clientsock)
|
|
{
|
|
while (Clientsock->hasPendingDatagrams()) {
|
|
QByteArray datagram;
|
|
datagram.resize(Clientsock->pendingDatagramSize());
|
|
Clientsock->readDatagram(datagram.data(), datagram.size());
|
|
|
|
emit recieveMessage(SourceType::c_sock,datagram);
|
|
}
|
|
}
|
|
}
|
|
|
|
|