def SetRelay(self, relay_status): status = List2Int(relay_status) cmd = [self.addr, 0x0F, 0x00, 0x03, 0x00, 0x03, 0x01, status] ListAddCrc(cmd) self.write(cmd) sleep(0.05) ret = self.read(8) if not ListCheckCrc(ret): raise TException('SetRelay Checksum Error! Response:%s' % List2Str(ret)) ret_check = [self.addr, 0x0f, 0, 3, 0, 3] ListAddCrc(ret_check) if ret != ret_check: raise TException('SetRelay Response Error! Response:%s' % List2Str(ret)) pass
def GetId(self): cmd = [self.addr, 3, 0x80, 0, 0, 6] ListAddCrc(cmd) self.write(cmd) sleep(0.05) ret = self.read(17) if not ListCheckCrc(ret): raise TException('Id Checksum Error! ID:%s' % List2Str(ret)) return ret[3:15] pass
def SetNetworkMode(self, is_broadcast=False, is_local=True): send_addr = 0 # 0是组播,非组播是地址值 network_mode = 1 # 1是非本地模式,0是本地模式 if not is_broadcast: send_addr = self.addr if is_local: network_mode = 0 cmd = [send_addr, 6, 0xA0, 2, 0, network_mode] ListAddCrc(cmd) print List2Str(cmd) self.write(cmd) if not is_broadcast: ret = self.read(8) print List2Str(ret) pass
def SetSwitchMode(self, is_broadcast=False, is_hold=True): send_addr = 0 # 0是组播,非组播是地址值 hold_mode = 0 # 0是短触,1是长触 if not is_broadcast: send_addr = self.addr if is_hold: hold_mode = 1 cmd = [send_addr, 6, 0xA0, 4, 0, hold_mode] ListAddCrc(cmd) print List2Str(cmd) self.write(cmd) if not is_broadcast: ret = self.read(8) print List2Str(ret) pass
def GetStatus(self): cmd = [self.addr, 2, 0, 0, 0, 6] cmd = ListAddCrc(cmd) self.write(cmd) sleep(0.05) ret = self.read(6) if not ListCheckCrc(ret): raise TException('GetStatus Checksum Error! ID:%s' % List2Str(ret)) else: status = Int2Listbin(ret[3]) status.reverse() self.switch_status = status[0:3] self.relay_status = status[3:6] self.switch_mode = status[6] self.network_mode = status[7]
def SetSerialConfig(self, baudrate="9600", data_number=8, check_number='N', stop_number=1): temp_baudrate = BAUD_LIST.index(baudrate) + 1 temp_bytesize = BYTE_LIST.index(data_number) + 1 temp_parity = PARITY_LIST.index(check_number) + 1 temp_stop = STOP_LIST.index(stop_number) + 1 print temp_baudrate print temp_bytesize print temp_parity print temp_stop if temp_baudrate != self.baudrate or \ temp_bytesize != self.bytesize or \ temp_parity != self.parity or \ temp_stop != self.stop: # 设置0xA000寄存器 _A000_value = temp_baudrate << 4 _A000_value += temp_bytesize print "A000 register value is: 0x%x" % _A000_value # 设置0xA001寄存器 _A001_value = temp_parity << 4 _A001_value += temp_stop print "A001 register value is: 0x%x" % _A001_value cmd = [0, 6, 0xA0, 0, _A000_value, _A001_value] ListAddCrc(cmd) print List2Str(cmd) self.write(cmd) cf.set("serial", "baudrate", temp_baudrate) cf.set("serial", "bytesize", temp_bytesize) cf.set("serial", "parity", temp_parity) cf.set("serial", "stop", temp_stop) cf.write(open("./conf.ini", "w")) else: print "same config." pass
print List2Str(cmd) self.write(cmd) if not is_broadcast: ret = self.read(8) print List2Str(ret) pass pass ics = ICS20("COM6", 1) try: ics.Init() print "id:" print List2Str(ics.id) # print "network mode:" # print ics.network_mode # print "switch mode:" # print ics.switch_mode # print "relay status:" # print ics.relay_status # print "switch status:" # print ics.switch_status # print "init, relay status:" # print ics.GetRelayStatus() # ics.SetRelay([1, 0, 0]) # sleep(0.3) # ics.SetRelay([1, 1, 0]) # sleep(0.3)