88ModbusMultiServer::ModbusMultiServer (QObject *parent)
99 : QObject{parent}
1010 ,_deviceId(1 )
11+ ,_workerThread(new QThread(this ))
1112{
13+ moveToThread (_workerThread);
14+ _workerThread->start ();
15+
16+ connect (this , &QObject::destroyed, _workerThread, &QThread::quit);
1217}
1318
1419// /
1520// / \brief ModbusServer::~ModbusServer
1621// /
1722ModbusMultiServer::~ModbusMultiServer ()
1823{
19- for (auto && s : _modbusServerList)
20- s->disconnectDevice ();
24+ closeConnections ();
25+ if (_workerThread && _workerThread->isRunning ())
26+ {
27+ _workerThread->quit ();
28+ _workerThread->wait ();
29+ }
2130}
2231
2332// /
@@ -38,6 +47,14 @@ void ModbusMultiServer::setDeviceId(quint8 deviceId)
3847 if (deviceId == _deviceId)
3948 return ;
4049
50+ if (QThread::currentThread () != _workerThread)
51+ {
52+ QMetaObject::invokeMethod (this , [this , deviceId]() {
53+ setDeviceId (deviceId);
54+ }, Qt::BlockingQueuedConnection);
55+ return ;
56+ }
57+
4158 _deviceId = deviceId;
4259
4360 for (auto && s : _modbusServerList)
@@ -64,7 +81,15 @@ bool ModbusMultiServer::isBusy() const
6481// /
6582void ModbusMultiServer::setBusy (bool busy)
6683{
67- for (auto && s : _modbusServerList)
84+ if (QThread::currentThread () != _workerThread)
85+ {
86+ QMetaObject::invokeMethod (this , [this , busy]() {
87+ setBusy (busy);
88+ }, Qt::BlockingQueuedConnection);
89+ return ;
90+ }
91+
92+ for (auto && s : _modbusServerList)
6893 s->setValue (QModbusServer::DeviceBusy, busy ? 0xffff : 0x0000 );
6994}
7095
@@ -83,6 +108,14 @@ bool ModbusMultiServer::useGlobalUnitMap() const
83108// /
84109void ModbusMultiServer::setUseGlobalUnitMap (bool use)
85110{
111+ if (QThread::currentThread () != _workerThread)
112+ {
113+ QMetaObject::invokeMethod (this , [this , use]() {
114+ setUseGlobalUnitMap (use);
115+ }, Qt::BlockingQueuedConnection);
116+ return ;
117+ }
118+
86119 _modbusDataUnitMap.setGlobalMap (use);
87120 reconfigureServers ();
88121}
@@ -96,6 +129,14 @@ void ModbusMultiServer::setUseGlobalUnitMap(bool use)
96129// /
97130void ModbusMultiServer::addUnitMap (int id, QModbusDataUnit::RegisterType pointType, quint16 pointAddress, quint16 length)
98131{
132+ if (QThread::currentThread () != _workerThread)
133+ {
134+ QMetaObject::invokeMethod (this , [this , id, pointType, pointAddress, length]() {
135+ addUnitMap (id, pointType, pointAddress, length);
136+ }, Qt::BlockingQueuedConnection);
137+ return ;
138+ }
139+
99140 _modbusDataUnitMap.addUnitMap (id, pointType, pointAddress, length);
100141 reconfigureServers ();
101142}
@@ -106,6 +147,14 @@ void ModbusMultiServer::addUnitMap(int id, QModbusDataUnit::RegisterType pointTy
106147// /
107148void ModbusMultiServer::removeUnitMap (int id)
108149{
150+ if (QThread::currentThread () != _workerThread)
151+ {
152+ QMetaObject::invokeMethod (this , [this , id]() {
153+ removeUnitMap (id);
154+ }, Qt::BlockingQueuedConnection);
155+ return ;
156+ }
157+
109158 _modbusDataUnitMap.removeUnitMap (id);
110159 reconfigureServers ();
111160}
@@ -152,14 +201,24 @@ QSharedPointer<QModbusServer> ModbusMultiServer::findModbusServer(ConnectionType
152201// /
153202QSharedPointer<QModbusServer> ModbusMultiServer::createModbusServer (const ConnectionDetails& cd)
154203{
204+ if (QThread::currentThread () != _workerThread)
205+ {
206+ QSharedPointer<QModbusServer> result;
207+ QMetaObject::invokeMethod (this , [&]() {
208+ result = createModbusServer (cd);
209+ }, Qt::BlockingQueuedConnection);
210+
211+ return result;
212+ }
213+
155214 auto modbusServer = findModbusServer (cd);
156215 if (modbusServer == nullptr )
157216 {
158217 switch (cd.Type )
159218 {
160219 case ConnectionType::Tcp:
161220 {
162- modbusServer = QSharedPointer<QModbusServer>(new ModbusTcpServer (this ));
221+ modbusServer = QSharedPointer<QModbusServer>(new ModbusTcpServer ());
163222 modbusServer->setProperty (" ConnectionDetails" , QVariant::fromValue (cd));
164223 modbusServer->setConnectionParameter (QModbusDevice::NetworkPortParameter, cd.TcpParams .ServicePort );
165224 modbusServer->setConnectionParameter (QModbusDevice::NetworkAddressParameter, cd.TcpParams .IPAddress );
@@ -217,6 +276,14 @@ QSharedPointer<QModbusServer> ModbusMultiServer::createModbusServer(const Connec
217276// /
218277void ModbusMultiServer::connectDevice (const ConnectionDetails& cd)
219278{
279+ if (QThread::currentThread () != _workerThread)
280+ {
281+ QMetaObject::invokeMethod (this , [this , cd]() {
282+ connectDevice (cd);
283+ }, Qt::BlockingQueuedConnection);
284+ return ;
285+ }
286+
220287 auto modbusServer = findModbusServer (cd);
221288 if (modbusServer == nullptr )
222289 {
@@ -243,6 +310,14 @@ void ModbusMultiServer::connectDevice(const ConnectionDetails& cd)
243310// /
244311void ModbusMultiServer::disconnectDevice (ConnectionType type, const QString& port)
245312{
313+ if (QThread::currentThread () != _workerThread)
314+ {
315+ QMetaObject::invokeMethod (this , [this , type, port]() {
316+ disconnectDevice (type, port);
317+ }, Qt::BlockingQueuedConnection);
318+ return ;
319+ }
320+
246321 auto modbusServer = findModbusServer (type, port);
247322 if (modbusServer != nullptr )
248323 {
@@ -251,6 +326,23 @@ void ModbusMultiServer::disconnectDevice(ConnectionType type, const QString& por
251326 }
252327}
253328
329+ // /
330+ // / \brief ModbusMultiServer::closeConnections
331+ // /
332+ void ModbusMultiServer::closeConnections ()
333+ {
334+ if (QThread::currentThread () != _workerThread)
335+ {
336+ QMetaObject::invokeMethod (this , [this ]() {
337+ closeConnections ();
338+ }, Qt::BlockingQueuedConnection);
339+ return ;
340+ }
341+
342+ for (auto && s : _modbusServerList)
343+ s->disconnectDevice ();
344+ }
345+
254346// /
255347// / \brief ModbusMultiServer::connections
256348// / \return
@@ -291,6 +383,14 @@ void ModbusMultiServer::removeModbusServer(QSharedPointer<QModbusServer> server)
291383// /
292384void ModbusMultiServer::reconfigureServers ()
293385{
386+ if (QThread::currentThread () != _workerThread)
387+ {
388+ QMetaObject::invokeMethod (this , [this ]() {
389+ reconfigureServers ();
390+ }, Qt::BlockingQueuedConnection);
391+ return ;
392+ }
393+
294394 for (auto && s : _modbusServerList)
295395 s->setMap (_modbusDataUnitMap);
296396}
@@ -347,6 +447,14 @@ QModbusDataUnit ModbusMultiServer::data(QModbusDataUnit::RegisterType pointType,
347447// /
348448void ModbusMultiServer::setData (const QModbusDataUnit& data)
349449{
450+ if (QThread::currentThread () != _workerThread)
451+ {
452+ QMetaObject::invokeMethod (this , [this , data]() {
453+ setData (data);
454+ }, Qt::BlockingQueuedConnection);
455+ return ;
456+ }
457+
350458 _modbusDataUnitMap.setData (data);
351459 for (auto && s : _modbusServerList)
352460 {
0 commit comments