Skip to content

Instantly share code, notes, and snippets.

@Bouni
Last active November 12, 2018 15:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bouni/49a292798e88a7a0956c308b3c9d1190 to your computer and use it in GitHub Desktop.
Save Bouni/49a292798e88a7a0956c308b3c9d1190 to your computer and use it in GitHub Desktop.
Get RS485 on a BeagleBoneGreen + Waveshare CAN RS485 CAPE up and running
import serial, fcntl, struct
#inspired by http://inspire.logicsupply.com/2014/09/beaglebone-rs-485-communication.html
#enable ttyO4 by editing /boot/uEnv.txt:
#cape_enable=capemgr.enable_partno=BB-UART4
# RX and TX Jumpers on the Waveshare Cape are set to UART4
# A jumper wire is connected from P9_15 to RSE (middle pin of the three)
s = serial.Serial(
port="/dev/ttyO4",
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
# Standard Linux RS485 ioctl:
TIOCSRS485 = 0x542F
# define serial_rs485 struct per Michael Musset's patch that adds gpio RE/DE control:
# (https://github.com/RobertCNelson/bb-kernel/blob/am33x-v3.8/patches/fixes/0007-omap-RS485-support
SER_RS485_ENABLED = (1 << 0)
SER_RS485_USE_GPIO = (1 << 5)
# Enable RS485 mode using a GPIO pin to control RE/DE:
RS485_FLAGS = SER_RS485_ENABLED | SER_RS485_USE_GPIO
# The GPIO pin to use, using the Kernel numbering:
RS485_RTS_GPIO_PIN = 48 # GPIO1_16 -> GPIO(1)_(16) = (1)*32+(16) = 48
# Pack the config into 8 consecutive unsigned 32-bit values:
# (per struct serial_rs485 in patched serial.h)
serial_rs485 = struct.pack('IIIIIIII',
RS485_FLAGS, # config flags
0, # delay in us before send
0, # delay in us after send
RS485_RTS_GPIO_PIN, # the pin number used for DE/RE
0, 0, 0, 0 # padding - space for more values
)
# Apply the ioctl to the open ttyO4 file descriptor:
fd=s.fileno()
fcntl.ioctl(fd, TIOCSRS485, serial_rs485)
s.write("Hi there!")
while True:
res = s.readline()
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment