forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

jinlicong
2024-06-06 b39a40b0191e0a8b155db74e885ff66e17b91896
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
 
/* ----------------------- 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;
}