raspberry pi 线程
raspberry pi threading
我是编程新手。我正在全力投入并构建一个项目,该项目将读取 GPIO 引脚(机器是 Raspberry Pi)输入电压,并最终显示在 qt creator 中的 lcdnumber 小部件上。现在,我使用光电管作为最终组件的 o2 电池的替代品。感谢所有发布代码的人,我有光电管的工作代码,它可以很好地打印电压。为了获得恒定和连续的电压,我使用了 while true 循环。当然,当我 运行 程序时,循环接管并且我程序的其他部分不会 运行。另一部分是一个带有液晶显示器和一些最终将连接到数字电位器的按钮的图形用户界面。 gui 运行 本身就很棒。光电池代码本身就很好用。将两者结合起来是我遇到的问题。从我过去几天读到的内容来看,在 python 或 Qt 中进行线程化似乎是可行的方法。我已经阅读了我能找到的所有示例和解释,但我被卡住了。 gui 弹出并运行,循环似乎没有运行。光电管的输出现在正在打印到终端,或者通常在我 运行 自己编码时打印,因为我还不知道如何将它连接到 Qt 液晶显示器。
如果有人可以查看我的代码并让我知道我在线程方面出了什么问题以及您可能看到的任何其他问题,我将不胜感激,因为我仍在学习。任何关于将光电管信号连接到 Qt LCD 的提示也将是一个巨大的帮助。
我正在使用 Raspbian(Debian Linux 的变体)和 Qt4/Qt Creator 3.2.1
请告诉我您可能需要或想要的其他信息。
import sys
import RPi.GPIO as GPIO
import time
import re
from PyQt4 import QtGui, uic, QtCore
from PyQt4.QtCore import QThread
import spidev
import os
import threading
from threading import Thread
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
o2zero = 26
o2span = 19
cozero = 13
cospan = 6
co2zero = 5
co2span = 21
status = "nil"
light_channel = 0
spi = spidev.SpiDev()
spi.open(0,0)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi('mainwindow.ui', self)
self.show()
QtCore.QObject.connect(self.o2_zero_up,QtCore.SIGNAL("clicked()"), self.o2zeroup)
QtCore.QObject.connect(self.o2_zero_down,QtCore.SIGNAL("clicked()"), self.o2zerodown)
QtCore.QObject.connect(self.o2_span_up,QtCore.SIGNAL("clicked()"), self.o2spanup)
QtCore.QObject.connect(self.o2_span_down,QtCore.SIGNAL("clicked()"), self.o2spandown)
QtCore.QObject.connect(self.co_zero_up,QtCore.SIGNAL("clicked()"), self.cozeroup)
QtCore.QObject.connect(self.co_zero_down,QtCore.SIGNAL("clicked()"), self.cozerodown)
QtCore.QObject.connect(self.co_span_up,QtCore.SIGNAL("clicked()"), self.cospanup)
QtCore.QObject.connect(self.co_span_down,QtCore.SIGNAL("clicked()"), self.cospandown)
QtCore.QObject.connect(self.co2_zero_up,QtCore.SIGNAL("clicked()"), self.co2zeroup)
QtCore.QObject.connect(self.co2_zero_down,QtCore.SIGNAL("clicked()"), self.co2zerodown)
QtCore.QObject.connect(self.co2_span_up,QtCore.SIGNAL("clicked()"), self.co2spanup)
QtCore.QObject.connect(self.co2_span_down,QtCore.SIGNAL("clicked()"), self.co2spandown)
QtCore.QObject.connect(self.close_button,QtCore.SIGNAL("clicked()"), self.gpiocleanup)
def o2zeroup(self):
GPIO.setup(o2zero, GPIO.OUT)
GPIO.output(o2zero, 1)
def o2zerodown(self):
GPIO.setup(o2zero, GPIO.OUT)
GPIO.output(o2zero, 0)
def o2spanup(self):
GPIO.setup(o2span, GPIO.OUT)
GPIO.output(o2span, 1)
def o2spandown(self):
GPIO.setup(o2span, GPIO.OUT)
GPIO.output(o2span, 0)
def cozeroup(self):
GPIO.setup(cozero, GPIO.OUT)
GPIO.output(cozero, 1)
def cozerodown(self):
GPIO.setup(cozero, GPIO.OUT)
GPIO.output(cozero, 0)
def cospanup(self):
GPIO.setup(cospan, GPIO.OUT)
GPIO.output(cospan, 1)
def cospandown(self):
GPIO.setup(cospan, GPIO.OUT)
GPIO.output(cospan, 0)
def co2zeroup(self):
GPIO.setup(co2zero, GPIO.OUT)
GPIO.output(co2zero, 1)
def co2zerodown(self):
GPIO.setup(co2zero, GPIO.OUT)
GPIO.output(co2zero, 0)
def co2spanup(self):
GPIO.setup(co2span, GPIO.OUT)
GPIO.output(co2span, 1)
def co2spandown(self):
GPIO.setup(co2span, GPIO.OUT)
GPIO.output(co2span, 0)
def gpiocleanup(self):
GPIO.cleanup()
def closeEvent(self, event):
print ("GPIO CleanUP")
GPIO.cleanup()
event.accept()
class O2_Channel(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def O2_Channel(self):
ReadChannel(channel)
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
ConvertVolts(data,places)
volts = (data * 3.3) / float(1023)
volts = round(volts,places)
return volts
def run(self):
while True:
light_level = ReadChannel(light_channel)
light_volts = ConvertVolts(light_level,2)
print "--------------------------------------------"
print("Light: {} ({}V)".format(light_level,light_volts))
light_channel = 0
temp_channel = 1
delay = .3
time.sleep(delay)
self.O2_Channel.start()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())
您必须创建一个 Thread 实例,并从主线程使用 start () 函数启动它。如果你想在你的 GUI 中显示数据,你必须从主线程来做,为此我创建了一个插槽和一个在有新读数时发出的信号。
我也将信号和插槽之间的连接方式更改为更现代的方式。
import sys
import RPi.GPIO as GPIO
import time
import re
from PyQt4 import QtGui, uic, QtCore
from PyQt4.QtCore import QThread
import spidev
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
o2zero = 26
o2span = 19
cozero = 13
cospan = 6
co2zero = 5
co2span = 21
status = "nil"
light_channel = 0
spi = spidev.SpiDev()
spi.open(0,0)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi('mainwindow.ui', self)
self.o2_zero_upclicked.connect(self.o2zeroup)
self.o2_zero_down.clicked.connect(self.o2zerodown)
self.o2_span_up.clicked.connect(self.o2spanup)
self.o2_span_down.clicked.connect(self.o2spandown)
self.co_zero_up.clicked.connect(self.cozeroup)
self.co_zero_down.clicked.connect(self.cozerodown)
self.co_span_up.clicked.connect(self.cospanup)
self.co_span_down.clicked.connect(self.cospandown)
self.co2_zero_up.clicked.connect(self.co2zeroup)
self.co2_zero_down.clicked.connect( self.co2zerodown)
self.co2_span_up.clicked.connect(self.co2spanup)
self.co2_span_down.clicked.connect(self.co2spandown)
self.close_button.clicked.connect(self.gpiocleanup)
self.thread = O2_Channel()
self.thread.changedValue.connect(self.onChangeValue)
self.thread.start()
self.show()
def onChangeValue(self, values):
light_level, light_volts = values
print "--------------------------------------------"
print("Light: {} ({}V)".format(light_level,light_volts))
def o2zeroup(self):
GPIO.setup(o2zero, GPIO.OUT)
GPIO.output(o2zero, 1)
def o2zerodown(self):
GPIO.setup(o2zero, GPIO.OUT)
GPIO.output(o2zero, 0)
def o2spanup(self):
GPIO.setup(o2span, GPIO.OUT)
GPIO.output(o2span, 1)
def o2spandown(self):
GPIO.setup(o2span, GPIO.OUT)
GPIO.output(o2span, 0)
def cozeroup(self):
GPIO.setup(cozero, GPIO.OUT)
GPIO.output(cozero, 1)
def cozerodown(self):
GPIO.setup(cozero, GPIO.OUT)
GPIO.output(cozero, 0)
def cospanup(self):
GPIO.setup(cospan, GPIO.OUT)
GPIO.output(cospan, 1)
def cospandown(self):
GPIO.setup(cospan, GPIO.OUT)
GPIO.output(cospan, 0)
def co2zeroup(self):
GPIO.setup(co2zero, GPIO.OUT)
GPIO.output(co2zero, 1)
def co2zerodown(self):
GPIO.setup(co2zero, GPIO.OUT)
GPIO.output(co2zero, 0)
def co2spanup(self):
GPIO.setup(co2span, GPIO.OUT)
GPIO.output(co2span, 1)
def co2spandown(self):
GPIO.setup(co2span, GPIO.OUT)
GPIO.output(co2span, 0)
def gpiocleanup(self):
GPIO.cleanup()
def closeEvent(self, event):
self.thread.stop()
self.thread.quit()
self.thread.wait()
self.thread.deleteLater()
print ("GPIO CleanUP")
GPIO.cleanup()
event.accept()
def ReadChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
def ConvertVolts(data, places):
volts = (data * 3.3) / float(1023)
volts = round(volts,places)
return volts
class O2_Channel(QtCore.QThread):
changedValue = QtCore.pyqtSignal(tuple)
def __init__(self):
QtCore.QThread.__init__(self)
self.mRunning = True
def run(self):
while self.mRunning:
light_level = ReadChannel(light_channel)
light_volts = ConvertVolts(light_level,2)
print "--------------------------------------------"
print("Light: {} ({}V)".format(light_level,light_volts))
self.changedValue.emit((light_level, light_volts))
light_channel = 0
temp_channel = 1
delay = .3
time.sleep(delay)
def stop(self):
self.mRunning = False
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())
我是编程新手。我正在全力投入并构建一个项目,该项目将读取 GPIO 引脚(机器是 Raspberry Pi)输入电压,并最终显示在 qt creator 中的 lcdnumber 小部件上。现在,我使用光电管作为最终组件的 o2 电池的替代品。感谢所有发布代码的人,我有光电管的工作代码,它可以很好地打印电压。为了获得恒定和连续的电压,我使用了 while true 循环。当然,当我 运行 程序时,循环接管并且我程序的其他部分不会 运行。另一部分是一个带有液晶显示器和一些最终将连接到数字电位器的按钮的图形用户界面。 gui 运行 本身就很棒。光电池代码本身就很好用。将两者结合起来是我遇到的问题。从我过去几天读到的内容来看,在 python 或 Qt 中进行线程化似乎是可行的方法。我已经阅读了我能找到的所有示例和解释,但我被卡住了。 gui 弹出并运行,循环似乎没有运行。光电管的输出现在正在打印到终端,或者通常在我 运行 自己编码时打印,因为我还不知道如何将它连接到 Qt 液晶显示器。
如果有人可以查看我的代码并让我知道我在线程方面出了什么问题以及您可能看到的任何其他问题,我将不胜感激,因为我仍在学习。任何关于将光电管信号连接到 Qt LCD 的提示也将是一个巨大的帮助。
我正在使用 Raspbian(Debian Linux 的变体)和 Qt4/Qt Creator 3.2.1
请告诉我您可能需要或想要的其他信息。
import sys
import RPi.GPIO as GPIO
import time
import re
from PyQt4 import QtGui, uic, QtCore
from PyQt4.QtCore import QThread
import spidev
import os
import threading
from threading import Thread
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
o2zero = 26
o2span = 19
cozero = 13
cospan = 6
co2zero = 5
co2span = 21
status = "nil"
light_channel = 0
spi = spidev.SpiDev()
spi.open(0,0)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi('mainwindow.ui', self)
self.show()
QtCore.QObject.connect(self.o2_zero_up,QtCore.SIGNAL("clicked()"), self.o2zeroup)
QtCore.QObject.connect(self.o2_zero_down,QtCore.SIGNAL("clicked()"), self.o2zerodown)
QtCore.QObject.connect(self.o2_span_up,QtCore.SIGNAL("clicked()"), self.o2spanup)
QtCore.QObject.connect(self.o2_span_down,QtCore.SIGNAL("clicked()"), self.o2spandown)
QtCore.QObject.connect(self.co_zero_up,QtCore.SIGNAL("clicked()"), self.cozeroup)
QtCore.QObject.connect(self.co_zero_down,QtCore.SIGNAL("clicked()"), self.cozerodown)
QtCore.QObject.connect(self.co_span_up,QtCore.SIGNAL("clicked()"), self.cospanup)
QtCore.QObject.connect(self.co_span_down,QtCore.SIGNAL("clicked()"), self.cospandown)
QtCore.QObject.connect(self.co2_zero_up,QtCore.SIGNAL("clicked()"), self.co2zeroup)
QtCore.QObject.connect(self.co2_zero_down,QtCore.SIGNAL("clicked()"), self.co2zerodown)
QtCore.QObject.connect(self.co2_span_up,QtCore.SIGNAL("clicked()"), self.co2spanup)
QtCore.QObject.connect(self.co2_span_down,QtCore.SIGNAL("clicked()"), self.co2spandown)
QtCore.QObject.connect(self.close_button,QtCore.SIGNAL("clicked()"), self.gpiocleanup)
def o2zeroup(self):
GPIO.setup(o2zero, GPIO.OUT)
GPIO.output(o2zero, 1)
def o2zerodown(self):
GPIO.setup(o2zero, GPIO.OUT)
GPIO.output(o2zero, 0)
def o2spanup(self):
GPIO.setup(o2span, GPIO.OUT)
GPIO.output(o2span, 1)
def o2spandown(self):
GPIO.setup(o2span, GPIO.OUT)
GPIO.output(o2span, 0)
def cozeroup(self):
GPIO.setup(cozero, GPIO.OUT)
GPIO.output(cozero, 1)
def cozerodown(self):
GPIO.setup(cozero, GPIO.OUT)
GPIO.output(cozero, 0)
def cospanup(self):
GPIO.setup(cospan, GPIO.OUT)
GPIO.output(cospan, 1)
def cospandown(self):
GPIO.setup(cospan, GPIO.OUT)
GPIO.output(cospan, 0)
def co2zeroup(self):
GPIO.setup(co2zero, GPIO.OUT)
GPIO.output(co2zero, 1)
def co2zerodown(self):
GPIO.setup(co2zero, GPIO.OUT)
GPIO.output(co2zero, 0)
def co2spanup(self):
GPIO.setup(co2span, GPIO.OUT)
GPIO.output(co2span, 1)
def co2spandown(self):
GPIO.setup(co2span, GPIO.OUT)
GPIO.output(co2span, 0)
def gpiocleanup(self):
GPIO.cleanup()
def closeEvent(self, event):
print ("GPIO CleanUP")
GPIO.cleanup()
event.accept()
class O2_Channel(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def O2_Channel(self):
ReadChannel(channel)
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
ConvertVolts(data,places)
volts = (data * 3.3) / float(1023)
volts = round(volts,places)
return volts
def run(self):
while True:
light_level = ReadChannel(light_channel)
light_volts = ConvertVolts(light_level,2)
print "--------------------------------------------"
print("Light: {} ({}V)".format(light_level,light_volts))
light_channel = 0
temp_channel = 1
delay = .3
time.sleep(delay)
self.O2_Channel.start()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())
您必须创建一个 Thread 实例,并从主线程使用 start () 函数启动它。如果你想在你的 GUI 中显示数据,你必须从主线程来做,为此我创建了一个插槽和一个在有新读数时发出的信号。
我也将信号和插槽之间的连接方式更改为更现代的方式。
import sys
import RPi.GPIO as GPIO
import time
import re
from PyQt4 import QtGui, uic, QtCore
from PyQt4.QtCore import QThread
import spidev
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
o2zero = 26
o2span = 19
cozero = 13
cospan = 6
co2zero = 5
co2span = 21
status = "nil"
light_channel = 0
spi = spidev.SpiDev()
spi.open(0,0)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi('mainwindow.ui', self)
self.o2_zero_upclicked.connect(self.o2zeroup)
self.o2_zero_down.clicked.connect(self.o2zerodown)
self.o2_span_up.clicked.connect(self.o2spanup)
self.o2_span_down.clicked.connect(self.o2spandown)
self.co_zero_up.clicked.connect(self.cozeroup)
self.co_zero_down.clicked.connect(self.cozerodown)
self.co_span_up.clicked.connect(self.cospanup)
self.co_span_down.clicked.connect(self.cospandown)
self.co2_zero_up.clicked.connect(self.co2zeroup)
self.co2_zero_down.clicked.connect( self.co2zerodown)
self.co2_span_up.clicked.connect(self.co2spanup)
self.co2_span_down.clicked.connect(self.co2spandown)
self.close_button.clicked.connect(self.gpiocleanup)
self.thread = O2_Channel()
self.thread.changedValue.connect(self.onChangeValue)
self.thread.start()
self.show()
def onChangeValue(self, values):
light_level, light_volts = values
print "--------------------------------------------"
print("Light: {} ({}V)".format(light_level,light_volts))
def o2zeroup(self):
GPIO.setup(o2zero, GPIO.OUT)
GPIO.output(o2zero, 1)
def o2zerodown(self):
GPIO.setup(o2zero, GPIO.OUT)
GPIO.output(o2zero, 0)
def o2spanup(self):
GPIO.setup(o2span, GPIO.OUT)
GPIO.output(o2span, 1)
def o2spandown(self):
GPIO.setup(o2span, GPIO.OUT)
GPIO.output(o2span, 0)
def cozeroup(self):
GPIO.setup(cozero, GPIO.OUT)
GPIO.output(cozero, 1)
def cozerodown(self):
GPIO.setup(cozero, GPIO.OUT)
GPIO.output(cozero, 0)
def cospanup(self):
GPIO.setup(cospan, GPIO.OUT)
GPIO.output(cospan, 1)
def cospandown(self):
GPIO.setup(cospan, GPIO.OUT)
GPIO.output(cospan, 0)
def co2zeroup(self):
GPIO.setup(co2zero, GPIO.OUT)
GPIO.output(co2zero, 1)
def co2zerodown(self):
GPIO.setup(co2zero, GPIO.OUT)
GPIO.output(co2zero, 0)
def co2spanup(self):
GPIO.setup(co2span, GPIO.OUT)
GPIO.output(co2span, 1)
def co2spandown(self):
GPIO.setup(co2span, GPIO.OUT)
GPIO.output(co2span, 0)
def gpiocleanup(self):
GPIO.cleanup()
def closeEvent(self, event):
self.thread.stop()
self.thread.quit()
self.thread.wait()
self.thread.deleteLater()
print ("GPIO CleanUP")
GPIO.cleanup()
event.accept()
def ReadChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
def ConvertVolts(data, places):
volts = (data * 3.3) / float(1023)
volts = round(volts,places)
return volts
class O2_Channel(QtCore.QThread):
changedValue = QtCore.pyqtSignal(tuple)
def __init__(self):
QtCore.QThread.__init__(self)
self.mRunning = True
def run(self):
while self.mRunning:
light_level = ReadChannel(light_channel)
light_volts = ConvertVolts(light_level,2)
print "--------------------------------------------"
print("Light: {} ({}V)".format(light_level,light_volts))
self.changedValue.emit((light_level, light_volts))
light_channel = 0
temp_channel = 1
delay = .3
time.sleep(delay)
def stop(self):
self.mRunning = False
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())