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
print c['port'] print c.__version__ if c.open('192.168.0.167', 3): try: # r_array = c.read_array("F20_CreamReceoption.Tickets",2) # print r_array # for tag in r_array: # print (tag) # x = c.read_tag(['Python_Teste_01']) # print x[0][1] # print(c.read_tag(['F20_CreamReceoption'])) # print(c.read_tag('Python_Teste_03')) # print(c.read_tag(['Python_Teste_04'])) print(c.read_string('F20_CreamReceoption.Tickets[0].StartTime')) # print(c.read_tag(['parts', 'ControlWord', 'Counts'])) # print(c.write_string('Python_Teste_06', 'Flex Automacao')) # print(c.write_tag(('TankSelectedToExpedition', 26, 'INT'))) # print(c.write_tag([('Counts', 26, 'INT')])) # print(c.write_tag([('Counts', -26, 'INT'), ('ControlWord', -30, 'DINT'), ('parts', 31, 'DINT')])) c.close() except Exception as e: c.close() print e pass # To read an array # r_array = c.read_array("F20_CreamReceoption", 1750) # for tag in r_array: # print (tag)