覆盆子pi穿线

时间:2017-05-08 17:40:17

标签: python pyqt pyqt4 python-multithreading

我是编程新手。我正在用双脚跳入并构建一个项目,该项目将读取GPIO引脚(机器是Raspberry Pi)的输入电压,最终将显示在qt creator中的lcdnumber小部件上。现在,我使用光电池作为最终组件的o2电池的替代品。感谢所有人发布代码,我有光电池的工作代码,可以很好地打印电压。为了获得恒定和连续的电压,我使用了一个真正的循环。当然,当我运行程序时,循环接管并且程序的其他部分不运行。另一部分是带有lcdnumber显示屏的gui和一些最终将连接到数字电位器的按钮。 gui本身运行得很好。光电管代码本身很好用。结合这两者是我遇到问题的地方。根据我过去几天所读到的内容,在python或Qt中进行线程化似乎是要走的路。我已经阅读了所有可以找到的例子和解释,而且我被卡住了。 gui弹出并运行,循环似乎没有运行。光电池的输出现在正在打印到终端,或者通常在我自己运行该代码时打印,因为我还不知道如何将它连接到Qt lcd。

如果有人可以查看我的代码并让我知道我在哪里遇到线程以及您可能会看到的任何其他问题,我会非常感激。关于将光电管信号连接到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_())

1 个答案:

答案 0 :(得分:0)

您必须创建一个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_())