David Ryan
Posts: 9
Joined: Wed May 16, 2018 3:53 pm

Trouble getting modbus over RS485 connecting to GPIO

Wed May 16, 2018 4:39 pm

Hello all,

I'm working with a custom RS485 adapter that I've connected to the following GPIO pins

GPIO 14/8 - TX
GPIO 15/10 - RX
GPIO 18/12 - Direction

My end goal to communicate via rs485-Modbus using Node. So far I've been able to send and receive messages via serial communications and manually flipping the 12 pin in user space. Example python code below[1]

I'm stuck at getting modbus across the adapter. The golden egg would be to find an existing driver so I could use this device with minimalModbus , which I have working with a USB adapter. Short of that I need to either

1 - Write my own driver (scary)
or
2 - Find a way to control the GPIO in Kernel space
or
3 - Send modbus over serial and switch the GPIO in user space. This options seems like it would be the easiest to do, but would also have the worst performance.

Any advice would be appreciated. This is my first real experience doing anything at this level on a Raspberry Pi or any other device so I wouldn't be shocked to find that I'm aiming in the wrong direction.

[1]
import time
import serial
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT);

ser = serial.Serial(
port= '/dev/ttyS0',
baudrate= 57600,
parity= serial.PARITY_NONE,
stopbits= serial.STOPBITS_ONE,
bytesize= serial.EIGHTBITS,
timeout=1
)



def write_add():
counter = 0;
message = 0
while (True):
print "writing",
GPIO.output(12,1) #set high/transmit
ser.write('%d \n'%(message))
time.sleep(0.005) #baud for 57600
#time.sleep(0.5) #baud for 9600
GPIO.output(12, 0) #pin set to low/receive


loop_count = 0
res =""
while (res == ""):
res =ser.readline();
if(res != ""):
print ""
print "Read Cycles: "+str(loop_count)+" Total: "+str(counter)
print res
message = int(res) + 1
counter = counter + 1
elif(loop_count > 10):
res = "start over"
else:
print ".",
loop_count = loop_count + 1

write_add()

User avatar
CFLanger
Posts: 36
Joined: Fri Feb 06, 2015 5:04 pm
Contact: Website

Re: Trouble getting modbus over RS485 connecting to GPIO

Wed May 16, 2018 5:44 pm

There are converters that take in 2 wire RS-232 and converts it to 2 wire RS-485 with "auto RTS control" Use one of those instead of attempting to do your own turn around control for half duplex communication. Google B&B
CFLanger
Senior Software Engineer at
www.ljtechnologies.com

David Ryan
Posts: 9
Joined: Wed May 16, 2018 3:53 pm

Re: Trouble getting modbus over RS485 connecting to GPIO

Wed May 16, 2018 8:22 pm

Unfortunately, my task is to make this work without an adapter.

I've got a USB-to-rs485 adapter that's working just fine.

I'm a little surprised there isn't a working rs485 driver out there somewhere.

Renate
Posts: 2
Joined: Sun Mar 22, 2015 8:16 pm
Location: Boston

Re: Trouble getting modbus over RS485 connecting to GPIO

Wed May 16, 2018 10:31 pm

You'd really need to write a driver to have this work.
For one, you usually only care in serial transmission if you have room in your buffer to stick another char.
In the case of switching direction for RS-485 you have to watch when the last bit is shifted out.

I looked into this whole thing and decided that a nice quick turnaround was easiest with a USB device.

It seems like most of the serial adapters (RS-232 or RS-485) adapters have fake chips.
Running 24*7 I'd see all sorts of USB bus errors.
I didn't have any immediate access to any genuine chips so I nailed together a 32u4 and a 75176.
It's been working 24*7 perfectly for over half a year.

User avatar
CFLanger
Posts: 36
Joined: Fri Feb 06, 2015 5:04 pm
Contact: Website

Re: Trouble getting modbus over RS485 connecting to GPIO

Thu May 17, 2018 3:41 pm

these just work!

http://www.ftdichip.com/Products/Cables/USBRS485.htm

I used 2 of these to connect a Pi2 to a Allen Bradley PLC using MODBUS AT 19200
If I remember I didnt even have to muck around with a driver.

They also have a clear body so you can see the cool LEDs blinking!
CFLanger
Senior Software Engineer at
www.ljtechnologies.com

scotty101
Posts: 3031
Joined: Fri Jun 08, 2012 6:03 pm

Re: Trouble getting modbus over RS485 connecting to GPIO

Thu May 17, 2018 3:54 pm

David Ryan wrote:
Wed May 16, 2018 8:22 pm
I'm a little surprised there isn't a working rs485 driver out there somewhere.
It shouldn't be surprising. RS485 is an electrical standard about the voltages, wires etc not a communications standard that tells you how to deal with full/half duplex.
https://en.wikipedia.org/wiki/RS-485#Protocols wrote:RS-485 is not a protocol; it's simply an electrical interface. Although many applications use RS-485 signal levels, the speed, format, and protocol of the data transmission is not specified by RS-485. Interoperability of even similar devices from different manufacturers is not assured by compliance with the signal levels alone.
There are many different ways of sending data over an RS485 compliant link and they can't all be covered by a single driver.

There is a linux driver but it won't work for all different devices that use RS-485
https://github.com/torvalds/linux/blob/ ... -rs485.txt
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 18 guests