RS485 Communications

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

User avatar
Steve
Matrix Staff
Posts: 3151
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 107 times
Been thanked: 414 times
Contact:

RS485 Communications

Postby Steve » Thu Nov 26, 2009 11:14 am

Electrical specifications:
RS485, and the similar RS422, are communication bus systems using two wire differential transmission of each signal. Differential transmission improves the signal's immunity to electrical noise, at the expense of the requirement for an extra conductor for each signal - usually in a twisted pair configuration. Specifications only define the electrical properties of the system (ISO physical layer). There are no specific communications protocols, though many have been adopted.

Image

RS485 is often used for serial data communications. The lack of a specific protocol means that several pre-existing ones have been adopted (with some adaptations to allow additional benefits of RS485 to be included), and new ones developed for specific applications.

RS485 can be used to transmit almost any digital signal that is within the frequency limits of the bus system. It is often used in industry to allow the transmission of sensor signals, rotary encoder pulses, and motor control signals around large machines. Large computer systems also use multiple, parallel, RS485/422 busses to connect high speed signals between cabinets.

Wiring and termination can be an important issue with many systems.
Terminating resistors are required at the ends of long cable runs to match the line impedance and prevent signal reflections. The terminating resistors can also be used to bias the signal lines so that a specific logic level is represented on the bus when it is not being driven by any of the nodes.

Common protocols:
RS485 can often be used to directly replace RS232 links - extending the transmission range and baud rate available. Many commercial products are available to perform this function.
An extension of RS232, available in UARTs of many microcontrollers, is the transmission of a 9th data bit. This is used in many RS485 systems to differentiate between two types of transmissions:

9th bit = 1, 8 bits = node address
9th bit = 0, 8 bits = data

Unlike RS232, which can only allow communication between two devices (officially), RS485 can allow communications between a small network of devices (maximum 32) using a multi-drop bus. Each node on the bus must have a unique address to allow information to be targeted at it. The bus transmitters in each device can be disabled, allowing other devices to transmit on the bus without causing conflict. The use of 9 data bits can allow packets of bytes to be transmitted. The first byte can be highlighted as the address of the intended recipient node by setting its 9th bit.

For simplicity, many RS485 networks have a single master node and multiple slave nodes. The master node initiates all communications and indicated the slave node being accessed by including its node addresses in the message.
RS485 does not have the in-built message collision detection and correction available in bus systems like CAN, but there are techniques to allow support for multi-master networks and unsolicited messages.


Network configurations:

A simple RS485 application is the direct transmission of the TX and RX signals generated by the UARTs of two microcontrollers. The use of standard RS485 drivers and twisted pair cables will allow the signals to be transmitted at higher baud rates, and over longer distances, than the use of RS232 drivers would allow. This is a full-duplex connection (allows simultaneous transmission in both directions) which requires a driver and receiver for each device (node), and four signal wires.

To reduce the wiring costs, a half-duplex connection can be used. This allows communication in either direction, but only one at a time, and requires a combined transmitter/receiver (transceiver) for each node. The ability to disable the RS485 transmitters allows each node to release the signal lines when not transmitting, allowing the other node to transmit if required. The handover of bus control from one node to another is the responsibility of the protocol being used.

Image

The half duplex bus can be directly expanded to include more nodes if a suitable software addressing scheme is adopted. The full-duplex bus can also be expanded if the driver in each slave node has a controlled enable/disable signal.
The TX signal from the master node can be connected to the RX of each slave node, and the TX from each slave node can be connected to the RX of the master, with a maximum of one being enabled at any time.

A common device for RS485 conversion is the SN75176 (or compatibles). This is a transceiver available in an 8-pin dip package. It contains a transmitter and receiver with common RS485 connections, but separate logic level TX (D) and RX (R) pins, and individual transmit and receive enable pins. The transmit enable is active high, while the receive enable is active low, allowing the two signals to be connected together as a single 'Direction' control: High = transmit; Low = receive.

Example programs:
Two example programs are attached to this article:

MasterNode
SlaveNode1

These allow a simple, single master, multiple slave, network to be created. Initially 1 master and 1 slave are represented. The slave has node address 1 set in the program; the master does not require a node address.

The network uses 9-bit data transmission, with the addressing protocol described above. An address byte contains the target node address (bits 0 to 4) and a read/write bit (bit 7).

The master node writes a count value to node 1, which is displayed on the Port B pins of node 1.

The master node then requests a read from node 1. The slave node should respond by sampling its ADC1 channel and returning the value to the master node. The master node displays the returned value on its Port B pins.

The slave program can be copied into additional nodes on the network, which should be programmed with unique addresses between 2 and 31. The master can then be programmed to communicate with the new nodes be adding code to the existing program.

The only transfers supported by this example protocol are single read and write operations. The protocol can be expanded to allow additional message types to be handled, allowing greater control of the slave nodes.
Attachments
SlaveNode1.fcf
(9 KiB) Downloaded 1692 times
MasterNode.fcf
(6.5 KiB) Downloaded 1517 times
These users thanked the author Steve for the post (total 4):
electro_dima (Thu May 02, 2013 1:46 pm) • medelec35 (Fri Jul 22, 2016 2:53 pm) • jgu1 (Fri Jul 22, 2016 7:03 pm) • joe nova (Sun Oct 16, 2016 1:40 pm)
Rating: 20%
 

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1128
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 521 times
Contact:

Re: RS485 Communications

Postby Steve001 » Wed Dec 16, 2009 9:45 am

There is some good stuff about RS485 Comms on Maxim-ic website

http://datasheets.maxim-ic.com/en/ds/MAX253.pdf

i have built circuit on page 6 and currantly exprimenting with it

Hope this is some use :mrgreen:

steve

some more usefull stuff

http://www.lammertbies.nl/comm/info/modbus.html
These users thanked the author Steve001 for the post:
medelec35 (Fri Jul 22, 2016 2:53 pm)
Rating: 5%
 
Success always occurs in private and failure in full view.

fresh
Posts: 47
Joined: Mon Jun 08, 2009 11:11 am
Contact:

Re: RS485 Communications

Postby fresh » Wed Dec 29, 2010 3:39 pm

Hello @ all,
great Article Steve. Your Master and Node code run perfect.
I have some question..
- When I send more than one byte, must i set a delay between the RS232 Makros ? Have anybody an example for me, please?
- For what are the Number in the rs232 recieve makro (nTimeout)
in your example are 40 and 50.

Thank you for help

Regards
Andreas

User avatar
Benj
Matrix Staff
Posts: 14797
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4759 times
Been thanked: 4361 times
Contact:

Re: RS485 Communications

Postby Benj » Thu Jan 06, 2011 1:25 pm

Hello Andreas,

When I send more than one byte, must i set a delay between the RS232 Makros ? Have anybody an example for me, please?


No you can simply send bytes after each other, no delay is required.

For what are the Number in the rs232 recieve makro (nTimeout)


This signifies the number of attempts to try and wait for incoming data before returning a timeout value of 255. If data is received then the macro will return immediatley.

fresh
Posts: 47
Joined: Mon Jun 08, 2009 11:11 am
Contact:

Re: RS485 Communications

Postby fresh » Sat Jan 08, 2011 2:53 pm

Hello Benj,
thank you for help.

[quote="Benj"
This signifies the number of attempts to try and wait for incoming data before returning a timeout value of 255. If data is received then the macro will return immediatley.[/quote]
Is this time in [ms] ?

Regards
Andreas

User avatar
Benj
Matrix Staff
Posts: 14797
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4759 times
Been thanked: 4361 times
Contact:

Re: RS485 Communications

Postby Benj » Mon Jan 10, 2011 9:39 am

Hello Andreas,

By default the number simply represents the number of times to check. However if you go into the properties of the RS232 component then you will find there is a setting "RX timeout units" that switches the timeout functionality to milliseconds.

fresh
Posts: 47
Joined: Mon Jun 08, 2009 11:11 am
Contact:

Re: RS485 Communications

Postby fresh » Tue Jan 11, 2011 7:04 am

Thank you for information, Benj.

kleberzeitounian
Posts: 5
Joined: Thu May 24, 2012 4:46 pm
Location: itapevi,sp,brasil
Contact:

Re: RS485 Communications

Postby kleberzeitounian » Tue Jun 05, 2012 7:14 pm

Good afternoon everyone, I am implementing the use of this example MASTER-SLAVE available for download here on the forum, but which way to change the address of the slave-master-slave1 slave2-slave3 by simply changing the MYADRESS = 1 to MYADRES = 2, not working, can help me.
Kleber Zeitounian
Zeitounian Tecnologia Ltda
Tel.: (11) 4773-5569
Cel.:(11) 9-8336-7655
e-mail.: kleber@zeitounian.com.br
HTTP://www.zeitounian.com.br
Skype = kleberzeitounian

electro_dima
Posts: 1
Joined: Tue Mar 26, 2013 9:15 am
Has thanked: 1 time
Contact:

Re: RS485 Communications

Postby electro_dima » Thu May 02, 2013 2:04 pm

Hi,
In attached file "SlaveNode1"+ "MasterNode" is RS232 used in component Macro.
It because Flowcode 5 not supported RS485 or RS422, right?
Will it lower capability of data transmission of RS422 or RS485 then?

I will need to use RS422 or 485 in one project

Thanks
B.R.

radist73
Posts: 36
Joined: Mon Dec 22, 2014 10:04 pm
Location: TALLINN
Has thanked: 5 times
Been thanked: 2 times
Contact:

Re: RS485 Communications

Postby radist73 » Thu Jul 21, 2016 7:08 am

Hi,
Sorry for this question, but I cant understand, how works logical operations with HEX.
https://courses.engr.illinois.edu/ece39 ... H01-2.html was helpful, but....

MasterNode send address 0x101 and 0x000, in SlaveNode1 I see (RxVal AND 0x100), after this Address check (RxVal AND 0x1f)=1

How works in this case logical operations (RxVal AND 0x100) and (RxVal AND 0x1f)=1 ?

Thanks!

User avatar
LeighM
Matrix Staff
Posts: 2014
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 480 times
Been thanked: 691 times
Contact:

Re: RS485 Communications

Postby LeighM » Fri Jul 22, 2016 2:21 pm

To help understand this, convert to binary

Code: Select all

0x0101 = 0000 0001 0000 0001
0x0100 = 0000 0001 0000 0000
AND ========================
0x0100 = 0000 0001 0000 0000

0x0101 = 0000 0001 0000 0001
0x001f = 0000 0000 0001 1111
AND ========================
0x0001 = 0000 0000 0000 0001

Hope that helps