# RS-485 interface

<abstract>
The RS485 is commonly used to connect DMX512, Modbus or generic RS485 serial devices.
</abstract>

Two RS485 lines are available on CM-Home. This lines are opto-isolated from
the CPU and available on screw terminals as shown below.

<img src="./rs485_ports.jpg" class="img-responsive">

## Using the RS485 ports

The ports are visible by Linux on <code>/dev/ttyUSB0</code> (left port) and <code>/dev/ttyUSB2</code> (right port) and can be 
managed by software like any other serial line. 
<br/>
The DE signal required to get the bus is managed in hardware by the FTDI FT4232 chip. 

### Python example

Send and receive chars using the left RS485 port. To do the same on the right port change <code>/dev/ttyUSB0</code> in <code>/dev/ttyUSB1</code>.

	#!/usr/bin/python
	import serial
	import time
	
	ser = serial.Serial(
	    port='/dev/ttyUSB0',
	    baudrate=115200,
	    timeout=1,
	    parity=serial.PARITY_NONE,
	    stopbits=serial.STOPBITS_ONE,
	    bytesize=serial.EIGHTBITS
	)
	ser.flushOutput()
	ser.flushInput()
	
	rx_counter=0
	tx_counter=0
	while True:
	    tx_counter=tx_counter+1
	    ser.write("Tx counter = %d | Rx counter = %d\n\r".encode('utf-8') % (tx_counter,rx_counter))
	    print("Tx counter = %d | Rx counter = %d" % (tx_counter,rx_counter))
	
	    a=ser.read(1)
	
	    if a:
	        rx_counter=rx_counter+1
	        ser.write("Tx counter = %d | Rx counter = %d | Received: %s %02x\n\r".encode('utf-8') % (tx_counter,rx_counter,a,ord(a)))
	        print("Tx counter = %d | Rx counter = %d | Received: %s %02x" % (tx_counter,rx_counter,a,ord(a)))

## Links

* [RS485 info](https://en.wikipedia.org/wiki/RS-485)
* [DMX512 info](https://en.wikipedia.org/wiki/DMX512)
* [MODBUS info](https://en.wikipedia.org/wiki/Modbus)

<hr>

@include='adv_cmpanel' 

