|
/* ----------------------- Modbus includes ----------------------------------*/
|
#include "mb.h"
|
//#include "mbport.h"
|
#include "regport.h"
|
|
//¼Ä´æÆ÷µØÖ·¼ä¾à
|
#define INDEX_BASE 100
|
/* ----------------------- Defines ------------------------------------------*/
|
#define REG_INPUT_START 1000
|
#define REG_INPUT_NREGS 4
|
|
/* ----------------------- Static variables ---------------------------------*/
|
static USHORT usRegInputStart = REG_INPUT_START;
|
static USHORT usRegInputBuf[REG_INPUT_NREGS];
|
|
/* ----------------------- Start implementation -----------------------------*/
|
/**
|
* @brief ¶ÁÊäÈë¼Ä´æÆ÷
|
* @param pucRegBuffer£º·µ»ØÊý¾ÝÖ¸Õë
|
usAddress £ºÊäÈë¼Ä´æÆ÷ÆðʼµØÖ·
|
usNRegs £ºÒª¶ÁµÄ¼Ä´æÆ÷ÊýÄ¿
|
* @retval ´íÎó´úÂë
|
*/
|
eMBErrorCode
|
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
|
{
|
eMBErrorCode eStatus = MB_ENOERR;
|
int iRegIndex;
|
|
if( ( usAddress >= REG_INPUT_START )
|
&& ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
|
{
|
iRegIndex = ( int )( usAddress - usRegInputStart );
|
while( usNRegs > 0 )
|
{
|
*pucRegBuffer++ =
|
( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
|
*pucRegBuffer++ =
|
( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
|
iRegIndex++;
|
usNRegs--;
|
}
|
}
|
else
|
{
|
eStatus = MB_ENOREG;
|
}
|
|
return eStatus;
|
}
|
/**
|
* @brief ¶Á/д±£³Ö¼Ä´æÆ÷
|
* @param pucRegBuffer£ºÊý¾ÝÖ¸Õë
|
usAddress £ºÊäÈë¼Ä´æÆ÷ÆðʼµØÖ·
|
usNRegs £ºÒª²Ù×÷µÄ¼Ä´æÆ÷ÊýÄ¿
|
eMode : ¶Ô±£³Ö¼Ä´æÆ÷µÄ²Ù×÷£¨¶Á»òд£©
|
* @retval ´íÎó´úÂë
|
*/
|
eMBErrorCode
|
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
|
eMBRegisterMode eMode )
|
{
|
eMBErrorCode eStatus = MB_ENOERR;
|
int iRegIndex;
|
volatile uint16_t * data_pointer;
|
volatile uint16_t my_regAddr;
|
|
// my_regAddr = usAddress - 1;
|
// switch(my_regAddr)
|
// {
|
// case VALVE_DTU_OUT_FLAG:
|
// data_pointer= (void *)&modbus_dtu_out_para;
|
// break;
|
// default:break;
|
// }
|
if(( usAddress >= VALVE_DTU_OUT_SADDR ) &&
|
( usAddress + usNRegs <= VALVE_DTU_OUT_SADDR + VALVE_DTU_OUT_NREGS ) )
|
{
|
data_pointer= (void *)&modbus_dtu_out_para;
|
}
|
if(( usAddress >= VALVE_DTU_OUT_SADDR ) &&
|
( usAddress + usNRegs <= VALVE_DTU_OUT_SADDR + VALVE_DTU_OUT_NREGS ) )
|
{
|
iRegIndex = ( int )( usAddress - VALVE_DTU_OUT_SADDR);
|
switch ( eMode )
|
{
|
/* Pass current register values to the protocol stack. */
|
case MB_REG_READ:
|
while( usNRegs > 0 )
|
{
|
*pucRegBuffer++ =
|
( unsigned char )( *(data_pointer + iRegIndex) >> 8 );
|
*pucRegBuffer++ =
|
( unsigned char )( *(data_pointer + iRegIndex) & 0xFF );
|
iRegIndex++;
|
usNRegs--;
|
}
|
break;
|
|
/* Update current register values with new values from the
|
* protocol stack. */
|
case MB_REG_WRITE:
|
while( usNRegs > 0 )
|
{
|
// usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
|
// usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
|
iRegIndex++;
|
usNRegs--;
|
}
|
}
|
}
|
else
|
{
|
eStatus = MB_ENOREG;
|
}
|
|
return eStatus;
|
}
|
|
|
eMBErrorCode
|
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
|
eMBRegisterMode eMode )
|
{
|
return MB_ENOREG;
|
}
|
|
eMBErrorCode
|
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
|
{
|
return MB_ENOREG;
|
}
|