def AttachBridge(databasepath, serialNumber): def onAttachHandler(event): logString = "Bridge Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Bridge Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Bridge Error " + str(event.device.getSerialNum()) + ", Error: " + event.description print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "Bridge Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Bridge Server Disconnect " + str(event.device.getSerialNum()) #print(logString) def bridgeDataHandler(event): logString = "Bridge Data " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute("INSERT INTO BRIDGE_DATA VALUES(NULL, DateTime('now'), ?, ?, ?)", (event.device.getSerialNum(), event.index, event.value)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = Bridge() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnBridgeDataHandler(bridgeDataHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
def openBridge(serialNum): # Create a bridge object try: bridge = Bridge() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: bridge.setOnAttachHandler(BridgeAttached) bridge.setOnDetachHandler(BridgeDetached) bridge.setOnErrorhandler(BridgeError) bridge.setOnBridgeDataHandler(BridgeData) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Calibration Script.\nOpening phidget object....") try: bridge.openPhidget(serialNum) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: bridge.waitForAttach(20000) print("Set data rate to %i ms ..." % (rate)) bridge.setDataRate(rate) sleep(1) print("Set Gain to %s..." % str(gainTable[gain])) setGainAllChanels(bridge, gain) sleep(1) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: bridge.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: displayDeviceInfo(bridge) return bridge
def openBridge(serialNum): #Create a bridge object try: bridge = Bridge() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: bridge.setOnAttachHandler(BridgeAttached) bridge.setOnDetachHandler(BridgeDetached) bridge.setOnErrorhandler(BridgeError) bridge.setOnBridgeDataHandler(BridgeData) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Calibration Script.\nOpening phidget object....") try: bridge.openPhidget(serialNum) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: bridge.waitForAttach(20000) print("Set data rate to %i ms ..." % (rate)) bridge.setDataRate(rate) sleep(1) print("Set Gain to %s..." % str(gainTable[gain])) setGainAllChanels(bridge,gain) sleep(1) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: bridge.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Exiting....") exit(1) else: displayDeviceInfo(bridge) return bridge
class PhidgetBridge(): def __init__(self): self.bridge = Bridge() self.bridge.setOnAttachHandler(BridgeAttached) self.bridge.setOnDetachHandler(BridgeDetached) self.bridge.setOnErrorhandler(BridgeError) self.bridge.setOnBridgeDataHandler(BridgeData) # self.data_to_be_sent_bridge = [] # self.times_to_be_sent_bridge = [] def open(self,waitTimeMS): self.bridge.openPhidget() try: self.bridge.waitForAttach(waitTimeMS) time.sleep(0.2) self.bridge.setDataRate(8) time.sleep(0.2) self.bridge.setGain(0, BridgeGain.PHIDGET_BRIDGE_GAIN_8) time.sleep(0.2) self.bridge.setEnabled(0, True) time.sleep(0.2) return 1 except: return -1 # def BridgeData(self,e): # timeStamp = time.time() # self.data_to_be_sent_bridge.append(e.value) # self.times_to_be_sent_bridge.append(timeStamp) def getData(self): global data_to_be_sent_bridge global times_to_be_sent_bridge #return the data and times and update the lists data_to_return = [data_to_be_sent_bridge,times_to_be_sent_bridge] data_to_be_sent_bridge = [] times_to_be_sent_bridge = [] return data_to_return def close(self): self.bridge.setEnabled(0,False) time.sleep(2) self.bridge.closePhidget()
class PhidgetBridge(): def __init__(self): self.bridge = Bridge() self.bridge.setOnAttachHandler(BridgeAttached) self.bridge.setOnDetachHandler(BridgeDetached) self.bridge.setOnErrorhandler(BridgeError) self.bridge.setOnBridgeDataHandler(BridgeData) # self.data_to_be_sent_bridge = [] # self.times_to_be_sent_bridge = [] def open(self, waitTimeMS): self.bridge.openPhidget() try: self.bridge.waitForAttach(waitTimeMS) time.sleep(0.2) self.bridge.setDataRate(8) time.sleep(0.2) self.bridge.setGain(0, BridgeGain.PHIDGET_BRIDGE_GAIN_8) time.sleep(0.2) self.bridge.setEnabled(0, True) time.sleep(0.2) return 1 except: return -1 # def BridgeData(self,e): # timeStamp = time.time() # self.data_to_be_sent_bridge.append(e.value) # self.times_to_be_sent_bridge.append(timeStamp) def getData(self): global data_to_be_sent_bridge global times_to_be_sent_bridge #return the data and times and update the lists data_to_return = [data_to_be_sent_bridge, times_to_be_sent_bridge] data_to_be_sent_bridge = [] times_to_be_sent_bridge = [] return data_to_return def close(self): self.bridge.setEnabled(0, False) time.sleep(2) self.bridge.closePhidget()
#print("%f" % (ev)) print(e.index, e.value) with open(fname, "a") as myfile: myfile.write("%2.1f\t" % (einp)) myfile.write("%f\n" % (ev)) ############################################## #print("Bridge %i: Input %i: %f" % (source.getSerialNum(), e.index, e.value)) ###################################### Main Program Code ###################################### ######################################################################################################### try: print("Please input the file name for readings:") fname = raw_input() bridge.setOnAttachHandler(BridgeAttached) bridge.setOnDetachHandler(BridgeDetached) ############################################## fptr = open(fname, "w") #fptr.write("data begins below:\n") fptr.close ############################################## bridge.setOnErrorhandler(BridgeError) bridge.setOnBridgeDataHandler(BridgeData) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try:
source = e.device if measure_DATA: DATA[e.index].append(float(e.value)) if e.index == measure_MEAS: MEAS[e.index].append(float(e.value)) if printing: if e.index ==2: print ("Sensor %i; Weight %f" %(e.index, K[e.index]*(e.value-offsets[e.index]))) #Main Program Code try: #logging example, uncomment to generate a log file #bridge.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log") bridge.setOnAttachHandler(BridgeAttached) bridge.setOnDetachHandler(BridgeDetached) bridge.setOnErrorhandler(BridgeError) bridge.setOnBridgeDataHandler(BridgeData) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: bridge.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1)
#list of bridge objects. Access serial by lBridges[0].getSerialNum() lBridges = [] #loop through bridges creating bridge objects for serial in iBridgeSerials: if options.verbose: print("---Opening %i...---"%serial) try: tempBridge = Bridge() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: tempBridge.setOnAttachHandler(BridgeAttached) tempBridge.setOnDetachHandler(BridgeDetached) tempBridge.setOnErrorhandler(BridgeError) tempBridge.setOnBridgeDataHandler(BridgeData) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) try: tempBridge.openPhidget(serial) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) if options.verbose: print("Waiting for attach....")
def AttachBridge(databasepath, serialNumber): def onAttachHandler(event): logString = "Bridge Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Bridge Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Bridge Error " + str( event.device.getSerialNum()) + ", Error: " + event.description print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "Bridge Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Bridge Server Disconnect " + str( event.device.getSerialNum()) #print(logString) def bridgeDataHandler(event): logString = "Bridge Data " + str(event.device.getSerialNum()) #print(logString) try: conn = sqlite3.connect(databasepath) conn.execute( "INSERT INTO BRIDGE_DATA VALUES(NULL, DateTime('now'), ?, ?, ?)", (event.device.getSerialNum(), event.index, event.value)) conn.commit() conn.close() except sqlite3.Error as e: print "An error occurred:", e.args[0] try: p = Bridge() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.setOnBridgeDataHandler(bridgeDataHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
#list of bridge objects. Access serial by lBridges[0].getSerialNum() lBridges = [] #loop through bridges creating bridge objects for serial in iBridgeSerials: if options.verbose: print("---Opening %i...---" % serial) try: tempBridge = Bridge() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: tempBridge.setOnAttachHandler(BridgeAttached) tempBridge.setOnDetachHandler(BridgeDetached) tempBridge.setOnErrorhandler(BridgeError) tempBridge.setOnBridgeDataHandler(BridgeData) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) try: tempBridge.openPhidget(serial) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) if options.verbose: print("Waiting for attach....")