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

IO_Modbus: works great now!

This commit is contained in:
travis-farmer 2024-11-30 13:33:28 -05:00
parent a85fa6d9c2
commit 9837cff4a5
No known key found for this signature in database
GPG Key ID: 0BC296791D14CB35
2 changed files with 23 additions and 8 deletions

View File

@ -226,6 +226,7 @@ ModbusRTUCommError ModbusRTUComm::readAdu(ModbusADU& adu) {
//if (millis() - startMillis >= _readTimeout) return MODBUS_RTU_COMM_TIMEOUT; //if (millis() - startMillis >= _readTimeout) return MODBUS_RTU_COMM_TIMEOUT;
_waiting_for_read = true; _waiting_for_read = true;
if (millis() - startMillis >= _readTimeout) { if (millis() - startMillis >= _readTimeout) {
//_serial.flush();
return MODBUS_RTU_COMM_TIMEOUT; return MODBUS_RTU_COMM_TIMEOUT;
} else { } else {
return MODBUS_RTU_COMM_WAITING; return MODBUS_RTU_COMM_WAITING;
@ -534,25 +535,35 @@ void Modbus::_loop(unsigned long currentMicros) {
switch (_operationCount) { switch (_operationCount) {
case 0: case 0:
error = writeMultipleHoldingRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->holdingRegisters, _currentNode->getNumHoldingRegisters()); error = writeMultipleHoldingRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->holdingRegisters, _currentNode->getNumHoldingRegisters());
if (error != 0 && error != 3) DIAG(F("ModbusHR: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumHoldingRegisters(), errorStrings[error]); if (error != 0 && error != MODBUS_RTU_MASTER_WAITING) DIAG(F("ModbusHR: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumHoldingRegisters(), errorStrings[error]);
if (error != 0 && error != 3) flagOK = false; if (error != 0 && (error != MODBUS_RTU_MASTER_WAITING || _waitCounter > 2)) flagOK = false;
break; break;
case 1: case 1:
error = writeMultipleCoils(_currentNode->getNodeID(), 0, (int*) _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 != MODBUS_RTU_MASTER_WAITING) DIAG(F("ModbusMC: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumCoils(), errorStrings[error]);
if (error != 0 && error != 3) flagOK = false; if (error != 0 && (error != MODBUS_RTU_MASTER_WAITING || _waitCounter > 2)) flagOK = false;
break; break;
case 2: case 2:
error = readDiscreteInputs(_currentNode->getNodeID(), 0, (int*) _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 != MODBUS_RTU_MASTER_WAITING) DIAG(F("ModbusDI: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumDiscreteInputs(), errorStrings[error]);
if (error != 0 && error != 3) flagOK = false; if (error != 0 && (error != MODBUS_RTU_MASTER_WAITING || _waitCounter > 2)) flagOK = false;
break; break;
case 3: case 3:
error = readInputRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->inputRegisters, _currentNode->getNumInputRegisters()); error = readInputRegisters(_currentNode->getNodeID(), 0, (uint16_t*) _currentNode->inputRegisters, _currentNode->getNumInputRegisters());
if (error != 0 && error != 3) DIAG(F("ModbusIR: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumInputRegisters(), errorStrings[error]); if (error != 0 && error != MODBUS_RTU_MASTER_WAITING) DIAG(F("ModbusIR: T%d F%d N%d %s"), _currentNode->getNodeID(), 0, _currentNode->getNumInputRegisters(), errorStrings[error]);
if (error != 0 && error != 3) flagOK = false; if (error != 0 && (error != MODBUS_RTU_MASTER_WAITING || _waitCounter > 2)) flagOK = false;
break; break;
} }
if (error == MODBUS_RTU_MASTER_WAITING) {
if (_waitCounter > 10) {
_resetWaiting();
_waitCounter = 0;
} else {
_waitCounter++;
}
} else {
_waitCounter = 0;
}
if (error != MODBUS_RTU_MASTER_WAITING) { if (error != MODBUS_RTU_MASTER_WAITING) {
if (_operationCount < 3) { if (_operationCount < 3) {
_operationCount++; _operationCount++;

View File

@ -315,6 +315,10 @@ private:
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, 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);
int _waitCounter = 0;
void _resetWaiting() {
_rtuComm._waiting_for_read = false;
}
public: public:
static void create(uint8_t busNo, HardwareSerial& serial, unsigned long baud, uint16_t cycleTimeMS=500, int8_t txPin=-1) { static void create(uint8_t busNo, HardwareSerial& serial, unsigned long baud, uint16_t cycleTimeMS=500, int8_t txPin=-1) {
new Modbus(busNo, serial, baud, cycleTimeMS, txPin); new Modbus(busNo, serial, baud, cycleTimeMS, txPin);