如何使用Raspberry Pi Pico从计算机读取数据?

发布于 2025-02-12 18:27:28 字数 43 浏览 1 评论 0原文

我如何使用催眠术从计算机中读取数据到Raspberry Pi Pico?

How do I read data from my computer to Raspberry Pi Pico using MicroPython with pyserial?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2025-02-19 18:27:28

我也遇到了这个问题,从这个 link 。要读取来自USB端口的数据,请使用“ sys.stdin.buffer.read(8)”。

这是一个简短的代码,您可以上传到PICO进行此操作。

import time
import sys

while True:
      print("hello")
      data = sys.stdin.buffer.read(8)
      # to convert to string use data = sys.stdin.buffer.read(8).decode("utf-8")
      print(data)

      time.sleep(1)

使用此代码,它将等到8个字节进来,直到那时它将闲置。我敢肯定,还有一种方法,只是还没有尝试过。

我使用Arduino IDE串行显示器发送数据。将此代码上传到Thonny中(将其保存为Main.py,以便在电源上启动),然后断开PICO,将其插入并打开Arduino串行显示器。发送类似“ 12345678”之类的东西,它应该吐回去。

编辑:

上面的代码将空闲,直到您发送数据为止。要使PICO继续运行,如果未发送数据,请使用以下( source ):

import time
import sys
import select

while True:
   print("hello")
   res = select.select([sys.stdin], [], [], 0)
   data = ""
   while res[0]:
      data = data + sys.stdin.read(1)
      res = select.select([sys.stdin], [], [], 0)
   print(data, end = "")
   time.sleep(1)

您可以使用thonny发送数据,您不需要Arduino IDE。

I also had this issue and from this link I was able to figure it out. To read data from the usb port use "sys.stdin.buffer.read(8)".

Here is a short code that you can upload to the pico to do this.

import time
import sys

while True:
      print("hello")
      data = sys.stdin.buffer.read(8)
      # to convert to string use data = sys.stdin.buffer.read(8).decode("utf-8")
      print(data)

      time.sleep(1)

With this code it will wait until 8 bytes come in, until then it will idle. I'm sure there is a way around it, just haven't tried yet.

I used the Arduino IDE serial monitor to send data. Upload this code in Thonny (save it as main.py so it starts on power up), then disconnect the pico, plug it back in, and open the Arduino serial monitor. Send something like '12345678' and it should spit it back out.

EDIT:

The above code will idle until you send it data. To have the pico continue running if no data is sent, use the following (source):

import time
import sys
import select

while True:
   print("hello")
   res = select.select([sys.stdin], [], [], 0)
   data = ""
   while res[0]:
      data = data + sys.stdin.read(1)
      res = select.select([sys.stdin], [], [], 0)
   print(data, end = "")
   time.sleep(1)

And you can just use Thonny to send the data, you don't need the Arduino IDE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文