Modbus RTU Client

Hi Community,

I can’t find a solution to implement my task, maybe someone of you from community made it already.

I’m using TRB145 (FW - TRB1_R_00.07.06.4), I have enabled and configured service Modbus Serial Client with Data To Server - data is sent from device via TRB145 to to Thingsboard IoT - all works fine !

But now I would like to send a Modbus write request via TRB145 to device. I can only do it by MQTT Modbus Gateway, but it does not allow to run on rs485 because it is already in use by Modbus Serial Client.

Any clue how to do it ?

@AndzejJ - could you advise, I dig into community and I feel you have best knowledge about similiar topics. Any help appreciated.

p.s. an idea do it by toggling profiles

Regards
Michal

Hello,

Yes, you are correct. Only one service can have RS485 enabled at the same time.

If you want to use Modbus RTU client to read data periodically, send it via Data to Server, and use MQTT gateway at the same time to write to Modbus registers, you will need a workaround.

I would suggest creating your own script. Since you can Modbus client enabled, you can use UBUS commands to send Modbus requests from the CLI. Mosquitto is also available for MQTT.

opkg -e /etc/opkg/openwrt/distfeeds.conf update

opkg -e /etc/opkg/openwrt/distfeeds.conf install mosquitto-client-ssl

Here’s an example of a script:

#!/bin/sh

# MQTT settings
BROKER=192.168.10.1
PORT=1883
TOPIC="/test"
#USERNAME="your_username"
#PASSWORD="your_password"


mosquitto_sub -h $BROKER -p $PORT -t $TOPIC | while IFS=',' read -r ID FUNCTION FIRST_REG VALUE

do
    ubus call modbus_client serial.test "{
        'serial_type': '/dev/rs485',
        'baudrate': 9600,
        'databits': 8,
        'stopbits': 1,
        'parity': 'none',
        'flowctrl': 'none',
        'timeout': 5,
        'id': $ID,
        'function': $FUNCTION,
        'first_reg': $FIRST_REG,
        'reg_count': '$VALUE',
        'data_type': 'hex',
        'no_brackets': 1
    }"

    if [ $? -eq 0 ]; then
        echo "Write command executed successfully."
    else
        echo "Failed to execute write command."
    fi
done

Though I am not sure if this works properly. In this example, I assume that the broker runs on RUT itself (192.168.10.1).

Should be able to set holding registers by publishing a message to /test topic:

1,16,100,D1

Make sure to adjust the script for your own needs and test it properly.

Kind Regards,

@AndzejJ

thank you for guidelines, it works as expected.

Regards
Michał

1 Like

This topic was automatically closed 40 hours after the last reply. New replies are no longer allowed.