fra*_*sto 5 micropython adafruit
我正在使用 Adafruit 的 Circuit Playground Express,并使用 Circuit Python 对其进行编程。
我想读取通过 USB 连接 Circuit Playground Express 的计算机传输的数据。使用input()工作正常,但我宁愿获取串行缓冲区,以便在没有输入时循环继续进行。就像是serial.read()。
import serial不适用于 Circuit Python,或者也许我必须安装一些东西。我还能做些什么来使用 Circuit Python 读取串行缓冲区吗?
小智 6
艾登的回答引导我走向正确的方向,我在这里找到了一个很好的(略有不同的)示例,说明如何使用supervisor.runtime.serial_bytes_availableAdafruit(特别是第 89-95 行): https: //learn.adafruit.com/AT-Hand-Raiser/电路python代码
code.py接受输入并以“RX:字符串”形式格式化新字符串的最小工作示例是
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))
Run Code Online (Sandbox Code Playgroud)
测试于:2019-08-02 Adafruit CircuitPython 4.1.0;Adafruit ItsyBitsy M0 Express 与 samd21g18。在 macOS 上的 mu-editor 中使用串行连接发送的消息。
样本输出
main.py output:
listening...
hello!
RX: hello!
Run Code Online (Sandbox Code Playgroud)
小智 5
我根据上面的帖子得到了一个简单的示例,不确定它的稳定性或有用性,但仍然将其发布在这里:
电路Python代码:
import supervisor
while True:
if supervisor.runtime.serial_bytes_available:
value = input().strip()
print(f"Received: {value}\r")
Run Code Online (Sandbox Code Playgroud)
电脑代码
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()
Run Code Online (Sandbox Code Playgroud)
c:\circuitpythontest>python TEST1.PY
Sending Command: [b'hello\n\r']
Reply was: [b'Received: hello']
Run Code Online (Sandbox Code Playgroud)
小智 4
现在这有点可能了!在CircuitPython 3.1.2 的1 月份稳定版本中,该函数serial_bytes_available已添加到supervisor模块中。
这允许您轮询串行字节的可用性。
例如,在 CircuitPython 固件(即boot.py)中,串行回显示例如下:
import supervisor
def serial_read():
if supervisor.runtime.serial_bytes_available():
value = input()
print(value)
Run Code Online (Sandbox Code Playgroud)
并确保当您在主机端创建串行设备对象时,将超时等待设置得非常小(即0.01)。
即在Python中:
import supervisor
def serial_read():
if supervisor.runtime.serial_bytes_available():
value = input()
print(value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11238 次 |
| 最近记录: |