serialtools.database module
- class serialtools.database.ByteSpec(name: str, *, length: int | str | None = None, allowed_values: Sequence[bytes] | bytes | int | None = None)
Bases:
object
- Paramref length:
Either the number of bytes or the name of a previous byte spec which specifies the length of this byte spec
- class serialtools.database.Database(message_spec: MessageSpec, signals: Sequence[Signal], *, endianness: Endianness, word_length_in_bits: int = 8)
Bases:
object
- encode_range(address: int, length_in_bytes: int, value: Callable[[Signal], int | float | str]) bytes
Encode all signals which are completely within the given range
- endianness: Endianness
The byte order
- message_spec: MessageSpec
The parts that a message consists of, e.g. an address to be read, the length of the data, the data itself
- class serialtools.database.Endianness(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
Enum
- BIG = 1
- LITTLE = 2
- class serialtools.database.Message(db: Database, timestamp: datetime | None, values: Mapping[str, bytes])
Bases:
object
- class serialtools.database.MessageSpec(params: Sequence[ByteSpec], *, implicit_address: int | None = None)
Bases:
object
- ADDRESS = 'address'
- class serialtools.database.Reader(bus: serial.Serial | ReadFromFileBus | WriteToFileBus, db: Database)
Bases:
object
- ignore_bytes_between_messages
Each instance of this class represents a setting which can be changed in a config file.
This class implements the descriptor protocol to return
value
if an instance of this class is accessed as an instance attribute. If you want to get this object you need to access it as a class attribute.
- log_raw_received_bytes(fn: str) None
write all received bytes to a log file
This log file can be read with an object created with:
from serialtools.bus import bus_creator bus = bus_creator.create_bus(create_bus.create_args(port=fn, rx_only=True))
- Parameters:
fn¶ – file name, where to log the received bytes
- read_byte() int
Receive one byte
- Returns:
The received byte
- Raises:
TimeoutError – If nothing has been received within the time frame passed as timeout when creating the bus
- read_n_bytes(n: int) bytes
Receive exactly
n
bytes- Parameters:
n¶ – Number of bytes to read
- Returns:
The received bytes
- Raises:
TimeoutError – If less than
n
bytes have been received within the time frame passed as timeout when creating the bus
- timeout
Each instance of this class represents a setting which can be changed in a config file.
This class implements the descriptor protocol to return
value
if an instance of this class is accessed as an instance attribute. If you want to get this object you need to access it as a class attribute.
- class serialtools.database.Signal(name: str, type: Type, address: int, *, bits: int | None = None, startbit: int | None = None, lsb: int | None = None, scale: float = 1, offset: float = 0, unit: str = '')
Bases:
object
- Paramref name:
The name of the signal
- Paramref type:
The data type, e.g. INT, UINT, FLOAT
- Paramref address:
The address which is used to request this value from the battery
- Paramref bits:
The size of this signal in bits, defaults to
Database.word_length_in_bits
- Paramref startbit:
If the word identified by
address
contains several smaller values the startbit specifies the first transmitted bit in the word belonging to the signal. Bit 0 is the first bit transmitted, not the least significant bit.- Paramref lsb:
If the word identified by
address
contains several smaller values the lsb specifies where the list significant bit of the signal is inside of the word.startbit
andlsb
are mutually exclusive, only one of them may be passed.- Paramref scale:
A factor which is multiplied to the raw value received on the bus in order to get a value of
unit
- Paramref offset:
A summand which is added to the raw value received on the bus in order to get a value of
unit
- Paramref unit:
The unit of the value
When decoding data from bytes received on the bus to human readable floats in the given unit the raw value is first multiplied with
scale
and thenoffset
is added to it as in cantools.