Skip to content

Hardware Controller Examples

Serial Raw Key/Value Protocol Example

Example

Image title

This example requires a serial device responding to commands to be connected.

You can make one using the Key/Value Test Gadget

The test gadget can be flashed to most PlatformIO compatible devices.

examples/hardware/serial_kv_controller.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import time

from kevinbotlib.hardware.controllers.keyvalue import RawKeyValueSerialController
from kevinbotlib.hardware.interfaces.serial import RawSerialInterface

# ! remember to change these settings for your testing environment
interface = RawSerialInterface(
    None, "/dev/ttyUSB0", 9600, timeout=1
)  # a timeout is useful to not stall at `controller.read_next()`

controller = RawKeyValueSerialController(interface, b"=", b"\n")

while True:
    print("Sending data")
    controller.write(b"test", b"1")

    pair = controller.read()
    if pair:
        print("Got data:", pair)
    else:
        print("Serial interface timeout")

    time.sleep(1)

  1. Arduino Nano image modified from an original image by MakeMagazinDE, licensed under CC BY-SA 4.0 (link).