def write_ptl(self, ptl_number, val=1): """ method:write_ptl, params:ptl_1,ptl_2, station """ db_number, start, bit = get_addr(self.ptl_address[ptl_number]) # db_number,start,bit,val res = self.plc_writer.write_bit(db_number, start, bit, val) return res
def write_complete(self, station, val=1): """ method:complate, parmas:ST10 """ db_number, start, bit = get_addr( self.st_done[self.station_2_st_done[station]]) res = self.plc_writer.write_bit(db_number, start, bit, val) return res
def test(self): val = 0 for ptl_name in self.ptl_address: db_number, start, bit = get_addr(self.ptl_address[ptl_name]) # db_number,start,bit,val self.plc_writer.write_bit(db_number, start, bit, val) time.sleep(0.1)
def write_rfid(self): sn = struct.pack('8s', 'mabotech'.encode('utf8')) db_number, start = get_addr(self.rfid_str['rfid_1']) print(db_number, start) result = self.checker_writer.write_rfid(db_number, start, sn) print(result) input() res = self.checker_writer.write_complete('ST10') print('write complete: ', res)
def read_rfid(self): # db_number, start = get_addr(self.rfid_str['rfid_6']) # print(db_number, start) # result = self.reader.read(db_number=db_number, start=start, size=8) # print(result) # read rfid_sn for item in self.rfid_str: db_number, start = get_addr(self.rfid_str[item]) print(db_number, start) result = self.reader.read(db_number=db_number, start=start, size=8) print(item, result)
def test_ptl_done(self): print('open ptl_1') self.checker_writer.write_ptl('ptl_1') print('oepned ptl_1 done') addr = self.test_addr2['ptl_1'] db_number, start, bit = get_addr(addr) while True: db_value = self.reader.read(540, 0, 300) print(db_number, start, bit) print('get ptl_done_1 status', end=": ") b = get_bit_value(db_value, start, bit) print(b) if b: print('True-------') self.checker_writer.write_ptl('ptl_1', val=0) time.sleep(0.5) db_value = self.reader.read(540, 0, 300) print('ptl_1_done', db_number, start, bit, end="===") b = get_bit_value(db_value, start, bit) print(b) break time.sleep(1) print('done!')
def run(self): # states = self.get_states() self.init() while self.running: db_value = None count = 0 while self.running: try: db_value = self.reader.read(540, 0, 1400) break except Exception as e: plc_logger.exception("plc read db_value error, wait 1s and go on!") count += 1 if count >= 5: plc_logger.error("read's count more than 5, the program will close, please check plc's network!") self.running = False return time.sleep(1) continue # 遍历灯的状态 for item in self.ptl.keys(): addr = self.ptl[item] db_number, start, bit = get_addr(addr) b = get_bit_value(db_value, start, bit) b = 1 if b else 0 if self.states[item] != b: self.states[item] = b data = '{"type": "state", "device": "%s", "value": %d, "station": "%s"}' % (item, b, self.device_2_station[item]) self.event_queue.put(data) # 遍历取料的状态 for item in self.ptl_done.keys(): addr = self.ptl_done[item] db_number, start, bit = get_addr(addr) b = get_bit_value(db_value, start, bit) if b: # 1. close ptl count = 0 while self.running: try: self.checker_writer.write_ptl(item, val=0) break except Exception as e: plc_logger.exception("close ptl error") count += 1 if count >= 5: self.running = False plc_logger.error("plc connection error, the program will close after 1s!") time.sleep(1) return time.sleep(0.1) # 遍历下一步按钮的状态 for item in self.next_cfm: addr = self.next_cfm[item] db_number, start, bit = get_addr(addr) b = get_bit_value(db_value, start, bit) b = 1 if b else 0 if self.states[item] != b: if not b: data = '{"type": "state", "device": "next", "value": 1, "station": "%s"}' % self.device_2_station[item] self.event_queue.put(data) plc_logger.info("pressed {}-next".format(item)) self.states[item] = b # 遍历相机按下状态 for item in self.camera: db_number, start, bit = get_addr(self.camera[item]) # print(db_number, start, bit) b = get_bit_value(db_value, start, bit) b = 1 if b else 0 if self.states[item] != b: if b: self.event_queue.put('{"type": "camera"}') plc_logger.info("pressed camera") else: count = 0 while self.running: try: self.checker_writer.write_camera_done(val=0) break except Exception as e: count += 1 plc_logger.exception("write camera done error!") if count >= 5: plc_logger.error("retry write camera done more than 5, the program wille close after 1s") self.running = False time.sleep(1) return time.sleep(0.1) self.states[item] = b time.sleep(0.1)
def write_done(self, val=0): db_number, start, bit = get_addr(self.camera_done['camera_done']) res = self.checker_writer.plc_writer.write_bit(db_number, start, bit, val) print(res)
def write_camera_done(self, val=0): db_number, start, bit = get_addr(self.camera_done['camera_done']) return self.plc_writer.write_bit(db_number, start, bit, val)