From 5de9d82e099448ec6a2129f5aae5a31ca208882f Mon Sep 17 00:00:00 2001 From: armink Date: Mon, 16 Sep 2013 13:35:57 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91F?= =?UTF-8?q?reeModbus=E4=B8=BB=E6=9C=BA=E5=AF=84=E5=AD=98=E5=99=A8=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=8A=9F=E8=83=BD=E7=9A=84=E5=AE=9E=E7=8E=B0=EF=BC=8C?= =?UTF-8?q?=E7=9B=AE=E5=89=8D=E6=98=AF=E5=BC=82=E6=AD=A5=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E5=90=8E=E6=9C=9F=E5=8F=AF=E4=BB=A5=E8=80=83=E8=99=91?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=90=8C=E6=AD=A5=E6=A8=A1=E5=BC=8F=EF=BC=9B?= =?UTF-8?q?=202=E3=80=81=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91FreeModbus?= =?UTF-8?q?=E4=B8=BB=E6=9C=BA=E5=AF=84=E5=AD=98=E5=99=A8=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=EF=BC=8C=E4=BD=8D=E4=BA=8E?= =?UTF-8?q?app=5Ftask.c=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- APP/src/app_task.c | 7 +- FreeModbus/modbus/functions/mbfuncholding_m.c | 285 +++++++++++++++--- FreeModbus/modbus/include/mb_m.h | 21 ++ FreeModbus/modbus/mb_m.c | 14 +- 4 files changed, 274 insertions(+), 53 deletions(-) diff --git a/APP/src/app_task.c b/APP/src/app_task.c index 56a4e4c..3c8fee7 100644 --- a/APP/src/app_task.c +++ b/APP/src/app_task.c @@ -48,9 +48,10 @@ void thread_entry_SysMonitor(void* parameter) //Test Modbus Master usModbusUserData[0] = (USHORT)(rt_tick_get()/10); usModbusUserData[1] = (USHORT)(rt_tick_get()%10); -// eMBMasterFuncWriteHoldingRegister(1,usModbusUserData,3); -// eMBMasterFuncWriteMultipleHoldingRegister(1,usModbusUserData,3,2); - eMBMasterReqReadHoldingRegister(1,3,2); + eMBMasterReqWriteHoldingRegister(1,usModbusUserData,3); +// eMBMasterReqWriteMultipleHoldingRegister(1,usModbusUserData,3,2); +// eMBMasterReqReadHoldingRegister(1,3,2); +// eMBMasterReqReadWriteMultipleHoldingRegister(1,usModbusUserData,3,2,5,2); } } diff --git a/FreeModbus/modbus/functions/mbfuncholding_m.c b/FreeModbus/modbus/functions/mbfuncholding_m.c index 5cc2fbd..6676339 100644 --- a/FreeModbus/modbus/functions/mbfuncholding_m.c +++ b/FreeModbus/modbus/functions/mbfuncholding_m.c @@ -43,31 +43,44 @@ #include "mbconfig.h" /* ----------------------- Defines ------------------------------------------*/ -#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0) -#define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 ) -#define MB_PDU_FUNC_READ_SIZE ( 4 ) +#define MB_PDU_REQ_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 ) +#define MB_PDU_REQ_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 ) +#define MB_PDU_REQ_READ_SIZE ( 4 ) #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_VALUE_OFF ( MB_PDU_DATA_OFF + 2 ) #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_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_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_WRITE_MUL_SIZE ( 4 ) -#define MB_PDU_FUNC_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_FUNC_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_FUNC_READWRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 ) -#define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 ) -#define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 9 ) +#define MB_PDU_REQ_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 ) +#define MB_PDU_REQ_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 ) +#define MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 ) +#define MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 ) +#define MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 ) +#define MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 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 ---------------------------------*/ +eMBException prveMBError2Exception( eMBErrorCode eErrorCode ); /* ----------------------- Start implementation -----------------------------*/ #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0 @@ -86,21 +99,53 @@ eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT * pusDataBuffer, USHOR vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterSetDestAddress(ucSndAddr); ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER; - ucMBFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] = usRegAddr >> 8; - ucMBFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] = usRegAddr; - ucMBFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] = pusDataBuffer[0] >> 8; - ucMBFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] = pusDataBuffer[0] ; - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_SIZE ); + ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr; + ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = pusDataBuffer[0] >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = pusDataBuffer[0] ; + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); } 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 #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0 eMBMasterReqErrCode -eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer, USHORT usRegAddr, USHORT usNRegs ) +eMBMasterReqWriteMultipleHoldingRegister(UCHAR ucSndAddr, + USHORT * pusDataBuffer, USHORT usRegAddr, USHORT usNRegs) { UCHAR *ucMBFrame; USHORT usRegIndex = 0; @@ -112,23 +157,72 @@ eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer { vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterSetDestAddress(ucSndAddr); - ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS; - ucMBFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8; - ucMBFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] = usRegAddr; - ucMBFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8; - ucMBFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ; - ucMBFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2; - ucMBFrame += MB_PDU_FUNC_WRITE_MUL_VALUES_OFF; + ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ; + ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2; + ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF; while( usNRegs > usRegIndex) { *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8; *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 ); } 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 #if MB_FUNC_READ_HOLDING_ENABLED > 0 @@ -146,23 +240,69 @@ eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRe vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterSetDestAddress(ucSndAddr); ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER; - ucMBFrame[MB_PDU_FUNC_READ_ADDR_OFF] = usRegAddr >> 8; - ucMBFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] = usRegAddr; - ucMBFrame[MB_PDU_FUNC_READ_REGCNT_OFF] = usNRegs >> 8; - ucMBFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] = usNRegs; - vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE ); + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr; + ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8; + ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs; + vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); } 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 #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0 eMBMasterReqErrCode -eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer, USHORT usReadRegAddr, USHORT usNReadRegs , - USHORT usWriteRegAddr, USHORT usNWriteRegs) +eMBMasterReqReadWriteMultipleHoldingRegister(UCHAR ucSndAddr, USHORT * pusDataBuffer, USHORT usWriteRegAddr, + USHORT usNWriteRegs, USHORT usReadRegAddr, USHORT usNReadRegs) { UCHAR *ucMBFrame; USHORT usRegIndex = 0; @@ -175,27 +315,80 @@ eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBu vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterSetDestAddress(ucSndAddr); ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS; - ucMBFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8; - ucMBFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr; - ucMBFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8; - ucMBFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ; - ucMBFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8; - ucMBFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr; - ucMBFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8; - ucMBFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ; - ucMBFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF] = usNWriteRegs * 2; - ucMBFrame += MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF; + ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr; + ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8; + ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ; + ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2; + ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF; while( usNWriteRegs > usRegIndex) { *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8; *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 ); } 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 diff --git a/FreeModbus/modbus/include/mb_m.h b/FreeModbus/modbus/include/mb_m.h index f4ee5b5..3db04e2 100644 --- a/FreeModbus/modbus/include/mb_m.h +++ b/FreeModbus/modbus/include/mb_m.h @@ -203,6 +203,27 @@ eMBMasterReqErrCode eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,USHORT * pusDataBuffer, USHORT usReadRegAddr, USHORT usNReadRegs , 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 *\brief These functions are interface for Modbus Master */ diff --git a/FreeModbus/modbus/mb_m.c b/FreeModbus/modbus/mb_m.c index eb16ef1..ee4cd38 100644 --- a/FreeModbus/modbus/mb_m.c +++ b/FreeModbus/modbus/mb_m.c @@ -101,33 +101,39 @@ BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void ); */ static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = { #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0 + //TODO Add Master function define {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID}, #endif #if MB_FUNC_READ_INPUT_ENABLED > 0 + //TODO Add Master function define {MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister}, #endif #if MB_FUNC_READ_HOLDING_ENABLED > 0 - {MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister}, + {MB_FUNC_READ_HOLDING_REGISTER, eMBMasterFuncReadHoldingRegister}, #endif #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0 - {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister}, + {MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBMasterFuncWriteMultipleHoldingRegister}, #endif #if MB_FUNC_WRITE_HOLDING_ENABLED > 0 - {MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister}, + {MB_FUNC_WRITE_REGISTER, eMBMasterFuncWriteHoldingRegister}, #endif #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0 - {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister}, + {MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBMasterFuncReadWriteMultipleHoldingRegister}, #endif #if MB_FUNC_READ_COILS_ENABLED > 0 + //TODO Add Master function define {MB_FUNC_READ_COILS, eMBFuncReadCoils}, #endif #if MB_FUNC_WRITE_COIL_ENABLED > 0 + //TODO Add Master function define {MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil}, #endif #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0 + //TODO Add Master function define {MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils}, #endif #if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0 + //TODO Add Master function define {MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs}, #endif };