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
         void sendNextMessage();
376
         void sendNextMessage();
377
         void timeout();
377
         void timeout();
378
         void dataAvailable();
378
         void dataAvailable();
379
+        void rateLimitTimeout();
379
     private:
380
     private:
380
         quint16 calculateCRC(QByteArray data);
381
         quint16 calculateCRC(QByteArray data);
381
         QextSerialPort *port;
382
         QextSerialPort *port;
382
         int delayTime;
383
         int delayTime;
383
         QTimer *messageDelayTimer;
384
         QTimer *messageDelayTimer;
384
         QTimer *commTimeout;
385
         QTimer *commTimeout;
386
+        QTimer *rateLimiter;
385
         int scanPosition;
387
         int scanPosition;
386
         bool waiting;
388
         bool waiting;
387
         QByteArray responseBuffer;
389
         QByteArray responseBuffer;
400
 @<ModbusNG implementation@>=
402
 @<ModbusNG implementation@>=
401
 ModbusNG::ModbusNG(DeviceTreeModel *model, const QModelIndex &index) :
403
 ModbusNG::ModbusNG(DeviceTreeModel *model, const QModelIndex &index) :
402
     QObject(NULL), messageDelayTimer(new QTimer), commTimeout(new QTimer),
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
     QDomElement portReferenceElement =
407
     QDomElement portReferenceElement =
406
         model->referenceElement(model->data(index, Qt::UserRole).toString());
408
         model->referenceElement(model->data(index, Qt::UserRole).toString());
422
     delayTime = (int)(((double)(1)/(double)(attributes.value("baud").toInt())) * 144000.0);
424
     delayTime = (int)(((double)(1)/(double)(attributes.value("baud").toInt())) * 144000.0);
423
     messageDelayTimer->setSingleShot(true);
425
     messageDelayTimer->setSingleShot(true);
424
     commTimeout->setSingleShot(true);
426
     commTimeout->setSingleShot(true);
427
+    rateLimiter->setSingleShot(true);
428
+    rateLimiter->setInterval(0);
425
     connect(messageDelayTimer, SIGNAL(timeout()), this, SLOT(sendNextMessage()));
429
     connect(messageDelayTimer, SIGNAL(timeout()), this, SLOT(sendNextMessage()));
426
     connect(commTimeout, SIGNAL(timeout()), this, SLOT(timeout()));
430
     connect(commTimeout, SIGNAL(timeout()), this, SLOT(timeout()));
427
     connect(port, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
431
     connect(port, SIGNAL(readyRead()), this, SLOT(dataAvailable()));
432
+    connect(rateLimiter, SIGNAL(timeout()), this, SLOT(rateLimitTimeout()));
428
     if(!port->open(QIODevice::ReadWrite))
433
     if(!port->open(QIODevice::ReadWrite))
429
     {
434
     {
430
 	    qDebug() << "Failed to open serial port";
435
 	    qDebug() << "Failed to open serial port";
527
     messageDelayTimer->start();
532
     messageDelayTimer->start();
528
 }
533
 }
529
 
534
 
535
+void ModbusNG::rateLimitTimeout()
536
+{
537
+	messageDelayTimer->start();
538
+}
539
+
530
 void ModbusNG::dataAvailable()
540
 void ModbusNG::dataAvailable()
531
 {
541
 {
532
     if(messageDelayTimer->isActive())
542
     if(messageDelayTimer->isActive())
605
     }
615
     }
606
     responseBuffer.clear();
616
     responseBuffer.clear();
607
     waiting = false;
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
 quint16 ModbusNG::calculateCRC(QByteArray data)
628
 quint16 ModbusNG::calculateCRC(QByteArray data)

Loading…
Cancel
Save