【问题标题】:pyserial/Modbus - troubleshooting "No communication with instrument"pyserial/Modbus - 故障排除“与仪器无通信”
【发布时间】:2019-02-22 20:00:38
【问题描述】:

Beaglebone Black Wireless:Ubuntu 16.04,

计算机:Ubuntu 16.04,

Python 3.5.2

我正在尝试使用 Beaglebone Black 通过 Modbus 与设备通信。我读过有人使用minimalmodbus 使用 USB/RS485 加密狗做同样的事情。我试过改变设置;每种可能的波特率、较短的电缆等。Beagle 中必须有一些东西需要配置/错误配置用于此类工作。

代码

#!/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)

...使用USB/RS485 加密狗。使用这个加密狗/代码组合在我的计算机上(过去 4 个月)产生了完美的结果,但在今天的 Beagle 上却没有。

计算机:收到消息:

[0, 0]

Beagle:收到错误:

IOError: No communication with instrument (no answer)

我正在寻找可能的故障排除途径。我已经验证了电缆不是太长,并且 USB 加密狗是可以的。我也排除了任何代码问题。

编辑 1: 忘记包含lsusbls /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

/dev/ttyUSB0

编辑 2:

我能够使用pyserial 并短接 Rxd 和 Txd + 和 - 端子来实现环回设置。

编辑 3:

我已经尝试了所有可能的接线配置。计算机上只有一种配置有效(Tx+ -> A 和 Tx- -> B)。在使用minimalmodbus 调试功能时,我可以看到相同的消息正在发送到 Beagle 上的设备和计算机 ('\x01\x03\x07Ø\x00\x02ED' (01 03 07 D8 00 02 45 44) )。计算机收到响应,而 Beagle 没有。

Beagle 可以轻松地将适配器安装到 /dev/ttyUSB0

编辑 4:

@Carlo Zanocco 请求输出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

编辑 5:

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

【问题讨论】:

  • 编辑您的问题并在控制台窗口中显示lsusb 的输出。
  • 请贴出Modbus设备名称并检查接线是否正确(包括终端电阻)
  • @CarloZanocco 该设备是具有 Modbus 功能的工业科里奥利流量计。接线正确;如果不是,它可能无法在我的计算机上运行,​​如问题中所述。此外,此设置中没有终端电阻。
  • @star_trac 你能发布这个命令的输出吗:“ls /dev/ | grep tty”。还使用库 pyserial 测试串口并检查发生了什么
  • @CarloZanocco 命令ls /dev/ | grep tty 返回与ls /dev/ttyUSB* 相同的内容,就像在问题中一样。另外你是什么意思“测试串口”?我不确定如何使用 pyserial 测试端口。我可以尝试通过 pyserial 通过 /dev/ttyUSB0 建立连接,这是我绝对可以做到的。

标签: python serial-port beagleboneblack beagleboard minimalmodbus


【解决方案1】:

我遇到了同样的问题,结果发现是终止和流量控制问题

我尝试了不同的 USB 加密狗,都适用于 pc 和 mac,以及 BeagleBone 上的许多不同配置,但没有任何效果。

低成本的 USB 加密狗适用于 PC,但对于 BeagleBone 等微控制器,您需要更好地处理总线规范的东西。

我发现一些简单且低成本的 TTL RS232 转 RS485 板在 BeagleBone 端无需任何特殊配置即可完美运行,因为它们会自动管理流控制。 您可以在 Ebay 或类似网站上搜索RS485 automatic flow control

由于 BeagleBone 上的默认串口已经与系统控制台耦合,您需要通过正确编辑文件 /boot/uboot/uEnv.txt 来enable another UART

然后您需要将 5V 电源和 GND(在 P9_5 和 P9_1 引脚上可用)和正确的串行引脚(即 P9_26 和 P9_24,如果您启用了 UART1)连接到适配器的 5V、GND、RX 和 TX 连接器(不要为 RTS 和 CTS 烦恼,因为适配器会为您管理这些)。

在您的代码中将端口名称更改为新的串行端口(即 /dev/ttyO1,如果您启用了 UART1)。

为了轻松调试代码,您可以通过 RS485 线连接 PC 上的 USB 加密狗和 BeagleBone 上的适配器,并打开两个串行终端。

【讨论】:

  • 感谢您的资源,我会得到其中的一个并从那里开始。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
相关资源
最近更新 更多