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

Automatic RTS configuration in libmodbus #410

Open sudhanshubhutani opened 7 years ago

sudhanshubhutani commented 7 years ago

libmodbus version

3.1.4

OS and/or distribution

Linux 4.1

Environment

32bit

Description

Hi libmodbus experts,

I have one RS485 port on my developement board and i have one RS485 T6441 COmet sensor which i need to interface using libmodbus.

My Isolated rs485 part is http://www.analog.com/en/products/interface-isolation/isolation/isolated-rs-485/adm2582e.html

My libmodbus source code calls modbus_rtu_set_serial_mode() after opening modbus context /dev/ttySC0 - is my rs485 device file

        #if HAVE_DECL_TIOCSRS485
            modbus_rtu_t *ctx_rtu = ctx->backend_data;
            struct serial_rs485 rs485conf;
            memset(&rs485conf, 0x0, sizeof(struct serial_rs485));
            if (ctx->debug) {
                fprintf(stderr, " RS 485 SETTING IOCTL WITH NEW PARAMS: %d \n", ctx->s);
            }

        if (mode == MODBUS_RTU_RS485) {
            rs485conf.flags |= SER_RS485_ENABLED;
            rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
            //rs485conf.delay_rts_before_send = 0;
            rs485conf.delay_rts_after_send = 0;

            if (ioctl(ctx->s, TIOCSRS485, &rs485conf) < 0) {
                if (ctx->debug) {
                 fprintf(stderr, "RS 485 SUD IOCTL FAILED\n");
                }
                return -1;
            }
            ctx_rtu->serial_mode = MODBUS_RTU_RS485;
            return 0;
        }

This will internally override settings for RS485 config in sc16is7xx_reconf_rs485 I am not able to get any success in configuring my SC I2C-UART IC DRIVER=sc16is7xx OF_NAME=sc16is740 OF_FULLNAME=/soc/i2c@c00a6000/sc16is740@49 OF_COMPATIBLE_0=nxp,sc16is740 OF_COMPATIBLE_N=1 MODALIAS=i2c:sc16is740

using modbus configuration

I always get select timeout failure, and unable to read/write registers for my RTU based comet sensor.

Can anyone please let me know what is wrong in libmodbus source code which is not configuring RTS signals corrects?

When i probe my RTS pin, it always stay low, after i do modbus_rtu_set_serial_mode() But, i see tx sending data to the RTU sensor, but RTS still stays low, not sure why, it should go high when TX is transmitting and it should come to low again.

Also, my CUSTOM RTS / CTS flag is 0, i.e HAVE_DECL_TIOCM_RTS = 0

Kindly give your thoughtful pointers

Posted my query to list group: https://groups.google.com/forum/#!topic/libmodbus/_wW4ucc4Vfc Do you recomment me to use custom rts, and manage RTS pin manaully? Do i need a change in termios.cflags for RTS automatic flow control? Has anybody used libmodbus with RS 485 based sensors?

Expected behaviour

RTS should automatically switch, as i am not using custom_rts function

fusionJose commented 5 years ago

I think you have to configure:

rs485conf.flags |= SER_RS485_RTS_ON_SEND;

instead of

rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;

That worked for me.