1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 21:01:25 +01:00

added analog, though still no positive test

This commit is contained in:
travis-farmer 2024-11-25 05:18:16 -05:00
parent 9fc3d1484e
commit 6d2586f88e
No known key found for this signature in database
GPG Key ID: 0BC296791D14CB35
2 changed files with 45 additions and 10 deletions

View File

@ -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);

View File

@ -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 {