// modbus.cpp // // A Rivendell switcher driver for Modbus TCP // // (C) Copyright 2017 Fred Gleason // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public // License along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // #include #include "modbus.h" Modbus::Modbus(RDMatrix *matrix,QObject *parent) : Switcher(matrix,parent) { modbus_istate=0; modbus_watchdog_active=false; modbus_gpis=matrix->gpis(); modbus_input_bytes=modbus_gpis/8; if((modbus_gpis%8)!=0) { modbus_input_bytes++; } for(int i=0;igpos(); modbus_ip_address=matrix->ipAddress(RDMatrix::Primary); modbus_ip_port=matrix->ipPort(RDMatrix::Primary); modbus_socket=new QSocket(this); connect(modbus_socket,SIGNAL(connected()),this,SLOT(connectedData())); connect(modbus_socket,SIGNAL(readyRead()),this,SLOT(readyReadData())); connect(modbus_socket,SIGNAL(error(int)),this,SLOT(errorData(int))); modbus_socket->connectToHost(modbus_ip_address.toString(),modbus_ip_port); modbus_watchdog_timer=new QTimer(this); connect(modbus_watchdog_timer,SIGNAL(timeout()),this,SLOT(watchdogData())); } Modbus::~Modbus() { delete modbus_watchdog_timer; delete modbus_socket; } RDMatrix::Type Modbus::type() { return RDMatrix::Modbus; } unsigned Modbus::gpiQuantity() { return modbus_gpis; } unsigned Modbus::gpoQuantity() { return modbus_gpos; } bool Modbus::primaryTtyActive() { return false; } bool Modbus::secondaryTtyActive() { return false; } void Modbus::processCommand(RDMacro *cmd) { } void Modbus::connectedData() { syslog(LOG_INFO, "connection to Modbus device at %s:%u established", (const char *)modbus_ip_address.toString(),0xffff&modbus_ip_port); modbus_watchdog_active=false; PollInputs(); } void Modbus::readyReadData() { char data[1501]; int n; char byte; uint16_t len; // char id; char count=0; int base=0; while((n=modbus_socket->readBlock(data,1500))>0) { for(int i=0;iclose(); modbus_socket->connectToHost(modbus_ip_address.toString(),modbus_ip_port); } void Modbus::ProcessInputByte(char byte,int base) { char mask=byte^modbus_input_states[base]; for(int i=0;i<8;i++) { if(((1<>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); }