1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-01-11 13:21:01 +01:00

fixed some errors

This commit is contained in:
travis-farmer 2024-11-26 17:18:14 -05:00
parent 8afb11f520
commit 83130f0c4a
No known key found for this signature in database
GPG Key ID: 0BC296791D14CB35
2 changed files with 30 additions and 53 deletions

View File

@ -282,11 +282,11 @@ void ModbusRTUMaster::begin(unsigned long baud, uint32_t config) {
ModbusRTUMasterError ModbusRTUMaster::readCoils(uint8_t id, uint16_t startAddress, bool buf[], uint16_t quantity) {
ModbusRTUMasterError ModbusRTUMaster::readCoils(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity) {
return _readValues(id, 1, startAddress, buf, quantity);
}
ModbusRTUMasterError ModbusRTUMaster::readDiscreteInputs(uint8_t id, uint16_t startAddress, bool buf[], uint16_t quantity) {
ModbusRTUMasterError ModbusRTUMaster::readDiscreteInputs(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity) {
return _readValues(id, 2, startAddress, buf, quantity);
}
@ -300,7 +300,7 @@ ModbusRTUMasterError ModbusRTUMaster::readInputRegisters(uint8_t id, uint16_t st
ModbusRTUMasterError ModbusRTUMaster::writeSingleCoil(uint8_t id, uint16_t address, bool value) {
ModbusRTUMasterError ModbusRTUMaster::writeSingleCoil(uint8_t id, uint16_t address, char value) {
return _writeSingleValue(id, 5, address, ((value) ? 0xFF00 : 0x0000));
}
@ -310,7 +310,7 @@ ModbusRTUMasterError ModbusRTUMaster::writeSingleHoldingRegister(uint8_t id, uin
ModbusRTUMasterError ModbusRTUMaster::writeMultipleCoils(uint8_t id, uint16_t startAddress, bool buf[], uint16_t quantity) {
ModbusRTUMasterError ModbusRTUMaster::writeMultipleCoils(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity) {
const uint8_t functionCode = 15;
if (id > 247) return MODBUS_RTU_MASTER_INVALID_ID;
if (!buf) return MODBUS_RTU_MASTER_INVALID_BUFFER;
@ -385,7 +385,7 @@ uint8_t ModbusRTUMaster::getExceptionResponse() {
ModbusRTUMasterError ModbusRTUMaster::_readValues(uint8_t id, uint8_t functionCode, uint16_t startAddress, bool buf[], uint16_t quantity) {
ModbusRTUMasterError ModbusRTUMaster::_readValues(uint8_t id, uint8_t functionCode, uint16_t startAddress, char buf[], uint16_t quantity) {
if (id < 1 || id > 247) return MODBUS_RTU_MASTER_INVALID_ID;
if (!buf) return MODBUS_RTU_MASTER_INVALID_BUFFER;
if (quantity == 0 || quantity > 2000) return MODBUS_RTU_MASTER_INVALID_QUANTITY;
@ -526,25 +526,22 @@ void Modbus::_loop(unsigned long currentMicros) {
//DIAG(F("Modbus Loop: %d : %d :: %d"), _currentMicros, _cycleStartTime, _currentNode);
if (_currentMicros - _cycleStartTime < _cycleTime) return;
_cycleStartTime = _currentMicros;
DIAG(F("Tick"));
if (_currentNode == NULL) return;
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" };
uint8_t error;
error = _modbusmaster->writeMultipleHoldingRegisters(_currentNode->getNodeID(), 0, _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]);
//error = _modbusmaster->writeMultipleCoils(_currentNode->getNodeID(), 0, _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]);
if (error == MODBUS_RTU_MASTER_EXCEPTION_RESPONSE) {
DIAG(F(": %s"), _modbusmaster->getExceptionResponse());
}
//error = _modbusmaster->readDiscreteInputs(_currentNode->getNodeID(), 0, _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]);
//error = _modbusmaster->readInputRegisters(_currentNode->getNodeID(), 0, _currentNode->inputRegisters, _currentNode->getNumInputRegisters());
//DIAG(F("ModbusIR: %d %d %d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumInputRegisters(), errorStrings[error]);
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]);
_currentNode = _currentNode->getNext();
//delayUntil(_currentMicros + _cycleTime * 1000UL);
}
@ -567,10 +564,7 @@ Modbusnode::Modbusnode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t nodeID,
_numDiscreteInputs = numDiscreteInputs;
_numHoldingRegisters = numHoldingRegisters;
_numInputRegisters = numInputRegisters;
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

@ -149,14 +149,14 @@ class ModbusRTUMaster {
void setTimeout(unsigned long timeout);
void begin(unsigned long baud, uint32_t config = SERIAL_8N1);
ModbusRTUMasterError readCoils(uint8_t id, uint16_t startAddress, bool buf[], uint16_t quantity);
ModbusRTUMasterError readDiscreteInputs(uint8_t id, uint16_t startAddress, bool buf[], uint16_t quantity);
ModbusRTUMasterError readCoils(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity);
ModbusRTUMasterError readDiscreteInputs(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity);
ModbusRTUMasterError readHoldingRegisters(uint8_t id, uint16_t startAddress, uint16_t buf[], uint16_t quantity);
ModbusRTUMasterError readInputRegisters(uint8_t id, uint16_t startAddress, uint16_t buf[], uint16_t quantity);
ModbusRTUMasterError writeSingleCoil(uint8_t id, uint16_t address, bool value);
ModbusRTUMasterError writeSingleCoil(uint8_t id, uint16_t address, char value);
ModbusRTUMasterError writeSingleHoldingRegister(uint8_t id, uint16_t address, uint16_t value);
ModbusRTUMasterError writeMultipleCoils(uint8_t id, uint16_t startAddress, bool buf[], uint16_t quantity);
ModbusRTUMasterError writeMultipleCoils(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity);
ModbusRTUMasterError writeMultipleHoldingRegisters(uint8_t id, uint16_t startAddress, uint16_t buf[], uint16_t quantity);
uint8_t getExceptionResponse();
@ -165,7 +165,7 @@ class ModbusRTUMaster {
ModbusRTUComm _rtuComm;
uint8_t _exceptionResponse = 0;
ModbusRTUMasterError _readValues(uint8_t id, uint8_t functionCode, uint16_t startAddress, bool buf[], uint16_t quantity);
ModbusRTUMasterError _readValues(uint8_t id, uint8_t functionCode, uint16_t startAddress, char buf[], uint16_t quantity);
ModbusRTUMasterError _readValues(uint8_t id, uint8_t functionCode, uint16_t startAddress, uint16_t buf[], uint16_t quantity);
ModbusRTUMasterError _writeSingleValue(uint8_t id, uint8_t functionCode, uint16_t address, uint16_t value);
@ -199,10 +199,10 @@ public:
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);
bool *coils;
bool *discreteInputs;
uint16_t *holdingRegisters;
uint16_t *inputRegisters;
char *coils[100];
char *discreteInputs[100];
uint16_t *holdingRegisters[100];
uint16_t *inputRegisters[100];
uint8_t getNodeID() {
return _nodeID;
@ -240,7 +240,7 @@ public:
// Return current state from this device
uint16_t pin = vpin - _firstVpin;
if (pin < _numDiscreteInputs) {
return discreteInputs[pin];
return discreteInputs[pin]? 1:0;
} else
return 0;
}
@ -248,7 +248,7 @@ public:
int _readAnalogue(VPIN vpin) override {
// Return acquired data value, e.g.
int pin = vpin - _firstVpin - _numDiscreteInputs;
return inputRegisters[pin];
return (int) inputRegisters[pin];
}
void _write(VPIN vpin, int value) override {
@ -256,7 +256,9 @@ public:
uint16_t pin = vpin - _firstVpin - _numDiscreteInputs - _numInputRegisters;
if (pin < _numCoils) {
if (value)
coils[pin] = value;
if (value == 1) coils[pin] = (char*) 0x1;
if (value == 0) coils[pin] = (char*) 0x0;
//coils[pin] = value;
else
coils[pin];
}
@ -266,32 +268,12 @@ public:
uint16_t pin = vpin - _firstVpin - _numDiscreteInputs - _numInputRegisters - _numCoils;
if (pin < _numHoldingRegisters) {
if (value)
holdingRegisters[pin] = value;
holdingRegisters[pin] = (uint16_t*) value;
else
holdingRegisters[pin];
}
}
void saveIncomingData(uint8_t index, uint8_t data) {
if (index < _numDiscreteInputs)
discreteInputs[index] = data;
}
uint8_t getOutputStates(uint8_t index) {
if (index < _numCoils)
return coils[index];
else
return 0;
}
uint16_t getNumInputs() {
return _numDiscreteInputs;
}
uint16_t getNumOutputs() {
return _numCoils;
}
char getType() {
return _type;
}
@ -348,8 +330,9 @@ public:
// Device-specific initialisation
void _begin() override {
ModbusRTUMaster _modbusmaster(*_serialD, _transmitEnablePin, -1);
_serialD->begin(_baud);
_modbusmaster.begin(_baud);
_serialD->begin(_baud, SERIAL_8N1);
//_serialD->println("test");
_modbusmaster.begin(_baud, SERIAL_8N1);
#if defined(DIAG_IO)
_display();
#endif