通用检测函数--dowhile0

 

 

 

 

方案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)\

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值