3

How does RS-485 communication between Raspberry Pi and various sensors work?

Note: I would like to use it with Pyhton 2.7.

Can I connect ADM485 to the UART (Tx, Rx) of Raspberry Pi? I want to use minimalmodbus to communicate with Arduinos receiving information from multiple temperature sensors.

Any there any references to this? I read the documentation of minimalmodbus and it sounds great. Can I go forward or is there any other better Python mdoules for a beginner like me?

SlySven
  • 3,621
  • 1
  • 18
  • 45
Ruthvik Vaila
  • 793
  • 2
  • 12
  • 21

2 Answers2

9

1- Managing the send / recieve modes by soft will most likely result in failure for the communication

[Ref.Link to be inserted here]

I am trying to hook up a Tristar TS-MPPT-60 to a Raspberry Pi using an RS-485 chip connected to the Raspberry Pi UART, and eventually add my Arduinos to the network. I have some MAX485 chips and a SparkFun RS-485 BOB to do my tests.

References:

2- To let the Raspberry Pi control the RTS / CTS you need access to pins 30 and 31

Here is what I got hooked up, but not tested yet: Original image: http://www.homegenie.it/forum/index.php?topic=208.0

Enter image description here

3- To use the UART you need to have it released by the Raspberry Pi internal use (undesired traffic):

The MakeFile is broken. Do not forget to fix this code for the gpio_setfunc function to compile:

In the Makefile change the line:

cp $(BINARIES) $(SCRIPTS) $(BINDIR)

to

cp $(BINARIES) $(BINDIR)

To make this restart safe, we put the command into /etc/rc.local:

# Enable CTS + RTS on P5 header for onboard serial
/usr/local/bin/gpio_setfunc 31 ALT3
/usr/local/bin/gpio_setfunc 30 ALT3

3-Here is a sample of code that should allow to communicate with slaves.

pymodbus rtu RS-485 communication

Pay attention to the corrections below, the code block in the previous link.

temps  = client.read_input_registers(30001, 4, unit=0x0a) # Address, count, slave address
print temps.registers
coil  = client.read_coils(10001, 8, unit=0x0a) # Address, count, slave address
print coil.bits

In the sample code, replace the undesired USB port with the TTL one:

port="/dev/ttyUSB0"
to
port="/dev/ttyAMA0"

All of this is untested, but that is a resume of all my findings up to now.

Now my latest issue is that the slave has a big 12 V mark on the Modbus port. Will my Raspberry Pi get fried? I'll keep you posted.

WARNING:

As Chris pointed out in the comments below, that amateur setup is untested and might fry your pi.

You've got MAX485 connected to 5V and its D/R/DE/RE pins connected to Pi's GPIO >directly. This isn't safe as GPIO pins are designed to operate at 3.3V whereas >the R pin will send 5V signals (not sure if the other pins ever send 5V signals >from MAX485). This mistake seems so common and yet I haven't read someone >saying they actually burnt their pi doing that, so maybe it's usually fine, but >technically you should use a 5V to 3.3V level converter. Or replace MAX485 with >MAX3485 and power via 3.3Vcc pin.

  • Welcome and thanks for what appears to be a thoughtful answer. I've removed your preamble about the nature of the Q&A, since, to clarify a significant detail indicated in comments on ppumkin's answer and the edit history, Ruthik asked a specific question and then a week later modified it to exclude the answer. That is NOT a good way to go about things -- it is asking other people to waste their time trying to help after you've been too lazy to make your requirements known in the first place. Something to consider WRT "nothing is more annoying than people who ...." ;) – goldilocks Jun 14 '15 at 20:25
  • Thank you for making my 1st post politically correct :-) – Mathieu Berube Jun 14 '15 at 20:31
  • Based on the lack of update, I'm guessing this didn't work? I'd love to do something like this on Raspberry Pi Zero W. – Winter Dragoness Nov 12 '17 at 22:19
  • I ended up using an Arduino + RS232 to TTL Converter. (serial instead of Modbus) and was planning having the Arduino and RPI talk through I2C. The main issue I had, was with the duplex comm. The timing of flicking the P5 pin to switch from tx to rx was a real pain with a half-plex MAX485. If you want to try MODBUS with a RPI, I highly recommend buying either 2 simplex chips or full-duplex like the MAX488 and MAX489 so you don't have to switch between tx / rx on the same line. If your interest is more into having code to talk to a TS-MPPT-60, I would gladly share some of my Arduino code. – Mathieu Berube Nov 14 '17 at 00:45
  • 1
    My interest is half-duplex (two-wire) with ability to toggle between transmit and receive in rs485 at 9600 baud to talk to a Tesla Wall Charger. It needs to be 3.3Vcc powered and work with pi Zero W. I'm planning on using MAX3485 or similar and controlling DE/RE pins via software using https://www.raspberrypi.org/forums/viewtopic.php?t=33982 or https://sourceforge.net/p/arduino-icsc/wiki/Raspberry Pi/ code. I know people have problems using software DE/RE at higher speed but it should be fine for 9600 baud. For higher speed, MAX13487E is recommended but needs 5V. – Winter Dragoness Nov 21 '17 at 19:31
  • 1
    Also, you've got MAX485 connected to 5V and its D/R/DE/RE pins connected to Pi's GPIO directly. This isn't safe as GPIO pins are designed to operate at 3.3V whereas the R pin will send 5V signals (not sure if the other pins ever send 5V signals from MAX485). This mistake seems so common and yet I haven't read someone saying they actually burnt their pi doing that, so maybe it's usually fine, but technically you should use a 5V to 3.3V level converter. Or replace MAX485 with MAX3485 and power via 3.3Vcc pin. – Winter Dragoness Nov 21 '17 at 20:49
  • Chris You are most probably completely right about the errors in my setup. I am an amateur. I couldn't find anything on the web to help me accomplish this so I made that lengthy post showing what I had up to that point hoping to find you. A bit sad you found this 2 years later, not when i was making that setup. I will add your comments to the post. Thank you. – Mathieu Berube Nov 22 '17 at 20:13
7

I do not think you will be able to directly connect the RS-485 to the RPi with software bitbanging.

It think the easiest way would be to use a USB <-> RS-485 device. Then you can use the USB as a serial port and let the rest happen as it should.

enter image description here

I did find something like that at Farnell and it says it supports Linux 2.4+ - But the driver might need to get tweaked to work on ARM version of Linux. Then just use Python or whatever using built in Serial Port tools and code!

enter image description here

Thanks to Phil Vallone for providing a solution to use the FTDI chip on the Pi

Piotr Kula
  • 17,307
  • 6
  • 65
  • 104
  • 1
    +1 Here is a answer which provides a link to the FTDI drivers http://raspberrypi.stackexchange.com/questions/14894/how-to-install-pitft-and-use-to-the-gpio-for-other-usage-like-serial-console/14896#14896 – PhillyNJ May 19 '14 at 14:29
  • Thanks for the link. Really awesome that there is a solution. I will just add references to my answer also. THanks again. – Piotr Kula May 19 '14 at 14:38
  • 1
    Happy to Help - Here is the text version. http://www.ftdichip.com/Drivers/D2XX/Linux/ReadMe-linux.txt – PhillyNJ May 19 '14 at 14:41
  • adding detail to ppumkin's answer : RS485 uses voltage levels that are different than the Pi has available. And also uses what is called "differential" output – Marla May 19 '14 at 14:57
  • can't i use UART pins Rx and Tx ? whats the necessity in going for USB to UART convertion when i have UART pins on raspberry pi. i will use ADM 485 . will this work? – Ruthvik Vaila May 26 '14 at 09:03
  • @ppumkin pls see the edited question. – Ruthvik Vaila May 27 '14 at 04:14
  • 3
    Well. What can I do? I answered you original question and you changed it substantially :( What's wrong with the USB device though? – Piotr Kula May 27 '14 at 08:33