方案1是状态机
现在我想用自己的方案2 函数指针风格
本博文做一个小帮助工具:
//fun--需要执行的函数
//chk--执行函数期待的返回值
//log--打印log的信息用fun的名字吧
//hlp--是否需要做其他事情 1-需要 0-不需要
//ext--是否结束流程return掉 1-需要 0-不需要
#define fun_chk( fun,chk,log,hlp,exit) \
if( (fun) == chk)\
printf("Function %s OK\r\n",log);\
else {printf("Function %s Failed \r\n",log);\
if(hlp)Scale.SetStatus(ScaleAbnormal);\
if(exit)return;}\
memset(tem , 0 , MAXCMDLEN);\
实例:
back = usart_send_receive_repeat(tem,strlen((const char*)ShakeHand)/2,100,1,&respone);
common_chk(back,1,"ShakeHand",0,0);sys_delay(500);
后面可以试试do--while(0)的技巧!
#define fun_chk( fun,chk,log,hlp,exit) \
do{\
if( (fun) == chk)\
printf("Function %s OK\r\n",log);\
else {printf("Function %s Failed \r\n",log);\
if(hlp)Scale.SetStatus(ScaleAbnormal);\
if(exit)return;}\
memset(tem , 0 , MAXCMDLEN);\
}while(0)\
实战:SIM800C
是宏定义 不是函数 不要*进去
uint8_t L506_module_init( void )
{
uint16_t TXlen=0,i=0;
char *TX = NULL;
char *RXCHK = NULL;
uint8_t rst=0;
for(i=0;i<sizeof(AtBank)/sizeof(*AtBank);)
{
TX = AtBank[i].At;
TXlen = strlen(TX);
RXCHK = AtBank[i].Chk;
rst = L506_TXRX_L2(TX , TXlen ,500,20, RXCHK);//成功返回TRUE
fun_chk( rst,i);
}
L506Run = RECEIVE;
}
#define fun_chk( fun,id) \
do{\
if( (fun) == TRUE)\
{ printf("Function i=%d OK\r\n",id);\
id++;}\
else {printf("Function i=%d Failed \r\n",id);\
id=0;}\
}while(0)\