mirror of https://github.com/arendst/Tasmota.git
Added ModbusBridge
This commit is contained in:
parent
1f2e69272f
commit
1cc091e4fa
|
@ -183,6 +183,7 @@ Note: `minimal` variant is not listed as it shouldn't be used outside of the [up
|
|||
| USE_NOVA_SDS | - | - / x | - | x | - | - |
|
||||
| USE_HPMA | - | - / x | - | x | - | - |
|
||||
| USE_SERIAL_BRIDGE | - | x / x | x | x | - | x |
|
||||
| USE_MODBUS_BRIDGE | - | - / - | - | - | - | - |
|
||||
| USE_MP3_PLAYER | - | - / x | - | x | - | - |
|
||||
| USE_AZ7798 | - | - / - | - | - | - | - |
|
||||
| USE_PN532_HSU | - | - / x | - | x | - | - |
|
||||
|
|
|
@ -300,6 +300,8 @@ const be_const_member_t lv_gpio_constants[] = {
|
|||
{ "ZIGBEE_RST", (int32_t) GPIO_ZIGBEE_RST },
|
||||
{ "ZIGBEE_RX", (int32_t) GPIO_ZIGBEE_RX },
|
||||
{ "ZIGBEE_TX", (int32_t) GPIO_ZIGBEE_TX },
|
||||
{ "MBR_RX", (int32_t) GPIO_MBR_RX },
|
||||
{ "MBR_TX", (int32_t) GPIO_MBR_TX },
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -235,6 +235,7 @@
|
|||
#define USE_HRE // Add support for Badger HR-E Water Meter (+1k4 code)
|
||||
//#define USE_A4988_STEPPER // Add support for A4988/DRV8825 stepper-motor-driver-circuit (+10k5 code)
|
||||
//#define USE_THERMOSTAT // Add support for Thermostat
|
||||
//#define USE_MODBUS_BRIDGE // Add support for software Modbus Bridge
|
||||
#undef DEBUG_THEO // Disable debug code
|
||||
#undef USE_DEBUG_DRIVER // Disable debug code
|
||||
#endif // FIRMWARE_SENSORS
|
||||
|
|
|
@ -194,6 +194,7 @@ enum UserSelectablePins {
|
|||
GPIO_TM1621_CS, GPIO_TM1621_WR, GPIO_TM1621_RD, GPIO_TM1621_DAT, // Sonoff POWR3xxD and THR3xxD LCD display
|
||||
GPIO_REL1_BI, GPIO_REL1_BI_INV, // 8 x Relays bistable
|
||||
GPIO_I2S_MCLK,
|
||||
GPIO_MBR_TX, GPIO_MBR_RX, // Modbus Bridge Serial interface
|
||||
GPIO_SENSOR_END };
|
||||
|
||||
// Error as warning to rethink GPIO usage
|
||||
|
@ -435,6 +436,7 @@ const char kSensorNames[] PROGMEM =
|
|||
D_GPIO_TM1621_CS "|" D_GPIO_TM1621_WR "|" D_GPIO_TM1621_RD "|" D_GPIO_TM1621_DAT "|"
|
||||
D_SENSOR_RELAY "_b|" D_SENSOR_RELAY "_bi|"
|
||||
D_SENSOR_I2S_MCLK "|"
|
||||
D_SENSOR_MBR_TX "|" D_SENSOR_MBR_RX "|"
|
||||
;
|
||||
|
||||
const char kSensorNamesFixed[] PROGMEM =
|
||||
|
@ -853,6 +855,10 @@ const uint16_t kGpioNiceList[] PROGMEM = {
|
|||
AGPIO(GPIO_SBR_TX), // Serial Bridge Serial interface
|
||||
AGPIO(GPIO_SBR_RX), // Serial Bridge Serial interface
|
||||
#endif
|
||||
#ifdef USE_MODBUS_BRIDGE
|
||||
AGPIO(GPIO_MBR_TX), // Modbus Bridge Serial interface
|
||||
AGPIO(GPIO_MBR_RX), // Modbus Bridge Serial interface
|
||||
#endif
|
||||
#ifdef USE_TCP_BRIDGE
|
||||
AGPIO(GPIO_TCP_TX), // TCP Serial bridge
|
||||
AGPIO(GPIO_TCP_RX), // TCP Serial bridge
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA TX"
|
||||
#define D_SENSOR_SBR_RX "SerBr RX"
|
||||
#define D_SENSOR_SBR_TX "SerBr TX"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 TX"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA - TX"
|
||||
#define D_SENSOR_SBR_RX "SerBr - RX"
|
||||
#define D_SENSOR_SBR_TX "SerBr - TX"
|
||||
#define D_SENSOR_MBR_TX "ModBr - Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr - Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri - TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech - RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 - TX"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -673,6 +673,8 @@
|
|||
#define D_SENSOR_HPMA_TX "HPMA Tx"
|
||||
#define D_SENSOR_SBR_RX "SerBr Rx"
|
||||
#define D_SENSOR_SBR_TX "SerBr Tx"
|
||||
#define D_SENSOR_MBR_TX "ModBr Tx"
|
||||
#define D_SENSOR_MBR_RX "ModBr Rx"
|
||||
#define D_SENSOR_SR04_TRIG "SR04 Tri/TX"
|
||||
#define D_SENSOR_SR04_ECHO "SR04 Ech/RX"
|
||||
#define D_SENSOR_SDM72_TX "SDM72 Tx"
|
||||
|
|
|
@ -782,6 +782,7 @@
|
|||
//#define USE_VINDRIKTNING // Add support for IKEA VINDRIKTNING particle concentration sensor (+0k6 code)
|
||||
// #define VINDRIKTNING_SHOW_PM1 // Display undocumented/supposed PM1.0 values
|
||||
// #define VINDRIKTNING_SHOW_PM10 // Display undocumented/supposed PM10 values
|
||||
#define USE_MODBUS_BRIDGE // Add support for software Modbus Bridge
|
||||
|
||||
// -- Power monitoring sensors --------------------
|
||||
#define USE_ENERGY_SENSOR // Add support for Energy Monitors (+14k code)
|
||||
|
|
|
@ -0,0 +1,438 @@
|
|||
/*
|
||||
xdrv_100_modbus_bridge.ino - modbus bridge support for Tasmota
|
||||
|
||||
Copyright (C) 2021 Theo Arends and Dániel Zoltán Tolnai
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/*
|
||||
Example Command: modbussend {"deviceaddress": 1, "functioncode": 3, "startaddress": 1, "type":"uint8", "count":4}
|
||||
*/
|
||||
|
||||
#ifdef USE_MODBUS_BRIDGE
|
||||
/*********************************************************************************************\
|
||||
* Modbus Bridge using Modbus library (TasmotaModbus)
|
||||
\*********************************************************************************************/
|
||||
|
||||
#define XDRV_100 100
|
||||
#define HARDWARE_FALLBACK 2
|
||||
#define MBR_MAX_VALUE_LENGTH 30
|
||||
#define MBR_SPEED TM_MODBUS_BAUDRATE
|
||||
#define MBR_MAX_REGISTERS 64
|
||||
|
||||
#define D_CMND_MODBUS_SEND "ModbusSend"
|
||||
#define D_CMND_MODBUS_SETBAUDRATE "ModbusSetBaudrate"
|
||||
#define D_CMND_MODBUS_SETSERIALCONFIG "ModbusSetSerialConfig"
|
||||
|
||||
#define D_JSON_MODBUS_RECEIVED "ModbusReceived"
|
||||
#define D_JSON_MODBUS_DEVICE_ADDRESS "DeviceAddress"
|
||||
#define D_JSON_MODBUS_FUNCTION_CODE "FunctionCode"
|
||||
#define D_JSON_MODBUS_START_ADDRESS "StartAddress"
|
||||
#define D_JSON_MODBUS_COUNT "Count"
|
||||
#define D_JSON_MODBUS_ENDIAN "Endian"
|
||||
#define D_JSON_MODBUS_TYPE "Type" // allready defined
|
||||
#define D_JSON_MODBUS_VALUES "Values"
|
||||
#define D_JSON_MODBUS_LENGTH "Length"
|
||||
|
||||
const char kModbusBridgeCommands[] PROGMEM = "|" // No prefix
|
||||
D_CMND_MODBUS_SEND "|" D_CMND_MODBUS_SETBAUDRATE "|" D_CMND_MODBUS_SETSERIALCONFIG;
|
||||
|
||||
void (*const ModbusBridgeCommand[])(void) PROGMEM = {
|
||||
&CmndModbusBridgeSend, &CmndModbusBridgeSetBaudrate, &CmndModbusBridgeSetConfig};
|
||||
|
||||
#include <TasmotaModbus.h>
|
||||
TasmotaModbus *tasmotaModbus = nullptr;
|
||||
|
||||
enum class ModbusBridgeError
|
||||
{
|
||||
noerror = 0,
|
||||
nodataexpected = 1,
|
||||
wrongdeviceaddress = 2,
|
||||
wrongfunctioncode = 3,
|
||||
wrongstartaddress = 4,
|
||||
wrongtype = 5,
|
||||
wrongregistercount = 6,
|
||||
wrongcount = 7
|
||||
};
|
||||
|
||||
enum class ModbusBridgeFunctionCode
|
||||
{
|
||||
mb_undefined = 0,
|
||||
mb_readCoilstartregister = 1,
|
||||
mb_readContactstartregister = 2,
|
||||
mb_readHoldingstartregister = 3,
|
||||
mb_readInputstartregister = 4,
|
||||
mb_writeSingleCoil = 5,
|
||||
mb_writeSinglestartregister = 6
|
||||
};
|
||||
|
||||
enum class ModbusBridgeType
|
||||
{
|
||||
mb_undefined,
|
||||
mb_uint8,
|
||||
mb_uint16,
|
||||
mb_uint32,
|
||||
mb_int8,
|
||||
mb_int16,
|
||||
mb_int32,
|
||||
mb_float,
|
||||
mb_raw,
|
||||
mb_bit8
|
||||
};
|
||||
|
||||
enum class ModbusBridgeEndian
|
||||
{
|
||||
mb_undefined,
|
||||
mb_msb,
|
||||
mb_lsb
|
||||
};
|
||||
|
||||
struct ModbusBridge
|
||||
{
|
||||
unsigned long polling_window = 0;
|
||||
int in_byte_counter = 0;
|
||||
bool raw = false;
|
||||
|
||||
ModbusBridgeFunctionCode functionCode = ModbusBridgeFunctionCode::mb_undefined;
|
||||
ModbusBridgeType type = ModbusBridgeType::mb_undefined;
|
||||
|
||||
uint8_t count = 0;
|
||||
uint16_t registerCount = 0;
|
||||
uint8_t deviceAddress = 0;
|
||||
uint8_t startAddress = 0;
|
||||
};
|
||||
|
||||
ModbusBridge modbusBridge;
|
||||
|
||||
/********************************************************************************************/
|
||||
|
||||
bool SetModbusBridgeBegin(void)
|
||||
{
|
||||
return tasmotaModbus->begin(Settings->baudrate * 300, ConvertSerialConfig(Settings->sserial_config)); // Reinitialize modbus port with new baud rate
|
||||
}
|
||||
|
||||
void SetModbusBridgeConfig(uint32_t serial_config)
|
||||
{
|
||||
if (serial_config > TS_SERIAL_8O2)
|
||||
{
|
||||
serial_config = TS_SERIAL_8N1;
|
||||
}
|
||||
if (serial_config != Settings->sserial_config)
|
||||
{
|
||||
Settings->sserial_config = serial_config;
|
||||
SetModbusBridgeBegin();
|
||||
}
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
|
||||
void ModbusBridgeHandle(void)
|
||||
{
|
||||
bool data_ready = tasmotaModbus->ReceiveReady();
|
||||
if (data_ready)
|
||||
{
|
||||
uint8_t *buffer;
|
||||
buffer = (uint8_t *)malloc(5 + modbusBridge.registerCount); // Addres(1), Function(1), Length(1), Data(1..n), CRC(2)
|
||||
uint32_t error = tasmotaModbus->ReceiveBuffer(buffer, modbusBridge.registerCount);
|
||||
|
||||
ModbusBridgeError errorcode = ModbusBridgeError::noerror;
|
||||
if (error)
|
||||
{
|
||||
AddLog(LOG_LEVEL_DEBUG, PSTR("MBS: MBR Driver error %d"), error);
|
||||
}
|
||||
else if (modbusBridge.deviceAddress == 0)
|
||||
errorcode = ModbusBridgeError::nodataexpected;
|
||||
else if (modbusBridge.deviceAddress != (uint8_t)buffer[0])
|
||||
errorcode = ModbusBridgeError::wrongdeviceaddress;
|
||||
else if ((uint8_t)modbusBridge.functionCode != (uint8_t)buffer[1])
|
||||
errorcode = ModbusBridgeError::wrongfunctioncode;
|
||||
else if ((uint8_t)modbusBridge.registerCount != (uint8_t)buffer[2])
|
||||
errorcode = ModbusBridgeError::wrongregistercount;
|
||||
else
|
||||
{
|
||||
if (modbusBridge.type == ModbusBridgeType::mb_raw)
|
||||
{
|
||||
Response_P(PSTR("{\"" D_JSON_MODBUS_RECEIVED "\":["));
|
||||
for (uint8_t i = 0; i < tasmotaModbus->ReceiveCount(); i++)
|
||||
{
|
||||
ResponseAppend_P(PSTR("%d"), buffer[i]);
|
||||
if (i < tasmotaModbus->ReceiveCount() - 1)
|
||||
ResponseAppend_P(PSTR(","));
|
||||
}
|
||||
ResponseAppend_P(PSTR("]}"));
|
||||
ResponseJsonEnd();
|
||||
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_MODBUS_RECEIVED));
|
||||
}
|
||||
else if ((buffer[1] > 0) && (buffer[0] < 5)) // Read Registers, writing is not supported at this moment
|
||||
{
|
||||
Response_P(PSTR("{\"" D_JSON_MODBUS_RECEIVED "\":{"));
|
||||
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_START_ADDRESS "\":%d,"), modbusBridge.startAddress);
|
||||
ResponseAppend_P(PSTR("\"" D_JSON_MODBUS_LENGTH "\":%d,"), tasmotaModbus->ReceiveCount());
|
||||
ResponseAppend_P(PSTR("\"" D_JSON_MODBUS_COUNT "\":%d,"), modbusBridge.count);
|
||||
ResponseAppend_P(PSTR("\"" D_JSON_MODBUS_VALUES "\":["));
|
||||
|
||||
for (uint8_t count = 0; count < modbusBridge.count; count++)
|
||||
{
|
||||
char svalue[MBR_MAX_VALUE_LENGTH + 1] = "";
|
||||
if (modbusBridge.type == ModbusBridgeType::mb_float)
|
||||
{
|
||||
float value = 0;
|
||||
((uint8_t *)&value)[3] = buffer[3 + (count * 4)]; // Get float values
|
||||
((uint8_t *)&value)[2] = buffer[4 + (count * 4)];
|
||||
((uint8_t *)&value)[1] = buffer[5 + (count * 4)];
|
||||
((uint8_t *)&value)[0] = buffer[6 + (count * 4)];
|
||||
ext_snprintf_P(svalue, sizeof(svalue), "%*_f", 10, &value);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((modbusBridge.type == ModbusBridgeType::mb_int32) ||
|
||||
(modbusBridge.type == ModbusBridgeType::mb_uint32))
|
||||
{
|
||||
uint32_t value = 0;
|
||||
((uint8_t *)&value)[3] = buffer[3 + (count * 4)]; // Get int values
|
||||
((uint8_t *)&value)[2] = buffer[4 + (count * 4)];
|
||||
((uint8_t *)&value)[1] = buffer[5 + (count * 4)];
|
||||
((uint8_t *)&value)[0] = buffer[6 + (count * 4)];
|
||||
if (modbusBridge.type == ModbusBridgeType::mb_int32)
|
||||
snprintf(svalue, MBR_MAX_VALUE_LENGTH, "%d", value);
|
||||
else
|
||||
snprintf(svalue, MBR_MAX_VALUE_LENGTH, "%u", value);
|
||||
}
|
||||
else if ((modbusBridge.type == ModbusBridgeType::mb_int16) ||
|
||||
(modbusBridge.type == ModbusBridgeType::mb_uint16))
|
||||
{
|
||||
uint16_t value = 0;
|
||||
((uint8_t *)&value)[1] = buffer[3 + (count * 2)];
|
||||
((uint8_t *)&value)[0] = buffer[4 + (count * 2)];
|
||||
if (modbusBridge.type == ModbusBridgeType::mb_int16)
|
||||
snprintf(svalue, MBR_MAX_VALUE_LENGTH, "%d", value);
|
||||
else
|
||||
snprintf(svalue, MBR_MAX_VALUE_LENGTH, "%u", value);
|
||||
}
|
||||
else if ((modbusBridge.type == ModbusBridgeType::mb_int8) ||
|
||||
(modbusBridge.type == ModbusBridgeType::mb_uint8))
|
||||
{
|
||||
if (modbusBridge.type == ModbusBridgeType::mb_int8)
|
||||
snprintf(svalue, MBR_MAX_VALUE_LENGTH, "%d", (int8_t)(buffer[3 + count]));
|
||||
else
|
||||
snprintf(svalue, MBR_MAX_VALUE_LENGTH, "%u", (uint8_t)(buffer[3 + count]));
|
||||
}
|
||||
else if (modbusBridge.type == ModbusBridgeType::mb_bit8)
|
||||
{
|
||||
uint8_t value = (uint8_t)(buffer[3 + count]);
|
||||
snprintf(svalue, MBR_MAX_VALUE_LENGTH, "%d%d%d%d%d%d%d%d", ((value >> 7) & 1), ((value >> 6) & 1), ((value >> 5) & 1), ((value >> 4) & 1), ((value >> 3) & 1), ((value >> 2) & 1), ((value >> 1) & 1), (value & 1));
|
||||
}
|
||||
}
|
||||
ResponseAppend_P(PSTR("%s"), svalue);
|
||||
if (count < modbusBridge.count - 1)
|
||||
ResponseAppend_P(PSTR(","));
|
||||
}
|
||||
|
||||
ResponseAppend_P(PSTR("]}"));
|
||||
ResponseJsonEnd();
|
||||
|
||||
if (errorcode == ModbusBridgeError::noerror)
|
||||
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_TELE, PSTR(D_JSON_MODBUS_RECEIVED));
|
||||
}
|
||||
}
|
||||
if (errorcode != ModbusBridgeError::noerror)
|
||||
{
|
||||
AddLog(LOG_LEVEL_DEBUG, PSTR("MBS: MBR Recv Error %d"), (uint8_t)errorcode);
|
||||
}
|
||||
modbusBridge.deviceAddress = 0;
|
||||
free(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
|
||||
void ModbusBridgeInit(void)
|
||||
{
|
||||
if (PinUsed(GPIO_MBR_RX) && PinUsed(GPIO_MBR_TX))
|
||||
{
|
||||
tasmotaModbus = new TasmotaModbus(Pin(GPIO_MBR_RX), Pin(GPIO_MBR_TX));
|
||||
uint8_t result = tasmotaModbus->Begin(MBR_SPEED);
|
||||
if (result)
|
||||
{
|
||||
if (2 == result)
|
||||
{
|
||||
Serial.begin(MBR_SPEED, SERIAL_8E1);
|
||||
ClaimSerial();
|
||||
}
|
||||
AddLog(LOG_LEVEL_DEBUG, PSTR("MBS: MBR %s ser init at %d baud"), (2 == result ? "HW" : "SW"), MBR_SPEED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Commands
|
||||
\*********************************************************************************************/
|
||||
|
||||
void CmndModbusBridgeSend(void)
|
||||
{
|
||||
JsonParser parser(XdrvMailbox.data);
|
||||
JsonParserObject root = parser.getRootObject();
|
||||
if (!root)
|
||||
return;
|
||||
|
||||
modbusBridge.deviceAddress = root.getUInt(PSTR(D_JSON_MODBUS_DEVICE_ADDRESS), 0);
|
||||
uint8_t functionCode = root.getUInt(PSTR(D_JSON_MODBUS_FUNCTION_CODE), 0);
|
||||
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);
|
||||
ModbusBridgeError errorcode = ModbusBridgeError::noerror;
|
||||
|
||||
if (modbusBridge.deviceAddress == 0)
|
||||
errorcode = ModbusBridgeError::wrongdeviceaddress;
|
||||
else if (modbusBridge.startAddress == 0)
|
||||
;
|
||||
else if (functionCode > 4)
|
||||
errorcode = ModbusBridgeError::wrongfunctioncode; // Writing is not supported
|
||||
else
|
||||
{
|
||||
modbusBridge.functionCode = static_cast<ModbusBridgeFunctionCode>(functionCode);
|
||||
if (modbusBridge.functionCode == ModbusBridgeFunctionCode::mb_undefined)
|
||||
errorcode = ModbusBridgeError::wrongfunctioncode;
|
||||
}
|
||||
|
||||
modbusBridge.type = ModbusBridgeType::mb_undefined;
|
||||
if (strcmp(stype, "int8") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_int8;
|
||||
modbusBridge.registerCount = 1 * modbusBridge.count;
|
||||
}
|
||||
else if (strcmp(stype, "int16") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_int16;
|
||||
modbusBridge.registerCount = 2 * modbusBridge.count;
|
||||
}
|
||||
else if (strcmp(stype, "int32") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_int32;
|
||||
modbusBridge.registerCount = 4 * modbusBridge.count;
|
||||
}
|
||||
else if (strcmp(stype, "uint8") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_uint8;
|
||||
modbusBridge.registerCount = 1 * modbusBridge.count;
|
||||
}
|
||||
else if (strcmp(stype, "uint16") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_uint16;
|
||||
modbusBridge.registerCount = 2 * modbusBridge.count;
|
||||
}
|
||||
else if (strcmp(stype, "uint32") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_uint32;
|
||||
modbusBridge.registerCount = 4 * modbusBridge.count;
|
||||
}
|
||||
else if (strcmp(stype, "float") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_float;
|
||||
modbusBridge.registerCount = 4 * modbusBridge.count;
|
||||
}
|
||||
else if (strcmp(stype, "raw") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_raw;
|
||||
modbusBridge.registerCount = modbusBridge.count;
|
||||
}
|
||||
else if (strcmp(stype, "bit8") == 0)
|
||||
{
|
||||
modbusBridge.type = ModbusBridgeType::mb_bit8;
|
||||
modbusBridge.registerCount = modbusBridge.count;
|
||||
}
|
||||
else
|
||||
errorcode = ModbusBridgeError::wrongtype;
|
||||
|
||||
if (modbusBridge.registerCount > MBR_MAX_REGISTERS) errorcode = ModbusBridgeError::wrongcount;
|
||||
|
||||
if (errorcode != ModbusBridgeError::noerror)
|
||||
{
|
||||
AddLog(LOG_LEVEL_DEBUG, PSTR("MBS: MBR Send Error %d"), (uint8_t)errorcode);
|
||||
return;
|
||||
}
|
||||
|
||||
tasmotaModbus->Send(modbusBridge.deviceAddress, (uint8_t)modbusBridge.functionCode, modbusBridge.startAddress, modbusBridge.registerCount);
|
||||
ResponseCmndDone();
|
||||
}
|
||||
|
||||
void CmndModbusBridgeSetBaudrate(void)
|
||||
{
|
||||
if (XdrvMailbox.payload >= 300)
|
||||
{
|
||||
XdrvMailbox.payload /= 300; // Make it a valid baudrate
|
||||
Settings->sbaudrate = XdrvMailbox.payload;
|
||||
SetModbusBridgeBegin();
|
||||
}
|
||||
ResponseCmndNumber(Settings->sbaudrate * 300);
|
||||
}
|
||||
|
||||
void CmndModbusBridgeSetConfig(void)
|
||||
{
|
||||
// See TasmotaModusConfig for possible options
|
||||
// ModbusConfig 0..23 where 3 equals 8N1
|
||||
// ModbusConfig 8N1
|
||||
|
||||
if (XdrvMailbox.data_len > 0)
|
||||
{
|
||||
if (XdrvMailbox.data_len < 3)
|
||||
{ // Use 0..23 as serial config option
|
||||
if ((XdrvMailbox.payload >= TS_SERIAL_5N1) && (XdrvMailbox.payload <= TS_SERIAL_8O2))
|
||||
{
|
||||
SetModbusBridgeConfig(XdrvMailbox.payload);
|
||||
}
|
||||
}
|
||||
else if ((XdrvMailbox.payload >= 5) && (XdrvMailbox.payload <= 8))
|
||||
{
|
||||
int8_t serial_config = ParseSerialConfig(XdrvMailbox.data);
|
||||
if (serial_config >= 0)
|
||||
{
|
||||
SetModbusBridgeConfig(serial_config);
|
||||
}
|
||||
}
|
||||
}
|
||||
ResponseCmndChar(GetSerialConfig(Settings->sserial_config).c_str());
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Interface
|
||||
\*********************************************************************************************/
|
||||
|
||||
bool Xdrv100(uint8_t function)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (FUNC_PRE_INIT == function)
|
||||
{
|
||||
ModbusBridgeInit();
|
||||
}
|
||||
else if (tasmotaModbus)
|
||||
{
|
||||
switch (function)
|
||||
{
|
||||
case FUNC_EVERY_250_MSECOND:
|
||||
ModbusBridgeHandle();
|
||||
break;
|
||||
case FUNC_COMMAND:
|
||||
result = DecodeCommand(kModbusBridgeCommands, ModbusBridgeCommand);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // USE_MODBUS_BRIDGE
|
Loading…
Reference in New Issue