如何将值从 Arduino 发送到 Python,然后使用该值 [英] How to send a value from Arduino to Python and then use that value

查看:25
本文介绍了如何将值从 Arduino 发送到 Python,然后使用该值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用 Python 远程控制的机器人,通过简单的 GUI 通过 Internet 发送控制消息.

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.

我的部分代码、GUI 和控制系统都运行良好,但我卡住了.我正在尝试使用视差 ping 传感器从 Arduino Mega 获取到物体的距离信息,并且将该值发送到我的 Python 控制脚本以显示在远程 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.

我遇到的主要问题是如何集成 Python 代码,该代码将使用已建立的 COM 端口与 Arduino 并发送消息告诉 Arduino 轮询 ping 传感器,然后发送到将接收的 Python 程序值,然后让我将该值插入到我的 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.

我已经有了控制 Arduino 的代码,它可以在我的简单 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()

我真正想做的是改进上面的代码并添加一个按钮来单击以告诉 Python 向 Arduino 发送消息以检查 ping 传感器并返回值.我对 Arduino 代码非常了解,但我最近两周才开始玩 Python.

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.

推荐答案

也许可以看看 Pyduino 项目:

pyduino 是一个库,它允许您与从 Python 中加载了 Firmata 协议的 Arduino 板进行通信.它目前支持 Firmata 协议的第 2 版.

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.

这篇关于如何将值从 Arduino 发送到 Python,然后使用该值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆