FreeModbus Slave 改进的eMbPoll()【worldsing 笔记】
eMbPoll()的作用是FreeMod协议通信过程中不断查询事件对列有无完速数据桢,并进行地址和CRD验证,最后运行和回复主机。
为了减小代码尺寸对eMbPoll进行改进:
原版:
1:
2: eMBErrorCode
3: eMBPoll( void )
4: {
5: static UCHAR *ucMBFrame;
6: static UCHAR ucRcvAddress;
7: static UCHAR ucFunctionCode;
8: static USHORT usLength;
9: static eMBException eException;
10:
11: int i;
12: eMBErrorCode eStatus = MB_ENOERR;
13: eMBEventType eEvent;
14:
15: /* Check if the protocol stack is ready. */
16: if( eMBState != STATE_ENABLED )
17: {
18: return MB_EILLSTATE;
19: }
20:
21: /* Check if there is a event available. If not return control to caller.
22: * Otherwise we will handle the event. */
23: if( xMBPortEventGet( &eEvent ) == TRUE )
24: {
25: switch ( eEvent )
26: {
27: case EV_READY:
28: break;
29:
30: case EV_FRAME_RECEIVED:
31: eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
32: if( eStatus == MB_ENOERR )
33: {
34: /* Check if the frame is for us. If not ignore the frame. */
35: if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
36: {
37: ( void )xMBPortEventPost( EV_EXECUTE );
38: }
39: }
40: break;
41:
42: case EV_EXECUTE:
43: ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
44: eException = MB_EX_ILLEGAL_FUNCTION;
45: for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
46: {
47: /* No more function handlers registered. Abort. */
48: if( xFuncHandlers[i].ucFunctionCode == 0 )
49: {
50: break;
51: }
52: else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
53: {
54: eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
55: break;
56: }
57: }
58:
59: /* If the request was not sent to the broadcast address we
60: * return a reply. */
61: if( ucRcvAddress != MB_ADDRESS_BROADCAST )
62: {
63: if( eException != MB_EX_NONE )
64: {
65: /* An exception occured. Build an error frame. */
66: usLength = 0;
67: ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
68: ucMBFrame[usLength++] = eException;
69: }
70: if( ( eMBCurrentMode == MB_ASCII ) && MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
71: {
72: vMBPortTimersDelay( MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS );
73: }
74: eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
75: }
76: break;
77:
78: case EV_FRAME_SENT:
79: break;
80: }
81: }
82: return MB_ENOERR;
83: }
改进后的eMbPoll():
1:
2: void eMBPoll( void ){
3:
4: static UCHAR *ucMBFrame;
5: static UCHAR ucFunctionCode;
6: static USHORT usLength;
7: static eMBException eException;
8: eMBEventType eEvent;
9: UCHAR i;
10: USHORT usCRC16;
11: if(xMBPortEventGet( &eEvent) == TRUE ){ //桢事件判断
12: if(eEvent == EV_FRAME_RECEIVED){
13: if(usRcvBufferPos < MB_SER_PDU_SIZE_MIN) //最小桢判断
14: return;
15: if(usMBCRC16((UCHAR *)ucRTUBuf, usRcvBufferPos ) != 0) //CRC判断
16: return;
17: if(IS_VALID_ADD){ //地址
18: ucMBFrame = (UCHAR *) &ucRTUBuf[MB_SER_PDU_PDU_OFF];
19: usLength = (USHORT)( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC);
20: ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
21: eException = MB_EX_ILLEGAL_FUNCTION;
22: for(i = 0; i < MB_FUNC_HANDLERS_MAX; i++ ){ //执行功能码
23: if( xFuncHandlers[i].ucFunctionCode == 0 ){
24: return;
25: }
26: else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode ){
27: eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
28: break;
29: }
30: }
31: if(IS_NOT_BROADCAST){ //回复主机
32: if( eException != MB_EX_NONE ){ //错误码
33: usLength = 0;
34: ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
35: ucMBFrame[usLength++] = eException;
36: }
37: if(eRcvState == STATE_RX_IDLE){ //发送
38: pucSndBufferCur = ( UCHAR * ) ucMBFrame - 1;
39: pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucMBAddress;
40: usSndBufferCount = usLength + 1;
41: usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
42: ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
43: ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
44: eSndState = STATE_TX_XMIT;
45: vMBPortSerialEnable( FALSE, TRUE );
46: }//发送结束
47: }//回复结束
48: }//地址判断
49: }//桢事件判断
50: }
51: }
改进说明:
1、eMbPoll()调用一次即可运行功能码和回复主机;
2、省去独立的接收函数peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength ); 而直接操作,(其实里面对算出数据桢的启始位置、和长度);
3、省去发送函数peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength ); 而直接操作;
4、省去返回值,因为调用处没有使用;
5、对功能的遍历i改成unsigned char类型,省去ucRcvAddress和eMBErrorCode eStatus = MB_ENOERR; 变量,
6、功能兼容原版本。
eMbPoll的经典之处在于功能的运行,——》函数指针,这部分在其它笔记中记录。
FreeModbus Slave 改进的eMbPoll()【worldsing 笔记】的更多相关文章
- FreeModbus Slave For AVR源代码 精简版2 【worldsing 笔记】
FreeModbus 源码:点击下载 线圈BUG解决(后来发现不一定是BUG) 1.eMBException eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * ...
- FreeModbus Slave RTU 精简版源代码【worldsing 笔记】
RTU精简版本 测试环境:IAR for avr 5.40 + M128 目前只优化了ModBusPort.c和ModBusRTU.c ModBusPort.c 566 bytes of CO ...
- emWin(ucGui)数值显示例程 -【worldsing笔记】
本例程下载:2.emWin5.26(ucGui)VS2008数字显示.zip 在emWin显示文本字符还是容易,我们也可以使用字符串和标准 C 库的函数来显示数值.然而,有时候这会是件困难的事.通 ...
- WS103C8例程——串口2【worldsing笔记】
在超MINI核心板 stm32F103C8最小系统板上调试Usart2功能:用Jlink 6Pin接口连接WStm32f103c8的Uart2,PC机向mcu发送数据,mcu收到数据后数据加1,回传给 ...
- Keil Mdk5.0 破解包 和谐包【worldsing笔记】
有关Keil MDK 5.0的介绍和下载 http://www.cnblogs.com/worldsing/p/3355911.html 下载地址 点击下载:http://pan.baidu.com/ ...
- Modbus Poll master-slave测试 Dtech USB转485(worldsing 笔记)
1,简介 网站地址:http://www.modbustools.com/ 该网站提供了几个软件工具,可以运行于windows 2000/XP/Vista/7环境下,用来测试和仿真Modebus设备. ...
- QTbaWidget控件几个例程 【worldsing笔记】
Qt Creator自带的 QTabWidget控件几个例程 在Qt Windos版本安装后,在Example目录可以找到与QTabWidget相关的工程Demo,如果按默认安装的话他们分别是: ...
- emWin5.24 VS2008模拟LCD12864 stm32 RTX移植 【worldsing笔记】
emWin for 12864 并口移植 源代码下载:RTX_emWin5.24_Keil_VS2008-20141122.zip 硬件环境: CPU: stm32f103ve LCD:st7 ...
- VS2008 工程中部分文件不参与编译 从生成中排除【Worldsing笔记】
Visual Studio 2008 .VS2008.VC2008工程源文件配置.编译配置 有时编写代码时,往往存在这样的需求(或是希望有这样的功能):一个工程经过不共同的配置实现不同的版本或是功 ...
随机推荐
- 【转】深入研究java.lang.Runtime类
一.概述 Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接. 一般不能实例化一个Runtime对象, ...
- 手机金属外壳加工工艺:铸造、锻造、冲压、CNC
现如今金属手机成为行业的热点,在消费电子产品中应用越来越广,本文详细介绍几种金属加工工艺及相关产品应用. 1.CNC+阳极:iPhone 5/6, HTC M7 2.锻造+CNC:华为P8,HTC M ...
- Oracle10g 回收站及彻底删除table : drop table xx purge
drop后的表被放在回收站(user_recyclebin)里,而不是直接删除掉.这样,回收站里的表信息就可以被恢复,或彻底清除. 1.通过查询回收站user_recyclebin获取被删除的表信息, ...
- C#中的Marshal
Const.MaxLengthOfBufferd的长度固定为0x2000 也就是8192 private bool SendMessage(int messageType, string ip, ...
- Android开发之ListView-SimpleAdapter的使用
SimpleAdapter: SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int r ...
- poj 3253 Fence Repair (哈夫曼树 优先队列)
题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...
- CSS基础深入之细说盒子模型
Html任何一个元素(element)都可以当成一个盒子(box)来看待,可以结合现实中的盒子来理解下文,下文其中一些单词应该是通俗易懂的需要记录的单词. 基本情况 每一个盒子都有一个内容区域(con ...
- [.NET WebAPI系列02] WebAPI 中的HTTP通信
[前言] 本节用于承上启下,通过第一节了解的WebAPI的基本语法,Controller CRUD方法的基本格式: 但很多场合,第一节中的Web API Controller方法返回的信息 过于简单, ...
- 使用MySQL
安装MySQL驱动 使用vpn $ pip install mysql-connector-python --allow-external mysql-connector-python import ...
- centos nginx 多端口配置过程记录
1. 编辑 /usr/local/nginx/vhosts/ 在此目录下增加一文件,如;ci.ainux.com,或复制一个文件 修改其中的端口和目录,更改log_format 名称 重启nginx ...