RS485 and Modbus

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

RS485 and Modbus

2,386 Views
alaskora_byk_mstang
Contributor I

Hello,

I want to use the modbus rtu protocol on our designed linux board-imx8mp Soc, so I need to enable the rs485 line in half-duplex. I have a few questions about this subject.

In the device tree, the rs485 connection is defined as follows

&uart3 {

pinctrl-names = "default";

pinctrl-0 = <&pinctrl_uart3>;

uart-has-rtscts;

rts-gpios = <&gpio2 10 GPIO_ACTIVE_LOW>;

cts-gpios = <&gpio2 11 GPIO_ACTIVE_LOW>;

status = "ok";

};

//Halfduplex

we are using  ISO3086DW as a rs485 transciver

Connection is diagram listed below

uart3 Tx  -------> rs485 transciver D

uart3 Rx -------> rs485 transive R

 

rts and cts pins are not connected.

GPIO0  -------> rs485 transciver's DE

GPIO1 ------->  rs485 transciver's RE

 

I can transfer data from the RS485 line by controlling the RE and DE pins with custom gpio0 and gpio1. But I am experiencing connection timeout problem with Modbus RTU. 

1.) Is there a convenient way to control Transmit and Receive directions?
2.) Should I connect the rts pins to the DE pin of the transciver and the uart driver should automatically control the transmit or receive direction
3.) Should I connect the cts pin to the RE pin of the transciver?

0 Kudos
Reply
5 Replies

2,338 Views
alaskora_byk_mstang
Contributor I

So rts and cts have no place in the use of rs485? For what purpose should they be used?

0 Kudos
Reply

2,321 Views
samsaprunoff
Contributor V

Good day,

Of course they do, well RTS, but not so much CTS.  You misunderstood my original post which was to highlight that it is simply not enough to disable the RS485's transceiver's Transmit and Receive enable pins with regards to Modbus communication specifications and/or under Linux.  You need to review the Modbus communication specs, etc and take these nuances into account with your Modbus firmware driver.

As for the receive and transmit pins...  you need to control the Receive and Transmit functionality of the RS485 interface.  You can do this a number of ways.  Most use the UART's RTS pin, as sometimes the RTS signal has additional UART properties that are not present with other standard GPIO pins.  You would need to review your UART's fucntionality and its RTS pin to see if this is the case.  Even so, most simply connect the RTS pin to the RS485's transceiver's Transmit (Tx) and Receive (Rx) Enable pins.  These pins have opposite assertion states (TxEnable is active high, and RxEnable is active low) and so the same output can be used to change the RS485's Tx and Rx states.  However, here again you need to review the RS485's transceiver's Tx  Enable and Rx Enable timing specs, as some RS485 device's actual Tx/Rx Enable/disable times can vary... and so can affect the actual signals being sent/received on the RS485's bus. 

You need to dig deeper in understanding the low level details of RS485 and Modbus, otherwise your implementation could have issues.

Cheers,

Sam

 

0 Kudos
Reply

2,264 Views
alaskora_byk_mstang
Contributor I

@samsaprunoff 

Hi 

RS-485 driver direction can be controlled via the CTS_B signal. (mentioned in the Reference manual of IMX8M Plus MX8MPRM, section 17.2.1.2 Features of the UART)

Okay, I connected the output of the cts pin to the DE and RE pins of the rs485 transciver. Then the device tree for uart3 was set as follows

 

// Only transmit

&uart3 {

pinctrl-names = "default";

pinctrl-0 = <&pinctrl_uart3>;

uart-has-rtscts

rts-gpios = <&gpio2 10 GPIO_ACTIVE_LOW>;

cts-gpios = <&gpio2 11 GPIO_ACTIVE_LOW>;

linux,rs485-enabled-at-boot-time; 

   rs485-rts-active-high;

   rs485-rx-during-tx; 

status = "okay";

};

 

As it is, I can only transmit.

When I update the device tree as follows, that is, when I make rs485-rts-active-low, this time I can only receive and not transmit

// Only receive

&uart3 {

pinctrl-names = "default";

pinctrl-0 = <&pinctrl_uart3>;

uart-has-rtscts

rts-gpios = <&gpio2 10 GPIO_ACTIVE_LOW>;

cts-gpios = <&gpio2 11 GPIO_ACTIVE_LOW>;

linux,rs485-enabled-at-boot-time; 

  rs485-rts-active-low;

status = "okay";

};

 

Uart driver can't control cts automatically or is there something else I need to do?

 

 

 

 

 

 
0 Kudos
Reply

2,260 Views
samsaprunoff
Contributor V

Good day,

The driver itself does not automatically control the RS485 Rx/Tx Enables.  There are some processor's that do have Uarts that can be configured for automatic RS485 Rx/Tx half duplex control, but even so these may not work for every application.  Ultimately, you will need to write your own Uart Driver that controls the RS485's Rx/Tx Enable pins along with maintaining the Modbus timing specifications.  Effectively, there is no setup and configuration of your Uart that will solve your issue... you need to write your own driver to make this all work.

Cheers,

Sam

 

 

0 Kudos
Reply

2,378 Views
samsaprunoff
Contributor V

Good day,

Outside of the half duplex control mechanisms you are reviewing, you also need to consider the Modbus's silent or dead time timing between packets.  Technically the dead time needs be 3.5 character times which in actual time is based upon the communication baud rate.  Addressing this may be quite challenging to accommodate under Linux, etc given its overall latency.  In fact this may be related to the issues you are experiencing.

Cheers,

Sam

 

 

 

0 Kudos
Reply