Exemplo n.º 1
0
class BuildPacket(object):
    synchronization = 'FA'
    adress = 'AF'
    typeConvertor = typeConvertor.TypeConvertor()
    crcCalculator = crc.CrcCalculator()

    def __init__(self, comand, parameters=None):
        # print command
        # print parameters
        self.comand = self.typeConvertor.IntToHex(comand)
        self.parameters = self.GetParametersInHexForm(comand, parameters)
        self.packetLenght = self.typeConvertor.IntToHex(6 +
                                                        len(self.parameters) /
                                                        2)
        self.crc = self.CalculateCrc()
        self.bytearray = self.GetBytePacket()

    def CalculateCrc(self):
        concatinatedString = self.synchronization + self.adress + self.packetLenght + self.comand + self.parameters
        byteString = bytearray.fromhex(concatinatedString)
        return self.crcCalculator.GetTwoBytesFromCrc32(byteString)

    def GetBytePacket(self):
        concatinatedString = self.synchronization + self.adress + self.packetLenght + self.comand + self.parameters + self.crc
        return bytearray.fromhex(concatinatedString)

    def GetParametersInHexForm(self, comand, parameters):
        if comand == CommandsList.echo:
            return self.GetByteStringFormString(parameters)
        elif comand == CommandsList.setCoordinates or comand == CommandsList.setCorectCoordinates:
            return self.GetByteStringFromFloatList(parameters)
        elif comand == CommandsList.dutyCycle:
            return self.typeConvertor.IntToHex(
                parameters[0]) + self.typeConvertor.FloatToHex(parameters[1])
        elif comand == CommandsList.setDirectionBit or comand == CommandsList.removeDirectionBit:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.setMotorVoltage:
            return self.typeConvertor.IntToHex(
                parameters[0]) + self.typeConvertor.FloatToHex(parameters[1])
        elif comand == CommandsList.setPidParameters:
            return self.GetByteStringFromFloatList(parameters)
        elif comand == CommandsList.setRotatoinSpeed:
            return self.GetByteStringFromFloatList(parameters)
        elif comand == CommandsList.setMovementSpeed:
            return self.typeConvertor.FloatToHex(
                parameters[0]) + self.typeConvertor.FloatToHex(
                    parameters[1]) + self.typeConvertor.FloatToHex(
                        parameters[2])
        elif comand == CommandsList.addPointToStack:
            return self.typeConvertor.FloatToHex(
                parameters[0]) + self.typeConvertor.FloatToHex(
                    parameters[1]) + self.typeConvertor.FloatToHex(
                        parameters[2]) + self.typeConvertor.IntToHex(
                            parameters[3])
        elif comand == CommandsList.setMovementParameters:
            return self.typeConvertor.FloatToHex(
                parameters[0]) + self.typeConvertor.FloatToHex(
                    parameters[1]) + self.typeConvertor.FloatToHex(
                        parameters[2]) + self.typeConvertor.FloatToHex(
                            parameters[3]) + self.typeConvertor.FloatToHex(
                                parameters[4])
        elif comand == CommandsList.setADCPinMode:
            return self.typeConvertor.IntToHex(
                parameters[0]) + self.typeConvertor.IntToHex(parameters[1])
        elif comand == CommandsList.getADCPinState:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.getDigitalPinState:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.setOutputState:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.getPinMode:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.setPinModeExit:
            return self.typeConvertor.IntToHex(
                parameters[0]) + self.typeConvertor.IntToHex(parameters[1])
        elif comand == CommandsList.getDiscretePinState:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.setDiscreteOutputState:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.determineCurrentPinMode:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.set12VState:
            return self.typeConvertor.IntToHex(parameters)
        elif comand == CommandsList.stopAllMotors:
            return self.typeConvertor.FloatToHex(
                parameters[0]) + self.typeConvertor.FloatToHex(
                    parameters[1]) + self.typeConvertor.FloatToHex(
                        parameters[2]) + self.typeConvertor.IntToHex(
                            parameters[3])
        elif comand == CommandsList.moveWithCorrection:
            return self.typeConvertor.FloatToHex(
                parameters[0]) + self.typeConvertor.FloatToHex(
                    parameters[1]) + self.typeConvertor.FloatToHex(
                        parameters[2]) + self.typeConvertor.FloatToHex(
                            parameters[3]) + self.typeConvertor.FloatToHex(
                                parameters[4]) + self.typeConvertor.FloatToHex(
                                    parameters[5]
                                ) + self.typeConvertor.IntToHex(parameters[6])
        return ""

    def GetByteStringFormString(self, parameters):
        outputString = ""
        for symbol in parameters:
            outputString = outputString + self.typeConvertor.IntToHex(
                ord(symbol))
        return outputString

    def GetByteStringFromIntList(self, parameters):
        outputString = ""
        for parameter in parameters:
            if type(parameter) is not int:
                raise Exception('Parameter should be integer number.')
            outputString = outputString + self.typeConvertor.IntToHex(
                parameter)
        return outputString

    def GetByteStringFromFloatList(self, parameters):
        outputString = ""
        for parameter in parameters:
            outputString = outputString + self.typeConvertor.FloatToHex(
                parameter)
        return outputString
Exemplo n.º 2
0
class ParsePacket(object):

    synchronization = 'FA'
    adress = 'FA'
    typeConvertor = typeConvertor.TypeConvertor()
    crcCalculator = crc.CrcCalculator()
    listOfComands = packetBuilder.CommandsList()

    def __init__(self, byteString):
        self.string = byteString
        self.byteArray = self.GetByteArrayFromRecievedString()
        self.recievedSynchronizationByte = self.byteArray[0]
        self.recievedAdress = self.byteArray[1]
        self.packetLenght = self.byteArray[2]
        self.command = self.byteArray[3]
        self.reply = self.GetReply()
        self.recievedcrc = self.typeConvertor.IntToHex(
            self.byteArray[len(self.byteArray) -
                           2]) + self.typeConvertor.IntToHex(
                               self.byteArray[len(self.byteArray) - 1])

        self.CheckCrc()

    def GetReply(self):
        if self.listOfComands.getCurentCoordinates == self.command or self.listOfComands.getCurrentSpeed == self.command:
            recievedPametersString = binascii.hexlify(
                self.byteArray[4:len(self.byteArray) - 2])
            invertedParametersString = self.typeConvertor.InvertStringArray(
                recievedPametersString)
            coordinatesList = []
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[16:24]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[8:16]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[0:8]))
            return coordinatesList

        if self.listOfComands.getDataIKSensors == self.command:
            recievedPametersString = binascii.hexlify(
                self.byteArray[4:len(self.byteArray) - 2])
            invertedParametersString = self.typeConvertor.InvertStringArray(
                recievedPametersString)
            coordinatesList = []
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[24:32]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[16:24]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[8:16]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[0:8]))
            return coordinatesList

        if self.listOfComands.getDataUSSensors == self.command:
            recievedPametersString = binascii.hexlify(
                self.byteArray[4:len(self.byteArray) - 2])
            invertedParametersString = self.typeConvertor.InvertStringArray(
                recievedPametersString)
            coordinatesList = []
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[32:40]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[24:32]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[16:24]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[8:16]))
            coordinatesList.append(
                self.typeConvertor.HexToFloat(invertedParametersString[0:8]))
            return coordinatesList

        if self.listOfComands.closeCubeCollector == self.command or self.listOfComands.getADCPinState == self.command:
            recievedPametersString = binascii.hexlify(
                self.byteArray[4:len(self.byteArray) - 2])
            invertedParametersString = self.typeConvertor.InvertStringArray(
                recievedPametersString)
            # check int 16 or int 8
            return int(invertedParametersString, 16)

        if self.listOfComands.isPointWasReached == self.command:
            recievedPametersString = binascii.hexlify(
                self.byteArray[4:len(self.byteArray) - 2])
            invertedParametersString = self.typeConvertor.InvertStringArray(
                recievedPametersString)
            return int(invertedParametersString, 16)

        if self.listOfComands.getManipulatorAngle == self.command:
            recievedPametersString = binascii.hexlify(
                self.byteArray[4:len(self.byteArray) - 2])
            invertedParametersString = self.typeConvertor.InvertStringArray(
                recievedPametersString)
            return self.typeConvertor.HexToFloat(invertedParametersString)

        return self.byteArray[4:len(self.byteArray) - 2][:-1]

    def CheckCrc(self):
        calculatedCrc = self.crcCalculator.GetTwoBytesFromCrc32(
            self.byteArray[0:len(self.byteArray) - 2])
        if calculatedCrc != self.recievedcrc:
            raise Exception('Expected crc: ' + self.recievedcrc +
                            ', but calculated: ' + calculatedCrc)

    def GetByteArrayFromRecievedString(self):
        byteString = ""
        for index in range(0, len(self.string)):
            hexSymbol = self.typeConvertor.IntToHex(
                ord(str(self.string[index])))
            #print str(index) + ': ' + hexSymbol
            byteString = byteString + hexSymbol
        byteString = bytearray.fromhex(byteString)
        #print 'end'
        return byteString