From 6551ce7b50959cd30c269e78d06e2b4db6574755 Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Thu, 28 Nov 2024 07:36:12 -0500 Subject: [PATCH] hardware test shows VPINs are off by one --- IO_Modbus.cpp | 32 +++++++++++++++++++------------- IO_Modbus.h | 24 +++++++++++++++--------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/IO_Modbus.cpp b/IO_Modbus.cpp index bbd6fdf..ad065e4 100644 --- a/IO_Modbus.cpp +++ b/IO_Modbus.cpp @@ -253,7 +253,7 @@ void ModbusRTUComm::writeAdu(ModbusADU& adu) { if (_rePin != VPIN_NONE) ArduinoPins::fastWriteDigital(_rePin, HIGH); _serial.write(adu.rtu, adu.getRtuLen()); _serial.flush(); - //delayMicroseconds(_postDelay); + delayMicroseconds(_postDelay); if (_dePin != VPIN_NONE) ArduinoPins::fastWriteDigital(_dePin, LOW); if (_rePin != VPIN_NONE) ArduinoPins::fastWriteDigital(_rePin, LOW); } @@ -515,11 +515,10 @@ void Modbus::_loop(unsigned long currentMicros) { const char* errorStrings[16] = { "success", "invalid id", "invalid buffer", "invalid quantity", "response timeout", "frame error", "crc error", "unknown comm error", "unexpected id", "exception response", "unexpected function code", "unexpected response length", "unexpected byte count", "unexpected address", "unexpected value", "unexpected quantity" }; - bool flagOK = false; -#if defined(MODBUS_STM_OK) && defined(MODBUS_STM_FAIL) && defined(MODBUS_STM_TICK) - ArduinoPins::fastWriteDigital(MODBUS_STM_TICK,HIGH); + bool flagOK = true; +#if defined(MODBUS_STM_COMM) + ArduinoPins::fastWriteDigital(MODBUS_STM_COMM,HIGH); #endif - uint8_t error; // send reads and writes, DIAG on errors other than 0 (Success), or 3 (Invalid Quantity) error = writeMultipleHoldingRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->holdingRegisters, _currentNode->getNumHoldingRegisters()); @@ -527,35 +526,42 @@ void Modbus::_loop(unsigned long currentMicros) { #ifdef DIAG_IO if (error == 0) DIAG(F("ModbusHR: T%d Success!"), _currentNode->getNodeID()); #endif - if (error == 0 || error == 3) flagOK = true; + if (error != 0 && error != 3) flagOK = false; error = writeMultipleCoils(_currentNode->getNodeID(), 0, (char*) _currentNode->coils, _currentNode->getNumCoils()); if (error != 0 && error != 3) DIAG(F("ModbusMC: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumCoils(), errorStrings[error]); #ifdef DIAG_IO if (error == 0) DIAG(F("ModbusMC: T%d Success!"), _currentNode->getNodeID()); #endif - if (error == 0 || error == 3) flagOK = true; + if (error != 0 && error != 3) flagOK = false; error = readDiscreteInputs(_currentNode->getNodeID(), 0, (char*) _currentNode->discreteInputs, _currentNode->getNumDiscreteInputs()); if (error != 0 && error != 3) DIAG(F("ModbusDI: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumDiscreteInputs(), errorStrings[error]); #ifdef DIAG_IO if (error == 0) DIAG(F("ModbusDI: T%d Success!"), _currentNode->getNodeID()); #endif - if (error == 0 || error == 3) flagOK = true; + if (error != 0 && error != 3) flagOK = false; error = readInputRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->inputRegisters, _currentNode->getNumInputRegisters()); if (error != 0 && error != 3) DIAG(F("ModbusIR: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumInputRegisters(), errorStrings[error]); #ifdef DIAG_IO if (error == 0) DIAG(F("ModbusIR: T%d Success!"), _currentNode->getNodeID()); #endif - if (error == 0 || error == 3) flagOK = true; + if (error != 0 && error != 3) flagOK = false; -#if defined(MODBUS_STM_OK) && defined(MODBUS_STM_FAIL) && defined(MODBUS_STM_TICK) +#if defined(MODBUS_STM_OK) if (flagOK == true) { ArduinoPins::fastWriteDigital(MODBUS_STM_OK,HIGH); - ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,LOW); } else { ArduinoPins::fastWriteDigital(MODBUS_STM_OK,LOW); - ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,HIGH); } - ArduinoPins::fastWriteDigital(MODBUS_STM_TICK,LOW); +#endif +#if defined(MODBUS_STM_FAIL) + if (flagOK == false) { + ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,HIGH); + } else { + ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,LOW); + } +#endif +#if defined(MODBUS_STM_COMM) + ArduinoPins::fastWriteDigital(MODBUS_STM_COMM,LOW); #endif _currentNode = _currentNode->getNext(); } diff --git a/IO_Modbus.h b/IO_Modbus.h index 786c92b..01b87e6 100644 --- a/IO_Modbus.h +++ b/IO_Modbus.h @@ -219,10 +219,11 @@ public: // Update current state for this device, in preparation the bus transmission uint16_t pin = vpin - _firstVpin - _numDiscreteInputs; if (pin < _numCoils) { - if (value) + if (value){ if (value == 1) coils[pin] = (char*) 0x1; if (value == 0) coils[pin] = (char*) 0x0; //coils[pin] = value; + } else coils[pin]; } @@ -231,16 +232,16 @@ public: int _readAnalogue(VPIN vpin) override { // Return acquired data value, e.g. int pin = vpin - _firstVpin - _numDiscreteInputs - _numCoils; - return (int) inputRegisters[pin]; + return (int) inputRegisters[pin-1]; } void _writeAnalogue(VPIN vpin, int value, uint8_t param1=0, uint16_t param2=0) override { uint16_t pin = vpin - _firstVpin - _numDiscreteInputs - _numCoils - _numInputRegisters; if (pin < _numHoldingRegisters) { if (value) - holdingRegisters[pin] = (uint16_t*) value; + holdingRegisters[pin-1] = (uint16_t*) value; else - holdingRegisters[pin]; + holdingRegisters[pin-1]; } } @@ -336,14 +337,19 @@ public: void _begin() override { _serialD->begin(_baud, SERIAL_8N1); _rtuComm.begin(_baud, SERIAL_8N1); - #if defined(MODBUS_STM_OK) && defined(MODBUS_STM_FAIL) && defined(MODBUS_STM_TICK) + #if defined(MODBUS_STM_OK) pinMode(MODBUS_STM_OK, OUTPUT); - pinMode(MODBUS_STM_FAIL, OUTPUT); - pinMode(MODBUS_STM_TICK, OUTPUT); ArduinoPins::fastWriteDigital(MODBUS_STM_OK,LOW); - ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,LOW); - ArduinoPins::fastWriteDigital(MODBUS_STM_TICK,LOW); #endif + #if defined(MODBUS_STM_FAIL) + pinMode(MODBUS_STM_FAIL, OUTPUT); + ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,LOW); + #endif + #if defined(MODBUS_STM_COMM) + pinMode(MODBUS_STM_COMM, OUTPUT); + ArduinoPins::fastWriteDigital(MODBUS_STM_COMM,LOW); + #endif + #if defined(DIAG_IO) _display(); #endif