def genControlInstr(self, ID, instr, val): if instr not in self.SUPPORTED_INSTRUCTIONS.keys(): err = "There is not a {} in wenkong's SUPPORTED_INSTRUCTIONS".format(instr) raise Exception(err) data = [ID, 0x06, 0x00, self.SUPPORTED_INSTRUCTIONS[instr], 0x00, val] crc = crc16() return crc.createarray(data)
def __receiveData(self): data = "" while self.__alive: try: time.sleep(0.1) n = self.__com.inWaiting() if n: if self.__receiveFlag: # data = data + self.__com.read(n) data = self.__com.read(n) strdata = data.encode("hex") listdata = [] for i in range(0, len(strdata), 2): listdata.append(int(strdata[i : i + 2], 16)) if crc16().calcrc(listdata): # print self.__port ,"Received Right Data : " , listdata , "." SmartServer().addDataItem((self.__port, data)) if time.time() - self.__starttime < 0.5: self.__timer.cancel() self.__cyclelist_mutex.acquire() if self.__CycleCmd["nbcount"] > 9: self.__CycleCmd["nbcount"] = 0 self.__cycleList.append(self.__CycleCmd) self.__cyclelist_mutex.release() self.sendNext() else: print "Received Error Data : ", listdata, "." except Exception as e: print "__receiveData got an error : %s" % e
def infraredCmdPack(ID,GN,RA,RV): data = [ID,GN,0x00,0x00,0x00,0x07] if GN == 6 : data[3] = RA data[5] = RV crc = crc16() return HexToString(crc.createarray(data))
def genPratrolInstr(self, ID): instr = [] for val in self.SUPPORTED_INSTRUCTIONS.values(): if val[1] > 0 : data = [ID,val,0x00,0x00,0x00,val[1]] crc = crc16() instr.append(crc.createarray(data)) return copy.deepcopy(instr)
def genPratrolInstr(self, ID): instr = [] addrs = {5: 44310, 4: 44320, 7: 44330, 25: 44340, 1: 44370, 6: 49001, 16: 28401, 15: 27901, 2: 28901} for addr in addrs.items(): addr1 = addr[1] >> 8 addr2 = addr[1] & 0xFF data = [ID, 0x03, addr1, addr2, 0x00, addr[0]] crc = crc16() instr.append(crc.createarray(data)) return copy.deepcopy(instr)
def genPratrolInstr(self, ID): arr = [] crc = crc16() data = [ID,0x03,0x00,0x14,0x00,0x03] arr.append(crc.createarray(data)) data = [ID,0x03,0x00,0x17,0x00,0x1A] arr.append(crc.createarray(data)) data = [ID,0x03,0x00,0x3F,0x00,0x12] arr.append(crc.createarray(data)) data = [ID,0x03,0x00,0x57,0x00,0x02] arr.append(crc.createarray(data)) # data = [ID,0x06,0x00,0x16,0x00,0x02] # arr.append(crc.createarray(data)) return arr
def tripleCgn(ID,type_id): data = [ID,0x03,0x00,0x00,0x00,type_id] addr = {5:44310, 4:44320, 7:44330, 25:44340, 1:44370, 6:49001, 16:28401, 15:27901, 2:28901} data[2] = addr.get(type_id)>>8; data[3] = addr.get(type_id)&0xff crc = crc16() return HexToString(crc.createarray(data))
def genControlInstr(self, ID, instr, val): if instr not in self.SUPPORTED_INSTRUCTIONS.keys(): err = "There is not a {} in triplecng's SUPPORTED_INSTRUCTIONS".format(instr) raise Exception(err) Addr = self.SUPPORTED_INSTRUCTIONS[instr][0] valtype = self.SUPPORTED_INSTRUCTIONS[instr][1] val1 = None val2 = None if valtype != 1 : val1 = val >> 8 val2 = val - val1 * 256 else : val1 = (val * 10 + 65536) >> 8 val2 = (val * 10 + 65536) & 0xff data = [ID, 0x10, Addr >> 8, Addr & 0xff, 0x00, 0x01, 0x02, val1, val2] crc = crc16() return [crc.createarray(data)]
def genControlInstr(self, ID, instr, io_port ,val): if instr not in ('DO', 'AO'): err = "There is not a {} in mokuai's SUPPORTED_INSTRUCTIONS".format(instr) raise Exception(err) if io_port < 1 or io_port > self.SUPPORTED_INSTRUCTIONS[instr][1] : err = "There is not a {} in mokuai's IO_PORT".format(instr) raise Exception(err) data = [ID,self.SUPPORTED_INSTRUCTIONS[instr][0],0x00,io_port,0x00,0x00] if instr == 'DO': if val == 1: data[4] = 0xff elif instr == 'AO': data[2] = (40001+io_port) >> 8 data[3] = (40001+io_port) - data[2]*256 data[4] = val >> 8 data[5] = val & 0xff crc = crc16() return crc.createarray(data)
def __processData(self,data): arr = [ord(x) for x in data] pos = 0 total_len = len(arr) rest_data = None while(pos < total_len): item_len = arr[pos+1] if pos + item_len > total_len: rest_arr = arr[pos:] rest_data = [chr(x) for x in rest_arr] break; else: item_data = arr[pos:pos+item_len] if crc16().calcrc(item_data): SmartServer().addProcessData((self.__port,item_data)) else: print "Received Error Data : ",item_data, "." pos = pos + item_len return rest_data
def genPratrolInstr(self, ID): instr = [] addrs = { 5:44310, 4:44320, 7:44330, 25:44340, 1:44370, 6:49001, 16:28401, 15:27901, 2:28901, } for addr in addrs.items(): addr1 = addr[1] >> 8; addr2 = addr[1] & 0xff data = [ID,0x03,addr1,addr2,0x00,addr[0]] if addr[0] == 15 or addr[0] == 16 or addr[0] == 2 : data[1] = 0x04 crc = crc16() instr.append(crc.createarray(data)) import copy return copy.deepcopy(instr)
def genPratrolInstr(self, ID): data = [ID,0x04,0x00,0x00,0x00,0x01] crc = crc16() return [crc.createarray(data)]
check_sum += data check_sum &= 0xff array.append(check_sum) return array def HexToString(array): snd = '' for i in array: snd += '%02x' % i return snd def plc_mokuaiCmdPack(ID, (PortType, portAmount)): data = [ID,PortType,0x00,0x00,0x00,portAmount] if PortType == 3 : data[3] = 42 crc = crc16() return HexToString(crc.createarray(data)) def infraredCmdPack(ID,GN,RA,RV): data = [ID,GN,0x00,0x00,0x00,0x07] if GN == 6 : data[3] = RA data[5] = RV crc = crc16() return HexToString(crc.createarray(data)) def infraredCmdPack1(ID): data = [0x99,ID,0x00,0xff,0xff] return HexToString(checkSum(data)) def sansuCmdPack1(ID):
def sansuCmdPack1(ID): data = [ID,0x03,0x00,0x64,0x00,0x03] crc = crc16() return HexToString(crc.createarray(data))
def wenkongCmdPack1(ID): data = [ID,0x07,0x81,0x00,0x01,0xff] crc = crc16() return HexToString(crc.createarray(data))
def vocCmdPack1(ID): data = [ID,0x04,0x00,0x00,0x00,0x06] crc = crc16() return HexToString(crc.createarray(data))
com = Serial('/dev/ttyUSB0',9600) import voc import co2 import infrared import sansu import ZMA194E import plc import mokuai import PFP6G # # com.open() import time # 3DA031B4 data = [0x0D,0x03,0x00,0x23,0x00,0x24] from crc_check import crc16 senddata = crc16().createarray(data) from PFP6G import PFP6G pfp6g = PFP6G() from baseData import devBaseData from baseData import dataConstraint DO1 = devBaseData('DO1',dataConstraint(1,1.0, None, None)) pfp6g.addDataItem('DO1', DO1) DO2 = devBaseData('DO2',dataConstraint(1,1.0, None, None)) pfp6g.addDataItem('DO2', DO2) DO3 = devBaseData('DO3',dataConstraint(1,1.0, None, None)) pfp6g.addDataItem('DO3', DO3) DO4 = devBaseData('DO4',dataConstraint(1,1.0, None, None)) pfp6g.addDataItem('DO4', DO4) DI1 = devBaseData('DI1',dataConstraint(1,1.0, None, None))