Connecting RS485 output to Raspberry Pi[Android Things]

1.8k views Asked by At

Just started working on Raspberry Pi with Android things, have a sensor that gives output through RS485 cables, I want to feed that output to Raspberry Pi, explored but got no proper solution for the same, If anyone had done this kind of stuff before you could guide me to make the connection either by using a converter or using MAX 485

What is the best way to get the output from RS485 to RPi? How it could be achieved? Thanks in Advance

2

There are 2 answers

0
Pathead On

I am not familiar with Android Things but hopefully this will point you in the right direction... I have had a lot of success using a USB to 485 converter and the minimalmodbus python library on a Raspberry Pi. See below for some example code that I have used in the past. It is pretty basic but should get you started.

import minimalmodbus
import serial

usbDevice = '/dev/ttyUSB0'

modbusSlaveID = 1

# can be 'ascii' or 'rtu'
modbusFormat = 'rtu'

registerToRead = 64

# 3 is for Holding Registers, 4 is for Input Registers
functionCode = 3

# initialize the device
device = minimalmodbus.Instrument(usbDevice, modbusSlaveID, modbusFormat)

# set the various options, which will depend on the device you are communicating with
device.debug = True
device.serial.baudrate = 9600
device.serial.bytesize = 8
device.serial.parity = serial.PARITY_NONE
device.serial.stopbits = 1
device.serial.timeout = 2   # seconds

print device.read_register(registerToRead, functioncode=functionCode)

p.s. This is my first Answer, hope I did it right...

0
devunwired On

The UART interface on most hardware is compatible with these types of sensors. By default, the UART pins on a board/module operate at TTL logic levels. Electrical standards like RS-232 and RS-485 use the same basic protocol, but modify the output voltage and configuration of the signal lines.

So in your case, you simply need to find a converter between TTL and RS-485 like the MAX485 that you mentioned. Connect that to any of the available UARTs on your board and use the same Peripheral I/O APIs to communicate with it from Android Things.