1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 13:21:23 +01:00
This commit is contained in:
travis-farmer 2024-11-27 07:35:00 -05:00
parent 5a4b87846c
commit 5c40577703
No known key found for this signature in database
GPG Key ID: 0BC296791D14CB35
2 changed files with 29 additions and 24 deletions

View File

@ -486,13 +486,16 @@ ModbusRTUMasterError ModbusRTUMaster::_translateCommError(ModbusRTUCommError com
* Modbus implementation * Modbus implementation
************************************************************/ ************************************************************/
// Constructor for Modbus // Constructor for Modbus
Modbus::Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_t cycleTimeMS, int8_t transmitEnablePin) { Modbus::Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_t cycleTimeMS, int8_t _dePin) {
_busNo = busNo;
//serial.write("\002\004",2);
ModbusRTUMaster _modbusmaster(serial, _dePin);
_baud = baud; _baud = baud;
_serialD = &serial; _serialD = &serial;
_busNo = busNo;
_cycleTime = cycleTimeMS * 1000UL; // convert from milliseconds to microseconds. _cycleTime = cycleTimeMS * 1000UL; // convert from milliseconds to microseconds.
_transmitEnablePin = transmitEnablePin;
//if (_transmitEnablePin != VPIN_NONE) { //if (_transmitEnablePin != VPIN_NONE) {
//pinMode(_transmitEnablePin, OUTPUT); //pinMode(_transmitEnablePin, OUTPUT);
//ArduinoPins::fastWriteDigital(_transmitEnablePin, 0); // transmitter initially off //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); //_modbusmaster.begin(baud);
//DIAG(F("ModbusInit: %d %d"), _transmitEnablePin, _baud); //DIAG(F("ModbusInit: %d %d"), _transmitEnablePin, _baud);
// Add device to HAL device chain // Add device to HAL device chain
serial.begin(baud, SERIAL_8N1);
IODevice::addDevice(this); IODevice::addDevice(this);
_modbusmaster.begin(baud, SERIAL_8N1);
// Add bus to CMRIbus chain. // Add bus to CMRIbus chain.
_nextBus = _busList; _nextBus = _busList;
_busList = this; _busList = this;
@ -516,7 +520,6 @@ Modbus::Modbus(uint8_t busNo, HardwareSerial serial, unsigned long baud, uint16_
// process any response data received. // process any response data received.
// 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 Modbus::_loop(unsigned long currentMicros) {
_currentMicros = currentMicros; _currentMicros = currentMicros;
if (_currentNode == NULL) { if (_currentNode == NULL) {
@ -532,16 +535,18 @@ void Modbus::_loop(unsigned long currentMicros) {
uint8_t error; uint8_t error;
//error = _modbusmaster->writeMultipleHoldingRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->holdingRegisters, _currentNode->getNumHoldingRegisters()); //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]); 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]); 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()); //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(); _currentNode = _currentNode->getNext();
//delayUntil(_currentMicros + _cycleTime * 1000UL); //delayUntil(_currentMicros + _cycleTime * 1000UL);
} }

View File

@ -199,10 +199,10 @@ public:
if (checkNoOverlap(firstVpin, nPins)) new Modbusnode(firstVpin, nPins, busNo, nodeID, numCoils, numDiscreteInputs, numHoldingRegisters, numInputRegisters); 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); 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 *coils[1968];
char *discreteInputs[100]; char *discreteInputs[2000];
uint16_t *holdingRegisters[100]; uint16_t *holdingRegisters[123];
uint16_t *inputRegisters[100]; uint16_t *inputRegisters[125];
uint8_t getNodeID() { uint8_t getNodeID() {
return _nodeID; return _nodeID;
@ -304,7 +304,6 @@ private:
uint8_t _busNo; uint8_t _busNo;
unsigned long _baud; unsigned long _baud;
int8_t _transmitEnablePin;
Modbusnode *_nodeListStart = NULL, *_nodeListEnd = NULL; Modbusnode *_nodeListStart = NULL, *_nodeListEnd = NULL;
Modbusnode *_currentNode = NULL; Modbusnode *_currentNode = NULL;
@ -319,20 +318,21 @@ private:
unsigned long _byteTransmitTime; // time in us for transmission of one byte unsigned long _byteTransmitTime; // time in us for transmission of one byte
static Modbus *_busList; // linked list of defined bus instances static Modbus *_busList; // linked list of defined bus instances
public: public:
static void create(uint8_t busNo, HardwareSerial& serial, unsigned long baud, uint16_t cycleTimeMS=500, int8_t transmitEnablePin=0) { 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, transmitEnablePin); new Modbus(busNo, serial, baud, cycleTimeMS, _dePin);
} }
HardwareSerial *_serialD; HardwareSerial *_serialD = nullptr;
ModbusRTUMaster *_modbusmaster; ModbusRTUMaster *_modbusmaster;
// Device-specific initialisation // Device-specific initialisation
void _begin() override { void _begin() override {
ModbusRTUMaster _modbusmaster(*_serialD, _transmitEnablePin, -1); //ModbusRTUMaster _modbusmaster(*_serialD, _transmitEnablePin, -1);
_serialD->begin(_baud, SERIAL_8N1);
//_serialD->println("test"); //_serialD->println("test");
_modbusmaster.begin(_baud, SERIAL_8N1);
#if defined(DIAG_IO) #if defined(DIAG_IO)
_display(); _display();
#endif #endif
@ -343,7 +343,7 @@ public:
// Display information about the device // Display information about the device
void _display() override { 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")); _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F("OK"));
} }
@ -368,7 +368,7 @@ public:
} }
protected: 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: public: