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

IO_Modbus: Works! pending further tests...

This commit is contained in:
travis-farmer 2024-11-28 16:59:13 -05:00
parent 367b86cd09
commit 411d5c0087
No known key found for this signature in database
GPG Key ID: 0BC296791D14CB35
2 changed files with 19 additions and 19 deletions

View File

@ -276,11 +276,11 @@ void Modbus::setTimeout(unsigned long timeout) {
ModbusRTUMasterError Modbus::readCoils(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity) { ModbusRTUMasterError Modbus::readCoils(uint8_t id, uint16_t startAddress, int buf[], uint16_t quantity) {
return _readValues(id, 1, startAddress, buf, quantity); return _readValues(id, 1, startAddress, buf, quantity);
} }
ModbusRTUMasterError Modbus::readDiscreteInputs(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity) { ModbusRTUMasterError Modbus::readDiscreteInputs(uint8_t id, uint16_t startAddress, int buf[], uint16_t quantity) {
return _readValues(id, 2, startAddress, buf, quantity); return _readValues(id, 2, startAddress, buf, quantity);
} }
@ -294,7 +294,7 @@ ModbusRTUMasterError Modbus::readInputRegisters(uint8_t id, uint16_t startAddres
ModbusRTUMasterError Modbus::writeSingleCoil(uint8_t id, uint16_t address, char value) { ModbusRTUMasterError Modbus::writeSingleCoil(uint8_t id, uint16_t address, int value) {
return _writeSingleValue(id, 5, address, ((value) ? 0xFF00 : 0x0000)); return _writeSingleValue(id, 5, address, ((value) ? 0xFF00 : 0x0000));
} }
@ -304,7 +304,7 @@ ModbusRTUMasterError Modbus::writeSingleHoldingRegister(uint8_t id, uint16_t add
ModbusRTUMasterError Modbus::writeMultipleCoils(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity) { ModbusRTUMasterError Modbus::writeMultipleCoils(uint8_t id, uint16_t startAddress, int buf[], uint16_t quantity) {
const uint8_t functionCode = 15; const uint8_t functionCode = 15;
if (id > 247) return MODBUS_RTU_MASTER_INVALID_ID; if (id > 247) return MODBUS_RTU_MASTER_INVALID_ID;
if (!buf) return MODBUS_RTU_MASTER_INVALID_BUFFER; if (!buf) return MODBUS_RTU_MASTER_INVALID_BUFFER;
@ -379,7 +379,7 @@ uint8_t Modbus::getExceptionResponse() {
ModbusRTUMasterError Modbus::_readValues(uint8_t id, uint8_t functionCode, uint16_t startAddress, char buf[], uint16_t quantity) { ModbusRTUMasterError Modbus::_readValues(uint8_t id, uint8_t functionCode, uint16_t startAddress, int buf[], uint16_t quantity) {
if (id < 1 || id > 247) return MODBUS_RTU_MASTER_INVALID_ID; if (id < 1 || id > 247) return MODBUS_RTU_MASTER_INVALID_ID;
if (!buf) return MODBUS_RTU_MASTER_INVALID_BUFFER; if (!buf) return MODBUS_RTU_MASTER_INVALID_BUFFER;
if (quantity == 0 || quantity > 2000) return MODBUS_RTU_MASTER_INVALID_QUANTITY; if (quantity == 0 || quantity > 2000) return MODBUS_RTU_MASTER_INVALID_QUANTITY;
@ -402,7 +402,7 @@ ModbusRTUMasterError Modbus::_readValues(uint8_t id, uint8_t functionCode, uint1
if (adu.getDataLen() != (1 + byteCount)) return MODBUS_RTU_MASTER_UNEXPECTED_LENGTH; if (adu.getDataLen() != (1 + byteCount)) return MODBUS_RTU_MASTER_UNEXPECTED_LENGTH;
if (adu.data[0] != byteCount) return MODBUS_RTU_MASTER_UNEXPECTED_BYTE_COUNT; if (adu.data[0] != byteCount) return MODBUS_RTU_MASTER_UNEXPECTED_BYTE_COUNT;
for (uint16_t i = 0; i < quantity; i++) { for (uint16_t i = 0; i < quantity; i++) {
buf[i] = (char) bitRead(adu.data[1 + (i >> 3)], i & 7); buf[i] = (int) bitRead(adu.data[1 + (i >> 3)], i & 7)? 1:0;
} }
return MODBUS_RTU_MASTER_SUCCESS; return MODBUS_RTU_MASTER_SUCCESS;
} }
@ -527,13 +527,13 @@ void Modbus::_loop(unsigned long currentMicros) {
if (error == 0) DIAG(F("ModbusHR: T%d Success!"), _currentNode->getNodeID()); if (error == 0) DIAG(F("ModbusHR: T%d Success!"), _currentNode->getNodeID());
#endif #endif
if (error != 0 && error != 3) flagOK = false; if (error != 0 && error != 3) flagOK = false;
error = writeMultipleCoils(_currentNode->getNodeID(), 0, (char*) _currentNode->coils, _currentNode->getNumCoils()); error = writeMultipleCoils(_currentNode->getNodeID(), 0, (int*) _currentNode->coils, _currentNode->getNumCoils());
if (error != 0 && error != 3) DIAG(F("ModbusMC: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumCoils(), errorStrings[error]); if (error != 0 && error != 3) DIAG(F("ModbusMC: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumCoils(), errorStrings[error]);
#ifdef DIAG_IO #ifdef DIAG_IO
if (error == 0) DIAG(F("ModbusMC: T%d Success!"), _currentNode->getNodeID()); if (error == 0) DIAG(F("ModbusMC: T%d Success!"), _currentNode->getNodeID());
#endif #endif
if (error != 0 && error != 3) flagOK = false; if (error != 0 && error != 3) flagOK = false;
error = readDiscreteInputs(_currentNode->getNodeID(), 0, (char*) _currentNode->discreteInputs, _currentNode->getNumDiscreteInputs()); error = readDiscreteInputs(_currentNode->getNodeID(), 0, (int*) _currentNode->discreteInputs, _currentNode->getNumDiscreteInputs());
if (error != 0 && error != 3) DIAG(F("ModbusDI: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumDiscreteInputs(), errorStrings[error]); if (error != 0 && error != 3) DIAG(F("ModbusDI: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumDiscreteInputs(), errorStrings[error]);
#ifdef DIAG_IO #ifdef DIAG_IO
if (error == 0) DIAG(F("ModbusDI: T%d Success!"), _currentNode->getNodeID()); if (error == 0) DIAG(F("ModbusDI: T%d Success!"), _currentNode->getNodeID());

View File

@ -168,10 +168,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[1968]; int *coils[100];
char *discreteInputs[2000]; int *discreteInputs[100];
uint16_t *holdingRegisters[123]; uint16_t *holdingRegisters[100];
uint16_t *inputRegisters[125]; uint16_t *inputRegisters[100];
uint8_t getNodeID() { uint8_t getNodeID() {
return _nodeID; return _nodeID;
@ -215,8 +215,8 @@ public:
void _write(VPIN vpin, int value) override { 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 (value == 1) coils[pin] = (char*) 0x1; if (value == 1) coils[pin] = (int*) 0x1;
if (value == 0) coils[pin] = (char*) 0x0; if (value == 0) coils[pin] = (int*) 0x0;
} }
int _readAnalogue(VPIN vpin) { int _readAnalogue(VPIN vpin) {
@ -309,7 +309,7 @@ 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
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, int buf[], uint16_t quantity);
ModbusRTUMasterError _readValues(uint8_t id, uint8_t functionCode, uint16_t startAddress, uint16_t 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); ModbusRTUMasterError _writeSingleValue(uint8_t id, uint8_t functionCode, uint16_t address, uint16_t value);
public: public:
@ -340,14 +340,14 @@ public:
#endif #endif
} }
ModbusRTUMasterError _translateCommError(ModbusRTUCommError commError); ModbusRTUMasterError _translateCommError(ModbusRTUCommError commError);
ModbusRTUMasterError readCoils(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity); ModbusRTUMasterError readCoils(uint8_t id, uint16_t startAddress, int buf[], uint16_t quantity);
ModbusRTUMasterError readDiscreteInputs(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity); ModbusRTUMasterError readDiscreteInputs(uint8_t id, uint16_t startAddress, int buf[], uint16_t quantity);
ModbusRTUMasterError readHoldingRegisters(uint8_t id, uint16_t startAddress, uint16_t 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 readInputRegisters(uint8_t id, uint16_t startAddress, uint16_t buf[], uint16_t quantity);
ModbusRTUMasterError writeSingleCoil(uint8_t id, uint16_t address, char value); ModbusRTUMasterError writeSingleCoil(uint8_t id, uint16_t address, int value);
ModbusRTUMasterError writeSingleHoldingRegister(uint8_t id, uint16_t address, uint16_t value); ModbusRTUMasterError writeSingleHoldingRegister(uint8_t id, uint16_t address, uint16_t value);
ModbusRTUMasterError writeMultipleCoils(uint8_t id, uint16_t startAddress, char buf[], uint16_t quantity); ModbusRTUMasterError writeMultipleCoils(uint8_t id, uint16_t startAddress, int buf[], uint16_t quantity);
ModbusRTUMasterError writeMultipleHoldingRegisters(uint8_t id, uint16_t startAddress, uint16_t buf[], uint16_t quantity); ModbusRTUMasterError writeMultipleHoldingRegisters(uint8_t id, uint16_t startAddress, uint16_t buf[], uint16_t quantity);
// Loop function (overriding IODevice::_loop(unsigned long)) // Loop function (overriding IODevice::_loop(unsigned long))
void _loop(unsigned long currentMicros) override; void _loop(unsigned long currentMicros) override;