如何获得Raspberry Pi Pico与PC /外部设备进行通信?

发布于 2025-01-27 09:46:14 字数 488 浏览 5 评论 0原文

例如,当我给代码5时,我想打开RPI PICO中的LED(通过电缆连接到PC)。

#This code will run in my computer (test.py)

x=int(input("Number?"))
if (x==5):
    #turn on raspberry pi pico led

RPI PICO的代码:

#This code will run in my rpi pico (pico.py)

from machine import Pin
led = Pin(25, Pin.OUT)

led.value(1)

反之亦然(在计算机上使用RPI PICO中的代码执行某些操作)。

如何在RPI PICO中调用/获取PC中的变量?

注意:我正在用OpenCV Python编写代码,我想从计算机上的计算机相机处理数据。我希望RPI PICO根据处理后的数据做出反应。

For example when I give 5 to the code, I want to turn on the LED in our RPi pico (connected to a PC via a cable).

#This code will run in my computer (test.py)

x=int(input("Number?"))
if (x==5):
    #turn on raspberry pi pico led

The code of the RPi pico:

#This code will run in my rpi pico (pico.py)

from machine import Pin
led = Pin(25, Pin.OUT)

led.value(1)

Or vice versa (doing something in the code on the computer with the code in the RPi pico).

How can I call/get a variable in the PC to the RPi pico?

Note: I am writing code with OpenCV Python and I want to process the data from my computer's camera on my computer. I want the RPi pico to react according to the processed data.

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

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

发布评论

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

评论(1

神爱温柔 2025-02-03 09:46:14

主机和PICO之间通信的一种简单方法是使用串行端口。我有一个 rp2040-Zero ,它以/dev/dev/dev呈现给主机/ttyacm0。如果我在RP2040上使用类似的代码:

import sys
import machine

led = machine.Pin(24, machine.Pin.OUT)

def led_on():
    led(1)

def led_off():
    led(0)


while True:
    # read a command from the host
    v = sys.stdin.readline().strip()

    # perform the requested action
    if v.lower() == "on":
        led_on()
    elif v.lower() == "off":
        led_off()

那么我可以在主机上运行此操作以闪烁LED:

import serial
import time

# open a serial connection
s = serial.Serial("/dev/ttyACM0", 115200)

# blink the led
while True:
    s.write(b"on\n")
    time.sleep(1)
    s.write(b"off\n")
    time.sleep(1)

这显然只是单向通信,但是您当然可以实现一种机制来将信息传递回主机。

A simple method of communicating between the host and the Pico is to use the serial port. I have a rp2040-zero, which presents itself to the host as /dev/ttyACM0. If I use code like this on the rp2040:

import sys
import machine

led = machine.Pin(24, machine.Pin.OUT)

def led_on():
    led(1)

def led_off():
    led(0)


while True:
    # read a command from the host
    v = sys.stdin.readline().strip()

    # perform the requested action
    if v.lower() == "on":
        led_on()
    elif v.lower() == "off":
        led_off()

Then I can run this on the host to blink the LED:

import serial
import time

# open a serial connection
s = serial.Serial("/dev/ttyACM0", 115200)

# blink the led
while True:
    s.write(b"on\n")
    time.sleep(1)
    s.write(b"off\n")
    time.sleep(1)

This is obviously just one-way communication, but you could of course implement a mechanism for passing information back to the host.

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