我对编码和其他东西很陌生。我正在研制一个带有HX711分接板的数字秤,并通过一个4位7段显示器输出数值
weighing()
循环以比我的显示多路复用时间慢的速度读取值,因此在读取值之前代码不会继续,从而导致显示像地狱一样闪烁。因此我尝试通过concurrent.futures
同时运行weighing()
循环和displaying()
循环。但是代码只执行weighing()
一次,然后卡在display()
循环中,所以它们不是并发运行的
我的代码一定有问题,请帮我澄清,并留下其他方法的建议。
import time
import RPi.GPIO as GPIO
import concurrent.futures
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
segments = [2, 3, 4, 5, 6, 7, 8, 9]
digits = [12, 13, 18, 19]
GPIO.setup(segments, GPIO.OUT)
GPIO.output(segments, GPIO.HIGH)
GPIO.setup(digits, GPIO.OUT)
GPIO.output(digits, GPIO.LOW)
nums ={
0:(1,1,1,1,1,1,0),
1:(0,1,1,0,0,0,0),
2:(1,1,0,1,1,0,1),
3:(1,1,1,1,0,0,1),
4:(0,1,1,0,0,1,1),
5:(1,0,1,1,0,1,1),
6:(1,0,1,1,1,1,1),
7:(1,1,1,0,0,0,0),
8:(1,1,1,1,1,1,1),
9:(1,1,1,1,0,1,1)}
switchpolarity = {1: 0,
0:1}
def display(value):
while 1:
s = [int(d) for d in str(value)]
for digit in range(0,len(s)):
for segment in range(0,7):
GPIO.output(segments[segment], switchpolarity[nums[s[digit]][segment]])
GPIO.output(digits[digit], 1)
time.sleep(0.01)
GPIO.output(digits[digit], 0)
EMULATE_HX711=False
if not EMULATE_HX711:
from hx711 import HX711
else:
from emulated_hx711 import HX711
def weighing():
while 1:
val = round(abs(hx.get_weight(1)))
print(val)
hx.power_down()
hx.power_up()
return(val)
hx = HX711(9, 10)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(754)
hx.reset()
hx.tare()
print("Tare done! Add weight now...")
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
weighing = executor.submit(weighing)
displaying = executor.submit(display, (t1.result()))
except(KeyboardInterrupt):
GPIO.cleanup()
我很抱歉在代码中输入错误,因为我在未经测试的情况下更改了流程名称。这是我的新代码,我可以说没有愚蠢的错误:
import time
import RPi.GPIO as GPIO
import concurrent.futures
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
segments = [2, 3, 4, 5, 6, 7, 8, 9]
digits = [12, 13, 18, 19]
GPIO.setup(segments, GPIO.OUT)
GPIO.output(segments, GPIO.HIGH)
GPIO.setup(digits, GPIO.OUT)
GPIO.output(digits, GPIO.LOW)
nums ={
0:(1,1,1,1,1,1,0),
1:(0,1,1,0,0,0,0),
2:(1,1,0,1,1,0,1),
3:(1,1,1,1,0,0,1),
4:(0,1,1,0,0,1,1),
5:(1,0,1,1,0,1,1),
6:(1,0,1,1,1,1,1),
7:(1,1,1,0,0,0,0),
8:(1,1,1,1,1,1,1),
9:(1,1,1,1,0,1,1)}
switchpolarity = {1: 0,
0:1}
def display(value):
while 1:
s = [int(d) for d in str(value)]
for digit in range(0,len(s)):
for segment in range(0,7):
GPIO.output(segments[segment], switchpolarity[nums[s[digit]][segment]])
GPIO.output(digits[digit], 1)
time.sleep(0.01)
GPIO.output(digits[digit], 0)
EMULATE_HX711=False
if not EMULATE_HX711:
from hx711 import HX711
else:
from emulated_hx711 import HX711
def weighing():
while 1:
val = round(abs(hx.get_weight(1)))
print(val)
hx.power_down()
hx.power_up()
return(val)
hx = HX711(9, 10)
hx.set_reading_format("MSB", "MSB")
hx.set_reference_unit(754)
hx.reset()
hx.tare()
print("Tare done! Add weight now...")
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
weighing1 = executor.submit(weighing)
displaying1 = executor.submit(display, (weighing1.result()))
except(KeyboardInterrupt):
GPIO.cleanup()
这是“称重”线程的主要功能:
return(val)
语句将导致函数在循环的第一次迭代结束时返回。函数返回后,线程完成。它再也不会跑了以下是您启动线程的方式:
如果我理解正确,*调用
executor.submit(weighing)
返回一个未来,调用t1.result()
**等待未来的完成,然后返回weighing
函数返回的任何值这意味着
executor.submit(display, ...)
在t1.result()
返回一个值之前不会发生,这意味着在第一个线程完成之前,第二个线程甚至不能启动海事组织
你的
weighing()
函数应该更新一个全局变量并继续循环,而不是返回一个值,并且您的
display()
函数应该通过从全局变量复制来获取显示的值,并且您可以忽略
ProcessPoolExecutor
返回的未来。您并没有真正将其用作执行器服务:您只是将其用作创建两个线程的一种方式*我实际上不是Python大师
**我假设
t1.result()
是一个复制/粘贴错误,您的意思是说weighing.result()
相关问题 更多 >
编程相关推荐