数据链可以断开链接后重新链接
This commit is contained in:
@@ -268,7 +268,7 @@ void landinggear::actuator_info(Parse::_actuator info)
|
||||
ui->label_0_temp->setText(QString::number(info.temp));
|
||||
|
||||
|
||||
qDebug() << info.left_brake << info.right_brake;
|
||||
//qDebug() << info.left_brake << info.right_brake;
|
||||
|
||||
|
||||
}
|
||||
|
||||
+181
-64
@@ -76,6 +76,20 @@ DLink::~DLink()
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -168,30 +182,54 @@ int DLink::SendMessageTo(quint8 ch, quint8 *msg, quint16 len)
|
||||
|
||||
foreach(clientNode node,clientSockets)
|
||||
{
|
||||
qint64 flag = node.sock->writeDatagram((const char *)msg,len,node.addr, node.port);
|
||||
if(flag != -1)
|
||||
if(node.sock->isOpen()||node.sock->isWritable()||node.sock->isValid())
|
||||
{
|
||||
//qDebug() << "Client Send Msg";
|
||||
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() << "Client Send Msg failure";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (serialPort)
|
||||
{
|
||||
qint64 flag = serialPort->write((const char *)msg,len);
|
||||
if(flag != -1)
|
||||
{
|
||||
//qDebug() << "serialPort Send Msg";
|
||||
}
|
||||
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() << "Client Send Msg failure";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (multicast_sock)
|
||||
{
|
||||
qint64 flag = multicast_sock->writeDatagram((const char *)msg, len,multicast_transmit_addr, multicast_transmit_port);
|
||||
if(flag != -1)
|
||||
if(multicast_sock->isOpen()||multicast_sock->isWritable())
|
||||
{
|
||||
//qDebug() << "multicast Send Msg";
|
||||
qint64 flag = multicast_sock->writeDatagram((const char *)msg, len,multicast_transmit_addr, multicast_transmit_port);
|
||||
if(flag != -1)
|
||||
{
|
||||
//qDebug() << "multicast Send Msg";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,20 +239,31 @@ int DLink::SendMessageTo(quint8 ch, quint8 *msg, quint16 len)
|
||||
//这个函数就在本线程内运行
|
||||
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);
|
||||
foreach (serialNode node, serials) {
|
||||
if(node.port)
|
||||
{
|
||||
if(node.name == port)
|
||||
{
|
||||
if(node.port->isOpen())
|
||||
{
|
||||
node.port->close();
|
||||
}
|
||||
|
||||
serialPort->setPortName(port);
|
||||
//断掉所有的连接
|
||||
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))
|
||||
{
|
||||
@@ -232,8 +281,9 @@ bool DLink::setupPort(const QString port, qint32 baudrate, QSerialPort::Parity p
|
||||
node.baud = baudrate;
|
||||
node.parity = parity;
|
||||
node.name = port;
|
||||
node.type = devtype::datalink;
|
||||
|
||||
serials.insert(serials.size(),node);
|
||||
serials.append(node);
|
||||
|
||||
emit showMessage(tr("Serial Port Open Success"));
|
||||
qWarning() << "Serial Port Open Success";
|
||||
@@ -257,7 +307,7 @@ bool DLink::setupPort(const QString port, qint32 baudrate, QSerialPort::Parity p
|
||||
* 发起任务读写,新建一个线程,航线读取发送结束,航线线程结束
|
||||
* 发起参数读写,新建一个线程,参数读取发送结束,参数线程结束
|
||||
*/
|
||||
bool DLink::PortSetup(const QString port, qint32 baud, QSerialPort::Parity parity, const char * method)
|
||||
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);
|
||||
@@ -274,10 +324,11 @@ bool DLink::PortSetup(const QString port, qint32 baud, QSerialPort::Parity parit
|
||||
|
||||
serialNode node;
|
||||
|
||||
node.port = serialPort;
|
||||
node.baud = baud;
|
||||
node.port = serialPort;
|
||||
node.baud = baud;
|
||||
node.parity = parity;
|
||||
node.name = port;
|
||||
node.name = port;
|
||||
node.type = type;
|
||||
|
||||
serials.insert(serials.size(),node);
|
||||
}
|
||||
@@ -310,6 +361,9 @@ bool DLink::PortState(const QString name)
|
||||
bool DLink::PortStop(const QString name)
|
||||
{
|
||||
bool flag = false;
|
||||
|
||||
int index = -1;
|
||||
|
||||
foreach (serialNode node, serials) {
|
||||
if(node.port)
|
||||
{
|
||||
@@ -327,10 +381,14 @@ bool DLink::PortStop(const QString name)
|
||||
delete node.port;
|
||||
node.port = nullptr;
|
||||
|
||||
serials.removeOne(node);
|
||||
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
@@ -447,22 +505,19 @@ void DLink::connectSignal(QVariant m_state,
|
||||
else if(m_Type.toString() == "UDP")
|
||||
{
|
||||
//断开连接
|
||||
if(Clientsock)
|
||||
for(int i = 0;i < clientSockets.count();i++)
|
||||
{
|
||||
for(int i = 0;i < clientSockets.count();i++)
|
||||
if((clientSockets.at(i).addr == QHostAddress(m_Param3.toString()))&&(clientSockets.at(i).port == m_Param4.toInt()))
|
||||
{
|
||||
if(clientSockets.at(i).addr == QHostAddress(m_Param3.toString()))
|
||||
{
|
||||
clientSockets.removeAt(i);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Clientsock->leaveMulticastGroup(QHostAddress(m_Param3.toString()));
|
||||
Clientsock->close();
|
||||
qWarning("leave multicast Group");
|
||||
delete Clientsock;
|
||||
Clientsock = nullptr;
|
||||
}
|
||||
|
||||
emit PortConnected(false,m_usrName,m_Type,m_Param1,m_Param2,m_Param3,m_Param4,m_Param5);
|
||||
@@ -486,35 +541,50 @@ void DLink::connectSignal(QVariant m_state,
|
||||
|
||||
bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHostAddress &remote_addr,int remote_port)
|
||||
{
|
||||
bool isSuccess = false;
|
||||
|
||||
if(Clientsock)
|
||||
{
|
||||
Clientsock->disconnect();
|
||||
delete Clientsock;
|
||||
Clientsock = nullptr;
|
||||
|
||||
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));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
qDebug() << "sock connect";
|
||||
|
||||
bool isSuccess = false;
|
||||
Clientsock = new QUdpSocket(this);
|
||||
|
||||
|
||||
int rst = Clientsock->bind(QHostAddress::AnyIPv4,local_port);
|
||||
QUdpSocket *clientsock = new QUdpSocket(this);
|
||||
int rst = clientsock->bind(QHostAddress::AnyIPv4,local_port,QAbstractSocket::ShareAddress);
|
||||
|
||||
if(rst)
|
||||
{
|
||||
|
||||
|
||||
if(Clientsock->open(QIODevice::ReadWrite))
|
||||
if(clientsock->open(QIODevice::ReadWrite))
|
||||
{
|
||||
connect(Clientsock, SIGNAL(readyRead()),
|
||||
connect(clientsock, SIGNAL(readyRead()),
|
||||
this, SLOT(readPendingDatagramsClient()));
|
||||
|
||||
clientNode node;
|
||||
node.sock = Clientsock;
|
||||
node.sock = clientsock;
|
||||
node.addr = remote_addr;
|
||||
node.port = remote_port;
|
||||
node.type = devtype::datalink;
|
||||
|
||||
clientSockets.append(node);
|
||||
qWarning() << "UdpSocket open:"
|
||||
@@ -527,20 +597,20 @@ bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHos
|
||||
}
|
||||
else
|
||||
{
|
||||
Clientsock->close();
|
||||
clientsock->close();
|
||||
isSuccess = false;
|
||||
qWarning() << "sock not open";
|
||||
delete Clientsock;
|
||||
Clientsock = nullptr;
|
||||
delete clientsock;
|
||||
clientsock = nullptr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
isSuccess = false;
|
||||
Clientsock->close();
|
||||
clientsock->close();
|
||||
qWarning("sock is not binded.");
|
||||
delete Clientsock;
|
||||
Clientsock = nullptr;
|
||||
delete clientsock;
|
||||
clientsock = nullptr;
|
||||
}
|
||||
|
||||
return isSuccess;
|
||||
@@ -596,6 +666,28 @@ bool DLink::setup_multicast(const QString &recieveIP, qint32 recievePort,
|
||||
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))
|
||||
@@ -613,6 +705,7 @@ bool DLink::setup_remote(const QString port, qint32 baud, QSerialPort::Parity pa
|
||||
node.baud = baud;
|
||||
node.parity = parity;
|
||||
node.name = port;
|
||||
node.type = devtype::remote;
|
||||
|
||||
serials.insert(serials.size(),node);
|
||||
}
|
||||
@@ -627,6 +720,29 @@ bool DLink::setup_remote(const QString port, qint32 baud, QSerialPort::Parity pa
|
||||
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))
|
||||
@@ -644,6 +760,7 @@ bool DLink::setup_rtk(const QString port, qint32 baudrate, QSerialPort::Parity p
|
||||
node.baud = baudrate;
|
||||
node.parity = parity;
|
||||
node.name = port;
|
||||
node.type = devtype::rtk;
|
||||
|
||||
serials.insert(serials.size(),node);
|
||||
}
|
||||
|
||||
+21
-2
@@ -27,7 +27,8 @@ class DLink : public QObject
|
||||
ganeral = 0,
|
||||
datalink,
|
||||
remote,
|
||||
rtk
|
||||
rtk,
|
||||
video
|
||||
};
|
||||
|
||||
struct clientNode{
|
||||
@@ -35,6 +36,15 @@ class DLink : public QObject
|
||||
QHostAddress addr;
|
||||
quint16 port;
|
||||
devtype type;
|
||||
|
||||
bool operator==(const clientNode& other)
|
||||
{
|
||||
if (this->sock == other.sock)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
struct serialNode{
|
||||
@@ -43,6 +53,15 @@ class DLink : public QObject
|
||||
qint32 baud;
|
||||
QSerialPort::Parity parity;
|
||||
devtype type;
|
||||
|
||||
bool operator==(const serialNode& other)
|
||||
{
|
||||
if (this->name == other.name)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -54,7 +73,7 @@ public:
|
||||
//bool statesPort(const QString name);
|
||||
//void stopPort(const QString name);
|
||||
|
||||
bool PortSetup(const QString port, qint32 baud, QSerialPort::Parity parity, const char *method);
|
||||
bool PortSetup(devtype type,const QString port, qint32 baud, QSerialPort::Parity parity, const char *method);
|
||||
|
||||
bool PortState(const QString name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user