做一个好的接口函数
Posted 珵诩媛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了做一个好的接口函数相关的知识,希望对你有一定的参考价值。
定义一群接口一样的函数,使用联合体结构体函数指针;
#include "stdio.h" typedef unsigned char u8; typedef unsigned short int u16; typedef unsigned int u32; #define MB_FUNC_HANDLERS_MAX 17 #define MB_ADDRESS_BROADCAST ( 0 ) #define MB_ADDRESS_MIN ( 1 ) #define MB_ADDRESS_MAX ( 7 ) #define MB_FUNC_NONE ( 0 ) #define MB_FUNC_READ_COILS ( 1 ) #define MB_FUNC_READ_DISCRETE_INPUTS ( 2 ) #define MB_FUNC_WRITE_SINGLE_COIL ( 5 ) #define MB_FUNC_WRITE_MULTIPLE_COILS ( 15 ) #define MB_FUNC_READ_HOLDING_REGISTER ( 3 ) #define MB_FUNC_READ_INPUT_REGISTER ( 4 ) #define MB_FUNC_WRITE_REGISTER ( 6 ) #define MB_FUNC_WRITE_MULTIPLE_REGISTERS ( 16 ) #define MB_FUNC_READWRITE_MULTIPLE_REGISTERS ( 23 ) #define MB_FUNC_DIAG_READ_EXCEPTION ( 7 ) #define MB_FUNC_DIAG_DIAGNOSTIC ( 8 ) #define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 ) #define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 ) #define MB_FUNC_OTHER_REPORT_SLAVEI typedef enum { MB_EX_NONE = 0x00, MB_EX_ILLEGAL_FUNCTION = 0x01, MB_EX_ILLEGAL_DATA_ADDRESS = 0x02, MB_EX_ILLEGAL_DATA_VALUE = 0x03, MB_EX_SLAVE_DEVICE_FAILURE = 0x04, MB_EX_ACKNOWLEDGE = 0x05, MB_EX_SLAVE_BUSY = 0x06, MB_EX_MEMORY_PARITY_ERROR = 0x08, MB_EX_GATEWAY_PATH_FAILED = 0x0A, MB_EX_GATEWAY_TGT_FAILED = 0x0B }ENUM_MB_EXCEPTION;//1 返回类型定义 typedef struct { u8 ucFunctionCode; ENUM_MB_EXCEPTION (*pxHandler)( u8 * , u16 * ); } STRU_MB_FUNCTION_HANDLER; static STRU_MB_FUNCTION_HANDLER * gpstruMbFunHandler; ENUM_MB_EXCEPTION MB_FuncReadCoils( u8 *pucPduFrame, u16 * pusLength ) { ENUM_MB_EXCEPTION enStatus = MB_EX_NONE; printf("Sl = %d comm = %d Led = %d \n",pucPduFrame[0],pucPduFrame[1],*pusLength); return enStatus; } ENUM_MB_EXCEPTION MB_FuncReadHoldingRegister( u8 *pucPduFrame, u16 * pusLength ) { ENUM_MB_EXCEPTION enStatus = MB_EX_NONE; printf("Sl = %d comm = %d Led = %d \n",pucPduFrame[0],pucPduFrame[1],*pusLength); return enStatus; } ENUM_MB_EXCEPTION MB_FuncWriteSingleCoil( u8 *pucPduFrame, u16 * pusLength ) { ENUM_MB_EXCEPTION enStatus = MB_EX_NONE; printf("Sl = %d comm = %d Led = %d \n",pucPduFrame[0],pucPduFrame[1],*pusLength); return enStatus; } ENUM_MB_EXCEPTION MB_FuncWriteHoldingRegister( u8 *pucPduFrame, u16 * pusLength ) { ENUM_MB_EXCEPTION enStatus = MB_EX_NONE; printf("Sl = %d comm = %d Led = %d \n",pucPduFrame[0],pucPduFrame[1],*pusLength); return enStatus; } ENUM_MB_EXCEPTION MB_FuncWriteMultipleCoils( u8 *pucPduFrame, u16 * pusLength ) { ENUM_MB_EXCEPTION enStatus = MB_EX_NONE; printf("Sl = %d comm = %d Led = %d \n",pucPduFrame[0],pucPduFrame[1],*pusLength); return enStatus; } ENUM_MB_EXCEPTION MB_FuncWriteMultipleHoldingRegister( u8 *pucPduFrame, u16 * pusLength ) { ENUM_MB_EXCEPTION enStatus = MB_EX_NONE; printf("Sl = %d comm = %d Led = %d \n",pucPduFrame[0],pucPduFrame[1],*pusLength); return enStatus; } ENUM_MB_EXCEPTION MB_Func0( u8 *pucPduFrame, u16 * pusLength ) { ENUM_MB_EXCEPTION enStatus = MB_EX_NONE; printf("Sl = %d comm = %d Led = %d \n",pucPduFrame[0],pucPduFrame[1],*pusLength); return enStatus; } static STRU_MB_FUNCTION_HANDLER gastruRtuFuncHandlers[MB_FUNC_HANDLERS_MAX] = { /* 0 */ {MB_FUNC_NONE, MB_Func0}, /* 1 */ {MB_FUNC_READ_COILS, MB_FuncReadCoils}, /* 2 */ {MB_FUNC_READ_DISCRETE_INPUTS, MB_FuncReadCoils}, /* 3 */ {MB_FUNC_READ_HOLDING_REGISTER, MB_FuncReadHoldingRegister}, /* 4 */ {MB_FUNC_READ_INPUT_REGISTER, MB_Func0}, /* 5 */ {MB_FUNC_WRITE_SINGLE_COIL, MB_FuncWriteSingleCoil}, /* 6 */ {MB_FUNC_WRITE_REGISTER, MB_FuncWriteHoldingRegister}, /* 7 */ {MB_FUNC_DIAG_READ_EXCEPTION, MB_Func0}, /* 8 */ {MB_FUNC_DIAG_DIAGNOSTIC, MB_Func0}, /* 9 */ {MB_FUNC_NONE, MB_Func0}, /* 10 */ {MB_FUNC_NONE, MB_Func0}, /* 11 */ {MB_FUNC_DIAG_GET_COM_EVENT_CNT, MB_Func0}, /* 12 */ {MB_FUNC_DIAG_GET_COM_EVENT_LOG, MB_Func0}, /* 13 */ {MB_FUNC_NONE, MB_Func0}, /* 14 */ {MB_FUNC_NONE, MB_Func0}, /* 15 */ {MB_FUNC_WRITE_MULTIPLE_COILS, MB_FuncWriteMultipleCoils}, /* 16 */ {MB_FUNC_WRITE_MULTIPLE_REGISTERS, MB_FuncWriteMultipleHoldingRegister} }; int main(void) { u8 i; u16 usLen = 55; u8 puiFrame[10] = {0x01,0x03,0x00,0x1A,0x00,0x04,0x65,0xCE}; ENUM_MB_EXCEPTION enException; for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ) { enException = gastruRtuFuncHandlers[i].pxHandler( puiFrame, &usLen ); } while(1); }
以上是关于做一个好的接口函数的主要内容,如果未能解决你的问题,请参考以下文章