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 theuser_end
andvirtual_end
attributes. Theport
property is a device name which can be given to the initializer ofserial.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 theclose()
method of theVirtualConnection
object that created it. This also closesuser_end
andvirtual_end
, and frees their file descriptors.If a
VirtualConnection
object is used in awith
block in the following manner:with VirtualConnection() as vc: # Some code here
close()
will be called automatically when thewith
block is exited.If all variables referring to a
VirtualConnection
object are removed withdel
or by reassigning them, the parallel thread will continue to run without a possibility of closing it withclose()
. In this case, all running instances of theVirtualConnection
class 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
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 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
VirtualConnection
object.- Type:
- 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, thedefault_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.
- 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 aserial.Serial
object is created.