mirror of
https://github.com/ElvishArtisan/rivendell.git
synced 2025-10-10 16:43:35 +02:00
2017-05-19 Fred Gleason <fredg@paravelsystems.com>
* Added support for the 'GPO Set' ['GO'] RML to the Modbus driver in 'ripcd/modbus.cpp' and 'ripcd/modbus.h'.
This commit is contained in:
@@ -15794,3 +15794,6 @@
|
|||||||
2017-05-18 Fred Gleason <fredg@paravelsystems.com>
|
2017-05-18 Fred Gleason <fredg@paravelsystems.com>
|
||||||
* Added a 1/10 second hold-off between input polling calls to the
|
* Added a 1/10 second hold-off between input polling calls to the
|
||||||
Modbus driver in 'ripcd/modbus.cpp' and 'ripcd/modbus.h'.
|
Modbus driver in 'ripcd/modbus.cpp' and 'ripcd/modbus.h'.
|
||||||
|
2017-05-19 Fred Gleason <fredg@paravelsystems.com>
|
||||||
|
* Added support for the 'GPO Set' ['GO'] RML to the Modbus driver
|
||||||
|
in 'ripcd/modbus.cpp' and 'ripcd/modbus.h'.
|
||||||
|
@@ -587,6 +587,7 @@ Driver Name: Modbus TCP
|
|||||||
Supported RML Commands:
|
Supported RML Commands:
|
||||||
GPI Enable ('GE')
|
GPI Enable ('GE')
|
||||||
GPI Set ('GI')
|
GPI Set ('GI')
|
||||||
|
GPO Set ('GO')
|
||||||
|
|
||||||
GENERAL NOTES:
|
GENERAL NOTES:
|
||||||
This driver supports GPI devices that use the industry-standard Modbus TCP
|
This driver supports GPI devices that use the industry-standard Modbus TCP
|
||||||
|
179
ripcd/modbus.cpp
179
ripcd/modbus.cpp
@@ -27,6 +27,7 @@ Modbus::Modbus(RDMatrix *matrix,QObject *parent)
|
|||||||
{
|
{
|
||||||
modbus_istate=0;
|
modbus_istate=0;
|
||||||
modbus_watchdog_active=false;
|
modbus_watchdog_active=false;
|
||||||
|
modbus_busy=false;
|
||||||
|
|
||||||
modbus_gpis=matrix->gpis();
|
modbus_gpis=matrix->gpis();
|
||||||
modbus_input_bytes=modbus_gpis/8;
|
modbus_input_bytes=modbus_gpis/8;
|
||||||
@@ -49,6 +50,17 @@ Modbus::Modbus(RDMatrix *matrix,QObject *parent)
|
|||||||
modbus_poll_timer=new QTimer(this);
|
modbus_poll_timer=new QTimer(this);
|
||||||
connect(modbus_poll_timer,SIGNAL(timeout()),this,SLOT(pollInputs()));
|
connect(modbus_poll_timer,SIGNAL(timeout()),this,SLOT(pollInputs()));
|
||||||
|
|
||||||
|
modbus_reset_mapper=new QSignalMapper(this);
|
||||||
|
connect(modbus_reset_mapper,SIGNAL(mapped(int)),
|
||||||
|
this,SLOT(resetStateData(int)));
|
||||||
|
for(int i=0;i<modbus_gpos;i++) {
|
||||||
|
modbus_reset_timers.push_back(new QTimer(this));
|
||||||
|
connect(modbus_reset_timers.back(),SIGNAL(timeout()),
|
||||||
|
modbus_reset_mapper,SLOT(map()));
|
||||||
|
modbus_reset_mapper->setMapping(modbus_reset_timers.back(),i);
|
||||||
|
modbus_reset_states.push_back(false);
|
||||||
|
}
|
||||||
|
|
||||||
modbus_watchdog_timer=new QTimer(this);
|
modbus_watchdog_timer=new QTimer(this);
|
||||||
connect(modbus_watchdog_timer,SIGNAL(timeout()),this,SLOT(watchdogData()));
|
connect(modbus_watchdog_timer,SIGNAL(timeout()),this,SLOT(watchdogData()));
|
||||||
}
|
}
|
||||||
@@ -57,6 +69,11 @@ Modbus::Modbus(RDMatrix *matrix,QObject *parent)
|
|||||||
Modbus::~Modbus()
|
Modbus::~Modbus()
|
||||||
{
|
{
|
||||||
delete modbus_watchdog_timer;
|
delete modbus_watchdog_timer;
|
||||||
|
delete modbus_poll_timer;
|
||||||
|
for(unsigned i=0;i<modbus_reset_timers.size();i++) {
|
||||||
|
delete modbus_reset_timers[i];
|
||||||
|
}
|
||||||
|
delete modbus_reset_mapper;
|
||||||
delete modbus_socket;
|
delete modbus_socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +110,65 @@ bool Modbus::secondaryTtyActive()
|
|||||||
|
|
||||||
void Modbus::processCommand(RDMacro *cmd)
|
void Modbus::processCommand(RDMacro *cmd)
|
||||||
{
|
{
|
||||||
|
switch(cmd->command()) {
|
||||||
|
case RDMacro::GO:
|
||||||
|
if((cmd->argQuantity()!=5)||
|
||||||
|
((cmd->arg(1).toString().lower()!="i")&&
|
||||||
|
(cmd->arg(1).toString().lower()!="o"))||
|
||||||
|
(cmd->arg(2).toInt()<1)||(cmd->arg(3).toInt()>modbus_gpos)||
|
||||||
|
(cmd->arg(2).toInt()>modbus_gpos)||
|
||||||
|
((cmd->arg(3).toInt()!=1)&&(cmd->arg(3).toInt()!=0)&&
|
||||||
|
(cmd->arg(1).toString().lower()!="i"))||
|
||||||
|
((cmd->arg(3).toInt()!=1)&&(cmd->arg(3).toInt()!=0)&&
|
||||||
|
(cmd->arg(3).toInt()!=-1)&&(cmd->arg(1).toString().lower()=="i"))||
|
||||||
|
(cmd->arg(4).toInt()<0)) {
|
||||||
|
cmd->acknowledge(false);
|
||||||
|
emit rmlEcho(cmd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(cmd->arg(3).toInt()==0) { // Turn OFF
|
||||||
|
if(cmd->arg(4).toInt()==0) {
|
||||||
|
if(cmd->arg(1).toString().lower()=="o") {
|
||||||
|
SetCoil(cmd->arg(2).toInt()-1,false);
|
||||||
|
emit gpoChanged(matrixNumber(),cmd->arg(2).toInt()-1,false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(cmd->echoRequested()) {
|
||||||
|
cmd->acknowledge(false);
|
||||||
|
emit rmlEcho(cmd);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(cmd->arg(4).toInt()==0) { // Turn ON
|
||||||
|
if(cmd->arg(1).toString().lower()=="o") {
|
||||||
|
SetCoil(cmd->arg(2).toInt()-1,true);
|
||||||
|
emit gpoChanged(matrixNumber(),cmd->arg(2).toInt()-1,true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { // Pulse
|
||||||
|
if(cmd->arg(1).toString().lower()=="o") {
|
||||||
|
SetCoil(cmd->arg(2).toInt()-1,cmd->arg(3).toInt()!=0);
|
||||||
|
modbus_reset_states[cmd->arg(2).toInt()-1]=cmd->arg(3).toInt()==0;
|
||||||
|
modbus_reset_timers[cmd->arg(2).toInt()-1]->
|
||||||
|
start(cmd->arg(4).toInt(),true);
|
||||||
|
emit gpoChanged(matrixNumber(),cmd->arg(2).toInt()-1,
|
||||||
|
cmd->arg(3).toInt()!=0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cmd->echoRequested()) {
|
||||||
|
cmd->acknowledge(true);
|
||||||
|
emit rmlEcho(cmd);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -112,12 +188,16 @@ void Modbus::readyReadData()
|
|||||||
int n;
|
int n;
|
||||||
char byte;
|
char byte;
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
// char id;
|
|
||||||
char count=0;
|
char count=0;
|
||||||
int base=0;
|
int base=0;
|
||||||
|
|
||||||
|
modbus_watchdog_timer->stop();
|
||||||
|
modbus_watchdog_timer->start(MODBUS_WATCHDOG_INTERVAL,true);
|
||||||
|
|
||||||
while((n=modbus_socket->readBlock(data,1500))>0) {
|
while((n=modbus_socket->readBlock(data,1500))>0) {
|
||||||
|
// logBytes((uint8_t *)data,n);
|
||||||
for(int i=0;i<n;i++) {
|
for(int i=0;i<n;i++) {
|
||||||
|
printf("istate: %d\n",modbus_istate);
|
||||||
byte=0xff&data[i];
|
byte=0xff&data[i];
|
||||||
switch(modbus_istate) {
|
switch(modbus_istate) {
|
||||||
case 0: // Transaction Identifier
|
case 0: // Transaction Identifier
|
||||||
@@ -157,16 +237,20 @@ void Modbus::readyReadData()
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 6: // Modbus ID
|
case 6: // Modbus ID
|
||||||
// id=byte;
|
|
||||||
modbus_istate=7;
|
modbus_istate=7;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 7: // Function Code
|
case 7: // Function Code
|
||||||
if(byte==0x02) {
|
if(byte==0x02) { // Read Inputs
|
||||||
modbus_istate=8;
|
modbus_istate=8;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
modbus_istate=0;
|
if(byte==0x05) { // Set Coil
|
||||||
|
modbus_istate=100;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
modbus_istate=0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -181,6 +265,34 @@ void Modbus::readyReadData()
|
|||||||
if(--count==0) {
|
if(--count==0) {
|
||||||
modbus_poll_timer->start(MODBUS_POLL_INTERVAL,true);
|
modbus_poll_timer->start(MODBUS_POLL_INTERVAL,true);
|
||||||
modbus_istate=0;
|
modbus_istate=0;
|
||||||
|
modbus_busy=false;
|
||||||
|
if(modbus_coil_lines.size()>0) {
|
||||||
|
SetCoil(modbus_coil_lines.front(),modbus_coil_states.front());
|
||||||
|
modbus_coil_lines.pop();
|
||||||
|
modbus_coil_states.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 100: // Output Address
|
||||||
|
modbus_istate=101;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 101:
|
||||||
|
modbus_istate=102;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 102: // Output Value
|
||||||
|
modbus_istate=103;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 103:
|
||||||
|
modbus_istate=0;
|
||||||
|
modbus_busy=false;
|
||||||
|
if(modbus_coil_lines.size()>0) {
|
||||||
|
SetCoil(modbus_coil_lines.front(),modbus_coil_states.front());
|
||||||
|
modbus_coil_lines.pop();
|
||||||
|
modbus_coil_states.pop();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -197,6 +309,11 @@ void Modbus::errorData(int err)
|
|||||||
|
|
||||||
void Modbus::pollInputs()
|
void Modbus::pollInputs()
|
||||||
{
|
{
|
||||||
|
if(modbus_busy) {
|
||||||
|
modbus_poll_timer->start(MODBUS_POLL_INTERVAL,true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
char msg[12];
|
char msg[12];
|
||||||
msg[0]=0x88; // Transaction Identifier
|
msg[0]=0x88; // Transaction Identifier
|
||||||
msg[1]=0x88;
|
msg[1]=0x88;
|
||||||
@@ -218,8 +335,14 @@ void Modbus::pollInputs()
|
|||||||
msg[11]=0xff&modbus_gpis;
|
msg[11]=0xff&modbus_gpis;
|
||||||
|
|
||||||
modbus_socket->writeBlock(msg,12);
|
modbus_socket->writeBlock(msg,12);
|
||||||
modbus_watchdog_timer->stop();
|
modbus_busy=true;
|
||||||
modbus_watchdog_timer->start(MODBUS_WATCHDOG_INTERVAL,true);
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Modbus::resetStateData(int line)
|
||||||
|
{
|
||||||
|
SetCoil(line,modbus_reset_states[line]);
|
||||||
|
emit gpoChanged(matrixNumber(),line,modbus_reset_states[line]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -249,3 +372,47 @@ void Modbus::ProcessInputByte(char byte,int base)
|
|||||||
}
|
}
|
||||||
modbus_input_states[base]=byte;
|
modbus_input_states[base]=byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Modbus::SetCoil(int line,bool state)
|
||||||
|
{
|
||||||
|
// printf("SetCoil(%d,%d)\n",line,state);
|
||||||
|
if(modbus_busy) {
|
||||||
|
modbus_coil_lines.push(line);
|
||||||
|
modbus_coil_states.push(state);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
modbus_busy=true;
|
||||||
|
|
||||||
|
char msg[12];
|
||||||
|
|
||||||
|
msg[0]=0x44; // Transaction Identifier
|
||||||
|
msg[1]=0x44;
|
||||||
|
|
||||||
|
msg[2]=0x00; // Protocol Identifier
|
||||||
|
msg[3]=0x00;
|
||||||
|
|
||||||
|
msg[4]=0x00; // Message Length
|
||||||
|
msg[5]=0x06;
|
||||||
|
|
||||||
|
msg[6]=0x01; // Modbus ID
|
||||||
|
|
||||||
|
msg[7]=0x05; // Function Code (Write Single Coil)
|
||||||
|
|
||||||
|
msg[8]=0xff&(line>>8); // Coil number
|
||||||
|
msg[9]=0xff&line;
|
||||||
|
|
||||||
|
msg[10]=0x00; // Coil state
|
||||||
|
msg[11]=0x00;
|
||||||
|
if(state) {
|
||||||
|
msg[10]=0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
modbus_socket->writeBlock(msg,12);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Modbus::InputState(int line) const
|
||||||
|
{
|
||||||
|
return (1<<(line%8)&modbus_input_states.at(line/8))!=0;
|
||||||
|
}
|
||||||
|
@@ -21,8 +21,10 @@
|
|||||||
#ifndef MODBUS_H
|
#ifndef MODBUS_H
|
||||||
#define MODBUS_H
|
#define MODBUS_H
|
||||||
|
|
||||||
|
#include <queue>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <qsignalmapper.h>
|
||||||
#include <qsocket.h>
|
#include <qsocket.h>
|
||||||
#include <qtimer.h>
|
#include <qtimer.h>
|
||||||
|
|
||||||
@@ -53,10 +55,13 @@ class Modbus : public Switcher
|
|||||||
void readyReadData();
|
void readyReadData();
|
||||||
void errorData(int err);
|
void errorData(int err);
|
||||||
void pollInputs();
|
void pollInputs();
|
||||||
|
void resetStateData(int line);
|
||||||
void watchdogData();
|
void watchdogData();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ProcessInputByte(char byte,int base);
|
void ProcessInputByte(char byte,int base);
|
||||||
|
void SetCoil(int line,bool state);
|
||||||
|
bool InputState(int line) const;
|
||||||
int modbus_istate;
|
int modbus_istate;
|
||||||
int modbus_input_bytes;
|
int modbus_input_bytes;
|
||||||
std::vector<char> modbus_input_states;
|
std::vector<char> modbus_input_states;
|
||||||
@@ -68,6 +73,12 @@ class Modbus : public Switcher
|
|||||||
uint16_t modbus_ip_port;
|
uint16_t modbus_ip_port;
|
||||||
int modbus_gpis;
|
int modbus_gpis;
|
||||||
int modbus_gpos;
|
int modbus_gpos;
|
||||||
|
std::queue<int> modbus_coil_lines;
|
||||||
|
std::queue<bool> modbus_coil_states;
|
||||||
|
bool modbus_busy;
|
||||||
|
QSignalMapper *modbus_reset_mapper;
|
||||||
|
std::vector<QTimer *> modbus_reset_timers;
|
||||||
|
std::vector<bool> modbus_reset_states;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user