From 6d2586f88ef975500d47a926f8b3735285e9cc54 Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Mon, 25 Nov 2024 05:18:16 -0500 Subject: [PATCH] added analog, though still no positive test --- IO_Modbus.cpp | 15 ++++++++++++--- IO_Modbus.h | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/IO_Modbus.cpp b/IO_Modbus.cpp index 8abe4d8..888b470 100644 --- a/IO_Modbus.cpp +++ b/IO_Modbus.cpp @@ -522,14 +522,21 @@ void Modbus::_loop(unsigned long currentMicros) { // If we're between read/write cycles then don't do anything else. if (_currentMicros - _cycleStartTime < _cycleTime) return; } + _cycleStartTime = _currentMicros; if (_currentNode == NULL) return; uint8_t error; + error = modbusmaster->writeMultipleHoldingRegisters(_currentNode->getNodeID(), 0, _currentNode->holdingRegisters, _currentNode->getNumHoldingRegisters()); + if (error != 0) DIAG(F("ModbusHR: %02d %04d %04d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumHoldingRegisters(), errorStrings[error]); + error = modbusmaster->writeMultipleCoils(_currentNode->getNodeID(), 0, _currentNode->coils, _currentNode->getNumCoils()); - if (error != 0) DIAG(F("Modbus: %02d %04d %04d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumCoils(), errorStrings[error]); + if (error != 0) DIAG(F("ModbusMC: %02d %04d %04d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumCoils(), errorStrings[error]); error = modbusmaster->readDiscreteInputs(_currentNode->getNodeID(), 0, _currentNode->discreteInputs, _currentNode->getNumDisInputs()); - if (error != 0) DIAG(F("Modbus: %02d %04d %04d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumDisInputs(), errorStrings[error]); + if (error != 0) DIAG(F("ModbusDI: %02d %04d %04d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumDisInputs(), errorStrings[error]); + + error = modbusmaster->readInputRegisters(_currentNode->getNodeID(), 0, _currentNode->inputRegisters, _currentNode->getNumInputRegisters()); + if (error != 0) DIAG(F("ModbusIR: %02d %04d %04d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumInputRegisters(), errorStrings[error]); } // Link to chain of CMRI bus instances @@ -541,13 +548,15 @@ Modbus *Modbus::_busList = NULL; ************************************************************/ // Constructor for Modbusnode object -Modbusnode::Modbusnode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID, uint8_t numCoils, uint8_t numDiscreteInputs) { +Modbusnode::Modbusnode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID, uint8_t numCoils, uint8_t numDiscreteInputs, uint8_t numHoldingRegisters, uint8_t numInputRegisters) { _firstVpin = firstVpin; _nPins = nPins; _busNo = busNo; _nodeID = nodeID; coils[numCoils]; discreteInputs[numDiscreteInputs]; + holdingRegisters[numHoldingRegisters]; + inputRegisters[numInputRegisters]; if ((unsigned int)_nPins < numDiscreteInputs + numCoils) DIAG(F("Modbusnode: bus:%d nodeID:%d WARNING number of Vpins does not cover all inputs and outputs"), _busNo, _nodeID); diff --git a/IO_Modbus.h b/IO_Modbus.h index 165225e..7590e87 100644 --- a/IO_Modbus.h +++ b/IO_Modbus.h @@ -191,14 +191,18 @@ private: bool _initialised = false; uint8_t numCoils; uint8_t numDiscreteInputs; + uint8_t numHoldingRegisters; + uint8_t numInputRegisters; public: - static void create(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID, uint8_t numCoils=0, uint8_t numDiscreteInputs=0) { - if (checkNoOverlap(firstVpin, nPins)) new Modbusnode(firstVpin, nPins, busNo, nodeID, numCoils, numDiscreteInputs); + static void create(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID, uint8_t numCoils=0, uint8_t numDiscreteInputs=0, uint8_t numHoldingRegisters=0, uint8_t numInputRegisters=0) { + if (checkNoOverlap(firstVpin, nPins)) new Modbusnode(firstVpin, nPins, busNo, nodeID, numCoils, numDiscreteInputs, numHoldingRegisters, numInputRegisters); } - Modbusnode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID, uint8_t numCoils=0, uint8_t numDiscreteInputs=0); + Modbusnode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID, uint8_t numCoils=0, uint8_t numDiscreteInputs=0, uint8_t numHoldingRegisters=0, uint8_t numInputRegisters=0); bool *coils; bool *discreteInputs; + uint16_t *holdingRegisters; + uint16_t *inputRegisters; uint8_t getNodeID() { return _nodeID; @@ -209,7 +213,12 @@ public: uint8_t getNumDisInputs() { return numDiscreteInputs; } - + uint8_t getNumHoldingRegisters() { + return numHoldingRegisters; + } + uint8_t getNumInputRegisters() { + return numInputRegisters; + } Modbusnode *getNext() { return _next; } @@ -227,7 +236,7 @@ public: _initialised = false; } - int _read(VPIN vpin) { + int _read(VPIN vpin) override { // Return current state from this device uint16_t pin = vpin - _firstVpin; if (pin < numDiscreteInputs) { @@ -236,7 +245,13 @@ public: return 0; } - void _write(VPIN vpin, int value) { + int _readAnalogue(VPIN vpin) override { + // Return acquired data value, e.g. + int pin = vpin - _firstVpin; + return inputRegisters[pin]; + } + + void _write(VPIN vpin, int value) override { // Update current state for this device, in preparation the bus transmission uint16_t pin = vpin - _firstVpin - numDiscreteInputs; if (pin < numCoils) { @@ -247,6 +262,16 @@ public: } } + void writeAnalogue(VPIN vpin, int value) { + uint16_t pin = vpin - _firstVpin - numInputRegisters; + if (pin < numHoldingRegisters) { + if (value) + holdingRegisters[pin] = value; + else + holdingRegisters[pin]; + } + } + void saveIncomingData(uint8_t index, uint8_t data) { if (index < numDiscreteInputs) discreteInputs[index] = data; @@ -300,7 +325,7 @@ private: int16_t _transmitEnablePin = VPIN_NONE; Modbusnode *_nodeListStart = NULL, *_nodeListEnd = NULL; Modbusnode *_currentNode = NULL; - + uint16_t _receiveDataIndex = 0; // Index of next data byte to be received. Modbus *_nextBus = NULL; // Pointer to next bus instance in list. unsigned long _cycleStartTime = 0; @@ -319,6 +344,7 @@ public: } HardwareSerial *_serial; ModbusRTUMaster *modbusmaster; + const char* errorStrings[]; // Device-specific initialisation void _begin() override {