大小端数据解析

#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是大端

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值