1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-04-21 12:31:19 +02:00

driver name change

This commit is contained in:
travis-farmer 2024-12-12 15:47:25 -05:00
parent cd1afaf04d
commit cd4230dafb
No known key found for this signature in database
GPG Key ID: 0BC296791D14CB35
2 changed files with 101 additions and 270 deletions

View File

@ -18,101 +18,101 @@
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>. * along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include "IO_Modbus.h" #include "IO_RS485.h"
#include "defines.h" #include "defines.h"
void Modbus::setTransactionId(uint16_t transactionId) { void RS485::setTransactionId(uint16_t transactionId) {
_setRegister(tcp, 0, transactionId); _setRegister(tcp, 0, transactionId);
} }
void Modbus::setProtocolId(uint16_t protocolId) { void RS485::setProtocolId(uint16_t protocolId) {
_setRegister(tcp, 2, protocolId); _setRegister(tcp, 2, protocolId);
} }
void Modbus::setLength(uint16_t length) { void RS485::setLength(uint16_t length) {
if (length < 3 || length > 254) _setRegister(tcp, 4, 0); if (length < 3 || length > 254) _setRegister(tcp, 4, 0);
else _setRegister(tcp, 4, length); else _setRegister(tcp, 4, length);
} }
void Modbus::setUnitId(uint8_t unitId) { void RS485::setUnitId(uint8_t unitId) {
tcp[6] = unitId; tcp[6] = unitId;
} }
void Modbus::setFunctionCode(uint8_t functionCode) { void RS485::setFunctionCode(uint8_t functionCode) {
pdu[0] = functionCode; pdu[0] = functionCode;
} }
void Modbus::setDataRegister(uint8_t index, uint16_t value) { void RS485::setDataRegister(uint8_t index, uint16_t value) {
_setRegister(data, index, value); _setRegister(data, index, value);
} }
void Modbus::setRtuLen(uint16_t rtuLen) { void RS485::setRtuLen(uint16_t rtuLen) {
setLength(rtuLen - 2); setLength(rtuLen - 2);
} }
void Modbus::setTcpLen(uint16_t tcpLen) { void RS485::setTcpLen(uint16_t tcpLen) {
setLength(tcpLen - 6); setLength(tcpLen - 6);
} }
void Modbus::setPduLen(uint16_t pduLen) { void RS485::setPduLen(uint16_t pduLen) {
setLength(pduLen + 1); setLength(pduLen + 1);
} }
void Modbus::setDataLen(uint16_t dataLen) { void RS485::setDataLen(uint16_t dataLen) {
setLength(dataLen + 2); setLength(dataLen + 2);
} }
uint16_t Modbus::getTransactionId() { uint16_t RS485::getTransactionId() {
return _getRegister(tcp, 0); return _getRegister(tcp, 0);
} }
uint16_t Modbus::getProtocolId() { uint16_t RS485::getProtocolId() {
return _getRegister(tcp, 2); return _getRegister(tcp, 2);
} }
uint16_t Modbus::getLength() { uint16_t RS485::getLength() {
uint16_t length = _getRegister(tcp, 4); uint16_t length = _getRegister(tcp, 4);
if (length < 3 || length > 254) return 0; if (length < 3 || length > 254) return 0;
else return length; else return length;
} }
uint8_t Modbus::getUnitId() { uint8_t RS485::getUnitId() {
return tcp[6]; return tcp[6];
} }
uint8_t Modbus::getFunctionCode() { uint8_t RS485::getFunctionCode() {
return pdu[0]; return pdu[0];
} }
uint16_t Modbus::getDataRegister(uint8_t index) { uint16_t RS485::getDataRegister(uint8_t index) {
return _getRegister(data, index); return _getRegister(data, index);
} }
uint16_t Modbus::getRtuLen() { uint16_t RS485::getRtuLen() {
uint16_t len = getLength(); uint16_t len = getLength();
if (len == 0) return 0; if (len == 0) return 0;
else return len + 2; else return len + 2;
} }
uint16_t Modbus::getTcpLen() { uint16_t RS485::getTcpLen() {
uint16_t len = getLength(); uint16_t len = getLength();
if (len == 0) return 0; if (len == 0) return 0;
else return len + 6; else return len + 6;
} }
uint16_t Modbus::getPduLen() { uint16_t RS485::getPduLen() {
uint16_t len = getLength(); uint16_t len = getLength();
if (len == 0) return 0; if (len == 0) return 0;
else return len - 1; else return len - 1;
} }
uint16_t Modbus::getDataLen() { uint16_t RS485::getDataLen() {
uint16_t len = getLength(); uint16_t len = getLength();
if (len == 0) return 0; if (len == 0) return 0;
else return len - 2; else return len - 2;
@ -120,29 +120,29 @@ uint16_t Modbus::getDataLen() {
void Modbus::updateCrc(uint8_t *buf, uint16_t len) { void RS485::updateCrc(uint8_t *buf, uint16_t len) {
uint16_t crc = _calculateCrc(buf, len); uint16_t crc = _calculateCrc(buf, len);
buf[len] = lowByte(crc); buf[len] = lowByte(crc);
buf[len + 1] = highByte(crc); buf[len + 1] = highByte(crc);
} }
bool Modbus::crcGood(uint8_t *buf, uint16_t len) { bool RS485::crcGood(uint8_t *buf, uint16_t len) {
uint16_t aduCrc = buf[len] | (buf[len + 1] << 8); uint16_t aduCrc = buf[len] | (buf[len + 1] << 8);
uint16_t calculatedCrc = _calculateCrc(buf, len); uint16_t calculatedCrc = _calculateCrc(buf, len);
if (aduCrc == calculatedCrc) return true; if (aduCrc == calculatedCrc) return true;
else return false; else return false;
} }
void Modbus::_setRegister(uint8_t *buf, uint16_t index, uint16_t value) { void RS485::_setRegister(uint8_t *buf, uint16_t index, uint16_t value) {
buf[index] = highByte(value); buf[index] = highByte(value);
buf[index + 1] = lowByte(value); buf[index + 1] = lowByte(value);
} }
uint16_t Modbus::_getRegister(uint8_t *buf, uint16_t index) { uint16_t RS485::_getRegister(uint8_t *buf, uint16_t index) {
return (buf[index] << 8) | buf[index + 1]; return (buf[index] << 8) | buf[index + 1];
} }
uint16_t Modbus::_calculateCrc(uint8_t *buf, uint16_t len) { uint16_t RS485::_calculateCrc(uint8_t *buf, uint16_t len) {
uint16_t value = 0xFFFF; uint16_t value = 0xFFFF;
for (uint16_t i = 0; i < len; i++) { for (uint16_t i = 0; i < len; i++) {
value ^= (uint16_t)buf[i]; value ^= (uint16_t)buf[i];
@ -161,7 +161,7 @@ uint16_t div8RndUp(uint16_t value) {
return (value + 7) >> 3; return (value + 7) >> 3;
} }
void Modbus::clearRxBuffer() { void RS485::clearRxBuffer() {
unsigned long startMicros = micros(); unsigned long startMicros = micros();
do { do {
if (_serialD->available() > 0) { if (_serialD->available() > 0) {
@ -173,12 +173,12 @@ void Modbus::clearRxBuffer() {
/************************************************************ /************************************************************
* Modbus implementation * RS485 implementation
************************************************************/ ************************************************************/
// Constructor for Modbus // Constructor for RS485
Modbus::Modbus(uint8_t busNo, HardwareSerial &serial, unsigned long baud, uint16_t cycleTimeMS, int8_t txPin, int waitA, int waitB) { RS485::RS485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, uint16_t cycleTimeMS, int8_t txPin, int waitA, int waitB) {
_baud = baud; _baud = baud;
_serialD = &serial; _serialD = &serial;
_txPin = txPin; _txPin = txPin;
@ -191,15 +191,15 @@ Modbus::Modbus(uint8_t busNo, HardwareSerial &serial, unsigned long baud, uint16
// Add device to HAL device chain // Add device to HAL device chain
IODevice::addDevice(this); IODevice::addDevice(this);
// Add bus to Modbus chain. // Add bus to RS485 chain.
_nextBus = _busList; _nextBus = _busList;
_busList = this; _busList = this;
} }
// Main loop function for Modbus. // Main loop function for RS485.
// Work through list of nodes. For each node, in separate loop entries // Work through list of nodes. For each node, in separate loop entries
// When the slot time has finished, move on to the next device. // When the slot time has finished, move on to the next device.
void Modbus::_loop(unsigned long currentMicros) { void RS485::_loop(unsigned long currentMicros) {
_currentMicros = currentMicros; _currentMicros = currentMicros;
if (_currentNode == NULL) { if (_currentNode == NULL) {
@ -212,8 +212,8 @@ void Modbus::_loop(unsigned long currentMicros) {
if (_currentNode == NULL) return; if (_currentNode == NULL) return;
bool flagOK = true; bool flagOK = true;
#if defined(MODBUS_STM_COMM) #if defined(RS485_STM_COMM)
ArduinoPins::fastWriteDigital(MODBUS_STM_COMM,HIGH); ArduinoPins::fastWriteDigital(RS485_STM_COMM,HIGH);
#endif #endif
if (taskCnt > 0) { if (taskCnt > 0) {
@ -443,36 +443,36 @@ if (taskCnt > 0) {
_currentNode = _currentNode->getNext(); _currentNode = _currentNode->getNext();
} }
#if defined(MODBUS_STM_OK) #if defined(RS485_STM_OK)
if (flagOK == true) { if (flagOK == true) {
ArduinoPins::fastWriteDigital(MODBUS_STM_OK,HIGH); ArduinoPins::fastWriteDigital(RS485_STM_OK,HIGH);
} else { } else {
ArduinoPins::fastWriteDigital(MODBUS_STM_OK,LOW); ArduinoPins::fastWriteDigital(RS485_STM_OK,LOW);
} }
#endif #endif
#if defined(MODBUS_STM_FAIL) #if defined(RS485_STM_FAIL)
if (flagOK == false) { if (flagOK == false) {
ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,HIGH); ArduinoPins::fastWriteDigital(RS485_STM_FAIL,HIGH);
} else { } else {
ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,LOW); ArduinoPins::fastWriteDigital(RS485_STM_FAIL,LOW);
} }
#endif #endif
#if defined(MODBUS_STM_COMM) #if defined(RS485_STM_COMM)
ArduinoPins::fastWriteDigital(MODBUS_STM_COMM,LOW); ArduinoPins::fastWriteDigital(RS485_STM_COMM,LOW);
#endif #endif
} }
// Link to chain of Modbus instances // Link to chain of RS485 instances
Modbus *Modbus::_busList = NULL; RS485 *RS485::_busList = NULL;
/************************************************************ /************************************************************
* Modbusnode implementation * RS485node implementation
************************************************************/ ************************************************************/
// Constructor for Modbusnode object // Constructor for RS485node object
Modbusnode::Modbusnode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID) { RS485node::RS485node(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID) {
_firstVpin = firstVpin; _firstVpin = firstVpin;
_nPins = nPins; _nPins = nPins;
_busNo = busNo; _busNo = busNo;
@ -482,8 +482,8 @@ Modbusnode::Modbusnode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID)
// Add this device to HAL device list // Add this device to HAL device list
IODevice::addDevice(this); IODevice::addDevice(this);
_display(); _display();
// Add Modbusnode to Modbus object. // Add RS485node to RS485 object.
Modbus *bus = Modbus::findBus(_busNo); RS485 *bus = RS485::findBus(_busNo);
if (bus != NULL) { if (bus != NULL) {
bus->addNode(this); bus->addNode(this);
return; return;

View File

@ -19,10 +19,10 @@
*/ */
/* /*
* Modbus * RS485
* ======= * =======
* To define a Modbus, example syntax: * To define a RS485, example syntax:
* Modbus::create(bus, serial, baud[, cycletime[, pin]]); * RS485::create(bus, serial, baud[, cycletime[, pin]]);
* *
* bus = 0-255 * bus = 0-255
* serial = serial port to be used (e.g. Serial3) * serial = serial port to be used (e.g. Serial3)
@ -32,7 +32,7 @@
* *
* Each bus must use a different serial port. * Each bus must use a different serial port.
* *
* ModbusNode * RS485Node
* ======== * ========
* To define a CMRI node and associate it with a CMRI bus, * To define a CMRI node and associate it with a CMRI bus,
* CMRInode::create(firstVPIN, numVPINs, bus, nodeID, type [, inputs, outputs]); * CMRInode::create(firstVPIN, numVPINs, bus, nodeID, type [, inputs, outputs]);
@ -41,56 +41,29 @@
* numVPINs = number of vpins (e.g. 72 for an SMINI node) * numVPINs = number of vpins (e.g. 72 for an SMINI node)
* bus = 0-255 * bus = 0-255
* nodeID = 0-127 * nodeID = 0-127
* numDiscreteInputs = number of discrete inputs
* numCoils = number of coils
*
* Reference: "LCS-9.10.1
* Layout Control Specification: CMRInet Protocol
* Version 1.1 December 2014."
*/ */
#ifndef IO_MODBUS_H #ifndef IO_RS485_H
#define IO_MODBUS_H #define IO_RS485_H
#include "IODevice.h" #include "IODevice.h"
uint16_t div8RndUp(uint16_t value); uint16_t div8RndUp(uint16_t value);
/********************************************************************** /**********************************************************************
* Modbusnode class * RS485node class
* *
* This encapsulates the state associated with a single Modbus node, * This encapsulates the state associated with a single RS485 node,
* which includes the nodeID, number of discrete inputs and coils, and * which includes the nodeID, number of discrete inputs and coils, and
* the states of the discrete inputs and coils. * the states of the discrete inputs and coils.
**********************************************************************/ **********************************************************************/
class Modbusnode : public IODevice { class RS485node : public IODevice {
private: private:
uint8_t _busNo; uint8_t _busNo;
uint8_t _nodeID; uint8_t _nodeID;
char _type; char _type;
Modbusnode *_next = NULL; RS485node *_next = NULL;
bool _initialised = false; bool _initialised = false;
static const uint8_t _numCoils=100;
static const uint8_t _numDiscreteInputs=100;
static const uint8_t _numHoldingRegisters=100;
static const uint8_t _numInputRegisters=100;
uint8_t _numBO=0;
uint8_t _numBI=0;
uint8_t _numAO=0;
uint8_t _numAI=0;
int dataBO[16];
int dataBI[16];
int dataAO[84];
int dataAI[84];
int capePinsBI[16];
int capePinsBO[16];
int capePinsPU[16];
int capePinsAO[16];
int capePinsAI[16];
int configBPinsO[16];
int configBPinsI[16];
int configBPinsPU[16];
int configAPinsO[16];
int configAPinsI[16];
// EX-IOExpander protocol flags // EX-IOExpander protocol flags
enum { enum {
EXIOINIT = 0xE0, // Flag to initialise setup procedure EXIOINIT = 0xE0, // Flag to initialise setup procedure
@ -106,43 +79,6 @@ private:
EXIOWRAN = 0xEA, // Flag we're sending an analogue write (PWM) EXIOWRAN = 0xEA, // Flag we're sending an analogue write (PWM)
EXIOERR = 0xEF, // Flag we've received an error EXIOERR = 0xEF, // Flag we've received an error
}; };
void resetInit() {
for (int i = 0; i < 16; i++) {
capePinsBI[i] = 0;
capePinsBO[i] = 0;
capePinsPU[i] = 0;
capePinsAO[i] = 0;
capePinsAI[i] = 0;
configBPinsO[i] = 0;
configBPinsI[i] = 0;
configBPinsPU[i] = 0;
configAPinsO[i] = 0;
configAPinsI[i] = 0;
}
}
void spitError(int pin) {
bool isBI = false;
bool isBO = false;
bool isPU = false;
bool isAI = false;
bool isAO = false;
int configPinNum = pin / 16;
int configPinBit = pin % 16;
if (bitRead(configBPinsI[configPinNum],configPinBit) == true) isBI = true;
if (bitRead(configBPinsO[configPinNum],configPinBit) == true) isBO = true;
if (bitRead(configBPinsPU[configPinNum],configPinBit) == true) isPU = true;
if (bitRead(configAPinsI[configPinNum],configPinBit) == true) isAI = true;
if (bitRead(configAPinsO[configPinNum],configPinBit) == true) isAO = true;
if (isBI && isPU) DIAG(F("IO_Modbus config eror: Bool Input with pull-up, pin: %d"),pin);
if (isBI && !isPU) DIAG(F("IO_Modbus config eror: Bool Input without pull-up, pin: %d"),pin);
if (isBO) DIAG(F("IO_Modbus config eror: Bool Output, pin: %d"),pin);
if (isAI) DIAG(F("IO_Modbus config eror: Analog Input, pin: %d"),pin);
if (isAO) DIAG(F("IO_Modbus config eror: Analog Output, pin: %d"),pin);
}
public: public:
enum ProfileType : int { enum ProfileType : int {
@ -172,35 +108,18 @@ public:
uint8_t* _analoguePinMap = NULL; uint8_t* _analoguePinMap = NULL;
static void create(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID) { static void create(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID) {
if (checkNoOverlap(firstVpin, nPins)) new Modbusnode(firstVpin, nPins, busNo, nodeID); if (checkNoOverlap(firstVpin, nPins)) new RS485node(firstVpin, nPins, busNo, nodeID);
} }
Modbusnode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID); RS485node(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID);
int *coils[_numCoils];
int *discreteInputs[_numDiscreteInputs];
uint16_t *holdingRegisters[_numHoldingRegisters];
uint16_t *inputRegisters[_numInputRegisters];
uint8_t getNodeID() { uint8_t getNodeID() {
return _nodeID; return _nodeID;
} }
uint8_t getNumCoils() {
return _numCoils;
}
uint8_t getNumDiscreteInputs() {
return _numDiscreteInputs;
}
uint8_t getNumHoldingRegisters() {
return _numHoldingRegisters;
}
uint8_t getNumInputRegisters() {
return _numInputRegisters;
}
RS485node *getNext() {
Modbusnode *getNext() {
return _next; return _next;
} }
void setNext(Modbusnode *node) { void setNext(RS485node *node) {
_next = node; _next = node;
} }
bool isInitialised() { bool isInitialised() {
@ -210,60 +129,11 @@ public:
_initialised = true; _initialised = true;
} }
bool addPinBI(VPIN vpin, bool inputPullup) {
int configPinNum = vpin / 16;
int configPinBit = vpin % 16;
bitSet(configBPinsI[configPinNum],configPinBit); // input
bitWrite(configBPinsPU[configPinNum],configPinBit,inputPullup);
if (_numBI + _numBO + _numAI + _numAO > _nPins) {
DIAG(F("IO_Modbus config error: Too many I/O pins vs VPINs: %d"),_numBI + _numBO + _numAI + _numAO);
return true;
}
_numBI++;
return false;
}
bool addPinBO(VPIN vpin) {
int configPinNum = vpin / 16;
int configPinBit = vpin % 16;
bitSet(configBPinsO[configPinNum],configPinBit); // input
if (_numBI + _numBO + _numAI + _numAO > _nPins) {
DIAG(F("IO_Modbus config error: Too many I/O pins vs VPINs: %d"),_numBI + _numBO + _numAI + _numAO);
return true;
}
_numBO++;
return false;
}
bool addPinAI(VPIN vpin) {
int configPinNum = vpin / 6;
int configPinBit = vpin % 16;
bitSet(configAPinsI[configPinNum],configPinBit); // input
if (_numBI + _numBO + _numAI + _numAO > _nPins) {
DIAG(F("IO_Modbus config error: Too many I/O pins vs VPINs: %d"),_numBI + _numBO + _numAI + _numAO);
return true;
}
_numAI++;
return false;
}
bool addPinAO(VPIN vpin) {
int configPinNum = vpin / 6;
int configPinBit = vpin % 16;
bitSet(configAPinsO[configPinNum],configPinBit); // input
if (_numBI + _numBO + _numAI + _numAO > _nPins) {
DIAG(F("IO_Modbus config error: Too many I/O pins vs VPINs: %d"),_numBI + _numBO + _numAI + _numAO);
return true;
}
_numBI++;
return false;
}
bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override { bool _configure(VPIN vpin, ConfigTypeEnum configType, int paramCount, int params[]) override {
if (paramCount != 1) return false; if (paramCount != 1) return false;
int pin = vpin - _firstVpin; int pin = vpin - _firstVpin;
int pin = vpin - _firstVpin; int pin = vpin - _firstVpin;
Modbus* mb = Modbus::findBus(0); RS485* mb = RS485::findBus(0);
int* param[] = {(int*)pin, (int*)configType, (int*)paramCount, (int*)params[0]}; int* param[] = {(int*)pin, (int*)configType, (int*)paramCount, (int*)params[0]};
mb->addTask(_nodeID, 3, 4, param); mb->addTask(_nodeID, 3, 4, param);
@ -271,7 +141,7 @@ public:
int _configureAnalogIn(VPIN vpin) override { int _configureAnalogIn(VPIN vpin) override {
int pin = vpin - _firstVpin; int pin = vpin - _firstVpin;
Modbus* mb = Modbus::findBus(0); RS485* mb = RS485::findBus(0);
int* params[] = {(int*)pin}; int* params[] = {(int*)pin};
mb->addTask(_nodeID, 3, 1, params); mb->addTask(_nodeID, 3, 1, params);
@ -279,7 +149,7 @@ public:
} }
void _begin() override { void _begin() override {
Modbus* mb = Modbus::findBus(0); RS485* mb = RS485::findBus(0);
if (mb->_txPin != VPIN_NONE) { if (mb->_txPin != VPIN_NONE) {
pinMode(mb->_txPin, OUTPUT); pinMode(mb->_txPin, OUTPUT);
ArduinoPins::fastWriteDigital(mb->_txPin, LOW); ArduinoPins::fastWriteDigital(mb->_txPin, LOW);
@ -429,7 +299,7 @@ public:
void _write(VPIN vpin, int value) override { void _write(VPIN vpin, int value) override {
if (_deviceState == DEVSTATE_FAILED) return; if (_deviceState == DEVSTATE_FAILED) return;
int pin = vpin - _firstVpin; int pin = vpin - _firstVpin;
Modbus* mb = Modbus::findBus(0); RS485* mb = RS485::findBus(0);
int* params[] = {(int*)pin, (int*)value}; int* params[] = {(int*)pin, (int*)value};
mb->addTask(_nodeID, 3, 2, params); mb->addTask(_nodeID, 3, 2, params);
} }
@ -453,7 +323,7 @@ public:
if (_deviceState == DEVSTATE_FAILED) return; if (_deviceState == DEVSTATE_FAILED) return;
int pin = vpin - _firstVpin; int pin = vpin - _firstVpin;
Modbus* mb = Modbus::findBus(0); RS485* mb = RS485::findBus(0);
int* params[] = {(int*)pin, (int*)value, (int*)profile, (int*)duration}; int* params[] = {(int*)pin, (int*)value, (int*)profile, (int*)duration};
mb->addTask(_nodeID, 3, 4, params); mb->addTask(_nodeID, 3, 4, params);
} }
@ -461,62 +331,23 @@ public:
uint8_t getBusNumber() { uint8_t getBusNumber() {
return _busNo; return _busNo;
} }
uint8_t getNumBinaryInputsVPINsMin() {
if (_numDiscreteInputs > 0) return _firstVpin;
else return 0;
}
uint8_t getNumBinaryInputsVPINsMax() {
if (_numDiscreteInputs > 0) return _firstVpin+_numDiscreteInputs-1;
else return 0;
}
uint8_t getNumBinaryOutputsVPINsMin() {
if (_numCoils > 0) return _firstVpin+_numDiscreteInputs;
else return 0;
}
uint8_t getNumBinaryOutputsVPINsMax() {
if (_numCoils > 0) return _firstVpin+_numDiscreteInputs+_numCoils-1;
else return 0;
}
uint8_t getNumAnalogInputsVPINsMin() {
if (_numInputRegisters > 0) return _firstVpin+_numDiscreteInputs+_numCoils;
else return 0;
}
uint8_t getNumAnalogInputsVPINsMax() {
if (_numInputRegisters > 0) return _firstVpin+_numDiscreteInputs+_numCoils+_numInputRegisters-1;
else return 0;
}
uint8_t getNumAnalogOutputsVPINsMin() {
if (_numHoldingRegisters > 0) return _firstVpin+_numDiscreteInputs+_numCoils+_numInputRegisters;
else return 0;
}
uint8_t getNumAnalogOutputsVPINsMax() {
if (_numHoldingRegisters > 0) return _firstVpin+_numDiscreteInputs+_numCoils+_numInputRegisters+_numHoldingRegisters-1;
else return 0;
}
void _display() override { void _display() override {
DIAG(F("Modbusnode configured on bus:%d nodeID:%d VPINs:%u-%u (B In) %u-%u (B Out) %u-%u (A In) %u-%u (A Out)"), DIAG(F("EX-IOExpander node:%d v%d.%d.%d Vpins %u-%u %S"), _nodeID, _majorVer, _minorVer, _patchVer, (int)_firstVpin, (int)_firstVpin+_nPins-1, _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
_busNo, _nodeID, getNumBinaryInputsVPINsMin(), getNumBinaryInputsVPINsMax(),
getNumBinaryOutputsVPINsMin(), getNumBinaryOutputsVPINsMax(),
getNumAnalogInputsVPINsMin(), getNumAnalogInputsVPINsMax(),
getNumAnalogOutputsVPINsMin(), getNumAnalogOutputsVPINsMax());
} }
}; };
/********************************************************************** /**********************************************************************
* Modbus class * RS485 class
* *
* This encapsulates the properties state of the bus and the * This encapsulates the properties state of the bus and the
* transmission and reception of data across that bus. Each Modbus * transmission and reception of data across that bus. Each RS485
* object owns a set of Modbusnode objects which represent the nodes * object owns a set of RS485node objects which represent the nodes
* attached to that bus. * attached to that bus.
**********************************************************************/ **********************************************************************/
class Modbus : public IODevice { class RS485 : public IODevice {
private: private:
// Here we define the device-specific variables. // Here we define the device-specific variables.
uint8_t _busNo; uint8_t _busNo;
@ -526,12 +357,12 @@ private:
void _setRegister(uint8_t *buf, uint16_t index, uint16_t value); void _setRegister(uint8_t *buf, uint16_t index, uint16_t value);
unsigned long _baud; unsigned long _baud;
Modbusnode *_nodeListStart = NULL, *_nodeListEnd = NULL; RS485node *_nodeListStart = NULL, *_nodeListEnd = NULL;
Modbusnode *_currentNode = NULL; RS485node *_currentNode = NULL;
uint8_t _exceptionResponse = 0; uint8_t _exceptionResponse = 0;
uint8_t getExceptionResponse(); uint8_t getExceptionResponse();
uint16_t _receiveDataIndex = 0; // Index of next data byte to be received. uint16_t _receiveDataIndex = 0; // Index of next data byte to be received.
Modbus *_nextBus = NULL; // Pointer to next bus instance in list. RS485 *_nextBus = NULL; // Pointer to next bus instance in list.
void setTimeout(unsigned long timeout); void setTimeout(unsigned long timeout);
unsigned long _cycleStartTime = 0; unsigned long _cycleStartTime = 0;
unsigned long _timeoutStart = 0; unsigned long _timeoutStart = 0;
@ -542,7 +373,7 @@ private:
unsigned long _byteTransmitTime; // time in us for transmission of one byte unsigned long _byteTransmitTime; // time in us for transmission of one byte
int _operationCount = 0; int _operationCount = 0;
static Modbus *_busList; // linked list of defined bus instances static RS485 *_busList; // linked list of defined bus instances
bool waitReceive = false; bool waitReceive = false;
int _waitCounter = 0; int _waitCounter = 0;
int _waitCounterB = 0; int _waitCounterB = 0;
@ -673,7 +504,7 @@ public:
uint16_t getDataLen(); uint16_t getDataLen();
void clearRxBuffer(); void clearRxBuffer();
static void create(uint8_t busNo, HardwareSerial& serial, unsigned long baud, uint16_t cycleTimeMS=500, int8_t txPin=-1, int waitA=10, int waitB=10) { static void create(uint8_t busNo, HardwareSerial& serial, unsigned long baud, uint16_t cycleTimeMS=500, int8_t txPin=-1, int waitA=10, int waitB=10) {
new Modbus(busNo, serial, baud, cycleTimeMS, txPin, waitA, waitB); new RS485(busNo, serial, baud, cycleTimeMS, txPin, waitA, waitB);
} }
HardwareSerial *_serialD; HardwareSerial *_serialD;
// Device-specific initialisation // Device-specific initialisation
@ -689,17 +520,17 @@ public:
_frameTimeout = (bitsPerChar * 1000000) / _baud + 1750; _frameTimeout = (bitsPerChar * 1000000) / _baud + 1750;
} }
clearRxBuffer(); clearRxBuffer();
#if defined(MODBUS_STM_OK) #if defined(RS485_STM_OK)
pinMode(MODBUS_STM_OK, OUTPUT); pinMode(RS485_STM_OK, OUTPUT);
ArduinoPins::fastWriteDigital(MODBUS_STM_OK,LOW); ArduinoPins::fastWriteDigital(RS485_STM_OK,LOW);
#endif #endif
#if defined(MODBUS_STM_FAIL) #if defined(RS485_STM_FAIL)
pinMode(MODBUS_STM_FAIL, OUTPUT); pinMode(RS485_STM_FAIL, OUTPUT);
ArduinoPins::fastWriteDigital(MODBUS_STM_FAIL,LOW); ArduinoPins::fastWriteDigital(RS485_STM_FAIL,LOW);
#endif #endif
#if defined(MODBUS_STM_COMM) #if defined(RS485_STM_COMM)
pinMode(MODBUS_STM_COMM, OUTPUT); pinMode(RS485_STM_COMM, OUTPUT);
ArduinoPins::fastWriteDigital(MODBUS_STM_COMM,LOW); ArduinoPins::fastWriteDigital(RS485_STM_COMM,LOW);
#endif #endif
#if defined(DIAG_IO) #if defined(DIAG_IO)
@ -716,13 +547,13 @@ public:
// Display information about the device // Display information about the device
void _display() override { void _display() override {
DIAG(F("Modbus Configured on Vpins:%d-%d %S"), _firstVpin, _firstVpin+_nPins-1, DIAG(F("RS485 Configured on Vpins:%d-%d %S"), _firstVpin, _firstVpin+_nPins-1,
_deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("OK")); _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("OK"));
} }
// Locate Modbusnode object with specified nodeID. // Locate RS485node object with specified nodeID.
Modbusnode *findNode(uint8_t nodeID) { RS485node *findNode(uint8_t nodeID) {
for (Modbusnode *node = _nodeListStart; node != NULL; node = node->getNext()) { for (RS485node *node = _nodeListStart; node != NULL; node = node->getNext()) {
if (node->getNodeID() == nodeID) if (node->getNodeID() == nodeID)
return node; return node;
} }
@ -730,19 +561,19 @@ public:
} }
// Add new Modbusnode to the list of nodes for this bus. // Add new RS485node to the list of nodes for this bus.
void addNode(Modbusnode *newNode) { void addNode(RS485node *newNode) {
if (!_nodeListStart) if (!_nodeListStart)
_nodeListStart = newNode; _nodeListStart = newNode;
if (!_nodeListEnd) if (!_nodeListEnd)
_nodeListEnd = newNode; _nodeListEnd = newNode;
else else
_nodeListEnd->setNext(newNode); _nodeListEnd->setNext(newNode);
//DIAG(F("Modbus: 260h nodeID:%d _nodeListStart:%d _nodeListEnd:%d"), newNode, _nodeListStart, _nodeListEnd); //DIAG(F("RS485: 260h nodeID:%d _nodeListStart:%d _nodeListEnd:%d"), newNode, _nodeListStart, _nodeListEnd);
} }
protected: protected:
Modbus(uint8_t busNo, HardwareSerial &serial, unsigned long baud, uint16_t cycleTimeMS, int8_t txPin, int waitA, int waitB); RS485(uint8_t busNo, HardwareSerial &serial, unsigned long baud, uint16_t cycleTimeMS, int8_t txPin, int waitA, int waitB);
public: public:
@ -750,8 +581,8 @@ public:
return _busNo; return _busNo;
} }
static Modbus *findBus(uint8_t busNo) { static RS485 *findBus(uint8_t busNo) {
for (Modbus *bus=_busList; bus!=NULL; bus=bus->_nextBus) { for (RS485 *bus=_busList; bus!=NULL; bus=bus->_nextBus) {
if (bus->_busNo == busNo) return bus; if (bus->_busNo == busNo) return bus;
} }
return NULL; return NULL;
@ -759,4 +590,4 @@ public:
}; };
#endif // IO_MODBUS_H #endif // IO_RS485_H