Work in progress...

This commit is contained in:
JeroenSt 2022-08-17 19:55:41 +02:00
parent 2c2a73506d
commit 92319eedd2
1 changed files with 26 additions and 16 deletions

View File

@ -102,7 +102,8 @@ enum class ModbusBridgeError
wrongstartaddress = 4, wrongstartaddress = 4,
wrongtype = 5, wrongtype = 5,
wrongregistercount = 6, wrongregistercount = 6,
wrongcount = 7 wrongcount = 7,
tomanydata = 8
}; };
enum class ModbusBridgeFunctionCode enum class ModbusBridgeFunctionCode
@ -150,7 +151,6 @@ struct ModbusBridge
uint8_t deviceAddress = 0; uint8_t deviceAddress = 0;
uint8_t count = 0; uint8_t count = 0;
bool raw = false; bool raw = false;
}; };
ModbusBridge modbusBridge; ModbusBridge modbusBridge;
@ -161,8 +161,10 @@ ModbusBridge modbusBridge;
// //
bool ModbusBridgeBegin(void) bool ModbusBridgeBegin(void)
{ {
if ((Settings->modbus_sbaudrate < 1) || (Settings->modbus_sbaudrate > (115200 / 300))) Settings->modbus_sbaudrate = (uint8_t)((uint32_t)MBR_BAUDRATE / 300); if ((Settings->modbus_sbaudrate < 1) || (Settings->modbus_sbaudrate > (115200 / 300)))
if (Settings->modbus_sconfig > TS_SERIAL_8O2) Settings->modbus_sconfig = TS_SERIAL_8N1; Settings->modbus_sbaudrate = (uint8_t)((uint32_t)MBR_BAUDRATE / 300);
if (Settings->modbus_sconfig > TS_SERIAL_8O2)
Settings->modbus_sconfig = TS_SERIAL_8N1;
int result = tasmotaModbus->Begin(Settings->modbus_sbaudrate * 300, ConvertSerialConfig(Settings->modbus_sconfig)); // Reinitialize modbus port with new baud rate int result = tasmotaModbus->Begin(Settings->modbus_sbaudrate * 300, ConvertSerialConfig(Settings->modbus_sconfig)); // Reinitialize modbus port with new baud rate
if (result) if (result)
@ -553,6 +555,12 @@ void CmndModbusBridgeSend(void)
// If write data is specified in JSON copy it into writeData array // If write data is specified in JSON copy it into writeData array
JsonParserArray jsonDataArray = root[PSTR(D_JSON_MODBUS_DATA)].getArray(); JsonParserArray jsonDataArray = root[PSTR(D_JSON_MODBUS_DATA)].getArray();
if (jsonDataArray.isArray()) if (jsonDataArray.isArray())
{
if (modbusBridge.registerCount > 40)
{
errorcode = ModbusBridgeError::tomanydata;
}
else
{ {
writeDataSize = jsonDataArray.size(); writeDataSize = jsonDataArray.size();
@ -569,6 +577,7 @@ void CmndModbusBridgeSend(void)
} }
} }
} }
}
// Handle errorcode and exit function when an error has occured // Handle errorcode and exit function when an error has occured
if (errorcode != ModbusBridgeError::noerror) if (errorcode != ModbusBridgeError::noerror)
@ -579,7 +588,8 @@ void CmndModbusBridgeSend(void)
} }
// If writing a single coil or single register, the register count is always 1. We also prevent writing data out of range // If writing a single coil or single register, the register count is always 1. We also prevent writing data out of range
if ((modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeSingleCoil) || (modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeSingleRegister)) modbusBridge.registerCount = 1; if ((modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeSingleCoil) || (modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeSingleRegister))
modbusBridge.registerCount = 1;
tasmotaModbus->Send(modbusBridge.deviceAddress, (uint8_t)modbusBridge.functionCode, modbusBridge.startAddress, modbusBridge.registerCount, writeData); tasmotaModbus->Send(modbusBridge.deviceAddress, (uint8_t)modbusBridge.functionCode, modbusBridge.startAddress, modbusBridge.registerCount, writeData);
free(writeData); free(writeData);