Modbus RTU Client

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,