RiCHeader.py
Go to the documentation of this file.
00001 __author__ = 'tom1231'
00002 
00003 import struct
00004 import binascii
00005 
00006 ID_PLACE = 1
00007 LENGTH_PLACE = 2
00008 DES_PLACE = 4
00009 CHECK_SUM_PLACE = 6
00010 
00011 # lst = ['\x01', '\x00', '\n', '\x00', '\x01', '\x10', '\x00', '\x00', '\x01', '\x00']
00012 
00013 
00014 class RiCHeader:
00015     def __init__(self):
00016         self.index = 0
00017         self._id = 0
00018         self._length = 0
00019         self._des = 0
00020         self._checkSum = 0
00021 
00022     def buildRequest(self, data):
00023         bytes = bytearray()
00024         while self.index < ID_PLACE:
00025             bytes.append(data[self.index])
00026             self.index += 1
00027         self._id = struct.unpack('<B', bytes)[0]
00028         bytes = bytearray()
00029         while self.index < LENGTH_PLACE:
00030             bytes.append(data[self.index])
00031             self.index += 1
00032         self._length = struct.unpack('<B', bytes)[0]
00033         bytes = bytearray()
00034         while self.index < DES_PLACE:
00035             bytes.append(data[self.index])
00036             self.index += 1
00037         self._des = struct.unpack('<H', bytes)[0]
00038         bytes = bytearray()
00039         while self.index < CHECK_SUM_PLACE:
00040             bytes.append(data[self.index])
00041             data[self.index] = '\x00'
00042             self.index += 1
00043         self._checkSum = struct.unpack('<H', bytes)[0]
00044         self.checkSumRes = self.calCheckSum(data)
00045 
00046     def getId(self):
00047         return self._id
00048 
00049     def getDes(self):
00050         return self._des
00051 
00052     def getLength(self):
00053         return self._length
00054 
00055     def getCheckSum(self):
00056         return self._checkSum
00057 
00058     def checkPackage(self):
00059         return self.checkSumRes == self._checkSum
00060 
00061     def calCheckSum(self, data):
00062         res = 0
00063         for val in data:
00064             res += ord(val)
00065         return res
00066 
00067     def dataTosend(self):
00068         return struct.pack('<B', self._id) \
00069                + struct.pack('<B', self._length) \
00070                + struct.pack('<H', self._des) \
00071                + struct.pack('<H', self._checkSum)


ric_board
Author(s): RoboTiCan
autogenerated on Fri Oct 27 2017 03:02:31