【问题标题】:Python read cmd AVRDUDE to update arduinoPython 读取 cmd AVRDUDE 来更新 arduino
【发布时间】:2021-04-17 15:10:28
【问题描述】:

我使用连接到 Arduino nano 的 Tkinter 在 python 中创建了一个 GUI。我可以使用我的 GUI 使用 AVRDUDE 在 Arduino 上刷新固件而没有问题,但这会在编程时弹出一个 CMD 框。我想要的是重定向或通过管道将其重定向到我的 GUI 的文本框。如果我使用普通的 windows 命令,如 ping、dir 等,这没有问题(示例代码和下面的屏幕截图),但是当我尝试刷新 Arduino 时,它会闪烁但仍在其自己的控制台中并且不会重定向。我猜 AVRDUDE printf 是问题所在,但有没有办法重定向输出?

我还尝试将"2> e:\\output.txt" 添加到末尾,就像在此链接https://learn.sparkfun.com/tutorials/raspberry-pi-stand-alone-programmer/parsing-output-from-avrdude-w-python 中一样,以保存到文件中,然后我可以读取文件。如果我在 CMD 中这样做,它可以工作,但如果我从 python 尝试,我会得到

参数错误>2

即使我尝试 >> test.txt。

import tkinter as tk
import subprocess, sys

main = tk.Tk()

IP = "ping 192.168.0.1"
date = ["cmd", "/C","date", "/T"]

arduino = ["W:\\Python\\Hopper_Tester\\update\\avrdude", "-CW:\\Python\Hopper_Tester\\update\\avrdude.conf", "-patmega328p", "-carduino", "-PCOM7", "-b115200", "-D", "-Uflash:w:C:\\Users\Frazer Telford\\Documents\\PlatformIO\\Projects\\hopper_test\\.pio\\build\\nanoatmega328\\firmware.hex:i", "2>", "e:\\flash.txt"]


RUN = IP

textbox = tk.Text(main)
textbox.pack()

def command_one():

    #item = subprocess.Popen(RUN,stdout=subprocess.PIPE, creationflags=subprocess.CREATE_NEW_CONSOLE)
    item = subprocess.Popen(RUN,stdout=subprocess.PIPE)
    #item.communicate()[0]

    # print(f"printing {item}")

    for line in item.stdout:
        output = (line.decode())
        textbox.insert(tk.END,output)
        textbox.see(tk.END)
        main.update_idletasks()

def command_two():
    result = subprocess.run(RUN, stdout=subprocess.PIPE, text = True)

    for line in result.stdout:
        #output = (line.decode())
        textbox.insert(tk.END,line)
        textbox.see(tk.END)
        main.update_idletasks()


tk.Button(main, text = "Function 1", command = command_one).pack(side=tk.LEFT, padx = 10)
tk.Button(main, text = "Function 2", command = command_two).pack(side=tk.LEFT, padx = 10)




main.mainloop()

【问题讨论】:

  • 查看如何创建minimal reproducible example。 Tk 相关的东西是无关紧要的。您可能可以使用两行代码之一创建一个示例。

标签: python cmd arduino avrdude


【解决方案1】:

在花了几个小时之后,我找到了一个可以解决的问题,如果有人有更好的方法,我将不胜感激。

def command_one():
item = subprocess.Popen(RUN,stderr=subprocess.PIPE)


for line in item.stderr:
    #output = (line.decode())
    textbox.insert(tk.END,line)
    main.update_idletasks()

textbox.see(tk.END)

我的错误是查看 stdout,我应该查看 stderr,不知道为什么,但我得到了我需要的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    相关资源
    最近更新 更多