Почему я получаю pymodbus ModbusIOException на 20% попыток? - PullRequest
0 голосов
/ 08 ноября 2019

Я получаю pymodbus.exceptions.ModbusIOException при 20% моих попыток.

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

Код вызывается с помощью события QTimer, и я попытался изменить этот диапазон с 30 мс до 5000 мс и получить те же результаты. Остальные 80% времени код работает как задумано. Это проблема с питоном или что-то еще?

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
...