1、【修改】用户的FreeModbus回调函数,使其支持主机模式及从机模式

Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
armink 2013-08-28 15:40:54 +08:00
parent e8c502eca6
commit 5cece07cd7
1 changed files with 57 additions and 27 deletions

View File

@ -63,9 +63,18 @@ eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
iRegIndex = ( int )( usAddress - usRegInStart );
while( usNRegs > 0 )
{
//Determine the master or slave
if (bMBRunInMasterMode)
{
pusRegInputBuf[iRegIndex] = *pucRegBuffer++ << 8;
pusRegInputBuf[iRegIndex] |= *pucRegBuffer++;
}
else
{
*pucRegBuffer++ = ( unsigned char )( pusRegInputBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( unsigned char )( pusRegInputBuf[iRegIndex] & 0xFF );
}
iRegIndex++;
usNRegs--;
}
@ -94,7 +103,7 @@ eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegi
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
USHORT * usRegHoldingBuf;
USHORT * pusRegHoldingBuf;
UCHAR REG_HOLDING_START;
UCHAR REG_HOLDING_NREGS;
UCHAR usRegHoldStart;
@ -102,14 +111,16 @@ eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegi
//Determine the master or slave
if (bMBRunInMasterMode)
{
usRegHoldingBuf = usMRegHoldBuf[ucMBMasterSendAddress];
pusRegHoldingBuf = usMRegHoldBuf[ucMBMasterSendAddress];
REG_HOLDING_START = M_REG_HOLDING_START;
REG_HOLDING_NREGS = M_REG_HOLDING_NREGS;
usRegHoldStart = usMRegHoldStart;
//If mode is read,the master will wirte the received date to bufffer.
eMode = MB_REG_WRITE;
}
else
{
usRegHoldingBuf = usSRegInBuf;
pusRegHoldingBuf = usSRegInBuf;
REG_HOLDING_START = S_REG_INPUT_START;
REG_HOLDING_NREGS = S_REG_INPUT_NREGS;
usRegHoldStart = usSRegInStart;
@ -125,8 +136,8 @@ eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegi
case MB_REG_READ:
while( usNRegs > 0 )
{
*pucRegBuffer++ = ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( unsigned char )( usRegHoldingBuf[iRegIndex] & 0xFF );
*pucRegBuffer++ = ( unsigned char )( pusRegHoldingBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( unsigned char )( pusRegHoldingBuf[iRegIndex] & 0xFF );
iRegIndex++;
usNRegs--;
}
@ -137,8 +148,8 @@ eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegi
case MB_REG_WRITE:
while( usNRegs > 0 )
{
usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
pusRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
pusRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
iRegIndex++;
usNRegs--;
}
@ -169,7 +180,7 @@ eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegis
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex , iRegBitIndex , iNReg;
iNReg = usNCoils / 8 + 1; //占用寄存器数量
UCHAR * ucCoilBuf;
UCHAR * pucCoilBuf;
UCHAR COIL_START;
UCHAR COIL_NCOILS;
UCHAR usCoilStart;
@ -177,14 +188,16 @@ eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegis
//Determine the master or slave
if (bMBRunInMasterMode)
{
ucCoilBuf = ucMCoilBuf[ucMBMasterSendAddress];
pucCoilBuf = ucMCoilBuf[ucMBMasterSendAddress];
COIL_START = M_COIL_START;
COIL_NCOILS = M_COIL_NCOILS;
usCoilStart = usMCoilStart;
//If mode is read,the master will wirte the received date to bufffer.
eMode = MB_REG_WRITE;
}
else
{
ucCoilBuf = ucSCoilBuf;
pucCoilBuf = ucSCoilBuf;
COIL_START = S_COIL_START;
COIL_NCOILS = S_COIL_NCOILS;
usCoilStart = usSCoilStart;
@ -201,7 +214,7 @@ eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegis
case MB_REG_READ:
while( iNReg > 0 )
{
*pucRegBuffer++ = xMBUtilGetBits(&ucCoilBuf[iRegIndex++] , iRegBitIndex , 8);
*pucRegBuffer++ = xMBUtilGetBits(&pucCoilBuf[iRegIndex++] , iRegBitIndex , 8);
iNReg --;
}
pucRegBuffer --;
@ -215,11 +228,11 @@ eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegis
case MB_REG_WRITE:
while(iNReg > 1) //最后面余下来的数单独算
{
xMBUtilSetBits(&ucCoilBuf[iRegIndex++] , iRegBitIndex , 8 , *pucRegBuffer++);
xMBUtilSetBits(&pucCoilBuf[iRegIndex++] , iRegBitIndex , 8 , *pucRegBuffer++);
iNReg--;
}
usNCoils = usNCoils % 8; //余下的线圈数
xMBUtilSetBits(&ucCoilBuf[iRegIndex++] , iRegBitIndex , usNCoils , *pucRegBuffer++);
xMBUtilSetBits(&pucCoilBuf[iRegIndex++] , iRegBitIndex , usNCoils , *pucRegBuffer++);
break;
}
}
@ -245,7 +258,7 @@ eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex , iRegBitIndex , iNReg;
iNReg = usNDiscrete / 8 + 1; //占用寄存器数量
UCHAR * ucDiscreteInputBuf;
UCHAR * pucDiscreteInputBuf;
UCHAR DISCRETE_INPUT_START;
UCHAR DISCRETE_INPUT_NDISCRETES;
UCHAR usDiscreteInputStart;
@ -253,14 +266,14 @@ eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
//Determine the master or slave
if (bMBRunInMasterMode)
{
ucDiscreteInputBuf = ucMDiscInBuf[ucMBMasterSendAddress];
pucDiscreteInputBuf = ucMDiscInBuf[ucMBMasterSendAddress];
DISCRETE_INPUT_START = M_DISCRETE_INPUT_START;
DISCRETE_INPUT_NDISCRETES = M_DISCRETE_INPUT_NDISCRETES;
usDiscreteInputStart = usMDiscInStart;
}
else
{
ucDiscreteInputBuf = ucSDiscInBuf;
pucDiscreteInputBuf = ucSDiscInBuf;
DISCRETE_INPUT_START = S_DISCRETE_INPUT_START;
DISCRETE_INPUT_NDISCRETES = S_DISCRETE_INPUT_NDISCRETES;
usDiscreteInputStart = usSDiscInStart;
@ -271,9 +284,25 @@ eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
iRegIndex = ( int )( usAddress - usDiscreteInputStart ) / 8 ; //每个寄存器存8个
iRegBitIndex = ( int )( usAddress - usDiscreteInputStart ) % 8 ; //相对于寄存器内部的位地址
//Determine the master or slave
if (bMBRunInMasterMode)
{
/* Update current coil values with new values from the
* protocol stack. */
while(iNReg > 1) //最后面余下来的数单独算
{
xMBUtilSetBits(&pucDiscreteInputBuf[iRegIndex++] , iRegBitIndex , 8 , *pucRegBuffer++);
iNReg--;
}
usNDiscrete = usNDiscrete % 8; //余下的线圈数
xMBUtilSetBits(&pucDiscreteInputBuf[iRegIndex++] , iRegBitIndex , usNDiscrete , *pucRegBuffer++);
}
else
{
while( iNReg > 0 )
{
*pucRegBuffer++ = xMBUtilGetBits(&ucDiscreteInputBuf[iRegIndex++] , iRegBitIndex , 8);
*pucRegBuffer++ = xMBUtilGetBits(&pucDiscreteInputBuf[iRegIndex++] , iRegBitIndex , 8);
iNReg --;
}
pucRegBuffer --;
@ -281,6 +310,7 @@ eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
*pucRegBuffer = *pucRegBuffer <<(8 - usNDiscrete); //高位补零
*pucRegBuffer = *pucRegBuffer >>(8 - usNDiscrete);
}
}
else
{
eStatus = MB_ENOREG;