1

I have Raspberry 2B with Raspbian Jessie. I want my Raspberry Pi can communicate with some sensors via RS-485 interface. I tried to use three types of adapters:

  • USB-RS-485 adapter;
  • USB-RS-232 adapter with RS-232 to RS-485 converter;
  • MAX485.

enter image description here

At first I tried to communicate with the sensors from my PC (Windows) and everything works just fine, serial ports and adapters work perfectly. Next I tried to repeat this on my Raspberry Pi.

A have done all preparations on the Raspberry Pi to communicate via serial port and RS-485 (using this article and others in the Internet). Then I wrote a communication script with Python (and Mono + C#, and C++ but there was the same problem). There is Python code for serial port connection, sender and receiver:

import serial
import time

# Such parameters are required for the sensors so I use them
receiver = serial.Serial(     
     port='/dev/ttyUSB0',
     baudrate = 9600,
     parity=serial.PARITY_EVEN,
     stopbits=serial.STOPBITS_ONE,
     bytesize=serial.SEVENBITS,
     timeout=1
     )

while 1:
      x = receiver.readline()
      print x

# -----------------------------------
sender = serial.Serial(     
     port='/dev/ttyUSB1',
     baudrate = 9600,
     parity=serial.PARITY_EVEN,
     stopbits=serial.STOPBITS_ONE,
     bytesize=serial.SEVENBITS,
     timeout=1
     )

while 1:
      sender.write('Test message')
      time.sleep(1)

When I connect both adapters to the Raspberry (to make an echo) this script works fine, I can send and receive messages in both directions. It's OK!

THE PROBLEM: when I try to connect the Raspberry with the sensors or with the PC with same Python script the Raspberry can only receive messages from the outside but it can't send messages outside (with different parameters, program languages, etc.). They are just lost. There are no any exceptions, error codes or something else.

Does anyone know what could be the problem? Help me please! Googling does not help. Maybe the problem is in permissions? Maybe I forgot about some settings of serial ports? Or do I use wrong devices (but they work with my PC)?

2 Answers 2

2

Alexander. I also try to use this adapters :
USB-RS485 enter image description here.

I need this adapters to polling counters of electroenergy (like Энергомера CE102M, Меркурий 230) with using Raspberry Pi. My Raspberry Pi is model B+ with raspbian jessie.

First, USB-RS485, recognized as /dev/ttyUSB0. But his driver (ch341) does not support mode 9600/7databits/even/1stopbit (as writen here (http://lxr.free-electrons.com/source/drivers/usb/serial/ch341.c?v=4.4), this does not support parity, various count databits and stopbits). However, there is driver (http://www.wch.cn/download/CH341SER_LINUX_ZIP.html) where fixed some problems. E.g. serial device now responds on changing count of databits. But not for parity. To test this i connect 2 same devices between themselves via RS485 and connect them to Raspberry Pi. Then i use picocom to change device paramters and trancive data between them. When i use this adapter with Windows, this works perfectly in mode 9600/7databits/even/1stopbit. Because used other driver.

Second, UART-RS485, recognized as /dev/ttyAMA0 (if all preparations is fine). With this device i don't find true way to connect this to GPIO (i relied on this scheme https://www.homegear.eu/index.php/RS485_Serial_Module). As i understand, besides GPIO 14 (UART RX) and GPIO 15 (UART TX), need RTS and CTS signals to hardware flow control. These features may get as described here (https://www.raspberrypi.org/forums/viewtopic.php?p=768770#p768770) on GPIO 16 and GPIO 17. I try this, but i recieve echo-signal when send some data over this device. And programm for polling my devices over RS-485 recieve error. If i don't use RTS/CTS, i can only write to this device, but can't read. I think, when use /dev/ttyAMA0, need right use RTS/CTS signals and use rtscts=True when create object of Serial:

ser=serial.Serial(port='/dev/ttyUSB0',baudrate = 9600,rtscts=True)

I also try connect these adapters together via RS-485 to test them (see http://raspberrypi.ru/forum/topic/1044/).

I also have USB-RS-485/CAN adapter (http://incotex-counter.blogspot.ru/2012/04/usb-rs485can.html), that works fine with two types of my devices (Энергомера СЕ102М (amount:36) and Меркурий 230 (amount:6), all devices connected to one bus RS-485). It connected to Raspberry Pi where runnning ser2net (to access to serial port over Internet). This device based on FTDI chip and his driver support all modes of serial ports (various parity, databits, stopbits).

Update. Now i build driver ch34x from sources (http://www.wch.cn/download/CH341SER_LINUX_ZIP.html): make make load and i have driver ch34x. Then i assign this driver to usb-rs485 dongle (see this https://unix.stackexchange.com/questions/284106/using-specific-driver-version-for-the-device-jointly-defaul-driver). Now, on restart this driver loaded firtst to work with usb-rs485. To polling Энергомера CE120M via RS-485 i use ser2net with config (/etc/ser2net.conf): #Energomera CE102M 2000:raw:200:/dev/ttyUSB0:9600 7DATABITS EVEN 1STOPBIT . In AminTools i use channel based on Ethernet (i use ip-address of Raspberry Pi and tcp port 2000). For Энергомера СЕ303 settings are same (7databits/even/1stopbit), beside you need select appropriate device type in AdminTools.

5
  • Thank you very much for such detailed answer!!! You gave me a ton of information I looking for for a very long time! By the way I had a same task to get information from an Энергомера СЕ 303 but I was not able to poll this meter with a Raspberry Pi and USB-RS485 and UART-RS485. For now I use USB/RS-485 ОВЕН АС4 but it is quite expensive. Commented May 29, 2016 at 17:35
  • Also now I am getting data from water flow meter УВР-011 that has both RS-485 and RS-232 ports. The interesting thing that this time I used RS-232 with HL-340 usb - serial adapter and it works perfectly "from the box" with a Raspberry (using simple Serial library)! Commented May 29, 2016 at 17:41
  • HL-340 looks like used same driver ch341 (from inf file for driver of that: USB\VID_1A86&PID_7523, USB\VID_1A86&PID_5523, USB\VID_4348&PID_5523 - first two comply driver ch341), or you may view dmesg out or lsmod to establish it. If used ch341 than it support only 8databits and none parity (imho). So if УВР-011 used same parameters of interface RS-485 it would work.
    – Ivan Ts
    Commented May 30, 2016 at 15:11
  • Thanks for the clarification, @Ivan! You right, for the УВР-011 required serial port settings are: 8N1 (baud rate: 9600 baud; parity: none, data bits: 8; stop bits: 1). I checked out on the Raspberry that both ports really use the same ch341 driver. Thats why the HL-340 works! Commented May 30, 2016 at 20:13
  • I misspoke: "I checked out on the Raspberry that both devices (USB-RS485 and HL-340) really use the same ch341 driver". Commented May 30, 2016 at 20:34
1

The problem of connecting the Raspberry Pi to my PC via serial port has been solved by setting all port parameters more precisely in both sides. The pySerial is working perfectly for all mentioned devices in case of using simple serial port.

But I couldn't set up communication with sensors via RS-485 standard where it is required. I couldn't receive any signal from any sensor. Nevertheless, I am newbie in serial ports and RS-485 and I'm not sure that I clearly understand such parameters as delay_before_tx/rx, RTS/CTS, DSR/DTR etc. Maybe I have just set it up wrong for my devices.

Then I used one of more complex devices. For example Russian ones: 'Меркурий 221 USB to CAN/RS-232/RS485' or 'Сапфир USB to RS232-RS485/422' (I thing there are a lot of international analogs). Such devices take care of all the low-level settings of RS-485 so you can use them like normal serial port. Luckily I have one of these (Сапфир). I used simple Serial class of the pySerial library and finally everything works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.