mirror of https://github.com/arendst/Tasmota.git
Added and fixed comments Co-authored-by: JeroenSt <nospam@nospam.org>
This commit is contained in:
parent
1d0c19311d
commit
c5612a50fb
|
@ -200,7 +200,8 @@ uint8_t TasmotaModbus::ReceiveBuffer(uint8_t *buffer, uint8_t register_count, ui
|
|||
} else {
|
||||
buffer[mb_len++] = data;
|
||||
if (3 == mb_len) {
|
||||
if ((buffer[1] == 5) || (buffer[1] == 6) || (buffer[1] == 15) || (buffer[1] == 16)) header_length = 4; // Addr, Func, StartAddr
|
||||
// If functioncode is 5,6,15 or 16 the header length is 4 instead of 3
|
||||
if ((buffer[1] == 5) || (buffer[1] == 6) || (buffer[1] == 15) || (buffer[1] == 16)) header_length = 4;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -163,13 +163,13 @@ struct ModbusBridge
|
|||
ModbusBridgeFunctionCode functionCode = ModbusBridgeFunctionCode::mb_undefined;
|
||||
ModbusBridgeType type = ModbusBridgeType::mb_undefined;
|
||||
|
||||
uint16_t dataCount = 0;
|
||||
uint16_t byteCount = 0;
|
||||
uint16_t startAddress = 0;
|
||||
uint8_t deviceAddress = 0;
|
||||
uint8_t count = 0;
|
||||
uint16_t dataCount = 0; // Number of bits or registers to read/write
|
||||
uint16_t byteCount = 0; // Number of bytes to read/write
|
||||
uint16_t startAddress = 0; // First address to read/write
|
||||
uint8_t deviceAddress = 0; // Modbus address of device to read
|
||||
uint8_t count = 0; // Number of values to read / write
|
||||
bool raw = false;
|
||||
uint8_t *buffer = nullptr;
|
||||
uint8_t *buffer = nullptr; // Buffer for storing read / write data
|
||||
};
|
||||
|
||||
ModbusBridge modbusBridge;
|
||||
|
@ -240,7 +240,7 @@ void ModbusBridgeSetBaudrate(uint32_t baudrate)
|
|||
|
||||
/********************************************************************************************/
|
||||
//
|
||||
// Handles data received from tasmota modbus wrapper and send this to (TCP or) MQTT client
|
||||
// Handles data received from tasmota modbus wrapper and send this to (TCP and/or) MQTT client
|
||||
//
|
||||
void ModbusBridgeHandle(void)
|
||||
{
|
||||
|
@ -248,7 +248,7 @@ void ModbusBridgeHandle(void)
|
|||
if (data_ready)
|
||||
{
|
||||
if (modbusBridge.byteCount == 0) modbusBridge.byteCount = modbusBridge.dataCount * 2;
|
||||
if (nullptr == modbusBridge.buffer)
|
||||
if (nullptr == modbusBridge.buffer) // If buffer is not initialized do not process received data
|
||||
{
|
||||
ModbusBridgeAllocError(PSTR("read"));
|
||||
modbusBridge.dataCount = 0;
|
||||
|
@ -256,7 +256,7 @@ void ModbusBridgeHandle(void)
|
|||
return;
|
||||
}
|
||||
memset(modbusBridge.buffer, 0, MBR_RECEIVE_BUFFER_SIZE);
|
||||
uint32_t error = modbusBridgeModbus->ReceiveBuffer(modbusBridge.buffer, 0, modbusBridge.byteCount);
|
||||
uint32_t error = modbusBridgeModbus->ReceiveBuffer(modbusBridge.buffer, 0, MBR_RECEIVE_BUFFER_SIZE - 9);
|
||||
|
||||
#ifdef USE_MODBUS_BRIDGE_TCP
|
||||
for (uint32_t i = 0; i < nitems(modbusBridgeTCP.client_tcp); i++)
|
||||
|
@ -281,25 +281,16 @@ void ModbusBridgeHandle(void)
|
|||
nrOfBytes += 1;
|
||||
client.write(header, 9);
|
||||
}
|
||||
else if (modbusBridge.buffer[1] <= 2)
|
||||
{
|
||||
header[4] = modbusBridge.byteCount >> 8;
|
||||
header[5] = modbusBridge.byteCount + 3;
|
||||
header[8] = modbusBridge.byteCount;
|
||||
client.write(header, 9);
|
||||
nrOfBytes += 1;
|
||||
client.write(modbusBridge.buffer + 3, modbusBridge.byteCount); // Don't send CRC
|
||||
nrOfBytes += modbusBridge.byteCount;
|
||||
}
|
||||
else if (modbusBridge.buffer[1] <= 4)
|
||||
{
|
||||
header[4] = modbusBridge.byteCount >> 8;
|
||||
header[5] = modbusBridge.byteCount + 3;
|
||||
header[8] = modbusBridge.byteCount;
|
||||
uint8_t received_data_bytes = modbusBridgeModbus->ReceiveCount() - 5;
|
||||
header[4] = received_data_bytes >> 8;
|
||||
header[5] = received_data_bytes + 3;
|
||||
header[8] = received_data_bytes;
|
||||
client.write(header, 9);
|
||||
nrOfBytes += 1;
|
||||
client.write(modbusBridge.buffer + 3, modbusBridge.byteCount); // Don't send CRC
|
||||
nrOfBytes += modbusBridge.byteCount;
|
||||
client.write(modbusBridge.buffer + 3, received_data_bytes); // Don't send CRC
|
||||
nrOfBytes += received_data_bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -343,20 +334,24 @@ void ModbusBridgeHandle(void)
|
|||
errorcode = ModbusBridgeError::wrongfunctioncode;
|
||||
else if ((uint8_t)modbusBridge.functionCode < 5)
|
||||
{
|
||||
// Do not check buffer[2] but received bytes for correct length but use the nr of received bytes
|
||||
uint8_t received_data_bytes = modbusBridgeModbus->ReceiveCount() - 5;
|
||||
|
||||
if ((uint8_t)modbusBridge.functionCode < 3)
|
||||
{
|
||||
if ((uint8_t)(((modbusBridge.dataCount - 1) >> 3) + 1) != (uint8_t)modbusBridge.buffer[2])
|
||||
// Check if returned number of bits matches the requested number of bits
|
||||
if ((uint8_t)(((modbusBridge.dataCount - 1) >> 3) + 1) > received_data_bytes)
|
||||
errorcode = ModbusBridgeError::wrongdataCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((modbusBridge.type == ModbusBridgeType::mb_int8 || modbusBridge.type == ModbusBridgeType::mb_uint8) && ((uint8_t)modbusBridge.dataCount * 2 != (uint8_t)modbusBridge.buffer[2]))
|
||||
if ((modbusBridge.type == ModbusBridgeType::mb_int8 || modbusBridge.type == ModbusBridgeType::mb_uint8) && ((uint8_t)modbusBridge.dataCount > received_data_bytes))
|
||||
errorcode = ModbusBridgeError::wrongdataCount;
|
||||
else if ((modbusBridge.type == ModbusBridgeType::mb_bit) && ((uint8_t)modbusBridge.dataCount * 2 != (uint8_t)modbusBridge.buffer[2]))
|
||||
else if ((modbusBridge.type == ModbusBridgeType::mb_bit) && ((uint8_t)modbusBridge.dataCount > received_data_bytes))
|
||||
errorcode = ModbusBridgeError::wrongdataCount;
|
||||
else if ((modbusBridge.type == ModbusBridgeType::mb_int16 || modbusBridge.type == ModbusBridgeType::mb_uint16) && ((uint8_t)modbusBridge.dataCount * 2 != (uint8_t)modbusBridge.buffer[2]))
|
||||
else if ((modbusBridge.type == ModbusBridgeType::mb_int16 || modbusBridge.type == ModbusBridgeType::mb_uint16) && ((uint8_t)modbusBridge.dataCount > received_data_bytes))
|
||||
errorcode = ModbusBridgeError::wrongdataCount;
|
||||
else if ((modbusBridge.type == ModbusBridgeType::mb_int32 || modbusBridge.type == ModbusBridgeType::mb_uint32 || modbusBridge.type == ModbusBridgeType::mb_float) && ((uint8_t)modbusBridge.dataCount * 2 != (uint8_t)modbusBridge.buffer[2]))
|
||||
else if ((modbusBridge.type == ModbusBridgeType::mb_int32 || modbusBridge.type == ModbusBridgeType::mb_uint32 || modbusBridge.type == ModbusBridgeType::mb_float) && ((uint8_t)modbusBridge.dataCount > received_data_bytes))
|
||||
errorcode = ModbusBridgeError::wrongdataCount;
|
||||
}
|
||||
}
|
||||
|
@ -364,6 +359,7 @@ void ModbusBridgeHandle(void)
|
|||
{
|
||||
if (modbusBridge.type == ModbusBridgeType::mb_raw)
|
||||
{
|
||||
// Ouput raw data as decimal bytes
|
||||
Response_P(PSTR("{\"" D_JSON_MODBUS_RECEIVED "\":{\"RAW\":["));
|
||||
for (uint8_t i = 0; i < modbusBridgeModbus->ReceiveCount(); i++)
|
||||
{
|
||||
|
@ -377,6 +373,7 @@ void ModbusBridgeHandle(void)
|
|||
}
|
||||
else if (modbusBridge.type == ModbusBridgeType::mb_hex)
|
||||
{
|
||||
// Output raw data as hexadecimal bytes
|
||||
Response_P(PSTR("{\"" D_JSON_MODBUS_RECEIVED "\":{\"HEX\":["));
|
||||
for (uint8_t i = 0; i < modbusBridgeModbus->ReceiveCount(); i++)
|
||||
{
|
||||
|
@ -388,8 +385,9 @@ void ModbusBridgeHandle(void)
|
|||
ResponseJsonEnd();
|
||||
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_MODBUS_RECEIVED));
|
||||
}
|
||||
else if ((modbusBridge.buffer[1] > 0) && (modbusBridge.buffer[1] < 7)) // Read Registers
|
||||
else if ((modbusBridge.buffer[1] > 0) && (modbusBridge.buffer[1] < 7))
|
||||
{
|
||||
// Read and process Registers
|
||||
uint8_t dataOffset = 3;
|
||||
Response_P(PSTR("{\"" D_JSON_MODBUS_RECEIVED "\":{"));
|
||||
ResponseAppend_P(PSTR("\"" D_JSON_MODBUS_DEVICE_ADDRESS "\":%d,"), modbusBridge.buffer[0]);
|
||||
|
@ -410,6 +408,7 @@ void ModbusBridgeHandle(void)
|
|||
uint8_t data_count = modbusBridge.count;
|
||||
if ((uint8_t)modbusBridge.functionCode < 3)
|
||||
{
|
||||
// Calculate number of values to return in bitmode
|
||||
if (modbusBridge.type == ModbusBridgeType::mb_int8 || modbusBridge.type == ModbusBridgeType::mb_uint8)
|
||||
data_count = (uint8_t)(((modbusBridge.count - 1) >> 3) + 1);
|
||||
else if (modbusBridge.type == ModbusBridgeType::mb_int16 || modbusBridge.type == ModbusBridgeType::mb_uint16)
|
||||
|
@ -426,7 +425,7 @@ void ModbusBridgeHandle(void)
|
|||
float value = 0;
|
||||
if (modbusBridge.buffer[1] < 3)
|
||||
{
|
||||
// In bit mode only convert returned bytes
|
||||
// In bit mode only convert returned bits to values
|
||||
if (modbusBridge.buffer[2] - (count * 4))
|
||||
((uint8_t *)&value)[0] = modbusBridge.buffer[dataOffset + (count * 4)]; // Get float values
|
||||
if ((modbusBridge.buffer[2] - (count * 4)) >> 1)
|
||||
|
@ -468,6 +467,7 @@ void ModbusBridgeHandle(void)
|
|||
uint32_t value = 0;
|
||||
if (modbusBridge.buffer[1] < 3)
|
||||
{
|
||||
// In bit mode only convert returned bits to values
|
||||
if (modbusBridge.buffer[2] - (count * 4))
|
||||
((uint8_t *)&value)[0] = modbusBridge.buffer[dataOffset + (count * 4)]; // Get uint values
|
||||
if (modbusBridge.buffer[2] - ((count * 4) - 1))
|
||||
|
@ -495,6 +495,7 @@ void ModbusBridgeHandle(void)
|
|||
uint16_t value = 0;
|
||||
if (modbusBridge.buffer[1] < 3)
|
||||
{
|
||||
// In bit mode only convert returned bits to values
|
||||
if (modbusBridge.buffer[2] - (count * 2))
|
||||
((uint8_t *)&value)[0] = modbusBridge.buffer[dataOffset + (count * 2)];
|
||||
if (modbusBridge.buffer[2] - ((count * 2) - 1))
|
||||
|
@ -747,10 +748,10 @@ void CmndModbusBridgeSend(void)
|
|||
modbusBridge.startAddress = root.getULong(PSTR(D_JSON_MODBUS_START_ADDRESS), 0);
|
||||
|
||||
const char *stype = root.getStr(PSTR(D_JSON_MODBUS_TYPE), "uint8");
|
||||
modbusBridge.count = root.getUInt(PSTR(D_JSON_MODBUS_COUNT), 1); // Number of bits or bytes to read / write
|
||||
modbusBridge.count = root.getUInt(PSTR(D_JSON_MODBUS_COUNT), 1); // Number of values to read / write
|
||||
|
||||
// If functioncode is 1, 2 or 15, the count is not the number of registers but the number
|
||||
// of bit to read or write, so calculate the number data bytes to read/write.
|
||||
// of bits to read or write, so calculate the number data bytes to read/write.
|
||||
if ((functionCode == 1) || (functionCode == 2) || (functionCode == 15)) bitMode = true;
|
||||
|
||||
if (modbusBridge.deviceAddress == 0)
|
||||
|
|
Loading…
Reference in New Issue