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
VirtualConnectionobject by accessing theuser_endandvirtual_endattributes. Theportproperty is a device name which can be given to the initializer ofserial.Serialas an argument.A
VirtualConnectionobject 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 theclose()method of theVirtualConnectionobject that created it. This also closesuser_endandvirtual_end, and frees their file descriptors.If a
VirtualConnectionobject is used in awithblock in the following manner:with VirtualConnection() as vc: # Some code here
close()will be called automatically when thewithblock is exited.If all variables referring to a
VirtualConnectionobject are removed withdelor by reassigning them, the parallel thread will continue to run without a possibility of closing it withclose(). In this case, all running instances of theVirtualConnectionclass can be closed with>>> VirtualConnection.close_all()
- sleep_time¶
How long (in seconds) the object waits after checking for input before doing it again.
- Type:
- 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
processattribute.- 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 withos.read()andos.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
VirtualConnectionobject.- Type:
- running_instances¶
Class attribute. A set of all currently running instances of the
VirtualConnectionclass.
- __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
processattribute. If no value is supplied, thedefault_processmethod 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
withblock; returns self.
- 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
Trueif the parallel thread is running,Falseotherwise.
- property port¶
Return a device name (e.g.
'/dev/pts/...') that can be used as the port argument when aserial.Serialobject is created.