forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

jinlicong
2024-04-25 a12102b90bda2f409babc8c241bfc66118a8cb70
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
#include "rs485_data_analysis_pro.h"
#include "rs485_read_data.h"
#include "mbcrc.h"
 
FLOW_METER_DATA_T    flow_meter_data_g;
 
/************ͨÓÃmodbus¶Áȡ֡*************************/
void sendModbusReadFrame(uint8_t slaveAddress,uint16_t dataAddress,uint8_t regNum)
{
    uint16_t crc;
    uint8_t sendFrame[8];
    
    sendFrame[0] = slaveAddress;         //´Ó»úµØÖ·
    sendFrame[1] = 0x03;                 //¹¦ÄܺÅ
    sendFrame[2] = dataAddress>>8;       //Êý¾ÝµØÖ·¸ß8λ
    sendFrame[3] = dataAddress&0x00FF;   //Êý¾ÝµØÖ·µÍ8λ
    sendFrame[4] = regNum>>8;       //¶ÁÈ¡Êý¾Ý¸öÊý¸ß8λ
    sendFrame[5] = regNum&0x00FF;   //¶ÁÈ¡Êý¾Ý¸öÊýµÍ8λ
  crc = usMBCRC16(0xFFFF,sendFrame,6);
  sendFrame[6] = crc&0x00FF;           //CRC16УÑéµÍ8λ
  sendFrame[7] = crc>>8;               //CRC16УÑé¸ß8λ
    
    //´®¿Ú·¢ËÍ
//  RS485_Send_Data(sendFrame,8);
    
    
    
    
    
    //ÖØÖÃDMA½ÓÊÕ
    
    
}
 
/************×Ô¶¨Òå¶Áȡ֡*************************/
 
 
 
 
 
 
 
/************¸÷ÐͺÅÊý¾Ý½âÎö*************************/
//modbusЭÒé03¶ÁÈ¡ÏìÓ¦Êý¾Ýͳһ¼ì²é
ErrorStatus    modbus03AnswerCheck(uint8_t * buf_p,uint8_t slaveAddress)
{
    uint16_t    crc;
    uint8_t    dataNum;
    //Ö÷Òª¼ì²éCRC£¬¿¼ÂÇ´Ó»úµØÖ·ºÍ¹¦ÄÜÂëµÄ¼ì²é
    if((slaveAddress!=buf_p[0])||(buf_p[1]!=0x03))
        return FAIL;
 
    
    dataNum = buf_p[2];
    if(dataNum>250)
            return FAIL;
    crc = buf_p[3 + dataNum +1 ];
    crc = (crc<<8) +buf_p[3 + dataNum ];
    if(crc != usMBCRC16(0xFFFF,buf_p,3 + dataNum))
        return FAIL;
    
    return PASS;
}
 
 
ErrorStatus    SIZHUMODBUSV2_0_dataAnalysis(uint8_t * buf_p,uint8_t slaveAddress)
{
    uint32_t SC_Accumulation_zs;
    uint16_t SC_Accumulation_xs;
    uint32_t WC_Accumulation_zs;
    uint16_t  WC_Accumulation_xs;
    uint32_t SC_Accumulation_zs_reverse;
    uint16_t SC_Accumulation_xs_reverse;
    uint32_t WC_Accumulation_zs_reverse;
    uint16_t  WC_Accumulation_xs_reverse;
    
    double WC_Accumulation_reverse;
    double SC_Accumulation_reverse;
    
    if(modbus03AnswerCheck(buf_p,slaveAddress) == FAIL)
        return FAIL;
    
    
    arrayA_2_arrayB(buf_p + 3 , (uint8_t *)&WC_Accumulation_zs, 4, BIG_ENDIAN) ;
    arrayA_2_arrayB(buf_p + 7 , (uint8_t *)&WC_Accumulation_xs, 2, BIG_ENDIAN) ;    
    arrayA_2_arrayB(buf_p + 14 , (uint8_t *)&WC_Accumulation_xs, 2, BIG_ENDIAN) ;    
        
    return PASS;
    
}
 
 
 
 
/**********************·µ»ØÊý¾Ý·ÖÎö*******************************/
ErrorStatus    rs485ComDataAnalysis(RS485_COM_UART_PARA_T * uart_para_p,uint16_t device_code,uint8_t slaveAddress)
{
    uint8_t *buf_p;
    if(uart_para_p->recv_length < 6)  //ĿǰӦ¸ÃûÓöµ½¹ý·µ»ØÊý¾ÝÉÙÓÚ6×Ö½ÚµÄ
        return FAIL;
    
    //È¥0
    for(uint16_t count_i=0;count_i< RS485_COM_REC_BUF_LENGTH;count_i ++)
    {
        if(uart_para_p->recv_buf[count_i] !=0)
        {
            buf_p = &uart_para_p->recv_buf[count_i];
            break;
        }else
        {
            if(count_i > (RS485_COM_REC_BUF_LENGTH >>2))
            {
                //buf¹ý°ë¶¼ÊÇ0£¬Ö±½Ó·µ»ØFAIL£¬·ÀÖ¹ºóÃæÊý¾Ý´¦ÀíÔ½½ç
                return FAIL;
            }
        }
    }
    
    switch (device_code){
        
                case SIZHUMODBUS1:        return FAIL;
                case SIZHUMODBUS2:    return FAIL;
                case SIZHUMODBUS3:    return FAIL;
        case SIZHUMODBUSV2_0:        return SIZHUMODBUSV2_0_dataAnalysis(buf_p,slaveAddress);
        case TIANXINCUSTOM:        return FAIL;
                case TIANXINMODBUS:    return FAIL;
                //ÐèÒª²¹³ä......
        default:return FAIL;
    }
    
    
    
}