Elektrix
Posts: 3
Joined: Wed Feb 20, 2019 6:33 pm

RS485 with UART and pyserial

Wed Feb 20, 2019 7:20 pm

Hello all,

this is my very first post in this beautiful forum. I found lots of information here already. Thanks to all.

At the moment I am trying to get RS485 up and running. This seems nothing special as there are lots of RS485 boards around. I already have this one: https://www.conrad.biz/de/raspberry-pi- ... 67832.html and it works in one of my environments. With other devices I can't get RS485 running perhaps beause of the behaviour of this device:

For RS485 communication the sender / receiver has to by switched in either send or receive mode. This is done normally by an additional output pin. The board I have doesn't do this and switches between send and receive depending on the state of TX. I think this is somewhat strange but seems to work and the advantage is that this additional output pin is not needed. Only TX and RX are necessary.

What I want to do is to create a standard RS485 bus. The transceiver switches to send mode then sends all the bits and then switches back to receive mode. What I found is that on the raspi the pin used to switch the transceiver is RTS (GPIO 17) and pyserial has a RS485 mode. So I set up the following code:

Code: Select all

#!/usr/bin/env python3
import serial
import serial.rs485

ser = serial.Serial(
               port="/dev/ttyAMA0",
               baudrate = 9600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=0,
               rtscts = True
           )
ser.rs485_mode = serial.rs485.RS485Settings()

if (ser.isOpen() == True):
  ser.close()
ser.open()
ser.reset_input_buffer

outstr = "5"
ser.write(outstr.encode(encoding = 'ascii'))
This code initializes the UART and simply puts out one character and is according to this info: https://pyserial.readthedocs.io/en/late ... l_api.html. When I comment out the line beginning with ser.rs485_mode... the code runs and I see the correct output signal on the TX pin with my scope. But with rs485 mode activated I get the following error that I don't understand:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 156, in _set_rs485_mode
fcntl.ioctl(self.fd, TIOCSRS485, buf)
OSError: [Errno 25] Inappropriate ioctl for device

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "./test485.py", line 14, in <module>
ser.rs485_mode = serial.rs485.RS485Settings()
File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 480, in rs485_mode
self._reconfigure_port()
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 434, in _reconfigure_port
self._set_rs485_mode(self._rs485_mode)
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 158, in _set_rs485_mode
raise ValueError('Failed to set RS485 mode: {}'.format(e))
ValueError: Failed to set RS485 mode: [Errno 25] Inappropriate ioctl for device
The pyserial package is python3-serial and the OS is rasobian stretch lite 2018-11-13. I am totally stuck here so any help is very much appreciated.

Elektrix

tempest766
Posts: 105
Joined: Sun Jan 07, 2018 3:44 pm

Re: RS485 with UART and pyserial

Thu Feb 21, 2019 3:37 am

For RS485 communication the sender / receiver has to by switched in either send or receive mode. This is done normally by an additional output pin. The board I have doesn't do this and switches between send and receive depending on the state of TX. I think this is somewhat strange but seems to work and the advantage is that this additional output pin is not needed. Only TX and RX are necessary.
scanning through your initial message I'm not seeing the buzzwords "mutli-drop", "half-duplex" or "master/slave" . I believe those concepts are important for what I understand you're trying to do.

This is a good starting point -- http://www.bb-elec.com/Learning-Center/ ... ndard.aspx

Elektrix
Posts: 3
Joined: Wed Feb 20, 2019 6:33 pm

Re: RS485 with UART and pyserial

Thu Feb 21, 2019 5:33 pm

Thank you for your reply. The transceiver on the raspi is always the busmaster in this RS485 bus. All other members are slaves. I want the bus to run in half duplex master/slave which is RS485 standard.

What I have understood so far is that GPIO17 which can be used as RTS for RS232 is used to switch send / receive mode of the transceiver in RS485 mode. This is what I want to do.

I hope it's a bit clearer now.

Elektrix

danjperron
Posts: 3393
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: RS485 with UART and pyserial

Thu Feb 21, 2019 7:27 pm

There is a class for rs485 settings in pyserial.

https://www.raspberrypi.org/forums/view ... 5#p1383709

Elektrix
Posts: 3
Joined: Wed Feb 20, 2019 6:33 pm

Re: RS485 with UART and pyserial

Fri Feb 22, 2019 7:19 pm

Thank you very much for this link!! This is exactly the information I was looking for. I will try it out and then come back.

Elektrix

TheJanggut
Posts: 1
Joined: Mon Jun 10, 2019 7:57 am

Re: RS485 with UART and pyserial

Mon Jun 10, 2019 8:38 am

Hi there

I would like to ask. Now want to asked something. How do I connect the RaspberryPi 3 Model B with USB to 2 Wire RS 485.

Return to “Interfacing (DSI, CSI, I2C, etc.)”