[C语言] 函数映射表应用于串口解析

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


#define uint8_t   unsigned char
#define uint16_t  unsigned short
#define uint32_t  unsigned long
#define uint64_t  unsigned long long

#define CMD_TXT_LEN 30
#define CMD_NUM_MAX 100

typedef struct
{
	char cmdString[CMD_TXT_LEN];
	long (*cmd_func)(void *param);
	int param_len;
} Command_Tab;


typedef struct
{
	int x;
	int y;
} TagPoint;

typedef struct
{
	int x;
	int y;
	int width;
	int height;
} TagMatrix;

typedef struct
{
	char name[10];
	uint8_t  age;
	float  tall;
	uint16_t score;	
} TagStudent;


int StrCmp(char *buf,char *str)
{
	int len = strlen( buf );
	for(int i=0;i<len;i++)
	{		
		if( buf[i]!= str[i])
		{
			return 0;
		}
		if(str[i] == 0 )
			return 0;
	}
	return 1;
}

long SetPoint(void *arg)
{
	TagPoint *parm = (TagPoint*)arg;
	if(parm == NULL)
	{
		return -1;
	}
	printf("[SetPoint], x=%d,y=%d.\n",parm->x,parm->y);
	return 0;
}

long SetMatrix(void *arg)
{
	TagMatrix *parm = (TagMatrix*)arg;
	if(parm == NULL)
	{
		return -1;
	}
	printf("[SetMatrix], x=%d,y=%d,Width=%d,Height=%d.\n",parm->x,parm->y,parm->width,parm->height);
	return 0;
}

long SetVersion(void *arg)
{
	char *str = (char*)arg;
	printf("[SetVersion],VER=%s\n",str);
	return 0;
}

long StudentShow(void *arg)
{
	TagStudent *parm = (TagStudent*)arg;
	if(parm == NULL)
	{
		return -1;
	}	
	printf("[StudentShow],name=%s,age=%d,score=%d,tall=%.1f\n",
	  parm->name,parm->age,parm->score,parm->tall);
	return 0;
}

long Delay(void *arg)
{		
	printf("[Delay]\n");
	return 0;
}

static const Command_Tab functionList[CMD_NUM_MAX]=
{
	{"SetPoint",SetPoint,sizeof(TagPoint)},
	{"SetMatrix",SetMatrix,sizeof(TagMatrix)},
	{"SetVersion",SetVersion,10},
	{"StudentShow",StudentShow,sizeof(TagStudent)},
	{"Delay",Delay,0},
	{(char*)NULL,NULL,0}
 
};

long DataAnalysis(char *buf,void *param)
{
	int result=-1;
	long (*userFunc)(char *data);
    char *str;
	
	for(int i=0;i<CMD_NUM_MAX;i++)
	{
		str = (char*)functionList[i].cmdString;
		if( str == NULL )
		{
			//printf("Can't find func:[%s]",buf);
			return 0;
		}
		

		if( StrCmp(buf,str)!=0 )
		{
			userFunc=functionList[i].cmd_func;
			result=(*userFunc)(param);
		}
	}
	return result;

}


int main(int argc, char **argv)
{
    printf("=======C_BuildTime=[%s_%s]==============\r\n\r\n",__DATE__ ,__TIME__ );
	//通过串口传输函数名字符串,加上数组结构体作为函数的输入形参
	int param1[4]={130,140};
    DataAnalysis("SetPoint",param1);
	
	int param2[4]={52,-80,100,95};
    DataAnalysis("SetMatrix",param2);
	
	char param3[10]="V1.00";
    DataAnalysis("SetVersion",param3);	
	
	TagStudent ts;
	memset(ts.name,0,sizeof(ts.name));
	memcpy(ts.name,"Tom",strlen("Tom"));
	ts.age = 20;
	ts.score = 680;
	ts.tall = 173.5;
	int len = sizeof(TagStudent);
	char *param4 = (char*)malloc(len);
	memcpy(param4,&ts,len);
	DataAnalysis("StudentShow",param4);	
	free( param4 );
    DataAnalysis("StudentShow",&ts);//方法2	
	
	DataAnalysis("Delay",NULL);
 	

    return 0;
}

#include <stdio.h>

#define uint8_t unsigned char
#define uint16_t unsigned short
#define uint32_t unsigned int

typedef struct
{
  uint8_t x;
  uint8_t y;
} ParamFunc1;

typedef struct
{
  uint16_t code;
} RetFunc1;


typedef struct
{
  char name[10];
  uint8_t age;
} ParamFunc2;

typedef struct
{
  uint32_t timestamp;
} RetFunc2;

long fun1(long wp, long lp)
{
  static RetFunc1 ret;
  ParamFunc1 *pm = (ParamFunc1 *)wp;
  printf("fun1 called. x=%d,y=%d.\n", pm->x, pm->y);
  ret.code = 0x1abb;
  return (long)&ret;
}

long fun2(long wp, long lp)
{
 static RetFunc2 ret;  
  ParamFunc2 *pm = (ParamFunc2 *)wp;
  printf("fun2 called. name=%s,age=%d.\n", pm->name, pm->age);
  ret.timestamp = 10086;
  return (long)&ret;
}

//指针函数表
long *func_tab[] = 
{&fun1, &fun2};

int main()
{
  // 定义函数指针,并作赋值测试
  long (*fp)(long wp, long lp);

  ParamFunc1 pm1 = {10, 20};
  ParamFunc2 pm2 = {"Jack", 35};

  RetFunc1* rt1;
  RetFunc2* rt2;

  for (size_t i = 0; i < 2; i++)
  {
    printf("fun_tab[%d] addr=0x%p.\n",i,func_tab[i]);
  }

  fp = func_tab[0];
  rt1 = (RetFunc1*)fp(&pm1, 0);
  printf("fun1:ret, code = %x.\n", rt1->code);

  fp = func_tab[1];
  rt2 = (RetFunc2*)fp(&pm2, 0);
  printf("fun2:ret, timestamp = %d.\n", rt2->timestamp);

  return 0;
}

运行输出:

fun1 called. x=10,y=20.
fun1:ret, code = 1abb.
fun2 called. name=Jack,age=35.
fun2:ret, timestamp = 10086.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值