1

我在 20% 的尝试中收到 pymodbus.exceptions.ModbusIOException。

'[Input/Output] No Response received from the remote unit/Unable to decode response' fcode = 3

该代码是通过 QTimer 事件调用的,我尝试将此范围从 30 毫秒更改为 5000 毫秒并收到相同的结果。另外 80% 的时间代码按预期工作。这是python时间问题还是其他什么?

def Start(self):
    self.Client = self.Create("COM1")

    self.MbTimer = QtCore.QTimer()
    self.MbTimer.timeout.connect(self.OnTimer)
    self.MbTimer.start(1000)

def Create(self,com):
    client = ModbusSerialClient(method='rtu', port=com, timeout=1,baudrate=19200)
    client.connect()
    return client

def OnTimer(self):
    value = self.Get(1)
    print(value)

def Get(self,address):
    try:
        rr = self.Client.read_holding_registers(address,1,unit=1)
    except Exception as e:
        return None

    if type(rr) == ModbusIOException:
        if(rr.fcode == 3):
            ##This line is reached ~20% of attempts
            return None
        else:
            raise Exception("not fcode 3:"+str(rr.fcode)+str(rr.message));
    elif(type(rr) == pdu.ExceptionResponse):
        return None
    else:
        value = float(rr.registers[0])
        return value
4

0 回答 0