ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength );peMBFrameReceiveCur将指向接收帧信息提取函数eMBErrorCode eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );pvMBFrameCloseCur将指向端口关闭函数void vMBPortClose( void )(portserial.c中定义);pxMBFrameCBByteReceived将指向接收中断状态机函数BOOL xMBRTUReceiveFSM( void );pxMBFrameCBTransmitterEmpty将指向发送中断状态机函数BOOL xMBRTUTransmitFSM( void );pxMBPortCBTimerExpired将指向超时中断函数BOOL xMBRTUTimerT35Expired( void )。
完成相应函数指针的初始化之后,会调用该模式的初始化函数完成相应从机地址ucMBAddress、从机端口ucPort、从机通信速率ulBaudRate、从机校验方式eParity的初始化。 ? eMBErrorCode eMBTCPInit( USHORT ucTCPPort )
Modbus TCP模式初始化函数。只有当配置文件使能对应的宏开关MB_TCP_ENABLED时,才会编译该函数。该函数会初始化所使用的TCP/IP端口号,并初始化相应的函数指针。 ? eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler
pxHandler ) Modbus功能注册函数。通过该函数,可以定义FreeModbus协议外的功能代码,并注册相应的功能函数。具体如何实现还没具体看。 ? eMBErrorCode eMBClose( void )
Modbus端口关闭函数。该函数通过函数指针pvMBFrameCloseCur(指向通讯端口关闭函数)来停止止Modbus端口上的通讯。 ? eMBErrorCode eMBEnable( void )
Modbus协议开始函数。该函数通过函数指针pvMBFrameStartCur(指向相应模式下的使能函数)来使能Modbus通讯。 ? eMBErrorCode eMBDisable( void )
Modbus协议终止函数。该函数通过函数指针pvMBFrameStopCur(指向相应模式下的终止函数)来终止Modbus协议。 ? eMBErrorCode eMBPoll( void )
Modbus事件轮询处理函数。该函数通过查询底层返回来的事件eEvent来决定当前该处理的事务。
处理过程为:若事件为EV_READY(Startup finished),则跳出,等待下一次查询;若事件为EV_FRAME_RECEIVED(Frame received),则通过函数指针peMBFrameReceiveCur(指向接收帧信息提取函数)来完成帧信息的提取,并向系统发送EV_EXECUTE(Execute function)事件;若事件为EV_EXECUTE,则根据已经从帧信息中提取到的功能码在
xFuncHandlers中查询对应的功能函数指针,查找到后通过函数指针调用相应的功能处理函数来完成帧信息的处理(向缓存数组中存放回复PDU),完成处理后,通过函数指针peMBFrameSendCur调用帧发送函数完成回复帧的发送;若事件为EV_FRAME_SENT(Frame sent),则跳出,等待下一次查询。
3. 关于mbconfig.h文件的理解
此文件为Modbus协议的配置文件。在移植时,应根据所选用的目标处理器灵活修改此文件,使之满足需要而代码最小。当然,若果你的处理器处理能力足够强,可以保持默认配置,或是根据需要,增加相应的功能的配置宏。文件内容及相应解释如下: #ifndef _MB_CONFIG_H #define _MB_CONFIG_H
//外部C编译器宏开关 #ifdef __cplusplus
PR_BEGIN_EXTERN_C #endif
11
/* ----------------------- Defines ------------------------------------------*/ /*! \\brief If Modbus ASCII support is enabled. */
#define MB_ASCII_ENABLED ( 1 )
/*! \\brief If Modbus RTU support is enabled. */
#define MB_RTU_ENABLED ( 1 )
/*! \\brief If Modbus TCP support is enabled. */
#define MB_TCP_ENABLED ( 0 )
/*! \\brief The character timeout value for Modbus ASCII. */
#define MB_ASCII_TIMEOUT_SEC ( 1 )
/*! \\brief Timeout to wait in ASCII prior to enabling transmitter. */ #ifndef MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS
#define MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ( 0 ) #endif
/*! \\brief Maximum number of Modbus functions codes the protocol stack */ #define MB_FUNC_HANDLERS_MAX ( 16 )
/*! \\brief Number of bytes which should be allocated for the Report Slave ID */ #define MB_FUNC_OTHER_REP_SLAVEID_BUF ( 32 )
/*! \\brief If the Report Slave ID function should be enabled. */ #define MB_FUNC_OTHER_REP_SLAVEID_ENABLED ( 1 )
/*! \\brief If the Read Input Registers function should be enabled. */ #define MB_FUNC_READ_INPUT_ENABLED ( 1 )
/*! \\brief If the Read Holding Registers function should be enabled. */ #define MB_FUNC_READ_HOLDING_ENABLED ( 1 )
/*! \\brief If the Write Single Register function should be enabled. */ #define MB_FUNC_WRITE_HOLDING_ENABLED ( 1 )
/*! \\brief If the Write Multiple registers function should be enabled. */ #define MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED ( 1 )
/*! \\brief If the Read Coils function should be enabled. */ #define MB_FUNC_READ_COILS_ENABLED ( 1 )
/*! \\brief If the Write Coils function should be enabled. */ #define MB_FUNC_WRITE_COIL_ENABLED ( 1 )
/*! \\brief If the Write Multiple Coils function should be enabled. */
12
#define MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED ( 1 )
/*! \\brief If the Read Discrete Inputs function should be enabled. */ #define MB_FUNC_READ_DISCRETE_INPUTS_ENABLED ( 1 )
/*! \\brief If the Read/Write Multiple Registers function should be enabled. */ #define MB_FUNC_READWRITE_HOLDING_ENABLED ( 1 )
/*! @} */
#ifdef __cplusplus
PR_END_EXTERN_C #endif #endif
六、 一些小技巧
1、 若Buffer的最后两个字节为16为CRC校验值,则对整个Buffer校验时,值为0;
13
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库FreeModbus学习笔记(3)在线全文阅读。
相关推荐: