Documentation for dummy_serial (which is a serial port mock)

dummy_serial: A dummy/mock implementation of a serial port for testing purposes.

dummy_serial.DEFAULT_TIMEOUT = 0.01

The default timeot value in seconds. Used if not set by the constructor.

dummy_serial.SLEEPTIME_READ = 0.001

Simulated read time, in seconds.

dummy_serial.SLEEPTIME_WRITE = 0.001

Simulated write time, in seconds.

dummy_serial.DEFAULT_BAUDRATE = 19200

The default baud rate. Used if not set by the constructor.

dummy_serial.VERBOSE = False

Set this to True for printing the communication, and also details on the port initialization.

Might be monkey-patched in the calling test module.

dummy_serial.RESPONSES = {b'EXAMPLEREQUEST': b'EXAMPLERESPONSE'}

A dictionary of respones from the dummy serial port.

The key is the message (bytes) sent to the dummy serial port, and the item is the response (bytes) from the dummy serial port.

Intended to be monkey-patched in the calling test module.

dummy_serial.DEFAULT_RESPONSE = b'NotFoundInResponseDictionary'

Response when no matching message (key) is found in the look-up dictionary.

Should not be an empty string, as that is interpreted as “no data available on port”.

Might be monkey-patched in the calling test module.

class dummy_serial.Serial(port: Optional[str], baudrate: int = 19200, bytesize: int = 8, parity: str = 'N', stopbits: Union[int, float] = 1, timeout: Optional[float] = None, xonxoff: bool = False, rtscts: bool = False, write_timeout: Optional[float] = None, dsrdtr: bool = False, inter_byte_timeout: Optional[float] = None)[source]

Dummy (mock) serial port for testing purposes.

Mimics the behavior of a serial port as defined by the pySerial module.

Args:
  • port:
  • timeout:

Note: As the portname argument not is used properly, only one port on dummy_serial can be used simultaneously.

is_open
reset_input_buffer() → None[source]
reset_output_buffer() → None[source]
flush() → None[source]
open() → None[source]

Open a (previously initialized) port on dummy_serial.

close() → None[source]

Close a port on dummy_serial.

write(inputdata: bytes) → int[source]

Write to a port on dummy_serial.

Args:
inputdata: data for sending to the port on dummy_serial. Will affect the response for subsequent read operations.
Returns:
Number of bytes written
read(size: int) → bytes[source]

Read from a port on dummy_serial.

The response is dependent on what was written last to the port on dummy_serial, and what is defined in the RESPONSES dictionary.

Args:
size (int): For compability with the real function.

If the response is shorter than size, it will sleep for timeout. If the response is longer than size, it will return only size bytes.