Coada
Posts: 1
Joined: Fri Feb 24, 2017 12:07 pm

Raspberry + RS485 shield | pySerial

Fri Feb 24, 2017 12:09 pm

Hello,

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

User avatar
paddyg
Posts: 2612
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Raspberry + RS485 shield | pySerial

Fri Feb 24, 2017 8:43 pm

This might or might not be relevant but it is some code I wrote that allowed one of my RPis to communicate with my solar inverter over RS485 (but it was a USB thing, you can get these on ebay for £2.63 inc delivery so that might be a possible option)

Code: Select all

#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals

import serial
import time
import struct

DELAY = 20 #3600 # check delay s

ser = serial.Serial('/dev/ttyUSB0')
enc = bytearray.fromhex('7E 01 A1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 A2')
log_file = 'log_file.csv'

next_check = time.time()

while True:
  tm = time.time()
  if tm > next_check:
    next_check = tm + DELAY
    ser.write(enc)
    tries = 0
    while ser.inWaiting() < 55 and tries < 500:
      time.sleep(0.01)
      tries += 1
    if tries >= 500: # timed out, save a blank line
      cvs_str = '{}-{},,,,,,,,,,,,,,,,,,,,\n'.format(ser.inWaiting(), tries)
    else:
      rec = ser.read(55)
      ser.flushInput()
      rec_sum = sum([int(i) for i in rec[1:54]])
      print(rec_sum % 256, int(rec[54]))
      d = struct.unpack('<4B5HLH4BH2B2HB4H14B', rec)
      cvs_str = '{:3.1f},{:3.1f},{:3.1f},{:3.1f},{:3.1f},{:d},{:3.1f},{:d},{:d},{:d},{:d},{:5.2f},{:d},{:d},{:3.1f},{:3.1f},{:d},{:3.1f},{:3.1f},{:3.1f},{:3.1f}\n'.format(
            d[4]/10.0, d[5]/10.0, d[6]/10.0, d[7]/10.0, d[8]/10.0, #5H DCV, DCA, GRIDV, GRIDA, TEMP
            d[9], #L TOT KWH
            d[10], #H STATE
            d[11], d[12], d[13], d[14], #4B -, -, MODEL, SDP SOFTWARE NO
            d[15]/100.0, #H GRID FREQ
            d[16], d[17], #2B COUNTRY STD, POWER CURVE NO
            d[18]/10.0, d[19]/10.0, #2H DC_IN_V, DC_IN_A
            d[20], #B GRID ON/OFF STATUS
            d[21]/1.0, d[22]/1.0, d[23]/10.0, d[24]/10.0) #4H KWH: THIS_M, LAST_M, THIS_D, LAST_D
    print(cvs_str)
    with open(log_file, 'a') as f:
      f.write(cvs_str)

  time.sleep(1.0)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”