如何从PC发送字节字符串到Raspberry Pi Pico?

问题描述 投票:0回答:1

使用 PySerial 我成功地将数据从 Raspberry Pi Pico 发送到 Windows PC,但反之则不然。

Windows PC 代码:

import serial
# Configure the serial connection
port = "COM5"
ser = serial.Serial(port, 9600)

message = b"Hey This is the message!!!!"
ser.write(message)

Raspberry Pi Pico 代码:

file = open('Results77.txt', 'wb')

while (True):
   # I do not know what to do here to get the message that I sent into the variable data
   data = input()
   file.write(data)
python pyserial micropython serial-communication raspberry-pi-pico
1个回答
0
投票

这与Pi Pico:通过 USB 电缆读取/写入数据

是同一个问题

Pico 代码可能看起来像这样

import select
import sys

# Set up the poll object
poll_obj = select.poll()
poll_obj.register(sys.stdin, select.POLLIN)

# Loop indefinitely
while True:
    # Wait for input on stdin
    poll_results = poll_obj.poll(1) # the '1' is how long it will wait for message before looping again (in microseconds)
    if poll_results:
        # Read the data from stdin (read data coming from PC)
        data = sys.stdin.readline().strip()

确保您的 Windows 设备使用预期的行结尾

ser.write("Hey This is the message!!!!\r".encode())
© www.soinside.com 2019 - 2024. All rights reserved.