Receive data from host computer using Circuit Python on Circuit Playground Express
Asked Answered
I

3

5

I am using a Circuit Playground Express from Adafruit, and I'm programming it with Circuit Python.

I want to read data transmitted from the computer to which the Circuit Playground Express is connected via USB. Using input() works fine, but I would rather get the buffer of the serial instead, so that the loop would go on while there's no input. Something like serial.read().

import serial does not work on Circuit Python, or maybe I must install something. Is there anything else I could do to read the serial buffer using Circuit Python?

Inflated answered 22/2, 2018 at 7:54 Comment(3)
By I want to read something do you mean you want to read characters sent by the computer that the CPE is connected to? I think you probably want to use sys.stdin. If you don't get anywhere with this you'll probably have more luck asking on the Micropython forum or trying any help resources available from Adafruit.Toothlike
Thanks @nekomatic. Yes, I want to read something sent by the computer CPE is connected to. I'll try sys.stdin. Thanks for the tip!Inflated
Apparently, as of now, this is not possible. See the complete discussion at Adafruit here.Inflated
G
4

This is now somewhat possible! In the January stable release of CircuitPython 3.1.2 the function serial_bytes_available was added to the supervisor module.

This allows you to poll the availability of serial bytes.

For example in the CircuitPython firmware (i.e. boot.py) a serial echo example would be:

import supervisor

def serial_read():
   if supervisor.runtime.serial_bytes_available():
       value = input()
       print(value)

and ensure when you create the serial device object on the host side you set the timeout wait to be very small (i.e. 0.01).

i.e in python:

import serial

ser = serial.Serial(
             '/dev/ttyACM0',
             baudrate=115200,
             timeout=0.01)

ser.write(b'HELLO from CircuitPython\n')
x = ser.readlines()
print("received: {}".format(x))
Gregale answered 17/4, 2019 at 2:4 Comment(0)
H
6

Aiden's answer lead me in the right direction and I found a nice (and slightly different) example of how to use supervisor.runtime.serial_bytes_available from Adafruit here (specifically lines 89-95): https://learn.adafruit.com/AT-Hand-Raiser/circuitpython-code

A minimum working example for code.py that takes the input and formats a new string in the form "RX: string" is

import supervisor

print("listening...")

while True:
    if supervisor.runtime.serial_bytes_available:
        value = input().strip()
        # Sometimes Windows sends an extra (or missing) newline - ignore them
        if value == "":
            continue
        print("RX: {}".format(value))

Tested on: Adafruit CircuitPython 4.1.0 on 2019-08-02; Adafruit ItsyBitsy M0 Express with samd21g18. Messages sent using the serial connection in mu-editor on macOS.

Sample output

main.py output:
listening...
hello!
RX: hello!
Horseweed answered 10/10, 2019 at 6:57 Comment(0)
A
6

I got a simple example to work based on above posts, not sure how stable or useful it is, but still posting it here:

CircuitPython Code:

import supervisor

while True:
    if supervisor.runtime.serial_bytes_available:
        value = input().strip()
        print(f"Received: {value}\r") 

PC Code

import time
import serial
ser = serial.Serial('COM6', 115200)  # open serial port

command = b'hello\n\r'
print(f"Sending Command: [{command}]")
ser.write(command)     # write a string

ended = False
reply = b''

for _ in range(len(command)):
    a = ser.read() # Read the loopback chars and ignore

while True:
    a = ser.read()

    if a== b'\r':
        break

    else:
        reply += a

    time.sleep(0.01)

print(f"Reply was: [{reply}]")

ser.close()
c:\circuitpythontest>python TEST1.PY
Sending Command: [b'hello\n\r']
Reply was: [b'Received: hello']
Avionics answered 11/5, 2020 at 21:7 Comment(0)
G
4

This is now somewhat possible! In the January stable release of CircuitPython 3.1.2 the function serial_bytes_available was added to the supervisor module.

This allows you to poll the availability of serial bytes.

For example in the CircuitPython firmware (i.e. boot.py) a serial echo example would be:

import supervisor

def serial_read():
   if supervisor.runtime.serial_bytes_available():
       value = input()
       print(value)

and ensure when you create the serial device object on the host side you set the timeout wait to be very small (i.e. 0.01).

i.e in python:

import serial

ser = serial.Serial(
             '/dev/ttyACM0',
             baudrate=115200,
             timeout=0.01)

ser.write(b'HELLO from CircuitPython\n')
x = ser.readlines()
print("received: {}".format(x))
Gregale answered 17/4, 2019 at 2:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.