Connections

PyLabware connection adapters.

class PyLabware.connections.AbstractConnection(connection_parameters)[source]

Bases: ABC

Base abstract class for all connection adapters.

Parameters:

connection_parameters (Dict) – Dictionary with connection settings relevant for the concrete type of connection.

DEFAULT_CONNECTION_PARAMETERS: Dict = {'address': None, 'command_delay': 0.5, 'encoding': 'UTF-8', 'port': None, 'receive_buffer_size': 128, 'receive_timeout': 1, 'receiving_interval': 0.05, 'transmit_timeout': 1}
abstract close_connection()[source]

Closes the connection.

This method has to be redefined in child classes.

abstract is_connection_open()[source]

Checks whether the connection is open.

This method has to be redefined in child classes.

Returns:

True if connection is open, False if connection is closed.

Return type:

(bool)

abstract open_connection()[source]

Opens the connection. This method should create self._connection object, set up necessary parameters and open connection. Connection object creation shouldn’t be done in __init__(), otherwise the connection object instance might not be reusable after close()-open() sequence.

This method has to be redefined in child classes.

abstract receive()[source]

Receives data from the device

This method has to be redefined in child classes.

Returns:

Data from the device.

Return type:

(str)

abstract transmit(msg)[source]

Transmits the data to the device.

This method has to be redefined in child classes.

Parameters:

msg (str) – Data to send.

class PyLabware.connections.HTTPConnection(connection_parameters)[source]

Bases: AbstractConnection

HTTP REST connection adapter based on Requests library.

Parameters:

connection_parameters (Dict) – Dictionary with connection settings for HTTP REST connection.

HTTP_DEFAULT_CONNECTION_PARAMETERS: Dict = {'headers': '', 'password': None, 'schema': 'http', 'user': None, 'verify_ssl': True}
close_connection()[source]

Closes connection.

is_connection_open()[source]

Dummy method as REST is stateless.

open_connection()[source]

Creates requests.Session() object & sets it’s parameters.

receive()[source]

Passes the request response back.

Returns:

HTTP reply from the device packed into LabDeviceReply object together with reply headers.

Return type:

(LabDeviceReply)

transmit(message)[source]

Sends request to the server.

Parameters:

message (Dict) – Dictionary containing method, endpoint and request data

class PyLabware.connections.SerialConnection(connection_parameters)[source]

Bases: AbstractConnection

Serial connection adapter.

Parameters:

connection_parameters (Dict) – Dictionary with connection settings relevant for the serial connection.

SERIAL_DEFAULT_CONNECTION_PARAMETERS: Dict = {'baudrate': 9600, 'bytesize': 8, 'dsrdtr': False, 'inter_byte_timeout': False, 'parity': 'N', 'rtscts': False, 'stopbits': 1, 'write_timeout': 0.5, 'xonxoff': False}
close_connection()[source]

Closes serial connection.

connection_listener()[source]

Periodically checks for new data on the connection, reads it, puts the data read into receive buffer and raises data ready flag.

is_connection_open()[source]

Checks whether the serial port is opened.

Returns:

If the serial port is open.

Return type:

(bool)

open_connection()[source]

Creates, sets up and opens serial connection.

receive(retries=3)[source]

Gets the data from receive buffer, clears the data ready flag and passes the data back.

Returns:

Reply from the device packed into LabDeviceReply object.

Return type:

(LabDeviceReply)

Parameters:

retries (int) –

transmit(msg)[source]

Sends message to the serial port.

Parameters:

msg (str) –

class PyLabware.connections.TCPIPConnection(connection_parameters)[source]

Bases: AbstractConnection

Socket-based TCP/IP connection adapter.

Default constructor.

Parameters:

connection_parameters (Dict) – Dictionary with connection settings for socket-based connection.

TCPIP_DEFAULT_CONNECTION_PARAMETERS: Dict = {'protocol': 'TCP'}
close_connection()[source]

Closes connection.

connection_listener()[source]

Periodically checks for new data on the connection, reads it, puts it into receive buffer and raises data ready flag.

is_connection_open()[source]

Checks whether the socket is open

Returns:

If the socket is open.

Return type:

(bool)

open_connection()[source]

Creates, sets up and and opens the socket.

receive(retries=3)[source]

Gets the data from receive buffer, clears the data ready flag and passes the data back.

Parameters:

retries (int) – Number of times to retry if receive times out. Introduced due to RCTDigitalHotplate test showing that around 0.02 % of attempted get_temperature calls timeout. These are spaced out and not all happening at once so 1 retry should be sufficient to protect against these anomalies and prevent unnecessary crashes.

Returns:

Reply from the device packed into LabDeviceReply object.

Return type:

(LabDeviceReply)

transmit(msg)[source]

Sends message to the socket.

Parameters:

msg (str) –