I know that similar questions might have been asked before. But I couldn't find a solution that fits my case. Apologies for the dumb question in advance.
I am reading two voltage values from a USB-hub (connected to two sensors). The problem is that, the way that my code does it, there will be an approx. 0.8-second delay between them so I can never have both their values at the same time ( if I decrease the any of the two time.sleep()
, the value of the second def will not be reported ). I was thinking that if both could run at the same time, maybe I could have values that belong to exact same time point and not shifted through time. If you have any comments that can improve this code, I appreciate it.
I thank you for your comments in advance.
import sys
import time
import datetime
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
try:
ch = VoltageRatioInput()
except RuntimeError as e:
print("Runtime Exception %s" % e.details)
print("Press Enter to Exit...\n")
readin = sys.stdin.read(1)
exit(1)
a=[]
b=[]
try:
start = time.time()
while True:
def VoltageRatioChangeHandler(e, voltageRatio):
n=voltageRatio
a.append(n)
ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandler)
ch.setHubPort(1)
ch.setIsHubPortDevice(1)
ch.openWaitForAttachment(5000)
if(ch.getChannelSubclass() == ChannelSubclass.PHIDCHSUBCLASS_VOLTAGERATIOINPUT_BRIDGE):
ch.setBridgeEnabled(1)
time.sleep(0.3)
ch.close()
end1 = time.time()
Elt1 = end1-start
print (Elt1)
print a
###
def VoltageRatioChangeHandler(e, voltageRatio2):
m=voltageRatio2
if m is None:
b.append(0)
else:
b.append(m)
ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandler)
ch.setHubPort(0)
ch.setIsHubPortDevice(0)
ch.openWaitForAttachment(5000)
if(ch.getChannelSubclass() == ChannelSubclass.PHIDCHSUBCLASS_VOLTAGERATIOINPUT_BRIDGE):
ch.setBridgeEnabled(1)
time.sleep(0.4)
ch.close()
end = time.time()
Elt = end - start
print (Elt)
print b
except KeyboardInterrupt:
print ("gracefully aborted")
sys.exit()