Port¶
This class is the blueprint for the port instances in the
hub.port
module. Those instances are automatically instantiated on
boot, and further populated when devices are plugged in. You cannot import or
instantiate this class manually.
-
class
Port
¶ Provides access to port configuration and devices on a port. Some methods and attributes can only be used if the port is in the right mode, as shown below.
Permanent port methods
-
pwm
(value: int) → None¶ Applies a PWM signal to the power pins of the port or device.
A PWM value of 0 has the same effect as
float()
.- Parameters
value – PWM value between -100 and +100. The polarity of the PWM signal matches the sign of the value. A value of 0 stops the PWM signal and leaves the port driver in the floating state.
-
callback
(function: Callable[[int], None]) → None¶ Sets the callback function that is called when a device is plugged into the port or unplugged from the port.
The function must accept one argument, which indicates why the callback was called. See the port constants for all possible values.
- Parameters
function – Callable function that takes one argument. Choose
None
to disable the callback.
-
mode
(mode: int, baud_rate=2400) → None¶ Sets the mode of the port.
This command initiates the mode change, but it does not wait for completion.
- Parameters
mode – Mode value. See the port constants for all possible values
baud_rate – New baud rate of the port, if applicable.
Attributes and methods for use with
MODE_DEFAULT
-
device
¶ Powered Up
Device
on this port. If no device is attached or the port is in a different mode, this attribute isNone
.
-
motor
¶ Powered Up
Motor
on this port. If no motor is attached or the port is in a different mode, this attribute isNone
.
-
info
() → dict¶ Gets information about the port and devices attached to it.
If no Powered Up device is plugged in or the port is not in the default mode, this returns
{'type': None}
.If a Powered Up device is detected, it returns a dictionary of the form:
{ # The device type. For example, the Medium Angular Motor. 'type': 75, # List of modes that can be combined. Each number encodes # bitflags of device modes that may be combined. In this # example, you may combine modes 1, 2, or 3 (14 == 1110), # or combine modes 0, 1, 2, and 3 (15 = 1111). # Device baud rate for UART devices. 'speed': 115200, # A list of dictionaries with information about each mode. 'modes': [mode0_info, mode1_info, ...], # A 24-byte unique serial number. 'uid': bytearray(b'\x00G\x00%\rG909523\x00\x00 ... '), # Device hardware version. 'hw_version': 4 # Device firmware version. 'fw_version': 268435456, }
The
modes
entry above is a list of dictionaries, one for each mode. For example, readinginfo()['modes'][3]
on the Medium Angular Motor gives:{ # Name of the mode. 'name': 'APOS', # Symbol or unit of the mode. 'symbol': 'DEG', # Data format for this mode. 'format': { # Number of values returned by this mode. 'datasets': 1, # Data type (int8=0, int16=1, int32=2, float=3). 'type': 1, # Number of digits. 'figures': 3, # Number of digits after the decimal point. 'decimals': 0 }, # 48 bit flags that indicate what this device can do or needs. 'capability': b'\x22\x00\x00\x00\x01\x04', # The output mapping bits as an 8 bit value. 'map_out': 8, # The input mapping bits as an 8 bit value. 'map_in': 8, # The minimum and maximum range of the data as a percentage. 'pct': (-200.0, 200.0), # The minimum and maximum range of the value in the SI unit. 'si': (-180.0, 179.0), # The minimum and maximum range of raw data. 'raw': (-180.0, 179.0) }
- Returns
Information dictionary as documented above.
Methods for use with
MODE_FULL_DUPLEX
andMODE_HALF_DUPLEX
-
baud
(baud: int) → None¶ Sets the baud rate of the serial port.
- Parameters
baud – The new baud rate.
-
read
(read: Union[int, any]) → int¶ Reads from the serial port.
- Parameters
read – If an integer is given, this reads the requested amount of bytes. If a buffer-like object is given, it reads as many bytes as fit in the buffer.
- Returns
If
read
was an integer, this returns the bytes that were read. Ifread
was a buffer-like object, this returns how many bytes were read into the buffer.
-
write
(write: bytes) → int¶ Writes bytes to the serial port.
- Parameters
write – The string to write.
- Returns
The number of bytes written.
Attributes for use with
MODE_GPIO
-