Search code examples
pythonserial-portbeagleboneblackbeagleboardminimalmodbus

pyserial/Modbus - troubleshooting "No communication with instrument"


Beaglebone Black Wireless: Ubuntu 16.04,

Computer: Ubuntu 16.04,

Python 3.5.2

I am trying to communicate with a device via Modbus using a Beaglebone Black. I have read of people doing this same thing with a USB/RS485 dongle using minimalmodbus. I've tried changing up the settings; every possible baud rate, shorter cable, etc. There must be something within the Beagle that needs to be configured/is mis-configured for this type of work.

Code

#!/usr/bin/env python
# -*- coding; utf-8 -*-

import minimalmodbus
import serial
import time

i = minimalmodbus.Instrument(port='/dev/ttyUSB0', slaveaddress=1, mode='rtu')
i.serial.baudrate = 115200
i.serial.bytesize = 8
i.serial.parity = serial.PARITY_EVEN
i.serial.stopbits = 1
i.serial.timeout = 1
i.handle_local_echo = None

while True:
    try:
        print(i.read_registers(2008, 2, 3))
    except serial.serialutil.SerialException as error:
        print(error)
    except FileNotFoundError as error2:
        print(error2)
    except IOError as error3:
        print(error3)
    time.sleep(1)

...using a USB/RS485 dongle. Using this dongle/code combination yields perfect results on my computer (for the past 4 months) but not the Beagle today.

Computer: Message received:

[0, 0]

Beagle: Error received:

IOError: No communication with instrument (no answer)

I am looking for possible avenues of troubleshooting. I have verified that the cable is not too long, and the USB dongle is OK. I have also ruled out any code issues.

Edit 1: Forgot to include lsusb and ls /dev/ttyUSB*:

Bus 002 Device 004: ID 1a86:7523 QinHeng Electronics HL-340 USB-Serial adapter
Bus 002 Device 003: ID 04d9:0024 Holtek Semiconductor, Inc.
Bus 002 Device 002: ID 0409:0059 NEC Corp. HighSpeed Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

and

/dev/ttyUSB0

Edit 2:

I am able to achieve a loopback setup using pyserial and shorting the Rxd and Txd + and - terminals.

Edit 3:

I have tried all possible wiring configurations. Only one configuration works on the computer (Tx+ -> A and Tx- -> B). While using the minimalmodbus debug feature, I can see that the same message is being sent to the device on the Beagle and the computer ('\x01\x03\x07Ø\x00\x02ED' (01 03 07 D8 00 02 45 44) ). The computer receives a response while the Beagle does not.

The Beagle has no trouble mounting the adapter to /dev/ttyUSB0.

Edit 4:

@Carlo Zanocco requested the output of ls /dev/ | grep tty

tty
tty0
tty1
tty10
tty11
tty12
tty13
tty14
tty15
tty16
tty17
tty18
tty19
tty2
tty20
tty21
tty22
tty23
tty24
tty25
tty26
tty27
tty28
tty29
tty3
tty30
tty31
tty32
tty33
tty34
tty35
tty36
tty37
tty38
tty39
tty4
tty40
tty41
tty42
tty43
tty44
tty45
tty46
tty47
tty48
tty49
tty5
tty50
tty51
tty52
tty53
tty54
tty55
tty56
tty57
tty58
tty59
tty6
tty60
tty61
tty62
tty63
tty7
tty8
tty9
ttyGS0
ttyS0
ttyS1
ttyS2
ttyS3
ttyS4
ttyS5
ttyUSB0

Edit 5:

Output of stty -F /dev/ttyUSB0 -a:

speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>;
swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
discard = ^O; min = 0; time = 0;
parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany
-imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl
-echoke -flusho -extproc

Solution

  • I experienced the same issues and it turned out it was a problem of termination and flow control.

    I tried different USB dongles, all working with pcs and macs, and many different configurations on the BeagleBone, but nothing worked.

    Low-cost USB dongles work well with PCs, but with microcontrollers, such as the BeagleBone, you need something that better handles the bus specifications.

    I found out that some simple and low-cost TTL RS232 to RS485 boards work perfectly without any special configuration on the BeagleBone side since they automatically manage flow control. You can search for RS485 automatic flow control on Ebay or similar websites.

    Since the default serial port on your BeagleBone is already coupled to the system console, you need to enable another UART by properly editing the file /boot/uboot/uEnv.txt.

    Then you need to connect 5V power supply and GND (available on P9_5 and P9_1 pins) and the correct serial pins (i.e. P9_26 and P9_24 if you enabled UART1) to the 5V, GND, RX and TX connectors of the adapter (do not bother with RTS and CTS since the adapter will manage those for you).

    In your code change the port name to the new serial port (i.e. /dev/ttyO1 if you enabled UART1).

    To easily debug your code, you can connect the USB dongle on a PC and the adapter on the BeagleBone through the RS485 wires and open two serial terminals.