Python Forum
Raspberry + RS485 shield | pySerial - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: General Coding Help (
+--- Thread: Raspberry + RS485 shield | pySerial (/thread-2179.html)

Raspberry + RS485 shield | pySerial - Coada - Feb-24-2017


I am using a RS485 shield for Raspberry v3 together with the pySerial class provided by Python to communicate with a device through the serial port.
The device I am communicating with, uses the S-Bus protocol and has a physical output with D+/D- and ground.
The device has a proprietary software that displays, in hex, what is broadcast and received on the device and on any other supplementary devices attached to it.
So, regarding topology, I went from the device's S-Bus port into the RS485 shield attached to the Raspberry.
At first, I was not sure that I will receive anything while listening to the port since I wasn't sure of the compatibility. Had to try it tough..

After I did the setup and made a little code in python using the pySerial class I was able to receive the same messages that are shown in the proprietary software of the device.
As a mention, in python, I had to encode the message received, to hex to have an exact match with what the proprietary software was displaying.

On the proprietary software I can give commands to the device and this device can trigger, from example, a relay.
While giving such a command and while monitoring the communication between the Raspberry and the device I "recorded" all the messages, in hex, that were transmitted on the S-bus protocol, including the code sent from the proprietary software to the device to trigger the relay.

My goal is to send the same command (as hex code) that triggers the relay from Raspberry and not from the proprietary software; and to work of course :D.
My logic (faulty or not ) was that sending the same hex code as the proprietary software sent to the device to trigger the relay should have the same effect. This seems like Captain Obvious.

Unfortunately, it didn't happen.
The message leaves the Raspberry (the led on the RS485 shield confirms it) but the relay isn't triggered and nothing is shown on the proprietary software display.
As a mention, I managed to command the relay with commands from the Raspberry to the device on the RS485 port also present on the device.
I am trying to make the commands work through the S-Bus port since it will benefit more.

Any ideas, hints of what could cause that ? Things to try ?

Serial Novice :D

RE: Raspberry + RS485 shield | pySerial - Larz60+ - Feb-24-2017

Some things come to mind.
Check the protocol on your serial port, you are mainly interested in number of databits
This only matters if the command that triggers the relay uses bit 7 (numbered 0-7)
for any purpose. It is possible to set databits to 7 or 8, with or without parity.

This is only important if the 7th bit is used, in whichcase, set data bits to 8, no parity.

Also, for your edification, hex is just the representstion used.

0101 0101 binary
is the same as 0x55 hex
is the same as 85 decimal.
is the same as 125 octal

RE: Raspberry + RS485 shield | pySerial - Coada - Mar-01-2017

Many thanks for the insight.

Data bits were set to 8 with no parity. No luck.
I will continue trying playing with the connection, see if I get anywhere.

RE: Raspberry + RS485 shield | pySerial - Larz60+ - Mar-01-2017

Does the baud rate match on both ends of the connection?
That's the transfer speed and if it doesn't match you'll just see garbage on the receiving end
(if anything at all)

Also, the fact that the led comes on only indicates data at the source, and nothing about it's format.

I would suggest using putty (available on line free) on the receiving end. that way you can play with the baud rate parity, etc
without changing the software (until after you know what you're dealing with). In addition, since you indicate that only the data
lines are being used, I expect something like full duplex may be used for hand shaking, which may or may not be the case.

RE: Raspberry + RS485 shield | pySerial - Coada - Apr-13-2017

Sorry for the awol. I've been meddling with other stuff. Thanks for the additional insight.

About the baud rate I'm pretty sure it is the same at both ends of the connection. I have some basic information, a document, about the serial communication used by the device, including the baud rate and I am using that for my python serial port configuration.
Unfortunately, I don't have any tools to measure it.

Regarding the integrity of the message I send from my RPI to the device, I tried to see what actually reaches the equipment by doing the following setup ( I think you will need to click on the image to enlarge it)
[Image: l6JKU4R.png]
I sent my message from the RPI to Device 1 and with the sniffer on PC (that was connected through serial with Device2 | Device 2 was connected to Device 1 | No link from RPI to PC) I saw on the PC the exact message sent without any noticeable distortion.
That made me believe, that a theoretically correct code, reaches Device 1.
That's the best I could do for now and with the tools I had.

More tests are coming with different settings. Will post any worthy failures and/or breakthroughs.

Thanks again !

RE: Raspberry + RS485 shield | pySerial - sparkz_alot - Apr-13-2017

In addition to Lars60+'s mention of baud rate, data bits and stop bits, many times you also need to set the flow control (or handshaking).  This can either be software or hardware controlled.  Software being Xon (ascii character 17) and Xoff (ascii character 19). Hardware makes use of pins on the chip, CTS (Clear to Send), DTR (Data Terminal Ready) and RTS (Ready To Send).

They make inline testers for RS232, RS422 and RS485 running from relatively cheap to outrageously expensive, depending on your needs and pocketbook.

RE: Raspberry + RS485 shield | pySerial - Larz60+ - Apr-13-2017

Don't forget there are software testers as well:

For example:
and more