From d193b8cb1a2196acd6781dd0077b9a3f6d50d718 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 16 Sep 2019 16:27:35 +0200 Subject: [PATCH] Update TasmotaModbus to 1.2.0 Update TasmotaModbus to 1.2.0 --- .../README.md | 0 .../examples/modbustest/modbustest.ino | 0 .../keywords.txt | 0 .../library.json | 2 +- .../library.properties | 2 +- .../src/TasmotaModbus.cpp | 51 ++++++++++++------- .../src/TasmotaModbus.h | 23 ++++++--- 7 files changed, 49 insertions(+), 29 deletions(-) rename lib/{TasmotaModbus-1.1.1 => TasmotaModbus-1.2.0}/README.md (100%) rename lib/{TasmotaModbus-1.1.1 => TasmotaModbus-1.2.0}/examples/modbustest/modbustest.ino (100%) rename lib/{TasmotaModbus-1.1.1 => TasmotaModbus-1.2.0}/keywords.txt (100%) rename lib/{TasmotaModbus-1.1.1 => TasmotaModbus-1.2.0}/library.json (93%) rename lib/{TasmotaModbus-1.1.1 => TasmotaModbus-1.2.0}/library.properties (93%) rename lib/{TasmotaModbus-1.1.1 => TasmotaModbus-1.2.0}/src/TasmotaModbus.cpp (67%) rename lib/{TasmotaModbus-1.1.1 => TasmotaModbus-1.2.0}/src/TasmotaModbus.h (76%) diff --git a/lib/TasmotaModbus-1.1.1/README.md b/lib/TasmotaModbus-1.2.0/README.md similarity index 100% rename from lib/TasmotaModbus-1.1.1/README.md rename to lib/TasmotaModbus-1.2.0/README.md diff --git a/lib/TasmotaModbus-1.1.1/examples/modbustest/modbustest.ino b/lib/TasmotaModbus-1.2.0/examples/modbustest/modbustest.ino similarity index 100% rename from lib/TasmotaModbus-1.1.1/examples/modbustest/modbustest.ino rename to lib/TasmotaModbus-1.2.0/examples/modbustest/modbustest.ino diff --git a/lib/TasmotaModbus-1.1.1/keywords.txt b/lib/TasmotaModbus-1.2.0/keywords.txt similarity index 100% rename from lib/TasmotaModbus-1.1.1/keywords.txt rename to lib/TasmotaModbus-1.2.0/keywords.txt diff --git a/lib/TasmotaModbus-1.1.1/library.json b/lib/TasmotaModbus-1.2.0/library.json similarity index 93% rename from lib/TasmotaModbus-1.1.1/library.json rename to lib/TasmotaModbus-1.2.0/library.json index 75066a512..c9639e164 100644 --- a/lib/TasmotaModbus-1.1.1/library.json +++ b/lib/TasmotaModbus-1.2.0/library.json @@ -1,6 +1,6 @@ { "name": "TasmotaModbus", - "version": "1.1.1", + "version": "1.2.0", "keywords": [ "serial", "io", "TasmotaModbus" ], diff --git a/lib/TasmotaModbus-1.1.1/library.properties b/lib/TasmotaModbus-1.2.0/library.properties similarity index 93% rename from lib/TasmotaModbus-1.1.1/library.properties rename to lib/TasmotaModbus-1.2.0/library.properties index 709a71106..9197e7cfe 100644 --- a/lib/TasmotaModbus-1.1.1/library.properties +++ b/lib/TasmotaModbus-1.2.0/library.properties @@ -1,5 +1,5 @@ name=TasmotaModbus -version=1.1.1 +version=1.2.0 author=Theo Arends maintainer=Theo Arends sentence=Basic modbus wrapper for TasmotaSerial for ESP8266. diff --git a/lib/TasmotaModbus-1.1.1/src/TasmotaModbus.cpp b/lib/TasmotaModbus-1.2.0/src/TasmotaModbus.cpp similarity index 67% rename from lib/TasmotaModbus-1.1.1/src/TasmotaModbus.cpp rename to lib/TasmotaModbus-1.2.0/src/TasmotaModbus.cpp index 842d242b2..dde74fd7c 100644 --- a/lib/TasmotaModbus-1.1.1/src/TasmotaModbus.cpp +++ b/lib/TasmotaModbus-1.2.0/src/TasmotaModbus.cpp @@ -61,10 +61,10 @@ void TasmotaModbus::Send(uint8_t device_address, uint8_t function_code, uint16_t frame[0] = mb_address; // 0xFE default device address or dedicated like 0x01 frame[1] = function_code; - frame[2] = (uint8_t)(start_address >> 8); - frame[3] = (uint8_t)(start_address); - frame[4] = (uint8_t)(register_count >> 8); - frame[5] = (uint8_t)(register_count); + frame[2] = (uint8_t)(start_address >> 8); // MSB + frame[3] = (uint8_t)(start_address); // LSB + frame[4] = (uint8_t)(register_count >> 8); // MSB + frame[5] = (uint8_t)(register_count); // LSB uint16_t crc = CalculateCRC(frame, 6); frame[6] = (uint8_t)(crc); frame[7] = (uint8_t)(crc >> 8); @@ -80,35 +80,48 @@ bool TasmotaModbus::ReceiveReady() uint8_t TasmotaModbus::ReceiveBuffer(uint8_t *buffer, uint8_t register_count) { - uint8_t len = 0; + mb_len = 0; uint32_t last = millis(); - while ((available() > 0) && (len < (register_count *2) + 5) && (millis() - last < 10)) { + while ((available() > 0) && (mb_len < (register_count *2) + 5) && (millis() - last < 10)) { uint8_t data = (uint8_t)read(); - if (!len) { // Skip leading data as provided by hardware serial + if (!mb_len) { // Skip leading data as provided by hardware serial if (mb_address == data) { - buffer[len++] = data; + buffer[mb_len++] = data; } } else { - buffer[len++] = data; - if (3 == len) { + buffer[mb_len++] = data; + if (3 == mb_len) { if (buffer[1] & 0x80) { // 01 84 02 f2 f1 - return buffer[2]; // 1 = Illegal Function, 2 = Illegal Address, 3 = Illegal Data, 4 = Slave Error + return buffer[2]; // 1 = Illegal Function, + // 2 = Illegal Data Address, + // 3 = Illegal Data Value, + // 4 = Slave Error + // 5 = Acknowledge but not finished (no error) + // 6 = Slave Busy + // 8 = Memory Parity error + // 10 = Gateway Path Unavailable + // 11 = Gateway Target device failed to respond } } } last = millis(); } - if (len < 7) { return 7; } // 7 = Not enough data - if (len != buffer[2] + 5) { - buffer[2] = len - 5; // As it's wrong anyway let's store actual number received in here (5 will be added by client) - return 8; // 8 = Unexpected result + if (mb_len < 7) { return 7; } // 7 = Not enough data + +/* + if (mb_len != buffer[2] + 5) { + buffer[2] = mb_len - 5; // As it's wrong anyway let's store actual number received in here (5 will be added by client) + return 3; // 3 = Unexpected result + } +*/ + + uint16_t crc = (buffer[mb_len -1] << 8) | buffer[mb_len -2]; + if (CalculateCRC(buffer, mb_len -2) != crc) { + return 9; // 9 = crc error } - uint16_t crc = (buffer[len -1] << 8) | buffer[len -2]; - if (CalculateCRC(buffer, len -2) != crc) { return 9; } // 9 = crc error - - return 0; // 0 = No error + return 0; // 0 = No error } uint8_t TasmotaModbus::Receive16BitRegister(uint16_t *value) diff --git a/lib/TasmotaModbus-1.1.1/src/TasmotaModbus.h b/lib/TasmotaModbus-1.2.0/src/TasmotaModbus.h similarity index 76% rename from lib/TasmotaModbus-1.1.1/src/TasmotaModbus.h rename to lib/TasmotaModbus-1.2.0/src/TasmotaModbus.h index 5176aa89e..2138e0c9d 100644 --- a/lib/TasmotaModbus-1.1.1/src/TasmotaModbus.h +++ b/lib/TasmotaModbus-1.2.0/src/TasmotaModbus.h @@ -37,21 +37,28 @@ class TasmotaModbus : public TasmotaSerial { bool ReceiveReady(); /* Return codes: - * 0 - No error - * 1 - Illegal function - * 2 - Illegal address - * 3 - Illegal data - * 4 - Slave error - * 7 - Not enough minimal data received - * 8 - Not enough data receieved - * 9 - Crc error + * 0 = No error + * 1 = Illegal Function, + * 2 = Illegal Data Address, + * 3 = Illegal Data Value, + * 4 = Slave Error + * 5 = Acknowledge but not finished (no error) + * 6 = Slave Busy + * 7 = Not enough minimal data received + * 8 = Memory Parity error + * 9 = Crc error + * 10 = Gateway Path Unavailable + * 11 = Gateway Target device failed to respond */ uint8_t ReceiveBuffer(uint8_t *buffer, uint8_t register_count); uint8_t Receive16BitRegister(uint16_t *value); uint8_t Receive32BitRegister(float *value); + uint8_t ReceiveCount(void) { return mb_len; } + private: uint8_t mb_address; + uint8_t mb_len; }; #endif // TasmotaModbus_h