Browse Source

Preparations for rate limiting between ModbusNG scans

Neal Wilson 8 years ago
parent
commit
ae28f67403
1 changed files with 19 additions and 2 deletions
  1. 19
    2
      src/modbus.w

+ 19
- 2
src/modbus.w View File

@@ -376,12 +376,14 @@ class ModbusNG : public QObject
376 376
         void sendNextMessage();
377 377
         void timeout();
378 378
         void dataAvailable();
379
+        void rateLimitTimeout();
379 380
     private:
380 381
         quint16 calculateCRC(QByteArray data);
381 382
         QextSerialPort *port;
382 383
         int delayTime;
383 384
         QTimer *messageDelayTimer;
384 385
         QTimer *commTimeout;
386
+        QTimer *rateLimiter;
385 387
         int scanPosition;
386 388
         bool waiting;
387 389
         QByteArray responseBuffer;
@@ -400,7 +402,7 @@ sub-tree. In this design, child nodes establish a scan list.
400 402
 @<ModbusNG implementation@>=
401 403
 ModbusNG::ModbusNG(DeviceTreeModel *model, const QModelIndex &index) :
402 404
     QObject(NULL), messageDelayTimer(new QTimer), commTimeout(new QTimer),
403
-    scanPosition(0), waiting(false)
405
+    rateLimiter(new QTimer), scanPosition(0), waiting(false)
404 406
 {
405 407
     QDomElement portReferenceElement =
406 408
         model->referenceElement(model->data(index, Qt::UserRole).toString());
@@ -422,9 +424,12 @@ ModbusNG::ModbusNG(DeviceTreeModel *model, const QModelIndex &index) :
422 424
     delayTime = (int)(((double)(1)/(double)(attributes.value("baud").toInt())) * 144000.0);
423 425
     messageDelayTimer->setSingleShot(true);
424 426
     commTimeout->setSingleShot(true);
427
+    rateLimiter->setSingleShot(true);
428
+    rateLimiter->setInterval(0);
425 429
     connect(messageDelayTimer, SIGNAL(timeout()), this, SLOT(sendNextMessage()));
426 430
     connect(commTimeout, SIGNAL(timeout()), this, SLOT(timeout()));
427 431
     connect(port, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
432
+    connect(rateLimiter, SIGNAL(timeout()), this, SLOT(rateLimitTimeout()));
428 433
     if(!port->open(QIODevice::ReadWrite))
429 434
     {
430 435
 	    qDebug() << "Failed to open serial port";
@@ -527,6 +532,11 @@ void ModbusNG::timeout()
527 532
     messageDelayTimer->start();
528 533
 }
529 534
 
535
+void ModbusNG::rateLimitTimeout()
536
+{
537
+	messageDelayTimer->start();
538
+}
539
+
530 540
 void ModbusNG::dataAvailable()
531 541
 {
532 542
     if(messageDelayTimer->isActive())
@@ -605,7 +615,14 @@ void ModbusNG::dataAvailable()
605 615
     }
606 616
     responseBuffer.clear();
607 617
     waiting = false;
608
-    messageDelayTimer->start(delayTime);
618
+    if(scanPosition == 0)
619
+    {
620
+	    rateLimiter->start();
621
+    }
622
+    else
623
+    {
624
+	    messageDelayTimer->start(delayTime);
625
+    }
609 626
 }
610 627
 
611 628
 quint16 ModbusNG::calculateCRC(QByteArray data)

Loading…
Cancel
Save