mirror of https://github.com/arendst/Tasmota.git
Changed Modbus to also support nr of bytes
for functioncode 1,2 & 15. https://ozeki.hu/p_5876-mobdbus-function-code-1-read-coils.html
This commit is contained in:
parent
5bd28954fa
commit
9c11eb41d4
|
@ -60,18 +60,18 @@ int TasmotaModbus::Begin(long speed, uint32_t config)
|
|||
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)
|
||||
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 *frame;
|
||||
uint8_t framepointer = 0;
|
||||
|
||||
if (byte_count == 0) byte_count = register_count * 2;
|
||||
if (function_code < 7)
|
||||
{
|
||||
frame = (uint8_t *)malloc(8); // Addres(1), Function(1), Start/Coil Address(2), Registercount or Data (2), CRC(2)
|
||||
}
|
||||
else
|
||||
{
|
||||
frame = (uint8_t *)malloc(9 + (register_count * 2)); // Addres(1), Function(1), Start/Coil Address(2),Quantity of registers (2), Bytecount(1), Data(1..n), CRC(2)
|
||||
frame = (uint8_t *)malloc(9 + byte_count); // Addres(1), Function(1), Start/Coil Address(2),Quantity of registers (2), Bytecount(1), Data(1..n), CRC(2)
|
||||
}
|
||||
|
||||
mb_address = device_address; // Save address for receipt check
|
||||
|
@ -125,10 +125,9 @@ uint8_t TasmotaModbus::Send(uint8_t device_address, uint8_t function_code, uint1
|
|||
free(frame);
|
||||
return 12; // Wrong register count
|
||||
}
|
||||
for (int registerpointer = 0; registerpointer < register_count; registerpointer++)
|
||||
for (int bytepointer = 0; bytepointer < byte_count; bytepointer++)
|
||||
{
|
||||
frame[framepointer++] = (uint8_t)(registers[registerpointer] >> 8); // MSB
|
||||
frame[framepointer++] = (uint8_t)(registers[registerpointer]); // LSB
|
||||
frame[framepointer++] = (uint8_t)(registers[bytepointer/2] >> (byte_count % 2 ? 0 : 1)); // MSB, LSB, MSB ....
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -162,12 +161,13 @@ bool TasmotaModbus::ReceiveReady()
|
|||
return (available() > 4);
|
||||
}
|
||||
|
||||
uint8_t TasmotaModbus::ReceiveBuffer(uint8_t *buffer, uint8_t data_count)
|
||||
uint8_t TasmotaModbus::ReceiveBuffer(uint8_t *buffer, uint8_t register_count, uint16_t byte_count)
|
||||
{
|
||||
mb_len = 0;
|
||||
uint32_t timeout = millis() + 10;
|
||||
uint8_t header_length = 3;
|
||||
while ((mb_len < (data_count * 2) + header_length + 2) && (millis() < timeout)) {
|
||||
if (byte_count == 0) byte_count = (register_count * 2);
|
||||
while ((mb_len < byte_count + header_length + 2) && (millis() < timeout)) {
|
||||
if (available()) {
|
||||
uint8_t data = (uint8_t)read();
|
||||
if (!mb_len) { // Skip leading data as provided by hardware serial
|
||||
|
|
|
@ -53,8 +53,11 @@ class TasmotaModbus : public TasmotaSerial {
|
|||
* 13 = Register data not specified
|
||||
* 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);
|
||||
uint8_t ReceiveBuffer(uint8_t *buffer, uint8_t register_count);
|
||||
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);
|
||||
// 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
|
||||
// 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 Receive8BitRegister(uint8_t *value);
|
||||
uint8_t Receive16BitRegister(uint16_t *value);
|
||||
uint8_t Receive32BitRegister(float *value);
|
||||
|
|
|
@ -155,6 +155,7 @@ struct ModbusBridge
|
|||
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;
|
||||
|
@ -239,9 +240,10 @@ void ModbusBridgeHandle(void)
|
|||
if (data_ready)
|
||||
{
|
||||
uint8_t *buffer;
|
||||
buffer = (uint8_t *)malloc(9 + (modbusBridge.dataCount * 2)); // Addres(1), Function(1), Length(1), Data(1..n), CRC(2)
|
||||
memset(buffer, 0, 9 + (modbusBridge.dataCount * 2));
|
||||
uint32_t error = tasmotaModbus->ReceiveBuffer(buffer, modbusBridge.dataCount);
|
||||
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)
|
||||
memset(buffer, 0, 9 + modbusBridge.byteCount);
|
||||
uint32_t error = tasmotaModbus->ReceiveBuffer(buffer, modbusBridge.byteCount);
|
||||
|
||||
#ifdef USE_MODBUS_BRIDGE_TCP
|
||||
for (uint32_t i = 0; i < nitems(modbusBridgeTCP.client_tcp); i++)
|
||||
|
@ -268,23 +270,23 @@ void ModbusBridgeHandle(void)
|
|||
}
|
||||
else if (buffer[1] <= 2)
|
||||
{
|
||||
header[4] = ((modbusBridge.dataCount * 2) + 3) >> 8;
|
||||
header[5] = (modbusBridge.dataCount * 2) + 3;
|
||||
header[8] = modbusBridge.dataCount * 2;
|
||||
header[4] = modbusBridge.byteCount >> 8;
|
||||
header[5] = modbusBridge.byteCount + 3;
|
||||
header[8] = modbusBridge.byteCount;
|
||||
client.write(header, 9);
|
||||
nrOfBytes += 1;
|
||||
client.write(buffer + 3, modbusBridge.dataCount * 2); // Don't send CRC
|
||||
nrOfBytes += modbusBridge.dataCount * 2;
|
||||
client.write(buffer + 3, modbusBridge.byteCount); // Don't send CRC
|
||||
nrOfBytes += modbusBridge.byteCount;
|
||||
}
|
||||
else if (buffer[1] <= 4)
|
||||
{
|
||||
header[4] = ((modbusBridge.dataCount * 2) + 3) >> 8;
|
||||
header[5] = (modbusBridge.dataCount * 2) + 3;
|
||||
header[8] = modbusBridge.dataCount * 2;
|
||||
header[4] = modbusBridge.byteCount >> 8;
|
||||
header[5] = modbusBridge.byteCount + 3;
|
||||
header[8] = modbusBridge.byteCount;
|
||||
client.write(header, 9);
|
||||
nrOfBytes += 1;
|
||||
client.write(buffer + 3, (modbusBridge.dataCount * 2)); // Don't send CRC
|
||||
nrOfBytes += modbusBridge.dataCount * 2;
|
||||
client.write(buffer + 3, modbusBridge.byteCount); // Don't send CRC
|
||||
nrOfBytes += modbusBridge.byteCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -646,13 +648,15 @@ void ModbusTCPHandle(void)
|
|||
|
||||
if (mbfunctioncode <= 2)
|
||||
{
|
||||
// Odd number of bytes for registers is not supported at this moment
|
||||
// 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]));
|
||||
modbusBridge.byteCount = ((registerCount - 1) >> 3) + 1;
|
||||
modbusBridge.dataCount = ((registerCount - 1) >> 4) + 1;
|
||||
}
|
||||
else if (mbfunctioncode <= 4)
|
||||
{
|
||||
registerCount = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[10]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[11]));
|
||||
modbusBridge.byteCount = registerCount * 2;
|
||||
modbusBridge.dataCount = registerCount;
|
||||
}
|
||||
else
|
||||
|
@ -660,6 +664,7 @@ void ModbusTCPHandle(void)
|
|||
// For functioncode 15 & 16 ignore bytecount, tasmotaModbus does calculate this
|
||||
uint8_t dataStartByte = mbfunctioncode <= 6 ? 10 : 13;
|
||||
registerCount = (buf_len - dataStartByte) / 2;
|
||||
modbusBridge.byteCount = 2;
|
||||
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]));
|
||||
|
@ -671,10 +676,10 @@ void ModbusTCPHandle(void)
|
|||
}
|
||||
}
|
||||
|
||||
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("MBS: MBRTCP to Modbus TransactionId:%d, deviceAddress:%d, functionCode:%d, startAddress:%d, registerCount:%d, recvCount:%d bitCount:%d"),
|
||||
modbusBridgeTCP.tcp_transaction_id, mbdeviceaddress, mbfunctioncode, mbstartaddress, registerCount, modbusBridge.dataCount, bitCount);
|
||||
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"),
|
||||
modbusBridgeTCP.tcp_transaction_id, mbdeviceaddress, mbfunctioncode, mbstartaddress, registerCount, modbusBridge.dataCount, bitCount, modbusBridge.byteCount);
|
||||
|
||||
tasmotaModbus->Send(mbdeviceaddress, mbfunctioncode, mbstartaddress, registerCount, writeData, bitCount);
|
||||
tasmotaModbus->Send(mbdeviceaddress, mbfunctioncode, mbstartaddress, registerCount, writeData, bitCount, modbusBridge.byteCount);
|
||||
|
||||
free(writeData);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue