November 5, 2021

Multiple USB RS232 and RS485 Modbus Devices on Raspberry Pi

2021-11-05 first draft

A Raspberry Pi 2 is used to monitor a small off-grid solar installation. Two Renogy charge controllers and a PZEM shunt monitor are connected to the Pi via a USB hub. The port numbers on the usb hub are used to reliably assign the USB-RS232 and USB-RS485 adapters to stable /dev/tty device names.

Create a file, /etc/udev/rules.d/solar.rules and include the following lines to create /dev/tty entries based on the hub port numbers.

#/etc/udev/rules.d/solar.rules
ACTION=="add", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", DRIVERS=="usb", SYMLINK+="tty.usb-$attr{devpath}"

Typical devices from udev will be:
    /dev/tty.usb-1.2.5
    /dev/tty.usb-1.2.6
    /dev/tty.usb-1.2.7

The following command is useful to view the port numbers. The dmesg command can be used to relate the adapter chip driver to the 'lsusb -t' output port numbers and /dev/tty names.

  lsusb -t
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=dwc_otg/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/5p, 480M
        |__ Port 1: Dev 3, If 0, Class=Vendor Specific Class, Driver=smsc95xx, 480M
        |__ Port 2: Dev 4, If 0, Class=Hub, Driver=hub/7p, 480M
            |__ Port 7: Dev 7, If 0, Class=Vendor Specific Class, Driver=pl2303, 12M
            |__ Port 5: Dev 5, If 0, Class=Vendor Specific Class, Driver=ch341, 12M
            |__ Port 6: Dev 6, If 0, Class=Vendor Specific Class, Driver=pl2303, 12M

A USB hub was used because the Raspberry Pi 2 power could not handle three USB adapters.

No comments:

Post a Comment