mirror of https://github.com/arendst/Tasmota.git
Rework in progress
ModbusTCP fully tested OK Modbus via Commands: function 1..5 tested ok function 6 fail function 15 & 16 not tested
This commit is contained in:
parent
9c11eb41d4
commit
1ac5b6fd7f
|
@ -60,11 +60,14 @@ int TasmotaModbus::Begin(long speed, uint32_t config)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t TasmotaModbus::Send(uint8_t device_address, uint8_t function_code, uint16_t start_address, uint16_t register_count, uint16_t *registers, uint16_t bit_count, uint16_t byte_count)
|
uint8_t TasmotaModbus::Send(uint8_t device_address, uint8_t function_code, uint16_t start_address, uint16_t count, uint16_t *write_data)
|
||||||
{
|
{
|
||||||
uint8_t *frame;
|
uint8_t *frame;
|
||||||
uint8_t framepointer = 0;
|
uint8_t framepointer = 0;
|
||||||
if (byte_count == 0) byte_count = register_count * 2;
|
|
||||||
|
uint16_t byte_count = count * 2; // In register mode count is nr of registers (2 bytes)
|
||||||
|
if ((function_code == 1) || (function_code == 2) || (function_code == 15)) byte_count = (count / 8) + 1; // In bitmode count is nr of bits
|
||||||
|
|
||||||
if (function_code < 7)
|
if (function_code < 7)
|
||||||
{
|
{
|
||||||
frame = (uint8_t *)malloc(8); // Addres(1), Function(1), Start/Coil Address(2), Registercount or Data (2), CRC(2)
|
frame = (uint8_t *)malloc(8); // Addres(1), Function(1), Start/Coil Address(2), Registercount or Data (2), CRC(2)
|
||||||
|
@ -82,52 +85,44 @@ uint8_t TasmotaModbus::Send(uint8_t device_address, uint8_t function_code, uint1
|
||||||
frame[framepointer++] = (uint8_t)(start_address); // LSB
|
frame[framepointer++] = (uint8_t)(start_address); // LSB
|
||||||
if (function_code < 5)
|
if (function_code < 5)
|
||||||
{
|
{
|
||||||
frame[framepointer++] = (uint8_t)(register_count >> 8); // MSB
|
frame[framepointer++] = (uint8_t)(count >> 8); // MSB
|
||||||
frame[framepointer++] = (uint8_t)(register_count); // LSB
|
frame[framepointer++] = (uint8_t)(count); // LSB
|
||||||
}
|
}
|
||||||
else if ((function_code == 5) || (function_code == 6))
|
else if ((function_code == 5) || (function_code == 6))
|
||||||
{
|
{
|
||||||
if (registers == NULL)
|
if (write_data == NULL)
|
||||||
{
|
{
|
||||||
free(frame);
|
free(frame);
|
||||||
return 13; // Register data not specified
|
return 13; // Register data not specified
|
||||||
}
|
}
|
||||||
if (register_count != 1)
|
if (count != 1)
|
||||||
{
|
{
|
||||||
free(frame);
|
free(frame);
|
||||||
return 12; // Wrong register count
|
return 12; // Wrong register count
|
||||||
}
|
}
|
||||||
frame[framepointer++] = (uint8_t)(registers[0] >> 8); // MSB
|
frame[framepointer++] = (uint8_t)(write_data[0] >> 8); // MSB
|
||||||
frame[framepointer++] = (uint8_t)(registers[0]); // LSB
|
frame[framepointer++] = (uint8_t)(write_data[0]); // LSB
|
||||||
}
|
}
|
||||||
else if ((function_code == 15) || (function_code == 16))
|
else if ((function_code == 15) || (function_code == 16))
|
||||||
{
|
{
|
||||||
if (function_code == 15)
|
frame[framepointer++] = (uint8_t)(count >> 8); // MSB
|
||||||
{
|
frame[framepointer++] = (uint8_t)(count); // LSB
|
||||||
frame[framepointer++] = (uint8_t)(bit_count >> 8); // MSB
|
|
||||||
frame[framepointer++] = (uint8_t)(bit_count); // LSB
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
frame[framepointer++] = (uint8_t)(register_count >> 8); // MSB
|
|
||||||
frame[framepointer++] = (uint8_t)(register_count); // LSB
|
|
||||||
}
|
|
||||||
|
|
||||||
frame[framepointer++] = register_count * 2;
|
frame[framepointer++] = byte_count;
|
||||||
|
|
||||||
if (registers == NULL)
|
if (write_data == NULL)
|
||||||
{
|
{
|
||||||
free(frame);
|
free(frame);
|
||||||
return 13; // Register data not specified
|
return 13; // Register data not specified
|
||||||
}
|
}
|
||||||
if (register_count == 0)
|
if (count == 0)
|
||||||
{
|
{
|
||||||
free(frame);
|
free(frame);
|
||||||
return 12; // Wrong register count
|
return 12; // Wrong register count
|
||||||
}
|
}
|
||||||
for (int bytepointer = 0; bytepointer < byte_count; bytepointer++)
|
for (uint16_t bytepointer = 0; bytepointer < byte_count; bytepointer++)
|
||||||
{
|
{
|
||||||
frame[framepointer++] = (uint8_t)(registers[bytepointer/2] >> (byte_count % 2 ? 0 : 1)); // MSB, LSB, MSB ....
|
frame[framepointer++] = (uint8_t)(write_data[bytepointer/2] >> (byte_count % 2 ? 0 : 1)); // MSB, LSB, MSB ....
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -53,11 +53,12 @@ class TasmotaModbus : public TasmotaSerial {
|
||||||
* 13 = Register data not specified
|
* 13 = Register data not specified
|
||||||
* 14 = To many registers
|
* 14 = To many registers
|
||||||
*/
|
*/
|
||||||
uint8_t Send(uint8_t device_address, uint8_t function_code, uint16_t start_address, uint16_t register_count, uint16_t *registers = NULL, uint16_t bit_count = 0, uint16_t byte_count = 0);
|
uint8_t Send(uint8_t device_address, uint8_t function_code, uint16_t start_address, uint16_t count, uint16_t *writeData = NULL);
|
||||||
// Registercount is nr of registers for function code 3,4 and 16, 1 for function code 5 & 6 and nr of coils/inputs for function code 1,2 and 15
|
// Count is nr of registers for function code 3,4,6 and 16, nr of coils/inputs for function code 1,2 and 15 and must be 1 for function code 5 and 6
|
||||||
// Bitcount is used for function code 15 to specify how many bits have to be set
|
|
||||||
// Bytecount is used for function code 1,2 and 15 to specify how many bytes have to be written
|
|
||||||
uint8_t ReceiveBuffer(uint8_t *buffer, uint8_t register_count, uint16_t byte_count = 0);
|
uint8_t ReceiveBuffer(uint8_t *buffer, uint8_t register_count, uint16_t byte_count = 0);
|
||||||
|
// Bytecount is mandatory for functioncode 1 and 2 because they can return an odd number of bytes.
|
||||||
|
|
||||||
uint8_t Receive8BitRegister(uint8_t *value);
|
uint8_t Receive8BitRegister(uint8_t *value);
|
||||||
uint8_t Receive16BitRegister(uint16_t *value);
|
uint8_t Receive16BitRegister(uint16_t *value);
|
||||||
uint8_t Receive32BitRegister(float *value);
|
uint8_t Receive32BitRegister(float *value);
|
||||||
|
|
|
@ -34,9 +34,15 @@
|
||||||
* ModbusSend {"deviceaddress": 1, "functioncode": 3, "startaddress": 1, "type":"uint16", "count":2}
|
* ModbusSend {"deviceaddress": 1, "functioncode": 3, "startaddress": 1, "type":"uint16", "count":2}
|
||||||
*
|
*
|
||||||
* -- Write multiple coils --
|
* -- Write multiple coils --
|
||||||
* ModbusSend {"deviceaddress": 1, "functioncode": 15, "startaddress": 1, "type":"uint16", "count":4, "values":[1,2,3,4]}
|
* ModbusSend {"deviceaddress": 1, "functioncode": 15, "startaddress": 1, "type":"bit", "count":4, "values":[1,0,1,1]}
|
||||||
*
|
*
|
||||||
* Info for modbusBridgeTCPServer: https://ipc2u.com/articles/knowledge-base/detailed-description-of-the-modbus-tcp-protocol-with-command-examples/
|
* Info for modbusBridgeTCPServer:
|
||||||
|
* https://ipc2u.com/articles/knowledge-base/detailed-description-of-the-modbus-tcp-protocol-with-command-examples/
|
||||||
|
*
|
||||||
|
* Info for modbus serial communications:
|
||||||
|
* https://ozeki.hu/p_5879-mobdbus-function-code-4-read-input-registers.html
|
||||||
|
* https://www.modbustools.com/modbus.html
|
||||||
|
* https://ipc2u.com/articles/knowledge-base/modbus-rtu-made-simple-with-detailed-descriptions-and-examples/
|
||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
|
||||||
#define XDRV_63 63
|
#define XDRV_63 63
|
||||||
|
@ -243,7 +249,8 @@ void ModbusBridgeHandle(void)
|
||||||
if (modbusBridge.byteCount == 0) modbusBridge.byteCount = modbusBridge.dataCount * 2;
|
if (modbusBridge.byteCount == 0) modbusBridge.byteCount = modbusBridge.dataCount * 2;
|
||||||
buffer = (uint8_t *)malloc(9 + modbusBridge.byteCount); // Addres(1), Function(1), Length(1), Data(1..n), CRC(2)
|
buffer = (uint8_t *)malloc(9 + modbusBridge.byteCount); // Addres(1), Function(1), Length(1), Data(1..n), CRC(2)
|
||||||
memset(buffer, 0, 9 + modbusBridge.byteCount);
|
memset(buffer, 0, 9 + modbusBridge.byteCount);
|
||||||
uint32_t error = tasmotaModbus->ReceiveBuffer(buffer, modbusBridge.byteCount);
|
uint32_t error = tasmotaModbus->ReceiveBuffer(buffer, 0, modbusBridge.byteCount);
|
||||||
|
modbusBridge.byteCount = 0;
|
||||||
|
|
||||||
#ifdef USE_MODBUS_BRIDGE_TCP
|
#ifdef USE_MODBUS_BRIDGE_TCP
|
||||||
for (uint32_t i = 0; i < nitems(modbusBridgeTCP.client_tcp); i++)
|
for (uint32_t i = 0; i < nitems(modbusBridgeTCP.client_tcp); i++)
|
||||||
|
@ -376,6 +383,7 @@ void ModbusBridgeHandle(void)
|
||||||
else if ((buffer[1] > 0) && (buffer[1] < 7)) // Read Registers
|
else if ((buffer[1] > 0) && (buffer[1] < 7)) // Read Registers
|
||||||
{
|
{
|
||||||
uint8_t dataOffset = 3;
|
uint8_t dataOffset = 3;
|
||||||
|
if (buffer[1] == 6) dataOffset = 4;
|
||||||
Response_P(PSTR("{\"" D_JSON_MODBUS_RECEIVED "\":{"));
|
Response_P(PSTR("{\"" D_JSON_MODBUS_RECEIVED "\":{"));
|
||||||
ResponseAppend_P(PSTR("\"" D_JSON_MODBUS_DEVICE_ADDRESS "\":%d,"), buffer[0]);
|
ResponseAppend_P(PSTR("\"" D_JSON_MODBUS_DEVICE_ADDRESS "\":%d,"), buffer[0]);
|
||||||
ResponseAppend_P(PSTR("\"" D_JSON_MODBUS_FUNCTION_CODE "\":%d,"), buffer[1]);
|
ResponseAppend_P(PSTR("\"" D_JSON_MODBUS_FUNCTION_CODE "\":%d,"), buffer[1]);
|
||||||
|
@ -641,45 +649,55 @@ void ModbusTCPHandle(void)
|
||||||
uint8_t mbfunctioncode = (uint8_t)modbusBridgeTCP.tcp_buf[7];
|
uint8_t mbfunctioncode = (uint8_t)modbusBridgeTCP.tcp_buf[7];
|
||||||
uint16_t mbstartaddress = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[8]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[9]));
|
uint16_t mbstartaddress = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[8]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[9]));
|
||||||
uint16_t *writeData = NULL;
|
uint16_t *writeData = NULL;
|
||||||
uint16_t bitCount = 0;
|
uint16_t count = 0;
|
||||||
uint16_t registerCount = 0;
|
|
||||||
|
|
||||||
modbusBridgeTCP.tcp_transaction_id = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[0]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[1]));
|
modbusBridgeTCP.tcp_transaction_id = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[0]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[1]));
|
||||||
|
|
||||||
if (mbfunctioncode <= 2)
|
if (mbfunctioncode <= 2)
|
||||||
{
|
{
|
||||||
// Odd number of bytes for registers is not supported at this moment (TasmotaModbus reads registers (words) not bytes)
|
// Odd number of bytes for registers is not supported at this moment (TasmotaModbus reads registers (words) not bytes)
|
||||||
registerCount = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[10]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[11]));
|
count = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[10]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[11]));
|
||||||
modbusBridge.byteCount = ((registerCount - 1) >> 3) + 1;
|
modbusBridge.byteCount = ((count - 1) >> 3) + 1;
|
||||||
modbusBridge.dataCount = ((registerCount - 1) >> 4) + 1;
|
modbusBridge.dataCount = ((count - 1) >> 4) + 1;
|
||||||
}
|
}
|
||||||
else if (mbfunctioncode <= 4)
|
else if (mbfunctioncode <= 4)
|
||||||
{
|
{
|
||||||
registerCount = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[10]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[11]));
|
count = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[10]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[11]));
|
||||||
modbusBridge.byteCount = registerCount * 2;
|
modbusBridge.byteCount = count * 2;
|
||||||
modbusBridge.dataCount = registerCount;
|
modbusBridge.dataCount = count;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// For functioncode 15 & 16 ignore bytecount, tasmotaModbus does calculate this
|
// For functioncode 15 & 16 ignore bytecount, tasmotaModbus does calculate this
|
||||||
uint8_t dataStartByte = mbfunctioncode <= 6 ? 10 : 13;
|
uint8_t dataStartByte = mbfunctioncode <= 6 ? 10 : 13;
|
||||||
registerCount = (buf_len - dataStartByte) / 2;
|
uint16_t byteCount = (buf_len - dataStartByte);
|
||||||
modbusBridge.byteCount = 2;
|
modbusBridge.byteCount = 2;
|
||||||
modbusBridge.dataCount = 1;
|
modbusBridge.dataCount = 1;
|
||||||
writeData = (uint16_t *)malloc(registerCount);
|
|
||||||
if (mbfunctioncode == 15) bitCount = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[10]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[11]));
|
writeData = (uint16_t *)malloc((byteCount / 2)+1);
|
||||||
for (uint8_t dataPointer = 0; dataPointer < registerCount; dataPointer++)
|
|
||||||
|
if ((mbfunctioncode == 15) || (mbfunctioncode == 16)) count = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[10]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[11]));
|
||||||
|
else count = 1;
|
||||||
|
|
||||||
|
for (uint16_t dataPointer = 0; dataPointer < byteCount; dataPointer++)
|
||||||
{
|
{
|
||||||
writeData[dataPointer] = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[dataStartByte+(dataPointer*2)]) << 8)
|
if (dataPointer % 2 == 0)
|
||||||
| ((uint16_t)modbusBridgeTCP.tcp_buf[dataStartByte + 1 + (dataPointer*2)]));
|
{
|
||||||
AddLog(LOG_LEVEL_DEBUG_MORE, "%d=%04X", dataPointer, writeData[dataPointer]);
|
writeData[dataPointer / 2] = (uint16_t)(((uint16_t)modbusBridgeTCP.tcp_buf[dataStartByte + dataPointer]) << 8);
|
||||||
|
AddLog(LOG_LEVEL_DEBUG_MORE, "%d=%04X", dataPointer/2, writeData[dataPointer/2]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writeData[dataPointer / 2] |= ((uint16_t)modbusBridgeTCP.tcp_buf[dataStartByte + dataPointer]);
|
||||||
|
AddLog(LOG_LEVEL_DEBUG_MORE, "%d=%04X", dataPointer/2, writeData[dataPointer/2]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("MBS: MBRTCP to Modbus TransactionId:%d, deviceAddress:%d, functionCode:%d, startAddress:%d, registerCount:%d, recvCount:%d bitCount:%d bytecount:%d"),
|
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("MBS: MBRTCP to Modbus TransactionId:%d, deviceAddress:%d, functionCode:%d, startAddress:%d, count:%d, recvCount:%d"),
|
||||||
modbusBridgeTCP.tcp_transaction_id, mbdeviceaddress, mbfunctioncode, mbstartaddress, registerCount, modbusBridge.dataCount, bitCount, modbusBridge.byteCount);
|
modbusBridgeTCP.tcp_transaction_id, mbdeviceaddress, mbfunctioncode, mbstartaddress, count, modbusBridge.dataCount);
|
||||||
|
|
||||||
tasmotaModbus->Send(mbdeviceaddress, mbfunctioncode, mbstartaddress, registerCount, writeData, bitCount, modbusBridge.byteCount);
|
tasmotaModbus->Send(mbdeviceaddress, mbfunctioncode, mbstartaddress, count, writeData);
|
||||||
|
|
||||||
free(writeData);
|
free(writeData);
|
||||||
}
|
}
|
||||||
|
@ -731,7 +749,8 @@ void CmndModbusBridgeSend(void)
|
||||||
if (strcmp(stype, "int8") == 0)
|
if (strcmp(stype, "int8") == 0)
|
||||||
{
|
{
|
||||||
modbusBridge.type = ModbusBridgeType::mb_int8;
|
modbusBridge.type = ModbusBridgeType::mb_int8;
|
||||||
modbusBridge.dataCount = bitMode ? modbusBridge.dataCount = modbusBridge.count : ((modbusBridge.count - 1) / 2) + 1;
|
modbusBridge.dataCount = bitMode ? modbusBridge.count : ((modbusBridge.count - 1) / 2) + 1;
|
||||||
|
if (bitMode) modbusBridge.byteCount = (modbusBridge.count / 8) + 1;
|
||||||
}
|
}
|
||||||
else if (strcmp(stype, "int16") == 0)
|
else if (strcmp(stype, "int16") == 0)
|
||||||
{
|
{
|
||||||
|
@ -741,12 +760,12 @@ void CmndModbusBridgeSend(void)
|
||||||
else if (strcmp(stype, "int32") == 0)
|
else if (strcmp(stype, "int32") == 0)
|
||||||
{
|
{
|
||||||
modbusBridge.type = ModbusBridgeType::mb_int32;
|
modbusBridge.type = ModbusBridgeType::mb_int32;
|
||||||
modbusBridge.dataCount = bitMode ? modbusBridge.dataCount = modbusBridge.count : 2 * modbusBridge.count;
|
modbusBridge.dataCount = bitMode ? modbusBridge.count : 2 * modbusBridge.count;
|
||||||
}
|
}
|
||||||
else if ((strcmp(stype, "uint8") == 0))
|
else if ((strcmp(stype, "uint8") == 0))
|
||||||
{
|
{
|
||||||
modbusBridge.type = ModbusBridgeType::mb_uint8;
|
modbusBridge.type = ModbusBridgeType::mb_uint8;
|
||||||
modbusBridge.dataCount = bitMode ? modbusBridge.dataCount = modbusBridge.count : ((modbusBridge.count - 1) / 2) + 1;
|
modbusBridge.dataCount = bitMode ? modbusBridge.count : ((modbusBridge.count - 1) / 2) + 1;
|
||||||
}
|
}
|
||||||
else if ((strcmp(stype, "uint16") == 0) || (strcmp(stype, "") == 0)) // Default is uint16
|
else if ((strcmp(stype, "uint16") == 0) || (strcmp(stype, "") == 0)) // Default is uint16
|
||||||
{
|
{
|
||||||
|
@ -756,27 +775,27 @@ void CmndModbusBridgeSend(void)
|
||||||
else if (strcmp(stype, "uint32") == 0)
|
else if (strcmp(stype, "uint32") == 0)
|
||||||
{
|
{
|
||||||
modbusBridge.type = ModbusBridgeType::mb_uint32;
|
modbusBridge.type = ModbusBridgeType::mb_uint32;
|
||||||
modbusBridge.dataCount = bitMode ? modbusBridge.dataCount = modbusBridge.count : 2 * modbusBridge.count;
|
modbusBridge.dataCount = bitMode ? modbusBridge.count : 2 * modbusBridge.count;
|
||||||
}
|
}
|
||||||
else if (strcmp(stype, "float") == 0)
|
else if (strcmp(stype, "float") == 0)
|
||||||
{
|
{
|
||||||
modbusBridge.type = ModbusBridgeType::mb_float;
|
modbusBridge.type = ModbusBridgeType::mb_float;
|
||||||
modbusBridge.dataCount = bitMode ? 2 * modbusBridge.count : modbusBridge.dataCount = modbusBridge.count;
|
modbusBridge.dataCount = bitMode ? 2 * modbusBridge.count : modbusBridge.count;
|
||||||
}
|
}
|
||||||
else if (strcmp(stype, "raw") == 0)
|
else if (strcmp(stype, "raw") == 0)
|
||||||
{
|
{
|
||||||
modbusBridge.type = ModbusBridgeType::mb_raw;
|
modbusBridge.type = ModbusBridgeType::mb_raw;
|
||||||
modbusBridge.dataCount = bitMode ? modbusBridge.dataCount = modbusBridge.count : ((modbusBridge.count - 1) / 2) + 1;
|
modbusBridge.dataCount = bitMode ? modbusBridge.count : ((modbusBridge.count - 1) / 2) + 1;
|
||||||
}
|
}
|
||||||
else if (strcmp(stype, "hex") == 0)
|
else if (strcmp(stype, "hex") == 0)
|
||||||
{
|
{
|
||||||
modbusBridge.type = ModbusBridgeType::mb_hex;
|
modbusBridge.type = ModbusBridgeType::mb_hex;
|
||||||
modbusBridge.dataCount = bitMode ? modbusBridge.dataCount = modbusBridge.count : ((modbusBridge.count - 1) / 2) + 1;
|
modbusBridge.dataCount = bitMode ? modbusBridge.count : ((modbusBridge.count - 1) / 2) + 1;
|
||||||
}
|
}
|
||||||
else if (strcmp(stype, "bit") == 0)
|
else if (strcmp(stype, "bit") == 0)
|
||||||
{
|
{
|
||||||
modbusBridge.type = ModbusBridgeType::mb_bit;
|
modbusBridge.type = ModbusBridgeType::mb_bit;
|
||||||
modbusBridge.dataCount = bitMode ? modbusBridge.dataCount = modbusBridge.count : ((modbusBridge.count - 1) / 16) + 1 ;
|
modbusBridge.dataCount = bitMode ? modbusBridge.count : ((modbusBridge.count - 1) / 16) + 1 ;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
errorcode = ModbusBridgeError::wrongtype;
|
errorcode = ModbusBridgeError::wrongtype;
|
||||||
|
@ -785,7 +804,7 @@ void CmndModbusBridgeSend(void)
|
||||||
// of bit to write, so calculate the number data bytes to write.
|
// of bit to write, so calculate the number data bytes to write.
|
||||||
if (modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeMultipleCoils)
|
if (modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeMultipleCoils)
|
||||||
{
|
{
|
||||||
modbusBridge.dataCount = (((modbusBridge.count - 1) / 16) + 1);
|
modbusBridge.dataCount = modbusBridge.count;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prevent buffer overflow due to usage of to many registers
|
// Prevent buffer overflow due to usage of to many registers
|
||||||
|
@ -825,6 +844,7 @@ void CmndModbusBridgeSend(void)
|
||||||
case ModbusBridgeFunctionCode::mb_writeSingleRegister:
|
case ModbusBridgeFunctionCode::mb_writeSingleRegister:
|
||||||
case ModbusBridgeFunctionCode::mb_writeSingleCoil:
|
case ModbusBridgeFunctionCode::mb_writeSingleCoil:
|
||||||
if (modbusBridge.count != 1) errorcode = ModbusBridgeError::wrongcount;
|
if (modbusBridge.count != 1) errorcode = ModbusBridgeError::wrongcount;
|
||||||
|
break;
|
||||||
case ModbusBridgeFunctionCode::mb_writeMultipleRegisters:
|
case ModbusBridgeFunctionCode::mb_writeMultipleRegisters:
|
||||||
if (modbusBridge.count != writeDataSize) errorcode = ModbusBridgeError::wrongcount;
|
if (modbusBridge.count != writeDataSize) errorcode = ModbusBridgeError::wrongcount;
|
||||||
break;
|
break;
|
||||||
|
@ -931,10 +951,14 @@ void CmndModbusBridgeSend(void)
|
||||||
if ((modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeSingleCoil) || (modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeSingleRegister))
|
if ((modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeSingleCoil) || (modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_writeSingleRegister))
|
||||||
modbusBridge.dataCount = 1;
|
modbusBridge.dataCount = 1;
|
||||||
|
|
||||||
uint8_t error = tasmotaModbus->Send(modbusBridge.deviceAddress, (uint8_t)modbusBridge.functionCode, modbusBridge.startAddress, modbusBridge.dataCount, writeData, modbusBridge.count);
|
uint8_t error = tasmotaModbus->Send(modbusBridge.deviceAddress, (uint8_t)modbusBridge.functionCode, modbusBridge.startAddress, modbusBridge.dataCount, writeData);
|
||||||
if (error)
|
|
||||||
AddLog(LOG_LEVEL_DEBUG, PSTR("MBS: MBR Driver send error %u"), error);
|
|
||||||
free(writeData);
|
free(writeData);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
{
|
||||||
|
AddLog(LOG_LEVEL_DEBUG, PSTR("MBS: MBR Driver send error %u"), error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
ResponseCmndDone();
|
ResponseCmndDone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue