1、【增加】FreeModbus主机寄存器相关功能的实现,目前是异步模式,后期可以考虑支持同步模式;
2、【增加】FreeModbus主机寄存器相关测试用例,位于app_task.c; Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
parent
3cf7da44e0
commit
5de9d82e09
|
@ -48,9 +48,10 @@ void thread_entry_SysMonitor(void* parameter)
|
||||||
//Test Modbus Master
|
//Test Modbus Master
|
||||||
usModbusUserData[0] = (USHORT)(rt_tick_get()/10);
|
usModbusUserData[0] = (USHORT)(rt_tick_get()/10);
|
||||||
usModbusUserData[1] = (USHORT)(rt_tick_get()%10);
|
usModbusUserData[1] = (USHORT)(rt_tick_get()%10);
|
||||||
// eMBMasterFuncWriteHoldingRegister(1,usModbusUserData,3);
|
eMBMasterReqWriteHoldingRegister(1,usModbusUserData,3);
|
||||||
// eMBMasterFuncWriteMultipleHoldingRegister(1,usModbusUserData,3,2);
|
// eMBMasterReqWriteMultipleHoldingRegister(1,usModbusUserData,3,2);
|
||||||
eMBMasterReqReadHoldingRegister(1,3,2);
|
// eMBMasterReqReadHoldingRegister(1,3,2);
|
||||||
|
// eMBMasterReqReadWriteMultipleHoldingRegister(1,usModbusUserData,3,2,5,2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,31 +43,44 @@
|
||||||
#include "mbconfig.h"
|
#include "mbconfig.h"
|
||||||
|
|
||||||
/* ----------------------- Defines ------------------------------------------*/
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
#define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||||
#define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
#define MB_PDU_REQ_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
#define MB_PDU_REQ_READ_SIZE ( 4 )
|
||||||
#define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
|
#define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
|
||||||
|
#define MB_PDU_FUNC_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||||
|
#define MB_PDU_FUNC_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
|
||||||
|
#define MB_PDU_FUNC_READ_SIZE_MIN ( 1 )
|
||||||
|
|
||||||
|
#define MB_PDU_REQ_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
||||||
|
#define MB_PDU_REQ_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_REQ_WRITE_SIZE ( 4 )
|
||||||
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
||||||
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
|
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
|
||||||
|
|
||||||
|
#define MB_PDU_REQ_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||||
|
#define MB_PDU_REQ_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||||
|
#define MB_PDU_REQ_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
|
||||||
|
#define MB_PDU_REQ_WRITE_MUL_SIZE_MIN ( 5 )
|
||||||
|
#define MB_PDU_REQ_WRITE_MUL_REGCNT_MAX ( 0x0078 )
|
||||||
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
|
#define MB_PDU_FUNC_WRITE_MUL_SIZE ( 4 )
|
||||||
#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
|
|
||||||
#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
|
|
||||||
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ( 0x0078 )
|
|
||||||
|
|
||||||
#define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
#define MB_PDU_REQ_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||||
#define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
#define MB_PDU_REQ_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
#define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
|
#define MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||||
#define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
|
#define MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
|
||||||
#define MB_PDU_FUNC_READWRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
|
#define MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
|
||||||
#define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
|
#define MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
|
||||||
#define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 9 )
|
#define MB_PDU_REQ_READWRITE_SIZE_MIN ( 9 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_READ_BYTECNT_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_READ_VALUES_OFF ( MB_PDU_DATA_OFF + 1 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 1 )
|
||||||
|
|
||||||
/* ----------------------- Static functions ---------------------------------*/
|
/* ----------------------- Static functions ---------------------------------*/
|
||||||
|
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||||
|
|
||||||
/* ----------------------- Start implementation -----------------------------*/
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
|
#if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
|
||||||
|
@ -86,21 +99,53 @@ eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT * pusDataBuffer, USHOR
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] = usRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] = usRegAddr;
|
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] = pusDataBuffer[0] >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = pusDataBuffer[0] >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] = pusDataBuffer[0] ;
|
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = pusDataBuffer[0] ;
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_SIZE );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen == ( MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_SIZE ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
/* Make callback to update the value. */
|
||||||
|
eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
|
||||||
|
usRegAddress, 1, MB_REG_WRITE );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid request because the length is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||||
|
|
||||||
eMBMasterReqErrCode
|
eMBMasterReqErrCode
|
||||||
eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer, USHORT usRegAddr, USHORT usNRegs )
|
eMBMasterReqWriteMultipleHoldingRegister(UCHAR ucSndAddr,
|
||||||
|
USHORT * pusDataBuffer, USHORT usRegAddr, USHORT usNRegs)
|
||||||
{
|
{
|
||||||
UCHAR *ucMBFrame;
|
UCHAR *ucMBFrame;
|
||||||
USHORT usRegIndex = 0;
|
USHORT usRegIndex = 0;
|
||||||
|
@ -112,23 +157,72 @@ eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer
|
||||||
{
|
{
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] = usRegAddr;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ;
|
||||||
ucMBFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2;
|
ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2;
|
||||||
ucMBFrame += MB_PDU_FUNC_WRITE_MUL_VALUES_OFF;
|
ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
|
||||||
while( usNRegs > usRegIndex)
|
while( usNRegs > usRegIndex)
|
||||||
{
|
{
|
||||||
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
||||||
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
||||||
}
|
}
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + 2*usNRegs );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
UCHAR *ucMBFrame;
|
||||||
|
USHORT usRegAddress;
|
||||||
|
USHORT usRegCount;
|
||||||
|
UCHAR ucRegByteCount;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen == MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE )
|
||||||
|
{
|
||||||
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
|
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] << 8 );
|
||||||
|
usRegCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
ucRegByteCount = ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF];
|
||||||
|
|
||||||
|
if( ucRegByteCount == 2 * usRegCount )
|
||||||
|
{
|
||||||
|
/* Make callback to update the register values. */
|
||||||
|
eRegStatus =
|
||||||
|
eMBRegHoldingCB( &ucMBFrame[MB_PDU_REQ_WRITE_MUL_VALUES_OFF],
|
||||||
|
usRegAddress, usRegCount, MB_REG_WRITE );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid request because the length is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||||
|
@ -146,23 +240,69 @@ eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRe
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER;
|
||||||
ucMBFrame[MB_PDU_FUNC_READ_ADDR_OFF] = usRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] = usRegAddr;
|
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
|
||||||
ucMBFrame[MB_PDU_FUNC_READ_REGCNT_OFF] = usNRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] = usNRegs;
|
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
UCHAR *ucMBFrame;
|
||||||
|
USHORT usRegAddress;
|
||||||
|
USHORT usRegCount;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN)
|
||||||
|
{
|
||||||
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
|
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] << 8 );
|
||||||
|
usRegCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
/* Check if the number of registers to read is valid. If not
|
||||||
|
* return Modbus illegal data value exception.
|
||||||
|
*/
|
||||||
|
if( ( usRegCount >= 1 ) && ( 2 * usRegCount == pucFrame[MB_PDU_FUNC_READ_BYTECNT_OFF] ) )
|
||||||
|
{
|
||||||
|
/* Make callback to fill the buffer. */
|
||||||
|
eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usRegCount, MB_REG_READ );
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid request because the length is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||||
|
|
||||||
eMBMasterReqErrCode
|
eMBMasterReqErrCode
|
||||||
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer, USHORT usReadRegAddr, USHORT usNReadRegs ,
|
eMBMasterReqReadWriteMultipleHoldingRegister(UCHAR ucSndAddr, USHORT * pusDataBuffer, USHORT usWriteRegAddr,
|
||||||
USHORT usWriteRegAddr, USHORT usNWriteRegs)
|
USHORT usNWriteRegs, USHORT usReadRegAddr, USHORT usNReadRegs)
|
||||||
{
|
{
|
||||||
UCHAR *ucMBFrame;
|
UCHAR *ucMBFrame;
|
||||||
USHORT usRegIndex = 0;
|
USHORT usRegIndex = 0;
|
||||||
|
@ -175,27 +315,80 @@ eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBu
|
||||||
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
vMBMasterSetDestAddress(ucSndAddr);
|
vMBMasterSetDestAddress(ucSndAddr);
|
||||||
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
|
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr;
|
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ;
|
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ;
|
||||||
ucMBFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF] = usNWriteRegs * 2;
|
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2;
|
||||||
ucMBFrame += MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF;
|
ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF;
|
||||||
while( usNWriteRegs > usRegIndex)
|
while( usNWriteRegs > usRegIndex)
|
||||||
{
|
{
|
||||||
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
|
||||||
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
|
||||||
}
|
}
|
||||||
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN + 2*usNWriteRegs );
|
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs );
|
||||||
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
|
||||||
}
|
}
|
||||||
return eErrStatus;
|
return eErrStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegReadAddress;
|
||||||
|
USHORT usRegReadCount;
|
||||||
|
USHORT usRegWriteAddress;
|
||||||
|
USHORT usRegWriteCount;
|
||||||
|
UCHAR *ucMBFrame;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN)
|
||||||
|
{
|
||||||
|
vMBMasterGetPDUSndBuf(&ucMBFrame);
|
||||||
|
usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U );
|
||||||
|
usRegReadAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] );
|
||||||
|
usRegReadAddress++;
|
||||||
|
|
||||||
|
usRegReadCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] << 8U );
|
||||||
|
usRegReadCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
usRegWriteAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] << 8U );
|
||||||
|
usRegWriteAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] );
|
||||||
|
usRegWriteAddress++;
|
||||||
|
|
||||||
|
usRegWriteCount = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] << 8U );
|
||||||
|
usRegWriteCount |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
if( ( 2 * usRegReadCount ) == pucFrame[MB_PDU_FUNC_READWRITE_READ_BYTECNT_OFF] )
|
||||||
|
{
|
||||||
|
/* Make callback to update the register values. */
|
||||||
|
eRegStatus = eMBRegHoldingCB( &ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF],
|
||||||
|
usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
|
||||||
|
|
||||||
|
if( eRegStatus == MB_ENOERR )
|
||||||
|
{
|
||||||
|
/* Make the read callback. */
|
||||||
|
eRegStatus = eMBRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF],
|
||||||
|
usRegReadAddress, usRegReadCount, MB_REG_READ);
|
||||||
|
}
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -203,6 +203,27 @@ eMBMasterReqErrCode
|
||||||
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer, USHORT usReadRegAddr, USHORT usNReadRegs ,
|
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer, USHORT usReadRegAddr, USHORT usNReadRegs ,
|
||||||
USHORT usWriteRegAddr, USHORT usNWriteRegs);
|
USHORT usWriteRegAddr, USHORT usNWriteRegs);
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
eMBException
|
||||||
|
eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
|
||||||
/*£¡ \ingroup modbus
|
/*£¡ \ingroup modbus
|
||||||
*\brief These functions are interface for Modbus Master
|
*\brief These functions are interface for Modbus Master
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -101,33 +101,39 @@ BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void );
|
||||||
*/
|
*/
|
||||||
static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
||||||
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
||||||
|
//TODO Add Master function define
|
||||||
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||||
|
//TODO Add Master function define
|
||||||
{MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
|
{MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||||
{MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
|
{MB_FUNC_READ_HOLDING_REGISTER, eMBMasterFuncReadHoldingRegister},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||||
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
|
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBMasterFuncWriteMultipleHoldingRegister},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
||||||
{MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
|
{MB_FUNC_WRITE_REGISTER, eMBMasterFuncWriteHoldingRegister},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||||
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
|
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBMasterFuncReadWriteMultipleHoldingRegister},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_READ_COILS_ENABLED > 0
|
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||||
|
//TODO Add Master function define
|
||||||
{MB_FUNC_READ_COILS, eMBFuncReadCoils},
|
{MB_FUNC_READ_COILS, eMBFuncReadCoils},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||||
|
//TODO Add Master function define
|
||||||
{MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
|
{MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||||
|
//TODO Add Master function define
|
||||||
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
|
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
|
||||||
#endif
|
#endif
|
||||||
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
||||||
|
//TODO Add Master function define
|
||||||
{MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
|
{MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue