Code: Select all
from Phidgets.Devices.InterfaceKit import InterfaceKit
import time
import random
class GestionRelais:
def __init__(self):
self.carte0 = InterfaceKit()
self.carte0.openPhidget(REPLACE_WITH_YOUR_BOARD1_SERIAL_NUMBER)
self.carte0.waitForAttach(10000)
self.carte1 = InterfaceKit()
self.carte1.openPhidget(REPLACE_WITH_YOUR_BOARD2_SERIAL_NUMBER)
self.carte1.waitForAttach(10000)
self.carte2 = InterfaceKit()
self.carte2.openPhidget(REPLACE_WITH_YOUR_BOARD3_SERIAL_NUMBER)
self.carte2.waitForAttach(10000)
def affiche_etat(self,titre):
print("%s\t[[%d, %d, %d, %d], [%d, %d, %d, %d], [%d, %d, %d, %d]]" % (
titre,
self.get_etat(0, 0), self.get_etat(0, 1), self.get_etat(0, 2), self.get_etat(0, 3),
self.get_etat(1, 0), self.get_etat(1, 1), self.get_etat(1, 2), self.get_etat(1, 3),
self.get_etat(2, 0), self.get_etat(2, 1), self.get_etat(2, 2), self.get_etat(2, 3)))
def get_etat(self,carte,port):
while True:
try:
if carte==0:
return 1 if self.carte0.getOutputState(port) else 0
elif carte == 1:
return 1 if self.carte1.getOutputState(port) else 0
elif carte == 2:
return 1 if self.carte2.getOutputState(port) else 0
else:
return
except:
print("Erreur.get_etat")
time.sleep(0.2)
def check_etat(self, etat_attendu,titre):
for carte in range(0,3):
for port in range(0,4):
if self.get_etat(carte, port)!=etat_attendu[carte][port]:
print("%s=\t%s" % (titre, etat_attendu))
self.affiche_etat("actuel=")
return
def set_etat(self,carte,port,etat):
b_etat = (etat==1)
while True:
try:
if carte==0:
self.carte0.setOutputState(port, b_etat)
elif carte == 1:
self.carte1.setOutputState(port, b_etat)
elif carte == 2:
self.carte2.setOutputState(port, b_etat)
else:
return
# le port ne passe pas immediatement a la valeur indiquee
# D'ou tempo + boucle de verification
time.sleep(0.1)
if self.get_etat(carte, port)==etat:
return
except:
print("Erreur.set_etat")
time.sleep(0.2)
gestion = GestionRelais()
mem_etat = [[gestion.get_etat(0, 0), gestion.get_etat(0, 1), gestion.get_etat(0, 2), gestion.get_etat(0, 3)],
[gestion.get_etat(1, 0), gestion.get_etat(1, 1), gestion.get_etat(1, 2), gestion.get_etat(1, 3)],
[gestion.get_etat(2, 0), gestion.get_etat(2, 1), gestion.get_etat(2, 2), gestion.get_etat(2, 3)]]
while True:
raz = random.randint(0,20)
if raz==0:
# print("RAZ")
mem_etat = [ [0,0,0,0], [0,0,0,0], [0,0,0,0] ]
for carte in range(0,3):
for port in range(0,4):
gestion.set_etat(carte,port,0)
gestion.check_etat(mem_etat, "err raz")
continue
elif raz==1:
# print("TOUT A 1")
mem_etat = [ [1,1,1,1], [1,1,1,1], [1,1,1,1] ]
for carte in range(0,3):
for port in range(0,4):
gestion.set_etat(carte,port,1)
gestion.check_etat(mem_etat, "err tout")
continue
carte = random.randint(0,2)
port = random.randint(0,3)
etat = random.randint(0,1)
tempo = float(random.randint(1,100))
if mem_etat[carte][port] == etat:
continue
# print("Changement etat carte=%d port=%d etat=%d" % (carte, port, etat))
# gestion.affiche_etat("etat avant")
mem_etat[carte][port] = etat
gestion.set_etat(carte,port,etat)
# gestion.affiche_etat("etat apres")
gestion.check_etat(mem_etat,"err avant")
time.sleep(tempo/10.0)
# gestion.check_etat(mem_etat,"err apres")
Code: Select all
def DigitalOutputDetached(self):
detached = self
try:
print("\nDetach event on Port %d Channel %d" % (detached.getHubPort(), detached.getChannel()))
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Press Enter to Exit...\n")
readin = sys.stdin.read(1)
exit(1)
Users browsing this forum: Ahrefs [Bot] and 0 guests