Minimalmodbus,随机生成错误的crc

Minimalmodbus, randomly generates wrong crc

我目前正在使用 RaspberryPi 进行数据采集项目。当我启动我的请求脚本时,有时(很少见但太频繁)我的从属(计算机)检测到 CRC 错误或无效长度。我猜这个错误可能来自这样一个事实,因为我的脚本很快并且在几秒钟内要求数百个注册,有时消息不完整并且我的奴隶将其检测为错误消息。我想知道 minimalmodbus 是否有可能时间不正确,有时会发送错误或部分请求(不完整)。

从站返回错误:

invalid request: Invalid CRC in request

当奴隶不知道该回答什么时,这是我在主人身上遇到的典型错误:

  error = SLAVE_ERRORS[str(e)]
KeyError: "Checksum error in rtu mode: '\x8aÿ' instead of '\x8fF' . The response is: '4ÿ\x07$Ê\x8aÿ' (plain response: '4ÿ\x07$Ê\x8aÿ')"**

我在从机上使用 modbus_tk 来模拟 modbus 从机。接下来通常是我的代码的一部分,它根据值的类型从从站请求值。

    try:
        try:
            var_register = file_var[i]['varRegister']
            var_type = file_var[i]['varType']
            var_use = file_var[i]["varUse"]
            var_name = file_var[i]["varName"].strip()

            if '#' in var_register:
                continue
            elif var_type=='U16' or var_type=='I16' or var_type=='S16':

                value = inst.read_register(
                    int(var_register),
                    0,
                    3, 
                    not bool(file_var[i]['varSigned'])
                ) 

            elif var_type=='U32' or var_type=='I32' or var_type=='S32':

                value = inst.read_long(
                    int(var_register),
                    3, 
                    not bool(file_var[i]['varSigned'])
                ) 

因为我的第一个猜测是时间问题,所以我随机插入了“time.sleep”来为我的请求计时,但错误仍然出现。而且它是完全随机的,有时它会工作 5 分钟,有时在我的第一个 CRC 错误发生前几秒钟。你知道我应该在哪里调查吗?预先感谢您的帮助!

编辑:我的 PC 充当从属设备,使用 modbus_tk 脚本模拟多个从属设备。 RPI 是主机请求寄存器及其值。从站都配置为在这些特定寄存器中具有值,以避免 IllegalAddress 错误。物理连接是一个 USB 到 RS485 转换器,RPI 配备了一个 HAT 处理 RS485 input/output。所以它实际上是一个ModBus RTU通讯。当请求进来时,slave 循环并发送一个应答。

EDIT2:所以我进一步调查了一下,发现了一些有趣的东西。我收到一个错误(这次是在主机上)从机显然发送了错误的校验和...在查看它时,我发现 modbus_tk 生成的校验和很好,但主机收到的答案是'一样。看起来有些字节在路上发生了变化,这很奇怪。那是从哪里来的?硬件问题?仅在轮询多个寄存器时才会出现此错误(一次超过 2 个)。如果请求的寄存器数量为 1 或 2,所有其他请求都可以。

听起来你的问题可能是模拟奴隶使用的byte order

不,这不是 奇怪的 ,它可能是噪音或其他确实很难war 的问题。

这正是 CRC 存在的原因,以确保您在主机上收到的值与从机发送的值相同。

您很可能有:

  1. 没有将 GND 连接在一起
  2. 一个非常糟糕的电源
  3. 电线很乱
  4. 有大型电动机或任何其他来源或噪音的环境

我的赌注是 1:您认为 RS485 是双线的(您只有那些 A 和 B 或 + 和 - 连接,对吗?)。

我不打算在这里详细介绍,因为您应该能够找到数千甚至数百万的参考资料和帖子,其中这个特定的 war 从一开始就一直在进行。可以这么说,如果你的总线上只有两根线有问题,你可能会通过添加第三根将两个设备上的 GND 连接在一起来解决问题。当一个或两个设备使用电池 运行 或具有隔离电源时,通常会出现这种情况。

如果您想了解更多,可以阅读this document。关于您的问题的一些特别有趣的段落:

Running RS-422/485 a few dozen feet over quality cable with good shielding and ground can be virtually error-free. However, that’s not what people use RS-422/485 for. Any electrical signal running over 1000 m of wire (such as 3300+ ft of antenna) will pick up noise. The differential twisted-pair nature of RS-422/485 helps reduce the impact of this noise, but you must assume a few communications errors occur every day. This means running long RS-422/485 lines is only suitable for protocols that include proper error-detect (a CRC or block-checksum).

和:

Unfortunately, grounding for RS-422/485 is one of the most misunderstood aspects of serial communications. Many people believe that RS-422/485 does not require a ground. One myth is that the plus (+) and minus (-) signals act like a current loop and complete the electrical circuit by themselves. Another myth is that a differential signal just compares two voltages without reference to common ground. But the biggest culprit is the fact that RS-422/485 wired without proper grounding appears to work.