c = ClxDriver() print c['port'] print c.__version__ if c.open('172.16.2.161'): while 1: try: print(c.read_tag(['ControlWord'])) print(c.read_tag(['parts', 'ControlWord', 'Counts'])) print(c.write_tag('Counts', -26, 'INT')) print(c.write_tag(('Counts', 26, 'INT'))) print(c.write_tag([('Counts', 26, 'INT')])) print( c.write_tag([('Counts', -26, 'INT'), ('ControlWord', -30, 'DINT'), ('parts', 31, 'DINT')])) sleep(1) except Exception as e: c.close() print e pass # To read an array r_array = c.read_array("TotalCount", 1750) for tag in r_array: print(tag) c.close()
class EIPFunctions: def __init__(self): # initialize a thread safe queue # load queue with a single plc obj self.plc = ClxDriver() self.mutex = Lock() def is_connected(self): with self.mutex: return self.plc.is_connected() def connect_plc(self, ip): with self.mutex: self.plc.open(ip) return self.plc.is_connected() def disconnect_plc(self): with self.mutex: if self.plc.is_connected(): self.plc.close() def read_tag(self, tag_name): with self.mutex: return self.plc.read_tag(tag_name)[0] def write_tag(self, tag_name, tag_value, tag_type): """ :param tag_name: tag name, or an array of tuple containing (tag name, value, data type) :param tag_value: the value to write or none if tag is an array of tuple or a tuple :param tag_type: the type of the tag to write or none if tag is an array of tuple or a tuple :return: None is returned in case of error otherwise the tag list is returned The type accepted are: - BOOL - SINT - INT' - DINT - REAL - LINT - BYTE - WORD - DWORD - LWORD """ with self.mutex: return self.plc.write_tag(tag_name, tag_value, tag_type) def read_tag_string(self, tag_name): with self.mutex: return self.plc.read_string(tag_name) def write_tag_string(self, tag_name, tag_value): with self.mutex: return self.plc.write_string(tag_name, tag_value) def read_tag_array(self, tag_name, count): with self.mutex: return self.plc.read_array(tag_name, count) def write_tag_array(self, tag_name, tag_array, tag_type): with self.mutex: self.plc.write_array(tag_name, tag_array, tag_type) # always return true; pycomm is not telling us if we succeed or not return True
from pycomm.ab_comm.clx import Driver as ClxDriver if __name__ == '__main__': c = ClxDriver(True, 'ClxDriver.log') if c.open('172.16.2.161'): print(c.read_tag(['ControlWord'])) print(c.read_tag(['parts', 'ControlWord', 'Counts'])) print(c.write_tag('Counts', -26, 'INT')) print(c.write_tag(('Counts', 26, 'INT'))) print(c.write_tag([('Counts', 26, 'INT')])) print(c.write_tag([('Counts', -26, 'INT'), ('ControlWord', -30, 'DINT'), ('parts', 31, 'DINT')])) # To read an array r_array = c.read_array("TotalCount", 1750) for tag in r_array: print (tag) # reset tha array to all 0 w_array = [] for i in xrange(1750): w_array.append(0) c.write_array("TotalCount", "SINT", w_array) c.close()