UniteLabs
Reference

python-bus examples

The runnable examples of the python-bus repository, with what each one shows.

The python-bus repository has runnable examples for the bus. Each example is one file about one idea. It runs against an in-process server or a fake device, so no hardware is needed.

To run an example, clone the repository and call its module:

hatch -e dev run python -m examples.tcp_connection

Start with tcp_connection, then timeouts, notifications, read_stream, discovery, status and concurrency.

Connections

ExampleShows
tcp_connectionSend one command over TCP and print the answer.
udp_connectionTalk UDP to a device, over the same bus as everything else.
http_requestTalk HTTP to a device, over the same bus as everything else.
binary_framingTalk to a device whose messages are neither text nor delimited.
custom_connectionAdd a connection type of your own with a ConnectionFactory.
multiple_devicesTalk to 2 devices over one bus.
statusFollow the state of a connection while it works and while it cannot be reached.

Commands

ExampleShows
complex_commandUse one command class with different request and response types.
command_builderBuild commands from request and response dataclasses with CommandBuilder.
void_commandSend a command the device does not answer, and one it does.
timeoutsGive a command only so much time, and see what happens when it is not enough.
late_answerSee what happens to the command that follows one the device did not answer in time.

Parallel commands and streams

ExampleShows
concurrencySend 3 commands at once over a single connection.
channel_poolSend 3 commands at once over a pool of 3 connections.
read_streamReceive many answers to a single request.
write_streamSend a series of values over one connection.
notificationsReceive messages a device sends without being asked.

Device selection

ExampleShows
discoveryFind the right device among the attached ones, without naming it.
which_deviceLeave the device out of the config, and see why that alone is refused.

Simulation

ExampleShows
simulation_modeAnswer a command without a device, with simulate and simulated=True.
simulation_stateKeep values between simulated commands in bus.state.

Last updated