From 5c40577703454099b66cf970d8c1fddc7a39f3ea Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Wed, 27 Nov 2024 07:35:00 -0500 Subject: [PATCH] updating --- IO_Modbus.cpp | 23 ++++++++++++++--------- IO_Modbus.h | 30 +++++++++++++++--------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/IO_Modbus.cpp b/IO_Modbus.cpp index 79279ae..7942bb7 100644 --- a/IO_Modbus.cpp +++ b/IO_Modbus.cpp @@ -486,13 +486,16 @@ ModbusRTUMasterError ModbusRTUMaster::_translateCommError(ModbusRTUCommError com * Modbus implementation ************************************************************/ + // Constructor for Modbus -Modbus::Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_t cycleTimeMS, int8_t transmitEnablePin) { - _busNo = busNo; +Modbus::Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_t cycleTimeMS, int8_t _dePin) { + + //serial.write("\002\004",2); + ModbusRTUMaster _modbusmaster(serial, _dePin); _baud = baud; _serialD = &serial; + _busNo = busNo; _cycleTime = cycleTimeMS * 1000UL; // convert from milliseconds to microseconds. - _transmitEnablePin = transmitEnablePin; //if (_transmitEnablePin != VPIN_NONE) { //pinMode(_transmitEnablePin, OUTPUT); //ArduinoPins::fastWriteDigital(_transmitEnablePin, 0); // transmitter initially off @@ -502,8 +505,9 @@ Modbus::Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_ //_modbusmaster.begin(baud); //DIAG(F("ModbusInit: %d %d"), _transmitEnablePin, _baud); // Add device to HAL device chain + serial.begin(baud, SERIAL_8N1); IODevice::addDevice(this); - + _modbusmaster.begin(baud, SERIAL_8N1); // Add bus to CMRIbus chain. _nextBus = _busList; _busList = this; @@ -516,7 +520,6 @@ Modbus::Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_ // process any response data received. // When the slot time has finished, move on to the next device. void Modbus::_loop(unsigned long currentMicros) { - _currentMicros = currentMicros; if (_currentNode == NULL) { @@ -532,16 +535,18 @@ void Modbus::_loop(unsigned long currentMicros) { uint8_t error; //error = _modbusmaster->writeMultipleHoldingRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->holdingRegisters, _currentNode->getNumHoldingRegisters()); - DIAG(F("ModbusHR: %d %d %d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumHoldingRegisters(), errorStrings[error]); + DIAG(F("ModbusHR: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumHoldingRegisters(), errorStrings[error]); - //error = _modbusmaster->writeMultipleCoils(_currentNode->getNodeID(), 0, (char*) _currentNode->coils, _currentNode->getNumCoils()); + error = _modbusmaster->writeMultipleCoils(_currentNode->getNodeID(), 0, (char*) _currentNode->coils, _currentNode->getNumCoils()); DIAG(F("ModbusMC: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumCoils(), errorStrings[error]); - error = _modbusmaster->readDiscreteInputs(_currentNode->getNodeID(), 0, (char*) _currentNode->discreteInputs, _currentNode->getNumDiscreteInputs()); + //error = _modbusmaster->readDiscreteInputs(_currentNode->getNodeID(), 0, (char*) _currentNode->discreteInputs, _currentNode->getNumDiscreteInputs()); DIAG(F("ModbusDI: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumDiscreteInputs(), errorStrings[error]); //error = _modbusmaster->readInputRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->inputRegisters, _currentNode->getNumInputRegisters()); - DIAG(F("ModbusIR: %d %d %d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumInputRegisters(), errorStrings[error]); + DIAG(F("ModbusIR: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumInputRegisters(), errorStrings[error]); + + _currentNode = _currentNode->getNext(); //delayUntil(_currentMicros + _cycleTime * 1000UL); } diff --git a/IO_Modbus.h b/IO_Modbus.h index d5a0bbc..6e64053 100644 --- a/IO_Modbus.h +++ b/IO_Modbus.h @@ -199,10 +199,10 @@ public: 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, uint8_t numDiscreteInputs, uint8_t numHoldingRegisters, uint8_t numInputRegisters); - char *coils[100]; - char *discreteInputs[100]; - uint16_t *holdingRegisters[100]; - uint16_t *inputRegisters[100]; + char *coils[1968]; + char *discreteInputs[2000]; + uint16_t *holdingRegisters[123]; + uint16_t *inputRegisters[125]; uint8_t getNodeID() { return _nodeID; @@ -304,7 +304,6 @@ private: uint8_t _busNo; unsigned long _baud; - int8_t _transmitEnablePin; Modbusnode *_nodeListStart = NULL, *_nodeListEnd = NULL; Modbusnode *_currentNode = NULL; @@ -319,20 +318,21 @@ private: unsigned long _byteTransmitTime; // time in us for transmission of one byte static Modbus *_busList; // linked list of defined bus instances - + public: - static void create(uint8_t busNo, HardwareSerial& serial, unsigned long baud, uint16_t cycleTimeMS=500, int8_t transmitEnablePin=0) { - new Modbus(busNo, serial, baud, cycleTimeMS, transmitEnablePin); + static void create(uint8_t busNo, HardwareSerial& serial, unsigned long baud, uint16_t cycleTimeMS=500, int8_t _dePin=0) { + new Modbus(busNo, serial, baud, cycleTimeMS, _dePin); } - HardwareSerial *_serialD; + HardwareSerial *_serialD = nullptr; ModbusRTUMaster *_modbusmaster; - + // Device-specific initialisation void _begin() override { - ModbusRTUMaster _modbusmaster(*_serialD, _transmitEnablePin, -1); - _serialD->begin(_baud, SERIAL_8N1); + //ModbusRTUMaster _modbusmaster(*_serialD, _transmitEnablePin, -1); + + //_serialD->println("test"); - _modbusmaster.begin(_baud, SERIAL_8N1); + #if defined(DIAG_IO) _display(); #endif @@ -343,7 +343,7 @@ public: // Display information about the device void _display() override { - DIAG(F("Modbus Configured on %d Vpins:%d-%d %S"), _transmitEnablePin, _firstVpin, _firstVpin+_nPins-1, + DIAG(F("Modbus Configured on Vpins:%d-%d %S"), _firstVpin, _firstVpin+_nPins-1, _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("OK")); } @@ -368,7 +368,7 @@ public: } protected: - Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_t cycleTimeMS, int8_t transmitEnablePin); + Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_t cycleTimeMS, int8_t _dePin); public: