Added RX and TX logging in TasmotaModbus driver

This commit is contained in:
JeroenSt 2022-08-27 09:11:36 +02:00
parent a5782f2ca0
commit 2e111dc112
1 changed files with 44 additions and 15 deletions

View File

@ -19,8 +19,13 @@
Documentation about modbus protocol: https://www.modbustools.com/modbus.html Documentation about modbus protocol: https://www.modbustools.com/modbus.html
*/ */
#include "Arduino.h"
#include "TasmotaModbus.h" #include "TasmotaModbus.h"
extern void AddLog(uint32_t loglevel, PGM_P formatP, ...);
enum LoggingLevels {LOG_LEVEL_NONE, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG_MORE};
TasmotaModbus::TasmotaModbus(int receive_pin, int transmit_pin) : TasmotaSerial(receive_pin, transmit_pin, 1) TasmotaModbus::TasmotaModbus(int receive_pin, int transmit_pin) : TasmotaSerial(receive_pin, transmit_pin, 1)
{ {
mb_address = 0; mb_address = 0;
@ -136,6 +141,16 @@ uint8_t TasmotaModbus::Send(uint8_t device_address, uint8_t function_code, uint1
frame[framepointer++] = (uint8_t)(crc); frame[framepointer++] = (uint8_t)(crc);
frame[framepointer++] = (uint8_t)(crc >> 8); frame[framepointer++] = (uint8_t)(crc >> 8);
uint8_t *buf;
uint16_t bufsize=(framepointer + 1) * 3;
buf = (uint8_t *)malloc(bufsize);
memset(buf, 0, bufsize);
uint16_t i;
for (i = 0; i < framepointer;i++)
snprintf((char *)&buf[i*3], (bufsize-i*3), "%02X ",frame[i]);
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("MBS: MBR Serial Send: %s"), buf);
free(buf);
flush(); flush();
write(frame, framepointer); write(frame, framepointer);
free(frame); free(frame);
@ -162,8 +177,30 @@ uint8_t TasmotaModbus::ReceiveBuffer(uint8_t *buffer, uint8_t data_count)
} else { } else {
buffer[mb_len++] = data; buffer[mb_len++] = data;
if (3 == mb_len) { if (3 == mb_len) {
if (buffer[1] & 0x80) { // 01 84 02 f2 f1 if ((buffer[1] == 5) || (buffer[1] == 6) || (buffer[1] == 15) || (buffer[1] == 16)) header_length = 4; // Addr, Func, StartAddr
if (0 == buffer[2]) { }
}
timeout = millis() + 10;
}
}
// RX Logging
uint8_t *buf;
uint16_t bufsize=(mb_len + 1) * 3;
buf = (uint8_t *)malloc(bufsize);
memset(buf, 0, bufsize);
uint16_t i;
for (i = 0; i < mb_len;i++)
snprintf((char *)&buf[i*3], (bufsize-i*3), "%02X ",buffer[i]);
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("MBS: MBR Serial Received: %s"), buf);
free(buf);
if (buffer[1] & 0x80)
{ // 01 84 02 f2 f1
if (0 == buffer[2])
{
return 3; // 3 = Illegal Data Value, return 3; // 3 = Illegal Data Value,
} }
return buffer[2]; // 1 = Illegal Function, return buffer[2]; // 1 = Illegal Function,
@ -176,14 +213,6 @@ uint8_t TasmotaModbus::ReceiveBuffer(uint8_t *buffer, uint8_t data_count)
// 10 = Gateway Path Unavailable // 10 = Gateway Path Unavailable
// 11 = Gateway Target device failed to respond // 11 = Gateway Target device failed to respond
} }
if ((buffer[1] == 5) || (buffer[1] == 6) || (buffer[1] == 15) || (buffer[1] == 16)) header_length = 4; // Addr, Func, StartAddr
}
}
timeout = millis() + 10;
}
}
if (mb_len < 6) { return 7; } // 7 = Not enough data if (mb_len < 6) { return 7; } // 7 = Not enough data