SerialManager()

Main class to establish and manage Serial connection with Bittle.

Attributes

  • port (str): Serial communication port.

  • baudrate (int): Baud rate.

  • timeout (int): Serial communication timeout in seconds.

  • parity (int): Serial communication parity, possible values: none, odd, even.

  • serial (serial.Serial): Serial communication instance.

Methods

  • initialize(): Sets serial communication parameters (port, baudrate, timeout and parity). If any of the parameters is updated after initialization, this method must be called to apply the changes.

  • discover_port(): Searches among avaliable communication ports the one associated to CH340 USB driver, which is used by Bittle. Returns True if found, False otherwise.

  • connect(): Starts serial communication, connects to Bittle and wait until full handshake is done (Bittle will send a response containing "Finished!" string at the end). Return True if connected successfully, False otherwise.

  • close_connection(): Closes serial communication.

  • send_msg(msg): Sends passed msg parameter (str) message to Bittle.

  • recv_msg(): Returns received message from Bittle (byte).

Example

The following example shows the complete process of searching, connecting and sending messages to Bittle through Serial connection.

import time

from pyBittle import serialManager

# Create SerialManager instance
srManager = serialManager.SerialManager()

# Search for Bittle's serial communication port
port_found = srManager.discover_port()
if not port_found:
    srManager.port = 'COM7'  # Set port manually

srManager.initialize()  # Apply parameters

connected = srManager.connect()  # Connect to Bittle
if connected:
    srManager.send_msg('ksit')  # Send SIT command
    received = srManager.recv_msg()  # Received response (bytes)
    time.sleep(6)  # Wait for SIT action to finish
    srManager.send_msg('d')  # Send SHUTDOWN command
    received = srManager.recv_msg()
    srManager.close_connection()

Last updated