Serial Piece

A base Piece for implementing serial interface communication using the puzzlepiece framework.

Defines a port selector param and a “connected” param. self.port is a pySerial Serial object that can be used to interact with the port. An ensurer is provided to make sure the user has connected to a serial port before performing operations.

Example implementation:

import puzzlepiece as pzp
from pzp_hardware.generic.hw_bases import serial

class Piece(serial.Base):
    serial_baud = 115200 # change the default baud rate if needed

    def define_params(self):
        super().define_params()

        @pzp.param.checkbox(self, 'open', 0)
        @self._ensure
        def open(value):
            if self.puzzle.debug:
                return value
            self.port.write(b'ens\r')
            self.port.read_until(b'\r')
            return value

For a full Piece implemented using this base, see pzp_hardware.thorlabs.shutter_sc10.Piece.

Requirements

This Piece has additional requirements. You will be asked to install them at runtime, or you may choose to install them ahead of time.

class pzp_hardware.generic.hw_bases.serial.Base(puzzle=None, custom_horizontal=None, param_defaults=None, *args, **kwargs)[source]

Bases: Piece

../_images/pzp_hardware.generic.hw_bases.serial.Base.png

Base Piece implementing serial communication.

serial_baud = 9600

Baud rate for the serial connection (set in your child class)

serial_timeout = 3

Timeout for the serial connection (set in your child class)

port = None

A pySerial port (only available when connected)

serial = None

A reference to the pySerial library (if not in debug mode)