voici une class en python que j’ai réalisé et que je fait partager pour ceux que ça peut intéresser. Elle permet de contrôler simplement votre carte IPX800 en utilisant le langage évolué qu’est python.
Vous trouverai a la fin de la class un petit bout de code utilisant les méthodes disponible actuellement.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# IPX800.class.py
#
# Free Copyright 2011 Guillaume JONVILLE <guillaume.jonville<AT>gmail<DOT>com>
#
#
import socket
class IPX8000:
adresse_ip = None
port = None
def __init__(self, ip, port=9870):
self.adresse_ip = ip
self.port = port
def getIntEntryValue(self,inputNum):
try:
req = "GetIn" + str(inputNum) + "\r\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
s.connect((self.adresse_ip,self.port))
s.send(req)
data = ""
i = False
while 1:
txt = s.recv(1)
if ord(txt) == 10:
if i == False:
i=True
else:
break
data = data + txt
s.close()
return data[data.find(chr(10))+2:data.find(chr(10))+3]
except:
return False
def getAntEntryValue(self,inputNum):
try:
req = "GetAn" + str(inputNum) + "\r\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
s.connect((self.adresse_ip,self.port))
s.send(req)
data = ""
i = False
while 1:
txt = s.recv(1)
if ord(txt) == 10:
if i == False:
i=True
else:
break
data = data + txt
s.close()
return data[data.find(chr(10))+2:data.find(chr(10))+3]
except:
return False
def setRelayValue(self,rNumber,rValue):
try:
if rValue:
rValue="1"
else:
rValue="0"
req = "Set" + str(rNumber) + str(rValue) + "\r\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
s.connect((self.adresse_ip,self.port))
s.send(req)
data = ""
while 1:
txt = s.recv(1)
if ord(txt) == 10:
break
data = data + txt
s.close()
if data.find("Success") != -1:
return True
else:
return False
except:
return False
def reset(self):
try:
req = "reset\r\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
s.connect((self.adresse_ip,self.port))
s.send(req)
return True
except:
return False
def setAllRelay(self,boolValue):
try:
if boolValue:
req = "Bit=11111111\r\n"
else:
req = "Bit=00000000\r\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(3)
s.connect((self.adresse_ip,self.port))
s.send(req)
data = ""
while 1:
txt = s.recv(1)
if ord(txt) == 10:
break
data = data + txt
s.close()
if data.find("Success") != -1:
return True
else:
return False
except:
return False
def main():
myipx800 = IPX8000("10.50.40.155",10000)
####################################################################################
####################################################################################
####################################################################################
if myipx800.setAllRelay(True):
print "All relay are up!!!"
else:
print "Error, can't up all relay!!!"
####################################################################################
####################################################################################
####################################################################################
for i in range(4):
value = myipx800.getIntEntryValue(i+1)
if value != False:
print "Actualy, bool Input " + str(i+1) + " = " + str(value) + "!!!"
else:
print "Error, can't get bool input " + str(i+1) + " value!!!"
####################################################################################
####################################################################################
####################################################################################
for i in range(2):
value = myipx800.getAntEntryValue(i+1)
if value != False:
print "Actualy, Analog Input " + str(i+1) + " = " + str(value) + "!!!"
else:
print "Error, can't get Analog input " + str(i+1) + " value!!!"
####################################################################################
####################################################################################
####################################################################################
if myipx800.setAllRelay(False):
print "All relay are down!!!"
else:
print "Error!!!, can't down all relay!!!"
####################################################################################
####################################################################################
####################################################################################
for i in range(:sunglasses::
if myipx800.setRelayValue(i+1,True):
print "Relay " + str(i+1) + " is up!!!"
else:
print "Error!!!, can't up relay" + str(i+1) + "!!!"
####################################################################################
####################################################################################
####################################################################################
for i in range(:sunglasses::
if myipx800.setRelayValue(i+1,False):
print "Relay " + str(i+1) + " is down!!!"
else:
print "Error!!!, can't up relay" + str(i+1) + "!!!"
####################################################################################
####################################################################################
####################################################################################
if myipx800.reset():
print "Reset is ok!!!"
else:
print "Error!!!, can't make a reset!!!"
return 0
if __name__ == '__main__':
main()
en output on a :
All relay are up!!!
Actualy, bool Input 1 = 1!!!
Actualy, bool Input 2 = 1!!!
Actualy, bool Input 3 = 1!!!
Actualy, bool Input 4 = 1!!!
Actualy, Analog Input 1 = 0!!!
Actualy, Analog Input 2 = 0!!!
All relay are down!!!
Relay 1 is up!!!
Relay 2 is up!!!
Relay 3 is up!!!
Relay 4 is up!!!
Relay 5 is up!!!
Relay 6 is up!!!
Relay 7 is up!!!
Relay 8 is up!!!
Relay 1 is down!!!
Relay 2 is down!!!
Relay 3 is down!!!
Relay 4 is down!!!
Relay 5 is down!!!
Relay 6 is down!!!
Relay 7 is down!!!
Relay 8 is down!!!
Reset is ok!!!