|
发表于 2013-9-24 00:29:40
|
显示全部楼层
本帖最后由 Semiconductor 于 2013-9-24 09:15 编辑
找到原因,如下
原【modbus】文件夹下的【functions】文件夹下的【mbfuncholding.c】文件中 的【eMBFuncReadWriteMultipleHoldingRegister】 函数为- eMBException
- eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
- {
- USHORT usRegReadAddress;
- USHORT usRegReadCount;
- USHORT usRegWriteAddress;
- USHORT usRegWriteCount;
- UCHAR ucRegWriteByteCount;
- UCHAR *pucFrameCur;
- eMBException eStatus = MB_EX_NONE;
- eMBErrorCode eRegStatus;
- if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
- {
- usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
- usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
- usRegReadAddress++;
- usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
- usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
- usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
- usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
- usRegWriteAddress++;
- usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
- usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
- ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
- if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
- ( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
- ( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
- {
- /* Make callback to update the register values. */
- eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
- usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
- if( eRegStatus == MB_ENOERR )
- {
- /* Set the current PDU data pointer to the beginning. */
- pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
- *usLen = MB_PDU_FUNC_OFF;
- /* First byte contains the function code. */
- *pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
- *usLen += 1;
- /* Second byte in the response contain the number of bytes. */
- *pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
- *usLen += 1;
- /* Make the read callback. */
- eRegStatus =
- eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
- if( eRegStatus == MB_ENOERR )
- {
- *usLen += 2 * usRegReadCount;
- }
- }
- if( eRegStatus != MB_ENOERR )
- {
- eStatus = prveMBError2Exception( eRegStatus );
- }
- }
- else
- {
- eStatus = MB_EX_ILLEGAL_DATA_VALUE;
- }
- }
- return eStatus;
- }
复制代码 修改为:- eMBException
- eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
- {
- USHORT usRegReadAddress;
- USHORT usRegReadCount;
- USHORT usRegWriteAddress;
- USHORT usRegWriteCount;
- UCHAR ucRegWriteByteCount;
- UCHAR *pucFrameCur;
- eMBException eStatus = MB_EX_NONE;
- eMBErrorCode eRegStatus;
- if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
- {
- usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
- usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
- //修改
- //usRegReadAddress++;
- usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
- usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
- usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
- usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
- //修改
- //usRegWriteAddress++;
- usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
- usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
- ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
- if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
- ( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
- ( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
- {
- /* Make callback to update the register values. */
- eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
- usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
- if( eRegStatus == MB_ENOERR )
- {
- /* Set the current PDU data pointer to the beginning. */
- pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
- *usLen = MB_PDU_FUNC_OFF;
- /* First byte contains the function code. */
- *pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
- *usLen += 1;
- /* Second byte in the response contain the number of bytes. */
- *pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
- *usLen += 1;
- /* Make the read callback. */
- eRegStatus =
- eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
- if( eRegStatus == MB_ENOERR )
- {
- *usLen += 2 * usRegReadCount;
- }
- }
- if( eRegStatus != MB_ENOERR )
- {
- eStatus = prveMBError2Exception( eRegStatus );
- }
- }
- else
- {
- eStatus = MB_EX_ILLEGAL_DATA_VALUE;
- }
- }
- return eStatus;
- }
复制代码 即将usRegReadAddress++;usRegReadAddress++;屏蔽即可 |
|