def test(com, times, logfile): log("{} - test started.".format(nowtimestr()), "w") count = 0 plc.write_coil(1, 0xfc00, 1) set_port(comid, 115200, 8, 1, 'N') while count <= times: count += 1 x = isonline(com, True) if x is 1: logline = "{} - hmi online success. count = {}".format( nowtimestr(), count) log(logfile, logline) plc.write_coil(1, 0xfc00, 0) print("{} - hmi powered off".format(nowtimestr())) if x is 0: log("{} hmi online err.".format(nowtimestr())) break x = isonline(com, False) if x is 2: logline = "{} - hmi offline success. count = {}".format( nowtimestr(), count) log(logline) plc.write_coil(1, 0xfc00, 1) print("{} - hmi powered on".format(nowtimestr())) if x is 0: log("{} hmi offline err.".format(nowtimestr())) break log("{} - test finished.".format(nowtimestr()))
def isonline(com: str, flag: bool): test_count = 0 while test_count <= 5000: test_count += 1 try: hmiport = set_port(com, 115200, 8, 1, 'N') s = hmiport.readline() if s: if b'ngrok-----' in s: print(s) l = s.strip().split(b" ") if len(l) == 3: if l[1] == b' ngrok-----FD_ISSET( it1->first, &readSet )&&tempinfo->isconnect==1----' \ b'tempinfo->istype=3': if flag: loginfo = l[0][-12:-1].decode( ) + " hmi online = True" log(loginfo) return 1 if l[1] == b' ngrok-----err------------socklist.count(mainsock)!=0&&mainsock!=0': if not flag: loginfo = l[0][-12:-1].decode( ) + " hmi online = False" log(loginfo) return 2 except Exception as e: print("{} - read hmi port fail. testing stopped: {}".format( nowtimestr(), e)) break return 0
time.sleep(2) set_power(box_y[m], 1) # 上电 box_powerstate[m] = 1 # 记录上电状态 box_record[m]["powstamp"] = nowtimestamp() # 上电时间 time.sleep(0.1) def write_json(dic: dict): if dic: with open("record.json", 'w', encoding='utf-8') as f: f.write(json.dumps(dic, ensure_ascii=False, indent=4)) return True if __name__ == "__main__": port = set_port("com7", 9600, 8, 1, "N") master = ModbusRtu(port) for y in ['m' + str(i) for i in range(5)]: set_power(y, 0) time.sleep(10) machinecodes = [ 'V020011811156605a9e4f5f659b', 'V02001181115661a675e8634171', 'V02001181115662a2d12c524140', 'V020011811156635a9e4f5f58d3', 'V02001181115664a2d12c0e20a2' ] box_y, box_acts, box_powerstate, box_record = {}, {}, {}, {} for m in machinecodes: box_y[m] = "m" + m[14] box_acts[m] = "None" box_powerstate[m] = 0 box_record[m] = {
client.connect(HOST, PORT, 125) client.loop_forever() return client def close_connect(mqtt_client: mqtt.Client): mqtt_client.disconnect() def offline_report(machinecode): print("v-box with machine code[{}] is offline!".format(machinecode)) if __name__ == "__main__": global box_acts port = set_port("com12", 9600, 7, 1, "E") machinecodes = [ 'V020011811156605a9e4f5f659b', 'V02001181115661a675e8634171', 'V02001181115662a2d12c524140', 'V020011811156635a9e4f5f58d3', 'V02001181115664a2d12c0e20a2' ] box_powerset = dict([(m, "y" + m[14]) for m in machinecodes]) box_acts = dict([(m, "None") for m in machinecodes]) topic = [("pibox/cts/" + machinecode, 0) for machinecode in machinecodes] timeout = 60 thread = threading._start_new_thread(mqtt_go, ()) i, j, z = 0, 0, 0 while i < 10000: i += 1 for m in machinecodes: set_power(box_powerset[m], 0)
#!/usr/bin/python # -*- coding:utf-8 -*- # ----------------------------------------------------------- # File Name: hmi_auto_restart_via_plc # Author: fan # date: 2019/1/5 # 通过hmi串口是否有数据发出,判断hmi是否启动正常,若启动正常则下电hmi延时并重启hmi,继续测试;若hmi未启动则停止测试并报告问题 # ----------------------------------------------------------- from modbus_rtu_master import ModbusRtu import time from make_time_formated import nowtimestr from watchcom import set_port, open_port, close_port, read_correct print("{} - test started.".format(nowtimestr())) hmiport = set_port('com9', 115200, 8, 1, 'N') plcportporp = ("com7", 9600, 8, 1, "N") plc = ModbusRtu(plcportporp) plc.write_coil(1, 0xfc00, 1) print("{} - hmi powered on".format(nowtimestr())) readbytes = b'\x0201440AM008253\x03' or b'\x02014603R0519081\x03' i = 0 while i <= 20000: i += 1 x = 0 try: while x < 30: try: x = read_correct(hmiport, readbytes) if x: print("{} - read hmi port success. test times: {}".format(nowtimestr(), i)) x = 30 # 跳出循环
def __init__(self, portprop): self._port = set_port(*portprop) # "COMx", 9600, 8, 1, "N" self.master = modbus_rtu.RtuMaster(self._port) self.master.set_timeout(5.0) self.master.set_verbose(True) self.slave_stations = list(range(1, 255))