Skip to content

Commit a07b2ae

Browse files
committed
1、【增加】FreeModbus主机离散量输入相关功能,并在app_task.c中增加相应测试用例,测试通过;
2、【修改】user_mb_app.c中涉及到使用xMBUtilSetBits()的几处逻辑,避免xMBUtilSetBits()在操作位数量为0时,出现数据越界及出错的bug; Signed-off-by: armink <[email protected]>
1 parent 7ad3e63 commit a07b2ae

14 files changed

+6877
-463
lines changed

APP/src/app_task.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ void thread_entry_SysMonitor(void* parameter)
4949
usModbusUserData[0] = (USHORT)(rt_tick_get()/10);
5050
usModbusUserData[1] = (USHORT)(rt_tick_get()%10);
5151
ucModbusUserData[0] = 0x1F;
52-
eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData);
52+
eMBMasterReqReadDiscreteInputs(1,3,8);
53+
// eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData);
5354
// eMBMasterReqWriteCoil(1,8,0xFF00);
5455
// eMBMasterReqReadCoils(1,3,8);
5556
// eMBMasterReqReadInputRegister(1,3,2);

EWARM/FreeModbus_Slave&Master+RTT+STM32.dep

+461-435
Large diffs are not rendered by default.

EWARM/FreeModbus_Slave&Master+RTT+STM32.ewp

+3
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,9 @@
18951895
<file>
18961896
<name>$PROJ_DIR$\..\FreeModbus\modbus\functions\mbfunccoils_m.c</name>
18971897
</file>
1898+
<file>
1899+
<name>$PROJ_DIR$\..\FreeModbus\modbus\functions\mbfuncdisc_m.c</name>
1900+
</file>
18981901
<file>
18991902
<name>$PROJ_DIR$\..\FreeModbus\modbus\functions\mbfuncholding_m.c</name>
19001903
</file>

EWARM/settings/FreeModbus_Slave&Master+RTT+STM32.wsdt

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
<Build><ColumnWidth0>20</ColumnWidth0><ColumnWidth1>1215</ColumnWidth1><ColumnWidth2>324</ColumnWidth2><ColumnWidth3>81</ColumnWidth3></Build><TerminalIO/><Debug-Log><ColumnWidth0>19</ColumnWidth0><ColumnWidth1>1622</ColumnWidth1></Debug-Log><Find-All-Declarations><ColumnWidth0>580</ColumnWidth0><ColumnWidth1>82</ColumnWidth1><ColumnWidth2>994</ColumnWidth2></Find-All-Declarations></Static>
1818
<Windows>
1919

20-
<Wnd0>
20+
<Wnd2>
2121
<Tabs>
2222
<Tab>
2323
<Identity>TabID-30370-1297</Identity>
2424
<TabName>Workspace</TabName>
2525
<Factory>Workspace</Factory>
2626
<Session>
2727

28-
<NodeDict><ExpandedNode>FreeModbus_Slave&amp;Master+RTT+STM32</ExpandedNode><ExpandedNode>FreeModbus_Slave&amp;Master+RTT+STM32/FreeModbusMaster</ExpandedNode><ExpandedNode>FreeModbus_Slave&amp;Master+RTT+STM32/FreeModbusMaster/Modbus</ExpandedNode></NodeDict></Session>
28+
<NodeDict><ExpandedNode>FreeModbus_Slave&amp;Master+RTT+STM32</ExpandedNode></NodeDict></Session>
2929
</Tab>
3030
</Tabs>
3131

32-
<SelectedTab>0</SelectedTab></Wnd0><Wnd1><Tabs><Tab><Identity>TabID-24390-6730</Identity><TabName>Build</TabName><Factory>Build</Factory><Session/></Tab><Tab><Identity>TabID-3984-13619</Identity><TabName>Find All Declarations</TabName><Factory>Find-All-Declarations</Factory><Session/></Tab></Tabs><SelectedTab>0</SelectedTab></Wnd1></Windows>
32+
<SelectedTab>0</SelectedTab></Wnd2><Wnd3><Tabs><Tab><Identity>TabID-24390-6730</Identity><TabName>Build</TabName><Factory>Build</Factory><Session/></Tab><Tab><Identity>TabID-3984-13619</Identity><TabName>Find All Declarations</TabName><Factory>Find-All-Declarations</Factory><Session/></Tab></Tabs><SelectedTab>0</SelectedTab></Wnd3></Windows>
3333
<Editor>
3434

3535

@@ -42,7 +42,7 @@
4242

4343

4444

45-
<Top><Row0><Sizes><Toolbar-01bf5d58><key>iaridepm.enu1</key></Toolbar-01bf5d58></Sizes></Row0></Top><Left><Row0><Sizes><Wnd0><Rect><Top>-2</Top><Left>-2</Left><Bottom>725</Bottom><Right>241</Right><x>-2</x><y>-2</y><xscreen>456</xscreen><yscreen>101</yscreen><sizeHorzCX>271429</sizeHorzCX><sizeHorzCY>104339</sizeHorzCY><sizeVertCX>144643</sizeVertCX><sizeVertCY>751033</sizeVertCY></Rect></Wnd0></Sizes></Row0></Left><Right><Row0><Sizes/></Row0></Right><Bottom><Row0><Sizes><Wnd1><Rect><Top>-2</Top><Left>-2</Left><Bottom>198</Bottom><Right>1682</Right><x>-2</x><y>-2</y><xscreen>1684</xscreen><yscreen>200</yscreen><sizeHorzCX>1002381</sizeHorzCX><sizeHorzCY>206612</sizeHorzCY><sizeVertCX>271429</sizeVertCX><sizeVertCY>104339</sizeVertCY></Rect></Wnd1></Sizes></Row0></Bottom><Float><Sizes/></Float></Positions>
45+
<Top><Row0><Sizes><Toolbar-00f440c8><key>iaridepm.enu1</key></Toolbar-00f440c8></Sizes></Row0></Top><Left><Row0><Sizes><Wnd2><Rect><Top>-2</Top><Left>-2</Left><Bottom>725</Bottom><Right>241</Right><x>-2</x><y>-2</y><xscreen>456</xscreen><yscreen>101</yscreen><sizeHorzCX>271429</sizeHorzCX><sizeHorzCY>104339</sizeHorzCY><sizeVertCX>144643</sizeVertCX><sizeVertCY>751033</sizeVertCY></Rect></Wnd2></Sizes></Row0></Left><Right><Row0><Sizes/></Row0></Right><Bottom><Row0><Sizes><Wnd3><Rect><Top>-2</Top><Left>-2</Left><Bottom>198</Bottom><Right>1682</Right><x>-2</x><y>-2</y><xscreen>1684</xscreen><yscreen>200</yscreen><sizeHorzCX>1002381</sizeHorzCX><sizeHorzCY>206612</sizeHorzCY><sizeVertCX>271429</sizeVertCX><sizeVertCY>104339</sizeVertCY></Rect></Wnd3></Sizes></Row0></Bottom><Float><Sizes/></Float></Positions>
4646
</Desktop>
4747
</Workspace>
4848

FreeModbus/modbus/functions/mbfunccoils_m.c

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
118118
usCoilCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF] << 8 );
119119
usCoilCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] );
120120

121+
/* Test if the quantity of coils is a multiple of 8. If not last
122+
* byte is only partially field with unused coils set to zero. */
121123
if( ( usCoilCount & 0x0007 ) != 0 )
122124
{
123125
ucByteCount = ( UCHAR )( usCoilCount / 8 + 1 );
+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
3+
* Copyright (C) 2013 Armink <[email protected]>
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* 3. The name of the author may not be used to endorse or promote products
15+
* derived from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20+
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22+
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*
28+
* File: $Id: mbfuncdisc_m.c,v 1.60 2013/10/15 8:48:20 Armink Add Master Functions Exp $
29+
*/
30+
31+
32+
33+
/* ----------------------- System includes ----------------------------------*/
34+
#include "stdlib.h"
35+
#include "string.h"
36+
37+
/* ----------------------- Platform includes --------------------------------*/
38+
#include "port.h"
39+
40+
/* ----------------------- Modbus includes ----------------------------------*/
41+
#include "mb.h"
42+
#include "mb_m.h"
43+
#include "mbframe.h"
44+
#include "mbproto.h"
45+
#include "mbconfig.h"
46+
47+
/* ----------------------- Defines ------------------------------------------*/
48+
#define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
49+
#define MB_PDU_REQ_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
50+
#define MB_PDU_REQ_READ_SIZE ( 4 )
51+
#define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 0 )
52+
#define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
53+
#define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
54+
55+
/* ----------------------- Static functions ---------------------------------*/
56+
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
57+
58+
/* ----------------------- Start implementation -----------------------------*/
59+
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
60+
#if MB_FUNC_READ_COILS_ENABLED > 0
61+
62+
eMBMasterReqErrCode
63+
eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn )
64+
{
65+
UCHAR *ucMBFrame;
66+
eMBMasterReqErrCode eErrStatus = MB_MRE_NO_ERR;
67+
68+
if ( xMBMasterGetIsBusy() ) eErrStatus = MB_MRE_MASTER_BUSY;
69+
else if ( ucSndAddr > MB_MASTER_TOTAL_SLAVE_NUM ) eErrStatus = MB_MRE_ILL_ARG;
70+
else
71+
{
72+
vMBMasterGetPDUSndBuf(&ucMBFrame);
73+
vMBMasterSetDestAddress(ucSndAddr);
74+
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS;
75+
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8;
76+
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr;
77+
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8;
78+
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn;
79+
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
80+
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
81+
}
82+
return eErrStatus;
83+
}
84+
85+
eMBException
86+
eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
87+
{
88+
USHORT usRegAddress;
89+
USHORT usDiscreteCnt;
90+
UCHAR ucNBytes;
91+
UCHAR *ucMBFrame;
92+
93+
eMBException eStatus = MB_EX_NONE;
94+
eMBErrorCode eRegStatus;
95+
96+
if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
97+
{
98+
vMBMasterGetPDUSndBuf(&ucMBFrame);
99+
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
100+
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
101+
usRegAddress++;
102+
103+
usDiscreteCnt = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF] << 8 );
104+
usDiscreteCnt |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] );
105+
106+
/* Test if the quantity of coils is a multiple of 8. If not last
107+
* byte is only partially field with unused coils set to zero. */
108+
if( ( usDiscreteCnt & 0x0007 ) != 0 )
109+
{
110+
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 );
111+
}
112+
else
113+
{
114+
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 );
115+
}
116+
117+
/* Check if the number of registers to read is valid. If not
118+
* return Modbus illegal data value exception.
119+
*/
120+
if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF])
121+
{
122+
/* Make callback to fill the buffer. */
123+
eRegStatus = eMBRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt );
124+
125+
/* If an error occured convert it into a Modbus exception. */
126+
if( eRegStatus != MB_ENOERR )
127+
{
128+
eStatus = prveMBError2Exception( eRegStatus );
129+
}
130+
}
131+
else
132+
{
133+
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
134+
}
135+
}
136+
else
137+
{
138+
/* Can't be a valid read coil register request because the length
139+
* is incorrect. */
140+
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
141+
}
142+
return eStatus;
143+
}
144+
145+
#endif
146+
#endif

FreeModbus/modbus/include/mb_m.h

+2
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData );
212212
eMBMasterReqErrCode
213213
eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
214214
USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer );
215+
eMBMasterReqErrCode
216+
eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn );
215217

216218
eMBException
217219
eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );

FreeModbus/modbus/mb_m.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
129129
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBMasterFuncWriteMultipleCoils},
130130
#endif
131131
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
132-
//TODO Add Master function define
133-
{MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
132+
{MB_FUNC_READ_DISCRETE_INPUTS, eMBMasterFuncReadDiscreteInputs},
134133
#endif
135134
};
136135

FreeModbus/port/user_mb_app.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegis
231231
iNReg--;
232232
}
233233
usNCoils = usNCoils % 8; //余下的线圈数
234-
xMBUtilSetBits(&pucCoilBuf[iRegIndex++] , iRegBitIndex , usNCoils , *pucRegBuffer++);
234+
if (usNCoils != 0) //xMBUtilSetBits方法 在操作位数量为0时存在bug
235+
{
236+
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, usNCoils,
237+
*pucRegBuffer++);
238+
}
235239
break;
236240
}
237241
}
@@ -294,8 +298,12 @@ eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
294298
xMBUtilSetBits(&pucDiscreteInputBuf[iRegIndex++] , iRegBitIndex , 8 , *pucRegBuffer++);
295299
iNReg--;
296300
}
297-
usNDiscrete = usNDiscrete % 8; //余下的线圈数
298-
xMBUtilSetBits(&pucDiscreteInputBuf[iRegIndex++] , iRegBitIndex , usNDiscrete , *pucRegBuffer++);
301+
usNDiscrete = usNDiscrete % 8; //余下的线圈数
302+
if (usNDiscrete != 0) //xMBUtilSetBits方法 在操作位数量为0时存在bug
303+
{
304+
xMBUtilSetBits(&pucDiscreteInputBuf[iRegIndex++], iRegBitIndex,
305+
usNDiscrete, *pucRegBuffer++);
306+
}
299307
}
300308
else
301309
{

0 commit comments

Comments
 (0)