如何将值从 Arduino 发送到 Python,然后使用该值

发布于 2024-11-10 03:34:58 字数 1693 浏览 4 评论 0原文

我正在构建一个使用 Python 进行远程控制的机器人,通过简单的 GUI 通过互联网发送控制消息。

我的部分代码(GUI 和控制系统)运行得很好,但我陷入了困境。我正在尝试使用视差 ping 传感器从 Arduino Mega 获取到物体的距离信息,并且将该值发送到我的 Python 控制脚本以显示在远程 GUI 上。

我遇到的主要问题是如何将Python代码集成到Arduino中,该代码将使用已经建立的COM端口并发送消息告诉Arduino轮询ping传感器,然后发送到将接收该值的Python程序,然后让我将该值插入到我的 GUI 中。

我已经有了这段代码来控制 Arduino,并且它可以通过我的简单 GUI 运行。

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)

from PythonCard import model

class MainWindow(model.Background):
    def on_SpdBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
    def on_FBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('F')
       ser.write(chr(spd))
    def on_BBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('B')
       ser.write(chr(spd))
    def on_LBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('L')
       ser.write(chr(spd))
    def on_RBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('R')
       ser.write(chr(spd))
    def on_SBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('S')
       ser.write('0')
    def on_PngDisBtn_mouseClick(self, event):
       ser.write('~')
       ser.write('P1')
       ser.write('p2')

app = model.Application(MainWindow)
app.MainLoop()

我真正想做的是改进上面的代码并添加一个按钮来告诉Python向Arduino发送消息以检查ping传感器并返回值。 我对 Arduino 代码非常熟悉,但我在过去两周才开始使用 Python。

I am in the process of building a robot that is remote controlled using Python to send control messages via the Internet through a simple GUI.

I have gotten part of my code working pretty well, the GUI and control systems, but I am stuck. I am trying to use a parallax ping sensor to get distance to objects information from an Arduino Mega, and send that value to my Python control script to be displayed on the remote GUI.

The main problem that I am having is how to integrate Python code that will use the already established COM port with the Arduino and send a message to tell the Arduino to poll the ping sensor and then send to a Python program which will receive the value, and then let me insert that value into my GUI.

I already have this code to control the Arduino, and it works, with my simple GUI.

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)

from PythonCard import model

class MainWindow(model.Background):
    def on_SpdBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
    def on_FBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('F')
       ser.write(chr(spd))
    def on_BBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('B')
       ser.write(chr(spd))
    def on_LBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('L')
       ser.write(chr(spd))
    def on_RBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('R')
       ser.write(chr(spd))
    def on_SBtn_mouseClick(self, event):
       spd = self.components.SpdSpin.value
       ser.write('@')
       ser.write('S')
       ser.write('0')
    def on_PngDisBtn_mouseClick(self, event):
       ser.write('~')
       ser.write('P1')
       ser.write('p2')

app = model.Application(MainWindow)
app.MainLoop()

What I would really like to do is improve the above code and add a button to click to tell Python to send a message to the Arduino to check the ping sensor and return the value.
I am very literate with the Arduino code, but I just started playing with Python in the last two weeks.

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

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

发布评论

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

评论(3

掩于岁月 2024-11-17 03:34:58

基本上,您只需向 Arduino 发送一个合适的命令,就像您已经在做的那样,然后等待 Arduino 发回一些信息; 当然,它的 python 端可能看起来像这样

ser.write('foo')
retval = ser.readline() # read a complete line (\r\n or \n terminated), 
    #or you could use read(n) where n is the number of bytes you want (default=1)
ping_data = retval.strip() # strip out the newline, if you read an entire line

,这会给你一个字符串,你可能想将它转换为 int 或 float 以便稍后在计算中使用它(使用 int(ping_data) 或 float (ping_data) 对于字符串,或者 struct.unpack 如果它是一个字节序列,需要首先解包为正常的内容,但这完全取决于您如何表示传感器数据)。

Basically, you'd just send a suitable command to the Arduino, much like you're already doing, but then wait for the Arduino to send something back; the python end of it might look something like this

ser.write('foo')
retval = ser.readline() # read a complete line (\r\n or \n terminated), 
    #or you could use read(n) where n is the number of bytes you want (default=1)
ping_data = retval.strip() # strip out the newline, if you read an entire line

of course, that'll get you a string, you'll probably want to convert it to an int or float in order to use it in calculations later (use int(ping_data) or float(ping_data) for strings, or struct.unpack in case its a byte sequence that needs unpacking to something sane first, but it all depends on how you represent the sensor data).

紫竹語嫣☆ 2024-11-17 03:34:58

也许可以查看Pyduino 项目

pyduino 是一个库,可让您在 Python 中与加载了 Firmata 协议的 Arduino 板进行通信。目前它支持 Firmata 协议的版本 2。

Maybe check out the Pyduino project:

pyduino is a library which allows you to communicate with Arduino boards loaded with the Firmata protocol from within Python. It currently supports version 2 of the Firmata protocol.

一花一树开 2024-11-17 03:34:58

首先,我要说的是,之前的答案很好,很有帮助,而且直接相关。我的评论更为笼统,适用于任何实现 Arduino 双向数据流的人。基本思想是设计数据流,以便人类可以输入进入 Arudino 草图的数据,并且人类可以读取来自 Arduino 草图的数据。这并不总是可行,但通常是可行的。

关键思想是使用串行监视器运行 Arduino 草图几次。您可以在 IDE 菜单的“工具”下找到“串行监视器”。您还可以键入 Ctrl-Shift-M 来调用串行监视器。

串行监视器显示 Arduino 草图发送回给您的内容。不过,它还允许您输入发送到 Arduino 草图的数据。换句话说,您只需使用串行监视器即可测试和调试串行数据流的两侧。

看看会出现什么。假设您的草图尝试通过 Serial.print() 发送回数据,这通常会很有帮助。一些笔记。绝对确保串行监视器内设置的波特率与草图中的波特率完全匹配(在几乎所有情况下 9600 都是一个不错的选择)。

第二个注释很关键。打开串行监视器会强制 Arduino 板上重置。你的草图(总是)重新开始。这是一件好事,因为它每次都能让你焕然一新。请注意,您可以强制重置,只需将波特率设置为 9600(即使它已经是 9600)。这使您可以在串行监视器内运行许多测试,而不必每次都重新启动串行监视器

First, let me say that the prior answers are good, helpful, and directly relevant. My comments are more general and apply to anyone implement a bidirectional data flow to and from an Arduino. The basic idea is to design your data flow so that it is human typeable for the data going to the Arudino sketch and human readable for the data coming from the Arduino sketch. This won't always be possible, but frequently it is.

The key idea is to run your Arduino sketch with the Serial Monitor a few times. You can find the Serial Monitor under Tools in the IDE menu. You can also type Ctrl-Shift-M to invoke the Serial Monitor.

The Serial Monitor displays what the Arduino sketch sends back to you. However, it also lets you type in data that gets sent to the Arduino sketch. In other words you test and debug both sides of the serial data flow, just using the Serial Monitor.

Look at what shows up. It will frequently be quite helpful assuming your sketch tries to send data back via Serial.print(). A few notes. Make absolutely sure that the baud rate set inside the Serial Monitor exactly matches the baud rate in your sketch (9600 is a good choice in almost all cases).

The second note is critical. Bringing up the Serial Monitor forces a reset on the Arduino board. Your sketch starts over (always). This is a good thing because it gives you a fresh run each time. Note that you can force a reset, just by setting the baud rate to 9600 (even if it is already 9600). This lets you run many tests inside the Serial Monitor without having to restart the Serial Monitor each time

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