Serial

Utility Functions

mil_tools.hexify(buff: bytes) bytes[source]

Display a buffer of bytes in hex format. This should take a bytes object, and will return a bytes string in hex format.

>>> p = CommandPacket.create_send_packet(data, can_id = can_id)
>>> p_bytes = p.to_bytes()
>>> print('Sending ', hexify(p_bytes))
Sending c0:14:09:48:45:4c:4c:4f:c1

NoopSerial

class mil_tools.NoopSerial(**kwargs)[source]

Inherits from serial.Serial, doing nothing for each function.

Allows super classes to implement custom behavior for simulating serial devices.

SimulatedSerial

Attributes
Methods
class mil_tools.SimulatedSerial[source]

Simulates a serial device, storing a buffer to be read in a program like a normal OS serial device.

Intended to be extended by other classes, which should override the write function to receive writes to the simulated device. These classes simply append to the buffer string which will be returned on reads to the simulated device.

Note: NoopSerial and SimulatedSerial are generic and are candidates for mil_common.

str(x)

Pretty prints the SimulatedSerial object, its address, and the start of its buffer.

repr(x)

Pretty prints the SimulatedSerial object, its address, and the start of its buffer. Equivalent to str(x).

buffer

A buffer of bytes waiting to be read from the device.

Type

bytes

property in_waiting: int

The number of bytes waiting to be read. This does not modify the buffer in any way; the buffer is only inspected.

Returns

The count of bytes in the buffer.

Return type

int

read(length: int) bytes[source]

Reads an amount of bytes from the buffer. All bytes read are removed from the buffer upon reading.

Parameters

length (int) – The amount of bytes desired to be read.

Returns

The bytes originating from the top of the buffer.

Return type

bytes

reset_input_buffer() None[source]

Resets the buffer to contain no content. The buffer is set to an empty byte string.