PID Class¶
-
class
metlinkpid.PID(serial: serial.serialposix.Serial, ignore_responses: bool = False)¶ A
PIDobject represents a serial connection to a physical display.PIDobjects are typically constructed using thePID.for_device()class method, and can send messages in the form ofMessageobjects, strings, or rawbytesusing thesend()method. It is possible toping()the display at regular intervals to persist the currently-displayed message.PIDobjects also manage the CRC checksumming & DLE/STX/ETX packet framing used by the display in what it receives & transmits, and ensure that every instruction sent to the display is acknowledged.PIDobjects are context managers, enabling automatic closing of the underlyingSerialconnection when used in awithblock:with PID.for_device(...) as pid: pid.send(...) # serial connection will never be open at this point
- Parameters
serial – a
serial.Serialobject. In normal use a correctly configured one is set byPID.for_device().ignore_responses – whether to ignore the response from the PID whenever
PID.send()is called. Defaults toFalse.
PID.for_device() Class Method¶
-
classmethod
PID.for_device(port: str, ignore_responses: bool = False) → metlinkpid.PID¶ Construct a
PIDobject connected to the specified serial device with a correctly configuredserial.Serialobject.The
Serialobject is configured to time out after 500ms during read operations, which is ample time for the display to send acknowledgement after being written to.- Parameters
port – the serial device name, such as
/dev/ttyUSB0on Linux orCOM1on Windows. The correct device name can be found on Linux by unplugging and re-plugging the display connection, runningdmesg, and inspecting the output for the device name.ignore_responses – whether to ignore the response from the PID whenever
PID.send()is called. Defaults toFalse.
- Raises
serial.SerialException – if the serial device can’t be found or can’t be configured.
send() Method¶
-
PID.send(data: Union[str, metlinkpid.Message, bytes]) → None¶ Send data to the display—most typically message data, although any
bytesdata can be sent.If a string is provided, it is converted to a
DisplayMessageobject usingDisplayMessage.from_str(), thento_bytes(), then CRC-checksummed and packet-framed before sending.If a
Messageobject is provided (usually aDisplayMessagebut sometimes aPingMessage), it is convertedto_bytes(), then CRC-checksummed and packet-framed before sending.If a
bytesobject is provided that is not a valid DLE/STX/ETX packet (\x10\x02 ··· \x10\x03), the bytes are CRC-checksummed and packet-framed before sending.If a
bytesobject is provided that is a valid DLE/STX/ETX packet, the packet is assumed to already contain a correct CRC checksum and sent without change.
- Parameters
- Raises
serial.SerialTimeoutException – if the display doesn’t respond with acknowledgement.
serial.SerialException – if any other serial device error occurs, such as the serial port being closed.
ping() Method¶
-
PID.ping() → None¶ “Ping” the display, which has no direct visual effect but impedes the automatic clearing of the display (which otherwise occurs after approximately one minute of inactivity).
In deployment, Metlink displays are typically pinged every ten seconds in addition to all other traffic sent to them.
This method is simply a convenience wrapper for
send()-ing aPingMessage:pid.send(PingMessage())
- Raises
serial.SerialTimeoutException – if the display doesn’t respond with acknowledgement.
serial.SerialException – if any other serial device error occurs, such as the serial port being closed.