stephane / libmodbus

A Modbus library for Linux, Mac OS, FreeBSD and Windows
http://libmodbus.org
GNU Lesser General Public License v2.1
3.52k stars 1.77k forks source link

modbus_rtu_set_serial_mode needs additional flags #742

Open jan-kardell opened 7 months ago

jan-kardell commented 7 months ago

libmodbus version

all

OS and/or distribution

OpenWrt 21.02, Linux 5.4

Environment

arm 32-bit

sc16is752 UART

Description

It is not enough to to set the SER_RS485_ENABLED for hw assisted rs485 direction control. It is also necessary to set either SER_RS485_RTS_AFTER_SEND or SER_RS485_RTS_ON_SEND. Some drivers check these flags and return a fails on the ioctl call.

In my case, the sc16is752 UART hardware only supports SER_RS485_RTS_AFTER_SEND, and it is therefore necessary to set SER_RS485_RTS_AFTER_SEND and clear SER_RS485_RTS_ON_SEND in addition to setting SER_RS485_ENABLED. And that is enforced by the driver. There are probably other drivers that also do that.

Maybe these flags has been introduce in some specific Linux version, so that the Linux version has to be checked, but I have not examined that.

Actual behavior if applicable

<...>

Expected behavior or suggestion

<...>

Steps to reproduce the behavior (commands or source code)

<...>

libmodbus output with debug mode enabled

<...>

jan-kardell commented 7 months ago

Just found #331.

Searching some more reveals that the sc16is7x driver do not call uart_get_rs485_mode(), and therefore do not have a default rs485 config, or a config from device tree (property rs485-rts-active-low).

Therefore neither SER_RS485_RTS_ON_SEND or SER_RS485_RTS_AFTER_SEND is set.

There are recent patches to fix that: https://lore.kernel.org/lkml/20230725142343.1724130-1-hugo@hugovil.com/T/

There are however cases where setting this in the device tree is not desirable, the device tree should describe the bord, not some external rs485-adapter that happens to be connected. So the need to support SER_RS485_RTS_ON_SEND and SER_RS485_RTS_AFTER_SEND remains.