1. Home
  2. Docs
  3. Python 3.X Documentation
  4. Serial Connection API
  5. Classes

Classes

 class serial_connection.SerialConnection(serial_number, baudrate=115200)

This class and its methods provide the means to establish a serial port connection to a device given its serial number or device name.

 __init__(serial_number, baudrate=115200)

Class constructor.

The baudrate is configurable, the remaining settings of the serial port connection are fixed to:

  • 8 data bits.
  • 1 stop bit.
  • No parity.
  • No software flow control.
  • No hardware flow control.
  • Line feed command terminator.

Parameters:

  • serial_number (str) – Serial number of the device to connect to.
  • baudrate (int, optional) – Serial port communication baudrate, defaults to 115200. The baudrate configured in the device must match with this baudrate.

Raises:

 

 __del__()

Class destructor.

If a serial port communication is open within the instance of the class being destroyed, the destructor will attempt to close it prior to destroying it.

 

 __str__()

Obtains a string representation of the instance.

 

 open()

Open the serial port communication with the device.

If the port is already open by the instance of the class, this method silently returns.

Raises: OpenError – An error ocurred while opening the port, some of the most common use cases include invalid baudrates, port already open by another application or port no longer exists in the host.

Returns: Instance of the class on which this method was called.

Return type: SerialConnection

 

 close()

Close the serial port communication with the device.

If the port is already closed in the instance of the class, this method silently returns.

Raises: CloseError – An error ocurred while closing the port.

Returns: Instance of the class on which this method was called.

Return type: SerialConnection

 

 send(command=b'info')

Sends a command to the device.

The command terminator is automatically appended to command, data in the input and output buffers of the serial port is discarded immediately prior to sending the command.

Raises: SendError – Unable to send the command to the device, port might not be open or port might no longer exist in the host.

Parameters: command (bytes, optional) – Command to send, defaults to b”info”.

Returns: Instance of the class on which this method was called.

Return type: SerialConnection

 

 receive(timeout_ms=100)

Receive data from the device.

Parameters: timeout_ms (float, optional) – Inter data timeout in milliseconds, defaults to 100. This timeout is refreshed everytime data is received.

Raises: ReceiveError – Failed to receive data from device

Returns: Data read from the device.

Return type: bytes

 

 property name

Name property.

Returns: Name of the device.

Return type: str

 

 property description

Description property.

Returns: Friendly description of the device.

Return type: str

 

 property baudrate

Baudrate property.

Returns: Baudrate configured for the serial port connection.

Return type: int

 

 static get_device_info(serial_number)

Obtains the name of the device.

Parameters: serial_number (str) – Serial number of the device to retrieve its info.

Raises:

Returns: A tuple containing the name of the device and the friendly description.

Return type: (str, str)