添加 初始化失败的重试功能
This commit is contained in:
@@ -108,21 +108,22 @@ QString Hd_SocketModule_ModbusTcp::ShortToHex(int value)
|
||||
Hd_SocketModule_ModbusTcp::Hd_SocketModule_ModbusTcp()
|
||||
{
|
||||
m_Mutex = new QMutex;
|
||||
m_Thread = new MyThread(this);
|
||||
m_Thread->setMutexLock(m_Mutex);
|
||||
//m_Thread = new MyThread(this);
|
||||
m_Thread.setMutexLock(m_Mutex);
|
||||
}
|
||||
|
||||
Hd_SocketModule_ModbusTcp::~Hd_SocketModule_ModbusTcp()
|
||||
{
|
||||
m_Thread->setRunStop();
|
||||
m_Thread->wait();
|
||||
m_Thread->quit();
|
||||
if (m_Thread)
|
||||
{
|
||||
qDebug() << "[INFO] "<< "~m_Thread()";
|
||||
delete m_Thread;
|
||||
m_Thread = nullptr;
|
||||
}
|
||||
m_Thread.setRunStop();
|
||||
m_Thread.wait();
|
||||
m_Thread.quit();
|
||||
m_Thread.terminate();
|
||||
//if (m_Thread)
|
||||
//{
|
||||
// qDebug() << "[INFO] "<< "~m_Thread()";
|
||||
// delete m_Thread;
|
||||
// m_Thread = nullptr;
|
||||
//}
|
||||
|
||||
qDebug() << "[INFO] " << "~Hd_SocketModule_ModbusTcp success!";
|
||||
}
|
||||
@@ -395,8 +396,8 @@ bool Hd_SocketModule_ModbusTcp::initParas(QByteArray& data)
|
||||
|
||||
qDebug() << "[TCP] " << "list: " << list;
|
||||
setSleepTimeAndReadNum(sleepvalue.toInt(), readvalue.toInt());
|
||||
m_Thread->setSocketIp(m_Station);
|
||||
if (!m_Thread->setIpAndPort(m_IP, m_host))
|
||||
m_Thread.setSocketIp(m_Station);
|
||||
if (!m_Thread.setIpAndPort(m_IP, m_host))
|
||||
{
|
||||
qDebug() << "[TCP] " << "m_IP:" << m_IP << " m_host:" << m_host << " connectToHost failed!";
|
||||
return false;
|
||||
@@ -406,13 +407,17 @@ bool Hd_SocketModule_ModbusTcp::initParas(QByteArray& data)
|
||||
//开启读PLC数据的客户端
|
||||
//开启socket
|
||||
//第一次运行,创建socket对象,后面发一次握手报文
|
||||
m_Thread->start();
|
||||
Sleep(500);
|
||||
|
||||
if (!m_Thread->getInitSuccessFlag())
|
||||
m_Thread.start();
|
||||
int cnt = 0;
|
||||
while (!m_Thread.getInitSuccessFlag())
|
||||
{
|
||||
qCritical() << "[TCP] " << "TCP init failed!";
|
||||
return false;
|
||||
QThread::msleep(100);
|
||||
cnt++;
|
||||
if(cnt>50)
|
||||
{
|
||||
qCritical() << "[TCP] " << "TCP init failed!";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//// 发送握手指令
|
||||
@@ -421,7 +426,7 @@ bool Hd_SocketModule_ModbusTcp::initParas(QByteArray& data)
|
||||
//WritePLCPack.append(array);
|
||||
|
||||
// 开启线程循环
|
||||
m_Thread->start();
|
||||
m_Thread.start();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -434,7 +439,7 @@ bool Hd_SocketModule_ModbusTcp::run()
|
||||
void Hd_SocketModule_ModbusTcp::setSleepTimeAndReadNum(int time, int num)
|
||||
{
|
||||
m_sleepTime = time;
|
||||
m_Thread->setReadPackValue(num);
|
||||
m_Thread.setReadPackValue(num);
|
||||
}
|
||||
////写单个寄存器指令,封装为报文
|
||||
//bool Hd_SocketModule_ModbusTcp::writeCommond(QString DM, QString Num)
|
||||
@@ -923,7 +928,6 @@ MyThread::~MyThread()
|
||||
delete m_mutex_lock;
|
||||
m_mutex_lock = nullptr;
|
||||
}
|
||||
std::exit(EXIT_SUCCESS);
|
||||
|
||||
}
|
||||
|
||||
@@ -998,6 +1002,7 @@ void MyThread::run()
|
||||
if (rtn == HDTcpStatus::Success)
|
||||
{
|
||||
qWarning() << "[TCP] " << " You have connected to the server ";
|
||||
init_success = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user