问题描述
对于我的项目,我需要读取两个 Arduino Mega 上的多个模拟引脚.PWM 等其他任务运行良好,但在读取模拟引脚时,我只能从其中一块板上读取值.其他显示无:
A0 板 1:0.5601
For my project I need to read multiple analog pins on two Arduino Mega's. Other tasks like PWM are working perfectly well but when reading analog Pins I can only read values from one of the boards. The others show None:
A0 Board 1: 0.5601
A0 Board 2:无
如果我只使用一块板,它们都会读取正确的值,但一起显示始终没有.
If I'm using only one board both of them are reading the correct values but together one shows always none.
这是一个简单的代码来测试它:
Here is a simple code to test it:
from pyfirmata.util import Iterator
import time
import threading
from pyfirmata import ArduinoMega
def read_1():
board1 = ArduinoMega("COM9")
iterator = Iterator(board1)
iterator.start()
analog_0_board1 = board1.get_pin("a:0:i")
while True:
time.sleep(0.5)
a = analog_0_board1.read()
print("A0 Board 1: " + str(a))
def read_2():
board2 = ArduinoMega("COM4")
iterator = Iterator(board2)
iterator.start()
analog_0_board2 = board2.get_pin("a:0:i")
while True:
time.sleep(0.5)
b = analog_0_board2.read()
print("A0 Board 2: " + str(b))
x = threading.Thread(target=read_1)
x.start()
y = threading.Thread(target=read_2)
y.start()
我读过多处理可以解决这个问题,但我不知道如何在 pyfirmata 中正确使用它,而真正的循环和 tkinter.
I've read about multiprocessing could fix the problem but I don't know how to use it correctly with pyfirmata, while true loops and tkinter.
最好的问候和感谢
推荐答案
我设法解决了这个问题,也许我当前的代码可以帮助其他人.我写了以下代码,而不是线程部分:
I managed to solve the problem and maybe my current code can help other people.Instead of the threading part I wrote the following code:
if __name__ == "__main__":
x = multiprocessing.Process(target=read_1)
x.start()
y = multiprocessing.Process(target=read_2)
y.start()
root.mainloop()
root.mainloop() 只是为了测试 tkinter 是否也可以.
The root.mainloop() is just to test if tkinter is also possible.
这篇关于在两个 Arduino Mega 上使用 pyfirmata 读取模拟值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!