""" Carlos Medrano ctmedra@unizar.es GPL v3 https://www.gnu.org/licenses/gpl-3.0.html """ import time import threading import collections import datetime import u3 """ OJO no esta pensado para que desde dos hilos distintos se creen dos objetos distintos. Uso: import u3 import time import streamReader as sr d = u3.U3() d.configIO(FIOAnalog = 0x0f) #d.streamConfig(NumChannels=3, PChannels=[0,1,193], NChannels=[31,31, 31], ScanFrequency=1) kk = sr.StreamReader(d) kk.start(NumChannels=3, PChannels=[0,1,193], NChannels=[31,31, 31], ScanFrequency=1) n=0 while(n<10): res = kk.get() print(res) time.sleep(0.25) n += 1 kk.stop(reset = True) d.close() """ logfile = 'streamReader.log' def loopDAQ(res, data, event): with open(logfile, 'w') as f: # Just erase the file pass try: #n = 0 for dic in res: if(event.is_set()): # Received True => stop event.clear() break data.append(dic) """ Check exception n += 1 if(n==5): a = 1/0 """ return except Exception as inst: now = str(datetime.datetime.now()) msg = str(type(inst)) with open(logfile, 'a') as f: f.write(now +' '+ msg+'\n') return def is_alive(thread): if(thread is None): return False else: return thread.is_alive() def end_daq_thread(event, th): event.set() while(event.is_set()): pass th.join() return class StreamReader(): def __init__(self, d, maxlen=2048): self.d = d self.data = collections.deque(maxlen = maxlen) self.event = threading.Event() self.daq_thread = None def start(self, NumChannels, PChannels, NChannels, ScanFrequency): d = self.d daq_thread = self.daq_thread if(not d.streamStarted and not is_alive(daq_thread)): self.data.clear() d.streamConfig(NumChannels=NumChannels, PChannels=PChannels, NChannels=NChannels, ScanFrequency=ScanFrequency) d.streamStart() time.sleep(0.2) res = d.streamData() daq_thread = threading.Thread(target = loopDAQ, args = (res, self.data, self.event)) self.daq_thread = daq_thread daq_thread.start() elif(d.streamStarted and not is_alive(daq_thread)): print('Thread was stopped. Restarting everything') self.data.clear() d.streamStop() #end_daq_thread(self.event, self.daq_thread) d.streamConfig(NumChannels=NumChannels, PChannels=PChannels, NChannels=NChannels, ScanFrequency=ScanFrequency) d.streamStart() time.sleep(0.2) res = d.streamData() daq_thread = threading.Thread(target = loopDAQ, args = (res, self.data, self.event)) self.daq_thread = daq_thread daq_thread.start() elif (not d.streamStarted and is_alive(daq_thread)): print('Stream was stopped. Restarting everything') self.data.clear() end_daq_thread(self.event, daq_thread) d.streamConfig(NumChannels=NumChannels, PChannels=PChannels, NChannels=NChannels, ScanFrequency=ScanFrequency) d.streamStart() time.sleep(0.2) res = d.streamData() daq_thread = threading.Thread(target = loopDAQ, args = (res, self.data, self.event)) self.daq_thread = daq_thread daq_thread.start() else: print('Stream and thread already started. Nothing is done') def __del__(self): self.stop(reset = True, msg = False) def stop(self, reset, msg = True): d = self.d data = self.data daq_thread = self.daq_thread event = self.event if(d.streamStarted and is_alive(daq_thread)): # Regular situation event.set() while(event.is_set()): pass daq_thread.join() d.streamStop() if(reset): data.clear() elif(not d.streamStarted and is_alive(daq_thread)): print('Stream not started but thread alive. Stop everything') event.set() while(event.is_set()): pass daq_thread.join() if(reset): data.clear() elif(d.streamStarted and not is_alive(daq_thread)): print('Stream started but thread not alive. Stop everything') print('Check streamReader.log') d.streamStop() if(reset): data.clear() else: if(msg): print('Stream and thread already stopped. Nothing is done') def get(self): """ Return None if there are no data, a new data from the DAQ or -1 if the data acquisition thread is not working for some reason """ try: daq_thread = self.daq_thread d = self.d if(d.streamStarted and not is_alive(daq_thread)): print('Thread has stopped unexpectedly see '+logfile+' file') res = -1 elif(not d.streamStarted and is_alive(daq_thread)): print('Streaming has stopped unexpectedly') res = -1 else: res = self.data.popleft() except IndexError: res = None return res def reset_input(self): data = self.data # Flushes data data.clear()