mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-12-25 05:31:24 +01:00
added analog, though still no positive test
This commit is contained in:
parent
9fc3d1484e
commit
6d2586f88e
@ -522,14 +522,21 @@ void Modbus::_loop(unsigned long currentMicros) {
|
|||||||
// If we're between read/write cycles then don't do anything else.
|
// If we're between read/write cycles then don't do anything else.
|
||||||
if (_currentMicros - _cycleStartTime < _cycleTime) return;
|
if (_currentMicros - _cycleStartTime < _cycleTime) return;
|
||||||
}
|
}
|
||||||
|
_cycleStartTime = _currentMicros;
|
||||||
if (_currentNode == NULL) return;
|
if (_currentNode == NULL) return;
|
||||||
|
|
||||||
uint8_t error;
|
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());
|
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());
|
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
|
// Link to chain of CMRI bus instances
|
||||||
@ -541,13 +548,15 @@ Modbus *Modbus::_busList = NULL;
|
|||||||
************************************************************/
|
************************************************************/
|
||||||
|
|
||||||
// Constructor for Modbusnode object
|
// 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;
|
_firstVpin = firstVpin;
|
||||||
_nPins = nPins;
|
_nPins = nPins;
|
||||||
_busNo = busNo;
|
_busNo = busNo;
|
||||||
_nodeID = nodeID;
|
_nodeID = nodeID;
|
||||||
coils[numCoils];
|
coils[numCoils];
|
||||||
discreteInputs[numDiscreteInputs];
|
discreteInputs[numDiscreteInputs];
|
||||||
|
holdingRegisters[numHoldingRegisters];
|
||||||
|
inputRegisters[numInputRegisters];
|
||||||
|
|
||||||
if ((unsigned int)_nPins < numDiscreteInputs + numCoils)
|
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);
|
DIAG(F("Modbusnode: bus:%d nodeID:%d WARNING number of Vpins does not cover all inputs and outputs"), _busNo, _nodeID);
|
||||||
|
38
IO_Modbus.h
38
IO_Modbus.h
@ -191,14 +191,18 @@ private:
|
|||||||
bool _initialised = false;
|
bool _initialised = false;
|
||||||
uint8_t numCoils;
|
uint8_t numCoils;
|
||||||
uint8_t numDiscreteInputs;
|
uint8_t numDiscreteInputs;
|
||||||
|
uint8_t numHoldingRegisters;
|
||||||
|
uint8_t numInputRegisters;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void create(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID, uint8_t numCoils=0, uint8_t numDiscreteInputs=0) {
|
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);
|
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 *coils;
|
||||||
bool *discreteInputs;
|
bool *discreteInputs;
|
||||||
|
uint16_t *holdingRegisters;
|
||||||
|
uint16_t *inputRegisters;
|
||||||
|
|
||||||
uint8_t getNodeID() {
|
uint8_t getNodeID() {
|
||||||
return _nodeID;
|
return _nodeID;
|
||||||
@ -209,7 +213,12 @@ public:
|
|||||||
uint8_t getNumDisInputs() {
|
uint8_t getNumDisInputs() {
|
||||||
return numDiscreteInputs;
|
return numDiscreteInputs;
|
||||||
}
|
}
|
||||||
|
uint8_t getNumHoldingRegisters() {
|
||||||
|
return numHoldingRegisters;
|
||||||
|
}
|
||||||
|
uint8_t getNumInputRegisters() {
|
||||||
|
return numInputRegisters;
|
||||||
|
}
|
||||||
Modbusnode *getNext() {
|
Modbusnode *getNext() {
|
||||||
return _next;
|
return _next;
|
||||||
}
|
}
|
||||||
@ -227,7 +236,7 @@ public:
|
|||||||
_initialised = false;
|
_initialised = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int _read(VPIN vpin) {
|
int _read(VPIN vpin) override {
|
||||||
// Return current state from this device
|
// Return current state from this device
|
||||||
uint16_t pin = vpin - _firstVpin;
|
uint16_t pin = vpin - _firstVpin;
|
||||||
if (pin < numDiscreteInputs) {
|
if (pin < numDiscreteInputs) {
|
||||||
@ -236,7 +245,13 @@ public:
|
|||||||
return 0;
|
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
|
// Update current state for this device, in preparation the bus transmission
|
||||||
uint16_t pin = vpin - _firstVpin - numDiscreteInputs;
|
uint16_t pin = vpin - _firstVpin - numDiscreteInputs;
|
||||||
if (pin < numCoils) {
|
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) {
|
void saveIncomingData(uint8_t index, uint8_t data) {
|
||||||
if (index < numDiscreteInputs)
|
if (index < numDiscreteInputs)
|
||||||
discreteInputs[index] = data;
|
discreteInputs[index] = data;
|
||||||
@ -319,6 +344,7 @@ public:
|
|||||||
}
|
}
|
||||||
HardwareSerial *_serial;
|
HardwareSerial *_serial;
|
||||||
ModbusRTUMaster *modbusmaster;
|
ModbusRTUMaster *modbusmaster;
|
||||||
|
|
||||||
const char* errorStrings[];
|
const char* errorStrings[];
|
||||||
// Device-specific initialisation
|
// Device-specific initialisation
|
||||||
void _begin() override {
|
void _begin() override {
|
||||||
|
Loading…
Reference in New Issue
Block a user