Example #1
0
    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 (ord(data[0]) == 0x99) :
#                             print self.__port ,"Received Right Data 0x99: " , listdata , "."
                        if (ord(data[0]) == 0x99 and Sum_Right(data)) or crc16().calcrc(listdata):
#                         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
Example #2
0
    def _dataProcess(self, data):
        strdata = data.encode("hex")
        listdata = []
        for i in range(0, len(strdata), 2):
            listdata.append(int(strdata[i : i + 2], 16))
        #         import os
        #         print os.getpid(), "_dataProcess : ", listdata
        dev_id = 0
        if (ord(data[0]) == 0x99 and Sum_Right(data)) or crc16().calcrc(listdata):
            if ord(data[0]) == 0x99:
                dev_id = ord(data[1])
            else:
                dev_id = ord(data[0])
            # 数据处理
            self._device_set.ParseData(dev_id, listdata)
            #             for key,value in self._device_set.getDeviceSet()[dev_id].getDataDict().items():
            #                 print '_dataProcess : ', key,value
            #             import test
            #             self._resultQueue.put_nowait({'handle' : test.doDataProcessing, 'data' : listdata})

            #             self._handleData(listdata)
            if self._cmd is not None and self._cmd["dev_id"] == dev_id:
                self._cycleList.append(self._cmd)
                # 设备上线
                if self._device_set.DisConnected(dev_id):
                    self._device_set.setDisConnect(dev_id, 0)