大小端数据解析

#include "can_parse.h"

#define SIGNAL_INTER_ORDER            (1)
#define SIGNAL_MOTOROLA_ORDER         (0)

#define BYTE_LENGTH                   (8)
#define SIGNAL_FILTER_MASK_SHIFT_STEP (1)

static uint8_t ComGetSignalFilterMask(uint8_t shiftNum)
{
    uint8_t i = 0;
    uint8_t sigFilterMask = 0;

    for (i = 0; i < shiftNum; i++) {
        sigFilterMask = (sigFilterMask << SIGNAL_FILTER_MASK_SHIFT_STEP) | (uint8_t) true;
    }

    return sigFilterMask;
}

static uint64_t ComMsgSignalInterAnalysis(const uint8_t* buffer, uint8_t signalStartBit, uint8_t signalLen)
{
    if (!buffer)
        return 0;

    uint8_t i = 0;
    uint64_t retSigVal = 0;
    Com_SignalInfoType_t sigInfo = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    signalStartBit = (uint8_t)(signalStartBit + signalLen - 1u);
    sigInfo.sigBytePos = (uint8_t)(signalStartBit / (uint8_t)BYTE_LENGTH);
    sigInfo.sigBitPos = (uint8_t)(signalStartBit % (uint8_t)BYTE_LENGTH);
    sigInfo.sig1stByteBitLen = (uint8_t)(sigInfo.sigBitPos + 1u);

    if (sigInfo.sig1stByteBitLen >= signalLen) {
        sigInfo.sigFilterMask = ComGetSignalFilterMask(sigInfo.sig1stByteBitLen);
        retSigVal = ((uint64_t)((uint64_t)sigInfo.sigFilterMask & (uint64_t)buffer[sigInfo.sigBytePos])
                     >> (sigInfo.sig1stByteBitLen - signalLen));
    } else {
        sigInfo.sigRestLen = (signalLen - sigInfo.sig1stByteBitLen);
        sigInfo.sigFilterMask = ComGetSignalFilterMask(sigInfo.sig1stByteBitLen);
        sigInfo.sigSwapNum++;

        sigInfo.sigRestByteNum = (uint8_t)(sigInfo.sigRestLen / (uint8_t)BYTE_LENGTH);
        sigInfo.sigRestBitNum = (uint8_t)(sigInfo.sigRestLen % (uint8_t)BYTE_LENGTH);
        sigInfo.sigSwapNum += sigInfo.sigRestByteNum;
        if ((sigInfo.sigRestBitNum > 0u) && (sigInfo.sigRestBitNum < (uint8_t)BYTE_LENGTH)) {
            sigInfo.sigSwapNum++;
            sigInfo.sigRightShiftNum = (uint8_t)((uint8_t)BYTE_LENGTH - sigInfo.sigRestBitNum);
        }

        retSigVal = ((uint64_t)buffer[sigInfo.sigBytePos] & (uint64_t)sigInfo.sigFilterMask);
        for (i = 1; i < sigInfo.sigSwapNum; i++) {
            retSigVal = retSigVal << BYTE_LENGTH;
            retSigVal = (retSigVal | buffer[sigInfo.sigBytePos - i]);
        }

        retSigVal = (uint64_t)(retSigVal >> sigInfo.sigRightShiftNum);
    }

    return retSigVal;
}

static uint64_t ComMsgSignalMotorolaAnalysis(const uint8_t* buffer, uint8_t signalStartBit, uint8_t signalLen)
{
    if (!buffer)
        return 0;

    uint8_t i = 0;
    uint64_t retSigVal = 0;
    Com_SignalInfoType_t sigInfo = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    sigInfo.sigBytePos = (uint8_t)signalStartBit / (uint8_t)BYTE_LENGTH;
    sigInfo.sigBitPos = (uint8_t)signalStartBit % (uint8_t)BYTE_LENGTH;
    sigInfo.sig1stByteBitLen = (uint8_t)(sigInfo.sigBitPos + 1u);

    if (sigInfo.sig1stByteBitLen >= signalLen) {
        sigInfo.sigFilterMask = ComGetSignalFilterMask(sigInfo.sig1stByteBitLen);
        retSigVal = ((uint64_t)((uint64_t)sigInfo.sigFilterMask & (uint64_t)buffer[sigInfo.sigBytePos])
                     >> (sigInfo.sig1stByteBitLen - signalLen));
    } else {
        sigInfo.sigRestLen = (signalLen - sigInfo.sig1stByteBitLen);
        sigInfo.sigFilterMask = ComGetSignalFilterMask(sigInfo.sig1stByteBitLen);
        sigInfo.sigSwapNum++;

        sigInfo.sigRestByteNum = (uint8_t)(sigInfo.sigRestLen / (uint8_t)BYTE_LENGTH);
        sigInfo.sigRestBitNum = (uint8_t)(sigInfo.sigRestLen % (uint8_t)BYTE_LENGTH);
        sigInfo.sigSwapNum += sigInfo.sigRestByteNum;
        if ((sigInfo.sigRestBitNum > 0u) && (sigInfo.sigRestBitNum < (uint8_t)BYTE_LENGTH)) {
            sigInfo.sigSwapNum++;
            sigInfo.sigRightShiftNum = ((uint8_t)BYTE_LENGTH - sigInfo.sigRestBitNum);
        }

        retSigVal = ((uint64_t)buffer[sigInfo.sigBytePos] & (uint64_t)sigInfo.sigFilterMask);
        for (i = 1; i < sigInfo.sigSwapNum; i++) {
            retSigVal = retSigVal << BYTE_LENGTH;
            retSigVal = (retSigVal | buffer[sigInfo.sigBytePos + i]);
        }

        retSigVal = (uint64_t)(retSigVal >> sigInfo.sigRightShiftNum);
    }

    return retSigVal;
}

uint64_t GetComMsgSignal(const uint8_t* buffer, uint8_t signalStartBit, uint8_t signalLen, bool signalOrder)
{
    uint64_t retVal = 0;
    if (buffer) {
        if (SIGNAL_INTER_ORDER == signalOrder) {
            retVal = ComMsgSignalInterAnalysis(buffer, signalStartBit, signalLen);
        } else {
            retVal = ComMsgSignalMotorolaAnalysis(buffer, signalStartBit, signalLen);
        }
    }
    return retVal;
}

使用:

uint64_t GetComMsgSignal(const uint8_t* buffer, uint8_t signalStartBit, uint8_t signalLen, bool signalOrder);

buffer:uint8类型的数组,即待解析源数据
signalStartBit:起始位
signalLen:数据解析长度
signalOrder:大小端,1是小端,0是大端

你可能感兴趣的:(c语言应用,c语言)