-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ModBus RTU to TCP gateway #9586
Comments
Tasmota always prefers MQTT protocol wherever it's possible. We strongly suggest to use the Mqtt gateway instead. |
At least I need Tasmota to act as TCP gateway because my heating regulator Trovis 5573 can only be configured using a software called Trovis View which only supports communication via TCP. My idea is to enhance your code with an option "TCP_BRIDGE_MODBUS" for according byte mangling (see python solution linked in description). |
Tasmota already has a TCP gateway. See config example used for the ZbBridge for connecting with HomeAssistant (ZHA) |
I linked to Tasmota TCP bridge already in the first post. But I need the gateway to mangle the bits a little. Another option would be to implement the gateway in the SML interface. The meter is already defined (baudrate, protocol, pins, etc) and thus the gateway only needs to insert its TCP commands along the other commands from the MQTT/console stream. Since a meter can only be connected to one host it would be great if continous meter value logging could be combined with (rare) meter configuration stuff (in my case with a TCP-based Windows app, a TCP-based smartphone app is available as well). |
The use case is very specific. So it will be probably not done from the main contributors. Maybe you are lucky and someone has the need too and provides a PR. |
Closing this request as there is no active development on this. Please, ask to reopen if you want to work on this. Thanks. |
@TheChatty Can you try this? I have added the modbustcpstart en modbustcpconnect functions. I also removed the (u)int8 option for modbus send command, because modbus registers are always 16 bits. The data received from the tcp client is not checked btw, it's just send to the modbus client. I will also write a small tcpclient to test this added feature. |
I flashed the .gz you provided but are unable to select "ModBr .." in Configuration? It's not listed. |
I will look at it tomorrow. Otherwise compile it from my branch. |
@Jason2866: A ModBus TCP gateway is a widely requested feature I'd say. Enhancing driver 41 or 63 is not really important. The code impact is rather small, the usage gain is much bigger. If implemented in driver 41 TCP/RTU protocol conversion could be handled by a new option for instance. |
@jeroenst: I compiled db4f710 myself and it offered ModBr Tx/Rx. But I don't receive anything yet (trying to read outside temp like in my SML script):
I need to check via plain serial bridge if communication is a problem atm... tomorrow or so... |
@TheChatty I did some improvements in my latest branch, try that instead. I was confused with the registers which are not 8 but 16 bits. https://github.com/jeroenst/Tasmota/tree/ModbusTCP Also have a look at the logging when using weblog 4 I will also have a look at it tomorrow if it still doesn't work. |
@TheChatty Here is a bin file based on my latest commit in the modbustcp branch. Can you give it a try? |
Here are the PHP tools that I use for simulaton. |
I could now use plain serial bridge (again) like this:
0x0115 --> 227 --> /10 --> 27.7° degrees outside But still no response via modbus bridge:
|
Something goes wrong with setting the baudrate. On 9600 I receive from esp8266: 0xf7 0x03 0x00 0x09 0x00 0x01 0x40 0x9e Investigation in progress.. |
For some reason modbusbaudrate is not working right and it also causes modbusbridge to become deaf to incomming data. Have to dig further for this to find out why this is happening. |
@TheChatty Can you maybe do a test at 9600 baud (without setting baudrate in tasmota) ? |
At 9600 baud I cannot test with my modbus client as it is fixed to 19200. What do you want me to test? |
It seems that data from every slave address greater than 4 is ignored by the modbus driver. Can you test it with address 1 or 2? |
19200 works fine below slave address 5, debugging now. |
I cannot change any modbus setting at the client side - sorry. Good look with the debugging. |
ok, almost there I think |
Bug found, and solved, here is the firmware without bug |
3156b06 does not change anything for me - still no response. |
So this is strange: 14:32:03.862 MBS: MBRTCP to Modbus Transactionid:2, deviceAddress:247, functionCode:16, startAddress:144, sendCount:2, recvCount:1 RX: 0xf7 0x10 0x00 0x90 0x00 0x01 0x02 0x06 0xc4 0x96 0x97 |
I may have found something: e9fca82 No doesn't work, but I can reproduce your bug now Found bug, finding solution. |
When looking at my serial data it's solved in my latest commit. |
c788e80 delivers this:
So Things got even worse. Once working read_holding_registers is also not working. Wireshark log:
Weblog:
|
Things got even worse. Once working read_holding_registers is also not working. Wireshark log:
Weblog:
|
Try latest commit for FC16 please. Also test if read_holding_registers is working, otherwise I will look at it tomorrow. |
And weblog (no more Driver receive errors):
This seems to be the right track. Even Trovis is working (as in: connection does not choke), but at first sight there are some values wrong (bad temperatures). Wireshark shows some malformed packets. In this TCP stream seems to be lot of Tasmota memory data. Needs to be analyzed. |
Returned bytes need to be even (limitation of TasmotaModbus.cpp) arendst#9586
I did some testing and bugfixing for functioncode 1 and 2. For functioncode 1 and 2 also an odd number of bytes can be returned, but this is not supported by TasmotaModbus.cpp. For function code 15 it's also allowed to send single bytes as data I discovered. I will adapt TasmotaModbus for this later. https://ozeki.hu/p_5876-mobdbus-function-code-1-read-coils.html |
Returned bytes need to be even (limitation of TasmotaModbus.cpp) arendst#9586
All code is now in the arendst/Tasmota development branch. Please create a new bug report if any bug is found, this issue is getting to long. |
@josegardo I don't forget about you, over a few weeks I will include your code/feature request in ModbusBridge. You are also welcome to create a patch file leaving all other functionality in ModbusBridge in tact. |
Read the code of TasmotaModbus. |
Hello, Best regards, |
Code is finished but on beta status. Here you can find the documentation: |
It is working for me in a low looad environment already, e.g. I can run a python script (ModbusTcpClient) and request single registers from a serial RTU modbus client connected to Tasmota. What doesn't work for me atm is an application which causes heavy load on the wires (vendor app to configure the heating regulator). The connection seems to choke. I need to further evaluate this. |
Hello, I am not sure, I understand the feature well. Best regards, |
Yes, it's a TCP/RTU gateway. It connects your serial-only modbus device to your Wifi. And you can also send MQTT requests at the same time. Awesome piece of code. |
Great! But when I go through the documentation, it is not at all clear how to configure that. Please see this as constructive feedback, not as criticism.
It is not clear where to configure that. Then, it goes directly to Commands. I would expect something like
A question on this:
Is that required to start the Bridge? Autostart possible? |
You've got a couple of mistakes in your reply. The base is to define USE_MODBUS_BRIDGE in user_config_override.h like any other feature that doesn't come precompiled. You have to define GPIOs like they are configured for your serial modbus device. Then you can use MQTT to send modbus requests which will be received via MQTT as well. Defining USE_MODBUS_BRIDGE_TCP additionally gives you something like Serial to TCP bridge except it provides transparent TCP/RTU protocol translation. No additional GPIOs are required but a start of the TCP server (see similar bridge feature). This start can be automated with rules. Requests sent with TCP part will only be answered via TCP. Likewise with MQTT. I haven't tried ESP32 myself yet but I can promise it works well with ESP8266. You're invited to improve documentation once you got acquainted with this feature. I haven't done so yet because I still analyze a failing heavy load scenario. The similar TCP bridge serves a good start for TCP part of this bridge. |
@TheChatty thank you for supporting other users. Maybe for the heavy load scenario I can add a little delay before processing. I can imagine that tasmota gets to little time to process other tasks in heavy load. |
@TheChatty can you give my latest commit a try? |
@jeroenst I'm helping others because you did a wonderful job already. Unfortunately I won't get to test any of this until two weeks. I'll give feedback then. |
@TheChatty no problem👍 |
Have you looked for this feature in other issues and in the docs?
I did. It's not implemented yet. Here is an existing gateway in C or stripped down in python. This is the basic idea behind a RTU/TCP gateway.
Is your feature request related to a problem? Please describe.
I want to be able to poll certain registers from my modbus device regularly via MQTT and be able to configure it via a TCP-based Windows app.
Describe the solution you'd like
Implement a RTU/TCP gateway in Tasmota which allows concurrent access to a modbus device on Tx/Rx.
Describe alternatives you've considered
Alternatively I can either connect Tasmota for MQTT-based access or my PC directly for TCP-based access.
Additional context
This gateway is a litte bit more than a simple serial TCP bridge in the sense that it adds and also strips a few bytes from each request/response.
(Please, remember to close the issue when the problem has been addressed)
The text was updated successfully, but these errors were encountered: