I've created a function for threading which continuously checks a chip register for an interrupt and if this interrupt occurs it'll get values from the chip, do some calculation and put the calculated values in a queue which is linked to a GUI created with tkinter.
def workerThread1(self):
##registers##
AWATTHR = 0x01
[...]
##variables##
cAWATTHR = 2.365579 * 10 ** (-5)
[...]
##register values##
b1MMODE = 0xFC
[...]
##set up SPI##
spi = spidev.SpiDev()
spi.open(0, 0)
spi.mode=0b01
##write register values##
spi.xfer2([WRITE|MMODE, b1MMODE], SPISPD, SPIDLY, SPILNG)
[...]
##loop##
spi.xfer2([WRITE|LINECYC, b1LINECYC, b2LINECYC], SPISPD, SPIDLY, SPILNG)
spi.xfer2([RSTATUS], SPISPD, SPIDLY, SPILNG)
while self.running == 1:
n = n + 1
rFREQ = spi.xfer2([FREQ, DBYTE, DBYTE], SPISPD, SPIDLY, SPILNG)
vFREQ = Bits(uint = rFREQ[1]*VMSB16BIT + rFREQ[2], length = 16)
sFREQ = sFREQ + vFREQ.uint
LENERGY = [...]
if LENERGY == '1':
rvFREQ = (1 / ((sFREQ / n) * 9.6 * 10 ** (-6)))
[...]
transfer = [rvFREQ, [...]]
self.queue.put(transfer)
n = 0
sFREQ = 0
spi.close()
Unfortunately I always get "IOError: (Errno 9) Bad file descriptor" with the following Traceback:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 552, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 505, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/pi/GUI-LOG/GUI1.py", line 277, in workerThread1
rFREQ = spi.xfer2([FREQ, DBYTE, DBYTE], SPISPD, SPIDLY, SPILNG)
IOError: [Errno 9] Bad file descriptor
I'm looking forward to any hints about what I'm doing wrong here.