From dfab58957d3f03c6bd58802072936c273d4a45e7 Mon Sep 17 00:00:00 2001 From: armink Date: Sat, 7 Dec 2013 14:18:00 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91F?= =?UTF-8?q?reeModbus=E4=B8=BB=E6=9C=BA=E5=8F=8A=E4=BB=8E=E6=9C=BA=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E5=87=BD=E6=95=B0=E4=B8=AD=E7=9A=84=E8=B5=B7=E5=A7=8B?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=EF=BC=8C=E8=A7=A3=E5=86=B3=E8=B5=B7=E5=A7=8B?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E5=9C=A8=E4=BD=BF=E7=94=A8=E8=BF=87=E7=A8=8B?= =?UTF-8?q?=E4=B8=AD=E5=A4=9A=E4=B8=80=E4=BD=8D=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- FreeModbus/port/user_mb_app.c | 18 ++++++++++-------- FreeModbus/port/user_mb_app.h | 16 ++++++++-------- FreeModbus/port/user_mb_app_m.c | 18 ++++++++++-------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/FreeModbus/port/user_mb_app.c b/FreeModbus/port/user_mb_app.c index 66240fe..1f7dd0e 100644 --- a/FreeModbus/port/user_mb_app.c +++ b/FreeModbus/port/user_mb_app.c @@ -54,7 +54,7 @@ eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) { eMBErrorCode eStatus = MB_ENOERR; - int iRegIndex; + USHORT iRegIndex; USHORT * pusRegInputBuf; UCHAR REG_INPUT_START; UCHAR REG_INPUT_NREGS; @@ -65,10 +65,11 @@ eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) REG_INPUT_NREGS = S_REG_INPUT_NREGS; usRegInStart = usSRegInStart; + usAddress--;//FreeModbus功能函数中已经加1,为保证与缓冲区首地址一致,故减1 if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) { - iRegIndex = ( int )( usAddress - usRegInStart ); + iRegIndex = usAddress - usRegInStart; while( usNRegs > 0 ) { *pucRegBuffer++ = (unsigned char) (pusRegInputBuf[iRegIndex] >> 8); @@ -100,7 +101,7 @@ eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) { eMBErrorCode eStatus = MB_ENOERR; - int iRegIndex; + USHORT iRegIndex; USHORT * pusRegHoldingBuf; UCHAR REG_HOLDING_START; UCHAR REG_HOLDING_NREGS; @@ -111,10 +112,11 @@ eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegi REG_HOLDING_NREGS = S_REG_HOLDING_NREGS; usRegHoldStart = usSRegHoldStart; + usAddress--;//FreeModbus功能函数中已经加1,为保证与缓冲区首地址一致,故减1 if( ( usAddress >= REG_HOLDING_START ) && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) ) { - iRegIndex = ( int )( usAddress - usRegHoldStart ); + iRegIndex = usAddress - usRegHoldStart; switch ( eMode ) { /* Pass current register values to the protocol stack. */ @@ -163,7 +165,7 @@ eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode ) { eMBErrorCode eStatus = MB_ENOERR; - int iRegIndex , iRegBitIndex , iNReg; + USHORT iRegIndex , iRegBitIndex , iNReg; UCHAR * pucCoilBuf; UCHAR COIL_START; UCHAR COIL_NCOILS; @@ -175,10 +177,10 @@ eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegis COIL_NCOILS = S_COIL_NCOILS; usCoilStart = usSCoilStart; + usAddress--;//FreeModbus功能函数中已经加1,为保证与缓冲区首地址一致,故减1 if( ( usAddress >= COIL_START ) && ( usAddress + usNCoils <= COIL_START + COIL_NCOILS ) ) { - usAddress-=usCoilStart; //计算绝对地址 iRegIndex = ( int )( usAddress - usCoilStart ) / 8 ; //每个寄存器存8个 iRegBitIndex = ( int )( usAddress - usCoilStart ) % 8 ; //相对于寄存器内部的位地址 switch ( eMode ) @@ -233,7 +235,7 @@ eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) { eMBErrorCode eStatus = MB_ENOERR; - int iRegIndex , iRegBitIndex , iNReg; + USHORT iRegIndex , iRegBitIndex , iNReg; UCHAR * pucDiscreteInputBuf; UCHAR DISCRETE_INPUT_START; UCHAR DISCRETE_INPUT_NDISCRETES; @@ -245,10 +247,10 @@ eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) DISCRETE_INPUT_NDISCRETES = S_DISCRETE_INPUT_NDISCRETES; usDiscreteInputStart = usSDiscInStart; + usAddress--;//FreeModbus功能函数中已经加1,为保证与缓冲区首地址一致,故减1 if( ( usAddress >= DISCRETE_INPUT_START ) && ( usAddress + usNDiscrete <= DISCRETE_INPUT_START + DISCRETE_INPUT_NDISCRETES ) ) { - usAddress-=usDiscreteInputStart; //计算绝对地址 iRegIndex = ( int )( usAddress - usDiscreteInputStart ) / 8 ; //每个寄存器存8个 iRegBitIndex = ( int )( usAddress - usDiscreteInputStart ) % 8 ; //相对于寄存器内部的位地址 diff --git a/FreeModbus/port/user_mb_app.h b/FreeModbus/port/user_mb_app.h index 440ca82..104dcf4 100644 --- a/FreeModbus/port/user_mb_app.h +++ b/FreeModbus/port/user_mb_app.h @@ -8,13 +8,13 @@ #include "mbutils.h" /* -----------------------Slave Defines -------------------------------------*/ -#define S_DISCRETE_INPUT_START 1 +#define S_DISCRETE_INPUT_START 0 #define S_DISCRETE_INPUT_NDISCRETES 16 -#define S_COIL_START 1 +#define S_COIL_START 0 #define S_COIL_NCOILS 64 -#define S_REG_INPUT_START 1 +#define S_REG_INPUT_START 0 #define S_REG_INPUT_NREGS 100 -#define S_REG_HOLDING_START 1 +#define S_REG_HOLDING_START 0 #define S_REG_HOLDING_NREGS 100 //从机模式:在保持寄存器中,各个地址对应的功能定义 #define S_HD_RESERVE 0 //保留 @@ -31,13 +31,13 @@ #define S_DI_RESERVE 1 //保留 /* -----------------------Master Defines -------------------------------------*/ -#define M_DISCRETE_INPUT_START 1 +#define M_DISCRETE_INPUT_START 0 #define M_DISCRETE_INPUT_NDISCRETES 16 -#define M_COIL_START 1 +#define M_COIL_START 0 #define M_COIL_NCOILS 64 -#define M_REG_INPUT_START 1 +#define M_REG_INPUT_START 0 #define M_REG_INPUT_NREGS 100 -#define M_REG_HOLDING_START 1 +#define M_REG_HOLDING_START 0 #define M_REG_HOLDING_NREGS 100 //主机模式:在保持寄存器中,各个地址对应的功能定义 #define M_HD_RESERVE 0 //保留 diff --git a/FreeModbus/port/user_mb_app_m.c b/FreeModbus/port/user_mb_app_m.c index a8f7d0b..5299259 100644 --- a/FreeModbus/port/user_mb_app_m.c +++ b/FreeModbus/port/user_mb_app_m.c @@ -56,7 +56,7 @@ eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) { eMBErrorCode eStatus = MB_ENOERR; - int iRegIndex; + USHORT iRegIndex; USHORT * pusRegInputBuf; UCHAR REG_INPUT_START; UCHAR REG_INPUT_NREGS; @@ -67,10 +67,11 @@ eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) REG_INPUT_NREGS = M_REG_INPUT_NREGS; usRegInStart = usMRegInStart; + usAddress--;//FreeModbus功能函数中已经加1,为保证与缓冲区首地址一致,故减1 if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) { - iRegIndex = ( int )( usAddress - usRegInStart ); + iRegIndex = usAddress - usRegInStart; while( usNRegs > 0 ) { pusRegInputBuf[iRegIndex] = *pucRegBuffer++ << 8; @@ -102,7 +103,7 @@ eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) { eMBErrorCode eStatus = MB_ENOERR; - int iRegIndex; + USHORT iRegIndex; USHORT * pusRegHoldingBuf; UCHAR REG_HOLDING_START; UCHAR REG_HOLDING_NREGS; @@ -115,10 +116,11 @@ eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, e //If mode is read,the master will wirte the received date to bufffer. eMode = MB_REG_WRITE; + usAddress--;//FreeModbus功能函数中已经加1,为保证与缓冲区首地址一致,故减1 if( ( usAddress >= REG_HOLDING_START ) && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) ) { - iRegIndex = ( int )( usAddress - usRegHoldStart ); + iRegIndex = usAddress - usRegHoldStart; switch ( eMode ) { /* Pass current register values to the protocol stack. */ @@ -167,7 +169,7 @@ eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode ) { eMBErrorCode eStatus = MB_ENOERR; - int iRegIndex , iRegBitIndex , iNReg; + USHORT iRegIndex , iRegBitIndex , iNReg; UCHAR * pucCoilBuf; UCHAR COIL_START; UCHAR COIL_NCOILS; @@ -181,10 +183,10 @@ eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eM //If mode is read,the master will wirte the received date to bufffer. eMode = MB_REG_WRITE; + usAddress--;//FreeModbus功能函数中已经加1,为保证与缓冲区首地址一致,故减1 if( ( usAddress >= COIL_START ) && ( usAddress + usNCoils <= COIL_START + COIL_NCOILS ) ) { - usAddress-=usCoilStart; //计算绝对地址 iRegIndex = ( int )( usAddress - usCoilStart ) / 8 ; //每个寄存器存8个 iRegBitIndex = ( int )( usAddress - usCoilStart ) % 8 ; //相对于寄存器内部的位地址 switch ( eMode ) @@ -239,7 +241,7 @@ eMBErrorCode eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) { eMBErrorCode eStatus = MB_ENOERR; - int iRegIndex , iRegBitIndex , iNReg; + USHORT iRegIndex , iRegBitIndex , iNReg; UCHAR * pucDiscreteInputBuf; UCHAR DISCRETE_INPUT_START; UCHAR DISCRETE_INPUT_NDISCRETES; @@ -251,10 +253,10 @@ eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscre DISCRETE_INPUT_NDISCRETES = M_DISCRETE_INPUT_NDISCRETES; usDiscreteInputStart = usMDiscInStart; + usAddress--;//FreeModbus功能函数中已经加1,为保证与缓冲区首地址一致,故减1 if( ( usAddress >= DISCRETE_INPUT_START ) && ( usAddress + usNDiscrete <= DISCRETE_INPUT_START + DISCRETE_INPUT_NDISCRETES ) ) { - usAddress-=usDiscreteInputStart; //计算绝对地址 iRegIndex = ( int )( usAddress - usDiscreteInputStart ) / 8 ; //每个寄存器存8个 iRegBitIndex = ( int )( usAddress - usDiscreteInputStart ) % 8 ; //相对于寄存器内部的位地址