From c296491149324c88146bdf094362405c9baaeb15 Mon Sep 17 00:00:00 2001 From: Fred Gleason Date: Thu, 18 May 2017 17:40:36 -0400 Subject: [PATCH] 2017-05-18 Fred Gleason * Added a 1/10 second hold-off between input polling calls to the Modbus driver in 'ripcd/modbus.cpp' and 'ripcd/modbus.h'. --- ChangeLog | 3 +++ ripcd/modbus.cpp | 63 +++++++++++++++++++++++++----------------------- ripcd/modbus.h | 4 ++- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/ChangeLog b/ChangeLog index fe28360e..f0096f2e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -15791,3 +15791,6 @@ * Added code to create and remove LOG_MODES table records when adding and removing Host definitions. * Incremented the database version to 263. +2017-05-18 Fred Gleason + * Added a 1/10 second hold-off between input polling calls to the + Modbus driver in 'ripcd/modbus.cpp' and 'ripcd/modbus.h'. diff --git a/ripcd/modbus.cpp b/ripcd/modbus.cpp index 35b21b18..f5d3be7e 100644 --- a/ripcd/modbus.cpp +++ b/ripcd/modbus.cpp @@ -46,6 +46,9 @@ Modbus::Modbus(RDMatrix *matrix,QObject *parent) connect(modbus_socket,SIGNAL(error(int)),this,SLOT(errorData(int))); modbus_socket->connectToHost(modbus_ip_address.toString(),modbus_ip_port); + modbus_poll_timer=new QTimer(this); + connect(modbus_poll_timer,SIGNAL(timeout()),this,SLOT(pollInputs())); + modbus_watchdog_timer=new QTimer(this); connect(modbus_watchdog_timer,SIGNAL(timeout()),this,SLOT(watchdogData())); } @@ -99,7 +102,7 @@ void Modbus::connectedData() "connection to Modbus device at %s:%u established", (const char *)modbus_ip_address.toString(),0xffff&modbus_ip_port); modbus_watchdog_active=false; - PollInputs(); + pollInputs(); } @@ -176,7 +179,7 @@ void Modbus::readyReadData() base=modbus_input_bytes-count; ProcessInputByte(byte,base); if(--count==0) { - PollInputs(); + modbus_poll_timer->start(MODBUS_POLL_INTERVAL,true); modbus_istate=0; } break; @@ -192,6 +195,34 @@ void Modbus::errorData(int err) } +void Modbus::pollInputs() +{ + char msg[12]; + msg[0]=0x88; // Transaction Identifier + msg[1]=0x88; + + msg[2]=0x00; // Protocol Identifier + msg[3]=0x00; + + msg[4]=0x00; // Message Length + msg[5]=0x06; + + msg[6]=0x01; // Modbus ID + + msg[7]=0x02; // Function Code (Read Discrete Input) + + msg[8]=0x00; // Starting Address + msg[9]=0x00; + + msg[10]=0xff&(modbus_gpis>>8); // Quantity of Inputs + msg[11]=0xff&modbus_gpis; + + modbus_socket->writeBlock(msg,12); + modbus_watchdog_timer->stop(); + modbus_watchdog_timer->start(MODBUS_WATCHDOG_INTERVAL,true); +} + + void Modbus::watchdogData() { if(!modbus_watchdog_active) { @@ -218,31 +249,3 @@ void Modbus::ProcessInputByte(char byte,int base) } modbus_input_states[base]=byte; } - - -void Modbus::PollInputs() -{ - char msg[12]; - msg[0]=0x88; // Transaction Identifier - msg[1]=0x88; - - msg[2]=0x00; // Protocol Identifier - msg[3]=0x00; - - msg[4]=0x00; // Message Length - msg[5]=0x06; - - msg[6]=0x01; // Modbus ID - - msg[7]=0x02; // Function Code (Read Discrete Input) - - msg[8]=0x00; // Starting Address - msg[9]=0x00; - - msg[10]=0xff&(modbus_gpis>>8); // Quantity of Inputs - msg[11]=0xff&modbus_gpis; - - modbus_socket->writeBlock(msg,12); - modbus_watchdog_timer->stop(); - modbus_watchdog_timer->start(MODBUS_WATCHDOG_INTERVAL,true); -} diff --git a/ripcd/modbus.h b/ripcd/modbus.h index e379219c..869c2659 100644 --- a/ripcd/modbus.h +++ b/ripcd/modbus.h @@ -32,6 +32,7 @@ #include +#define MODBUS_POLL_INTERVAL 100 #define MODBUS_WATCHDOG_INTERVAL 1000 class Modbus : public Switcher @@ -51,15 +52,16 @@ class Modbus : public Switcher void connectedData(); void readyReadData(); void errorData(int err); + void pollInputs(); void watchdogData(); private: void ProcessInputByte(char byte,int base); - void PollInputs(); int modbus_istate; int modbus_input_bytes; std::vector modbus_input_states; QSocket *modbus_socket; + QTimer *modbus_poll_timer; QTimer *modbus_watchdog_timer; bool modbus_watchdog_active; QHostAddress modbus_ip_address;