123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #include "stdlib.h"
- #include "string.h"
- #include "port.h"
- #include "mb.h"
- #include "mbframe.h"
- #include "mbproto.h"
- #include "mbconfig.h"
- #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
- #define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
- #define MB_PDU_FUNC_READ_SIZE ( 4 )
- #define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
- eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
- #if MB_FUNC_READ_COILS_ENABLED > 0
- eMBException
- eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
- {
- USHORT usRegAddress;
- USHORT usDiscreteCnt;
- UCHAR ucNBytes;
- UCHAR *pucFrameCur;
- eMBException eStatus = MB_EX_NONE;
- eMBErrorCode eRegStatus;
- if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
- {
- usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
- usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
- usRegAddress++;
- usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
- usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
-
- if( ( usDiscreteCnt >= 1 ) &&
- ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
- {
-
- pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
- *usLen = MB_PDU_FUNC_OFF;
-
- *pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
- *usLen += 1;
-
- if( ( usDiscreteCnt & 0x0007 ) != 0 )
- {
- ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
- }
- else
- {
- ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
- }
- *pucFrameCur++ = ucNBytes;
- *usLen += 1;
- eRegStatus =
- eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
-
- if( eRegStatus != MB_ENOERR )
- {
- eStatus = prveMBError2Exception( eRegStatus );
- }
- else
- {
-
- *usLen += ucNBytes;;
- }
- }
- else
- {
- eStatus = MB_EX_ILLEGAL_DATA_VALUE;
- }
- }
- else
- {
-
- eStatus = MB_EX_ILLEGAL_DATA_VALUE;
- }
- return eStatus;
- }
- #endif
|