virtualconnection

This module handles the simulation of the serial connection in a VirtualPump.

class turboctl.virtualpump.virtualconnection.VirtualConnection(process=None, buffer_size=1024, sleep_time=0.01)

A virtual serial connection.

Data can be sent through a VirtualConnection object by accessing the user_end and virtual_end attributes. The port property is a device name which can be given to the initializer of serial.Serial as an argument.

A VirtualConnection object runs code in a parallel thread, which will continue running until it is closed or the Python interpreter exits. A parallel thread can be closed by calling the close() method of the VirtualConnection object that created it. This also closes user_end and virtual_end, and frees their file descriptors.

If a VirtualConnection object is used in a with block in the following manner:

with VirtualConnection() as vc:
    # Some code here

close() will be called automatically when the with block is exited.

If all variables referring to a VirtualConnection object are removed with del or by reassigning them, the parallel thread will continue to run without a possibility of closing it with close(). In this case, all running instances of the VirtualConnection class can be closed with

>>> VirtualConnection.close_all()
buffer_size

The buffer size for the connection (how many bits are read at once).

Type:

int

sleep_time

How long (in seconds) the object waits after checking for input before doing it again.

Type:

float

process

The method used for processing input and forming output. Its signature should be

process(self, input_: bytes) -> output: bytes

A machine or other device that communicates with its user can be simulated by assigning a suitable method to the process attribute.

Type:

function

virtual_end

This end of the connection is used by process() to read and write data. It can be written to and read from with os.read() and os.write().

Type:

file-like object

user_end

This end of the connection is meant to be used by a user to send commands to and read data from a simulated device. os.read() doesn’t seem to work with it, and the serial module should be used instead.

Type:

file-like object

thread

The parallel thread that runs most functionality in a VirtualConnection object.

Type:

threading.Thread

running_instances

Class attribute. A set of all currently running instances of the VirtualConnection class.

__init__(process=None, buffer_size=1024, sleep_time=0.01)

Initialize a new VirtualConnection.

The new instance starts the parallel thread automatically.

Parameters:
  • process (function) – The function assigned to the process attribute. If no value is supplied, the default_process method will be used instead.

  • buffer_size – The value of buffer_size.

  • sleep_time – The value of sleep_time.

__enter__()

Called at the beginning of a with block; returns self.

__exit__(type_, value, traceback)

Called upon exiting a with block; calls close().

close()

Stop the parallel thread and close the connection.

This function returns only after the parallel thread has actually stopped.

classmethod close_all()

Close all running instances of this class.

This function returns only after all parallel threads have actually stopped.

is_running()

Return True if the parallel thread is running, False otherwise.

property port

Return a device name (e.g. '/dev/pts/...') that can be used as the port argument when a serial.Serial object is created.

static default_process(input_)

Form output based on input_.

This is the default method assigned to process, and simply returns input_ uncanged.

Parameters:

input – A bytes-like object.

Returns:

input_.