Skip to main content

This module provides the Keyhole class, which allows easy line-by-line text communication over a serial port, or through a stdin/stdout pipe. It is particularly useful for implementing the computer’s side of the interaction when talking to a microcontroller that has been programmed using the Arduino-IDE Keyhole library. In that case, exposed sketch variables can be accessed as attributes of a Keyhole instance in Python.

The Arduino side:

#include "Keyhole.h"
Keyhole k;

void setup()
{
    Serial.begin(9600);
}
void loop()
{
    static float foo = 1.23;
    static String bar = "hello";

    // insert main sketch logic here (use foo and bar to do whatever is so important)

    if(k.begin()) // expose the variables
    {
        k.variable("foo", foo);
        k.variable("bar", bar);
        k.end();
    }
}

The Python side:

from keyhole import Keyhole

# k = Keyhole('COM4')                 # windows example
k = Keyhole('/dev/cu.usbmodem[0-9]*') # macos example (globbing is OK if the match is unique)

print( k.bar ) # queries an exposed sketch variable
k.foo = 4.56   # assigns to an exposed sketch variable and makes the .variable() call return true

print( k() )  # get all exposed variables

In Python, you can also communicate similarly with a process on the local computer, exchanging messages in the same format over stdin/stdout. To do this, initialize your Keyhole instance via the pipe argument instead of the port argument:

from keyhole import Keyhole

k = Keyhole( pipe='python -m keyhole.pipe' ) # running the `keyhole.pipe` submodule
                                             # provides a toy example
print( k.spam )
k.eggs = 12
print( k() )

Metadata

Release files for keyhole-comm 1.17.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for keyhole-comm 1.17.0
File Interpreter ABI Platform
keyhole_comm-1.17.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Release files / keyhole_comm-1.17.0-py2.py3-none-any.whl

Download URL keyhole_comm-1.17.0-py2.py3-none-any.whl
Size 16.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f38a34dc07b504b3dc6a2c4cd0d21221ea72c0271390c5128ad28ca1deeff18d
BLAKE2b-256 checksum
How to use checksums
7293cba4df0ee3f8b4ddabc0aa9d92ac59ac7c82d35f91c7229d10e000534305
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

1.17.0 This release

1 release file

1.16.0

1 release file

1.15.0

1 release file

1.14.0

1 release file

1.13.0

1 release file

1.12.0

1 release file

1.11.0

1 release file

1.10.0

1 release file

1.9.0

1 release file

1.8.3

1 release file

1.8.2

1 release file

1.8.1

1 release file

1.8.0

1 release file

1.7.0

1 release file

1.6.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page