我是Python的新手,尝试用Raspberry在RPI上编写一个小脚本。 我的问题是,代码在Thonny中工作得很好,但是当我在LXTerminal中加载它时,有些函数不能正常工作。你知道吗
当我在Thonny中进行计算并按下按钮21 30秒时,有效值(round((effectivelytime/refresh)*100))将为50%,但当我在terminal中执行相同操作时,有效值将为0,但如果我根本不按下按钮21,有效值将为100。你知道吗
就像有效计时器只发送“满值”或什么都没有。你知道吗
有人有主意吗?你知道吗
!/usr/bin/env python
import pyodbc
import time
from multiprocessing import Process, Value
from gpiozero import Button
from threading import Thread
button21 = Button(21)
button20 = Button(20)
cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER=****;PORT=1433;DATABASE=****;UID=****;PWD=****;TDS_Version=8.7;')
counter=Value('h', 0)
x=Value('h', 0)
refresh = 10
elapsed_stoptime = 0
effectivelytime = 1
effectively = 0
precounter = 0
def function1():
while True:
global effectivelytime
global effectively
global precounter
effectivelytime = x.value
effectively = round((effectivelytime / refresh)*100)
if counter.value == precounter:
effectively = 0
cursor = cnxn.cursor()
cursor.execute('INSERT INTO ****.dbo.****(counter, effectively) VALUES ({},{})'.format (counter.value, effectively,))
cnxn.commit()
cursor.close()
print("Printed To DB - Counter = ", counter.value, ", Effectively = ", effectively,)
x.value = 0
precounter = counter.value
time.sleep(refresh)
def function2():
button21.wait_for_release()
counter.value = counter.value + 1
print (counter.value, "Cykel")
def effectivelytimer():
while True:
if button20.is_pressed:
x.value = x.value + 1
time.sleep(1)
else:
time.sleep(1)
def trigcounter():
t2 = Thread(group=None,target=function2)
t2.start()
button21.when_pressed = trigcounter
if __name__ == '__main__':
t1 = Thread(group=None,target=function1)
t3 = Thread(group=None,target=effectivelytimer)
t1.start()
t3.start()
我根本没有收到任何错误消息。你知道吗
Thonny使用Python3。脚本的shebang行(
!/usr/bin/env python
)引用python2。尝试将!/usr/bin/env python
更改为!/usr/bin/env python3
相关问题 更多 >
编程相关推荐