Broker¶
Module pydobotlab.broker. A broker is a local TCP server that owns serial ports and relays commands from several Magician instances, including instances in other processes. Magician connects through a running broker. When the control panel is installed, it raises by default when none is running. The control panel starts a broker automatically. The wire protocol is described in The DobotBroker.
| Member | Description |
|---|---|
DobotBroker |
TCP broker for serial ports. |
DobotBroker.start |
Start serving. |
DobotBroker.stop |
Stop serving, disconnect clients, and close all ports. |
DobotBroker.attach |
Open a port and manage it. |
DobotBroker.detach |
Close a managed port. |
DobotBroker.attached_ports |
List the managed ports. |
DobotBroker.client_count |
Number of connected clients. |
is_broker_reachable |
Whether a broker accepts connections. |
list_broker_ports |
List the ports of a running broker. |
DobotBroker¶
TCP broker for serial ports. A port that a client requests and that is not attached is opened on demand and closed when its last client disconnects. Ports opened with attach stay open until detach or stop.
Parameters
| Name | Type | Description |
|---|---|---|
host |
str or None, default None |
Address to listen on. None uses the configured default, "127.0.0.1" unless changed. |
port |
int or None, default None |
TCP port. None uses the configured default, 8765 unless changed. 0 selects a free port. |
Attributes
| Name | Type | Description |
|---|---|---|
host |
str |
Address to listen on. |
port |
int |
TCP port. After start, the port in use. |
Notes
DobotBroker is a context manager. Entering the block calls start and returns the broker; leaving it calls stop.
Example
from pydobotlab.broker import DobotBroker
with DobotBroker() as broker:
broker.attach("COM3")
input("Broker running. Press Enter to stop.")
DobotBroker.start¶
Start serving on a background thread. Has no effect if the broker is already running.
Raises
OSError: the address is already in use.
DobotBroker.stop¶
Stop serving and close all attached ports. Connected clients are disconnected without waiting for them; their next command raises DobotConnectionError.
DobotBroker.attach¶
Open port and manage it until detach or stop. Returns the existing transport if the port is already attached.
Parameters
| Name | Type | Description |
|---|---|---|
port |
str |
Serial port. |
baudrate |
int or None, default None |
Serial baud rate. None uses the configured default, as for Magician. |
timeout |
float or None, default None |
Read timeout per byte, seconds. None uses the configured default. |
Returns SerialTransport: the open transport.
Raises
DobotConnectionErrorand its subclasses: the port could not be opened.
DobotBroker.detach¶
Close port and stop managing it. Has no effect if the port is not attached.
DobotBroker.attached_ports¶
Return the attached ports, sorted.
DobotBroker.client_count¶
Return the number of clients connected to an arm through the broker.
is_broker_reachable¶
Return whether a broker accepts connections at host:port. A result is reused for a short time.
Parameters
| Name | Type | Description |
|---|---|---|
host |
str or None, default None |
Broker host. None uses the configured default. |
port |
int or None, default None |
Broker TCP port. None uses the configured default. |
timeout |
float or None, default None |
Connection timeout, seconds. None uses the configured default, 0.15 unless changed. |
list_broker_ports¶
Return the ports attached to a running broker.
Parameters
| Name | Type | Description |
|---|---|---|
host |
str or None, default None |
Broker host. None uses the configured default. |
port |
int or None, default None |
Broker TCP port. None uses the configured default. |
timeout |
float or None, default None |
Socket timeout, seconds. None uses the configured default, 1.0 unless changed. |
Raises
OSError: no broker is reachable.