[Python] How to diagnose a serial communication crash that happens randomly and very seldom?

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I'm connecting to a Lakeshore 336 Temperature controller from Linux Mint via pyserial. My app works relatively well and has operated for months at a time without error. However, it sometimes happen that the device in question stops responding generating the following error message:

    device reports readiness to read but returned no data (device disconnected or multiple access on port?)

    This error was also happening when I ran an earlier version of this GUI on a RaspberriPi4 thus likely not due to the specifics of the runtime environment.

    The API for this device inherits from a custom parent class which implements the read function as shown below. Note that I am using semaphores to try to avoid this issue but it still happens! Can someone suggest a way to diagnose (trigger) and perhaps cure this issue?

    def read(self, command):
    response = ''
    if self.mutex.tryLock(self.waitLock):
    try:
    if self.ser is not None:
    if self.serialDevice:
    self.ser.write(bytes(command + self.settings['ending'],'utf-8'))
    response = self.ser.readline().decode('utf-8').strip()
    else:
    self.openSocket()
    self.ser.sendall((command+self.settings['ending']).encode())
    response, listening = '', True
    while listening:
    response += self.ser.recv(128).decode()
    if self.settings['ending'] in response:
    response = response.strip()
    listening = False
    self.closeSocket()
    except Exception as e:
    print('While reading {} from {}, SerialDevice:read raised:'.format(command, self.settings['name']), e)
    response = ''
    finally:
    self.mutex.unlock()
    #pass
    else:
    print('{} not locking while attempting {}'.format(self.settings['name'], command))
    return response

    Continue reading...

Compartilhe esta Página