在 Raspberry Pi 3B+ 上使用 minimalmodbus python 库和 Waveshare RS485 CAN HAT (B) 的 Modbus 校验和错误

Modbus Checksum Error using minimalmodbus python library with Waveshare RS485 CAN HAT (B) on Raspberry Pi 3B+

提问人:Farizno 提问时间:11/16/2023 最后编辑:Farizno 更新时间:11/20/2023 访问量:56

问:

我正在尝试使用 minimalmodbus 库从 MP Filtri 公司称为 ICM 的工业传感器读取寄存器值。我正在使用连接在 Raspberry Pi 3B+ 顶部的 Waveshare RS485 CAN HAT (B)。

当我尝试读取寄存器时,我收到如下校验和错误响应(用户名已编辑,因为它包含我的姓氏):Checksum Error

我正在使用外部 24VDC 电源为 Waveshare Hat(为 Pi 供电)和 MP Filtri ICM 传感器供电。传感器的 A 线和 B 线正确连接到 Waveshare Hat 上“RS485_0”的 A 和 B 端子。传感器和 Pi/hat 共享相同的电源,并且接地已连接。

我已经配置了 Waveshare 帽子,如 Waveshare wiki 中的“安装库”和“驱动程序配置”部分所示。按照描述在 /boot/config.txt 中配置驱动程序后,我可以看到设备已作为 ttySC0 和 ttySC1 添加到 /dev。此外,我运行了建议的命令并得到以下输出: GREP SPI **这一点很重要,因为您将在下面的代码中看到,我可以返回任何东西的唯一波特率是 921600,如 ttySC0 和 ttySC1 的屏幕截图所示dmesg | grep -i '\(can\|spi\)'

ICM传感器手册的Modbus部分显示以下设置信息:

3.1 Modbus 设置

协议类型 RTU(非 ASCII) 数据位 8
停止位 1
需要奇偶校验,甚至
波特率 自动感应 1200-115200
信令 RS485
节点地址 204(或用户设置)

3.2 通信检查
您应该能够从寄存器 0(从 Modbus 节点地址 204)读取产品 ID 代码。产品 ID 代码为值 54237(十进制)或0xD3DD(十六进制)。

在编写 Python 脚本以从传感器(产品 ID)读取寄存器 0 时,我使用了 minimalmodbus 文档中的基本代码。我还使用了设备中设置的 Modbus 从站地址 4。上面的文档页面指出,地址 204 可以用作永久地址,但也可以在设备中设置地址,它也将在其上进行响应。我尝试了两者,结果相同,但是如果以后将更多设备添加到总线中,我将使用设备中设置的 salve 地址,即 4。我在名为 basic.py 的文件中使用的代码如下所示:

#!/usr/bin/env python3

import minimalmodbus

instrument = minimalmodbus.Instrument('/dev/ttySC0', 4, debug = True)  # port name, slave address (in decimal)

instrument.serial.baudrate = 921600             # Baud
instrument.serial.bytesize = 8                  # bytesize
instrument.serial.parity   = minimalmodbus.serial.PARITY_EVEN
instrument.serial.stopbits = 1                  # stop bits
instrument.serial.timeout  = 0.5                # seconds
instrument.mode = minimalmodbus.MODE_RTU        # rtu or ascii mode
instrument.clear_buffers_before_each_transaction = True

## Read Product ID From Register 0 ##
prodid = instrument.read_register(0, 0)  # Registernumber, number of decimals
print(prodid)

我收到的响应是校验和错误,如上所示,也在这里:Checksum Error

为什么我会收到此校验和错误消息,以及如何消除此错误?另外,为什么唯一有效的波特率是 921600?这是因为 ttySC0 的基本波特率显示为这个速率吗?

树莓派 MinimalModbus

评论

0赞 Brits 11/16/2023
“只有波特率才有效 921600” - 获得损坏的响应并不意味着它有效!文档显示传感器支持 1200-115200(所以不是 921600;waveshare 应该支持 300~921600)。
0赞 Farizno 11/16/2023
我尝试了几种不同的波特率。默认值为 9600。我尝试了最小值 1200,最大值 115200,以及介于两者之间的一些。它们都在调试模式下给出响应,即设备期望接收 7 个字节,但它收到 0。它还以:“raise NoResponseError(”No communication with the instrument (no answer)“) minimalmodbus 结尾。NoResponseError:无与仪器通信(无应答)
0赞 Brits 11/16/2023
OK - unfortunately there are a lot of variables here so solving this can take time (I generally connect to devices from a PC with a known good RS485 adaptor first). Firstly node address; the manual screenshot you include above says but you have used (which matches the full manual you linked) please confirm settings (worth adding current settings to your question rather than relying on the manual access settings on the device). There is also a "Diagnostics Display" on the device that should tell you how many chars it has received (good for verifying comms).2044
0赞 Farizno 11/17/2023
I've edited the post by changing the photo of the manual text to a blockquote. I also added notation describing the node address used being address 4 as the set address in the device, but also that address 204 is a hard coded address that can also be used. Neither option makes any difference. Also to note: The diagnostics display shows "char errors" when running the python program, which can vary from 1-4 char errors. It also shows frames received, accepted, and sent. The received frames varies from 1-4, but accepted and sent remain at 0.
0赞 Brits 11/17/2023
And what baudrate does it show (first check is ensuring the device is detecting the baudrate you are using). I don't think that this is going to be a good fit for stackoverflow because there are too many unknowns (i.e. the issue may just be that you need to run an earth connection) and stackoverflow is not setup for discussions;

答:

1赞 Farizno 11/20/2023 #1

After trying many different options, baud rates, coding, and multiple exchanges with Waveshare's customer service department, I finally came to a very simple solution to my question.

The Waveshare RS485 CAN HAT (B) has jumpers on each of the two RS485 channels that enable a 120 ohm resistor by default. Waveshare's customer service suggested trying without the resistors enabled, as they are not always needed on RS-485 communication (according to them). As soon as i removed the jumper, the program responded and I was able to read registers from the sensor.

评论

0赞 Brits 11/20/2023
They do mention this in their FAQ; bit unexpected (RS485 generally works without termination over short distances; but I did not think that having the termination would be an issue)