码迷,mamicode.com
首页 > 其他好文 > 详细

C 面向对象编程 --- 一模块的串口协议解析

时间:2019-01-26 17:03:58      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:改进   bool   ima   距离   har   标准   ted   image   逻辑   

// 任务目的
// 解析串口收到的54个字节。这54个字节包含了8个车道的5大信息以及校验信息。
// 实现了查询每条车道包含了哪些信息。

技术分享图片

 

#include <stdio.h>
#include <malloc.h>
#include <assert.h>

typedef unsigned char mybool;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;

mybool SumCheck(const u8* pScrData);

typedef enum{
RoadID1=1, // 距离雷达最远的那条车道, 一个绿化带也算一条车道。
RoadID2,
RoadID3,
RoadID4,
RoadID5,
RoadID6,
RoadID7,
RoadID8,
RoadIDNull
}RoadID_;


typedef enum{
T_LongCarCnt=1,
T_CarCnt,
T_SmallCarCnt,
T_Occupancy,
T_AveSpeed,
T_AskNull
}T_ask_;


struct CertainRoad_; // 下面定义函数指针时,形参有这个结构体的指针, 所以,这里的结构体一定要先声明。

typedef struct CertainRoad_{
RoadID_ RoadID;

u8(*pGetUpdated_LongCarCnt) ( struct CertainRoad_ *pRoad, const u8 *pScrData); // 第二个参数是54字节的数据哦
u8(*pGetUpdated_CarCnt) ( struct CertainRoad_ *pRoad, const u8 *pScrData);
u8(*pGetUpdated_SmallCarCnt)( struct CertainRoad_ *pRoad, const u8 *pScrData);
u8(*pGetUpdated_Occupancy) ( struct CertainRoad_ *pRoad, const u8 *pScrData);
u8(*pGetUpdated_AveSpeed) ( struct CertainRoad_ *pRoad, const u8 *pScrData);

void(*pGetUpdatedAllresults)( struct CertainRoad_ *pRoad, const u8 *pScrData, u8 *result);

}CertainRoad; // Certain是某的意思。CertainRoad:某条车道。


// 根据车道号获取该时间段内的长车的流量
// 规律:长车的流量 = pScrData[pRoad->RoadID+2];
u8 GetUpdated_LongCarCnt(CertainRoad* pRoad, const u8* pScrData)
{
if( !(pRoad->RoadID >= RoadID1)&&(pRoad->RoadID <= RoadID8) )
assert(0);
return pScrData[pRoad->RoadID+2];
}

u8 GetUpdated_CarCnt(CertainRoad* pRoad, const u8* pScrData)
{
if( !(pRoad->RoadID >= RoadID1)&&(pRoad->RoadID <= RoadID8) )
assert(0);
return pScrData[pRoad->RoadID+15];
}

u8 GetUpdated_SmallCarCnt(CertainRoad* pRoad, const u8* pScrData)
{
if( !(pRoad->RoadID >= RoadID1)&&(pRoad->RoadID <= RoadID8) )
assert(0);
return GetUpdated_CarCnt(pRoad,pScrData) - GetUpdated_LongCarCnt(pRoad, pScrData);
}

u8 GetUpdated_Occupancy(CertainRoad* pRoad, const u8* pScrData)
{
if( !(pRoad->RoadID >= RoadID1)&&(pRoad->RoadID <= RoadID8) )
assert(0);
return pScrData[pRoad->RoadID+28];
}

u8 GetUpdated_AveSpeed(CertainRoad* pRoad, const u8* pScrData)
{
if( !(pRoad->RoadID >= RoadID1)&&(pRoad->RoadID <= RoadID8) )
assert(0);
return pScrData[pRoad->RoadID+41];
}

void GetUpdatedAllresults(CertainRoad* pRoad, const u8* pScrData, u8* result)
{
if( !(pRoad->RoadID >= RoadID1)&&(pRoad->RoadID <= RoadID8) )
assert(0);

result[0] = pRoad->pGetUpdated_LongCarCnt (pRoad, pScrData);
result[1] = pRoad->pGetUpdated_CarCnt (pRoad, pScrData);
result[2] = pRoad->pGetUpdated_SmallCarCnt(pRoad, pScrData);
result[3] = pRoad->pGetUpdated_Occupancy (pRoad, pScrData);
result[4] = pRoad->pGetUpdated_AveSpeed (pRoad, pScrData);
}


CertainRoad* CreatNewRoad(RoadID_ RoadID)
{
CertainRoad* pNewRoadObj = (CertainRoad*)malloc(sizeof(CertainRoad));

pNewRoadObj->RoadID = RoadID;

pNewRoadObj->pGetUpdated_LongCarCnt = GetUpdated_LongCarCnt;
pNewRoadObj->pGetUpdated_CarCnt = GetUpdated_CarCnt;
pNewRoadObj->pGetUpdated_SmallCarCnt= GetUpdated_SmallCarCnt;
pNewRoadObj->pGetUpdated_Occupancy = GetUpdated_Occupancy;
pNewRoadObj->pGetUpdated_AveSpeed = GetUpdated_AveSpeed;

// 对上面的5个再次封装,提供统一接口
pNewRoadObj->pGetUpdatedAllresults = GetUpdatedAllresults;

return pNewRoadObj;
}


//串口底层收到数据,先进性简单逻辑判断,
//判断依据:0xff是第一个数据,之后的是0x1b 0x10 0x11 0x12四者之一。
//不符合上述依据,丢弃数据。
//符合上述依据,开始计数(要算上之前的2个),一共计数到54字节,发送一个信号量给相应的处理任务进行解析。
//处理任务进行: 和校验 、 原始数据的格式封装与转换。
int main(void)
{
// 车道1 2 3 4 5 6 7 车道8 // 这里模拟一下原始数据
u8 FakeData[54]={0xFF, 0x1B, 0x09, 0x02, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x76, 0x94, // T_LongCarCnt:这一行都是长车流量
0xFF, 0x10, 0x09, 0xf5, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x01, 0x12, // T_CarCnt 总车流量
0xFF, 0x11, 0x09, 0x20, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x03, 0x3f, // T_Occupancy 占有率
0xFF, 0x12, 0x0B, 0x88, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x00, 0x30, 0xD4};// T_AveSpeed 平均速度

// 感觉这样写得很丑,其实输入就应该是一个车道的ID,u8类型的数据即可,而不是这样填写枚举值。
// 改进方法:在CreatNewRoad函数内增加断言,利用switch case,将u8的ID值转为枚举值。并assert断言,防止输入参数大于8。
// 暂不改进
CertainRoad* pRoad1 = CreatNewRoad(RoadID1); // 每条车道是一个对象
CertainRoad* pRoad2 = CreatNewRoad(RoadID2);
CertainRoad* pRoad3 = CreatNewRoad(RoadID3);
CertainRoad* pRoad4 = CreatNewRoad(RoadID4);
CertainRoad* pRoad5 = CreatNewRoad(RoadID5);
CertainRoad* pRoad6 = CreatNewRoad(RoadID6);
CertainRoad* pRoad7 = CreatNewRoad(RoadID7);
CertainRoad* pRoad8 = CreatNewRoad(RoadID8);
// 注:当前VS是C89标准
printf(" Main Begin \n\n");

if(SumCheck(FakeData))
{
u8 LongCarCnt=0;
u8 CarCnt=0;
u8 SmallCarCnt=0;
u8 Occupancy=0;
u8 AveSpeed=0;

u8 Result[5] = {0};
u8 i=0, j=0;
CertainRoad*pRoad = (CertainRoad*)0; // NULL

for(i=1; i<=8; i++)
{
switch(i)
{
case 1:
pRoad = pRoad1;break;
case 2:
pRoad = pRoad2;break;
case 3:
pRoad = pRoad3;break;
case 4:
pRoad = pRoad4;break;
case 5:
pRoad = pRoad5;break;
case 6:
pRoad = pRoad6;break;
case 7:
pRoad = pRoad7;break;
case 8:
pRoad = pRoad8;break;
}

//本接口的使用方式一: 让用户一个一个调用
LongCarCnt = pRoad->pGetUpdated_LongCarCnt(pRoad, FakeData);
printf("pRoad%d: LongCarCnt = %d \n",i, LongCarCnt);

CarCnt = pRoad->pGetUpdated_CarCnt(pRoad, FakeData);
printf("pRoad%d: CarCnt = %d \n", i, CarCnt);

SmallCarCnt = pRoad->pGetUpdated_SmallCarCnt(pRoad, FakeData);
printf("pRoad%d: SmallCarCnt = %d \n", i, SmallCarCnt);

Occupancy = pRoad->pGetUpdated_Occupancy(pRoad, FakeData);
printf("pRoad%d: Occupancy = %d \n", i, Occupancy);

AveSpeed = pRoad->pGetUpdated_AveSpeed(pRoad, FakeData);
printf("pRoad%d: AveSpeed = %d \n", i, AveSpeed);

//本接口的使用方式二:让用户一个一个调用太麻烦了,再封装一层,直接返回5个有物理意义的字节作为结果。
pRoad->pGetUpdatedAllresults(pRoad, FakeData, Result);

for(j=0; j<5; j++)
{
printf(" Result[%d] = %d \n", j, Result[j]);
}
printf(" -----pRoad%d ---OVER---\n\n", i);
}
}
printf(" \n\n");
return 0;
}


//和校验,这是一个独立使用的函数
mybool SumCheck(const u8* pScrData)
{
mybool ret = 0;
u8 i = 0;
u16 CheckSUM1=0, CheckSUM2=0, CheckSUM3=0, CheckSUM4=0;

for(i=3; i<=11; i++)
{
CheckSUM1 += pScrData[i];
}
CheckSUM1 = CheckSUM1%256;

for(i=16; i<=24; i++)
{
CheckSUM2 += pScrData[i];
}
CheckSUM2 = CheckSUM2%256;

for(i=29; i<=37; i++)
{
CheckSUM3 += pScrData[i];
}
CheckSUM3 = CheckSUM3%256;

for(i=42; i<=52; i++)
{
CheckSUM4 += pScrData[i];
}
CheckSUM4 = CheckSUM4%256;

if( ( (u8)CheckSUM1 == pScrData[12]) && ((u8)CheckSUM2 == pScrData[25]) \
&& ((u8)CheckSUM3 == pScrData[38]) && ((u8)CheckSUM4 == pScrData[53]) )
{
ret = 1;
printf(" 本次串口收到的54字节 和检验 OK \n\n");
}
return ret;
}

 

C 面向对象编程 --- 一模块的串口协议解析

标签:改进   bool   ima   距离   har   标准   ted   image   逻辑   

原文地址:https://www.cnblogs.com/happybirthdaytoyou/p/10323766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!