update
This commit is contained in:
296
leakage_system/usr/protocol/proto_modbus_lib.c
Normal file
296
leakage_system/usr/protocol/proto_modbus_lib.c
Normal file
@@ -0,0 +1,296 @@
|
||||
#include "proto_modbus_lib.h"
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
#include "bsp_Uart.h"
|
||||
|
||||
/**************************************************
|
||||
*<2A><><EFBFBD>ƣ<EFBFBD> modbus_lib_crc16
|
||||
*<2A><><EFBFBD>ܣ<EFBFBD> modbus crc16У<36><D0A3>
|
||||
*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> p_data -- У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
|
||||
* len -- <20><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||||
*<2A><><EFBFBD>أ<EFBFBD> У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
**************************************************/
|
||||
u16 modbus_lib_crc16(u8 *p_data, u16 len)
|
||||
{
|
||||
u16 crc16 = 0xFFFF;
|
||||
u16 i, j;
|
||||
for (i = 0; i < len; i++) {
|
||||
crc16 ^= p_data[i];
|
||||
for (j = 0; j < 8; j++) {
|
||||
if (crc16 & 0x0001) {
|
||||
crc16 >>= 1;
|
||||
crc16 ^= 0xA001;
|
||||
} else {
|
||||
crc16 >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc16;
|
||||
}
|
||||
|
||||
/************************************
|
||||
* <20><><EFBFBD><EFBFBD>: Float2u16
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>16<31><36><EFBFBD><EFBFBD>16λ
|
||||
* <20><><EFBFBD><EFBFBD>: float_data : Ҫת<D2AA><D7AA><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
|
||||
h_l : <20><><EFBFBD>ظ<EFBFBD>16λ<36><CEBB><EFBFBD>ǵ<EFBFBD>16λ
|
||||
* <20><><EFBFBD><EFBFBD>: <20><><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD>ĸ<EFBFBD>16λ<36><CEBB><EFBFBD>ǵ<EFBFBD>16λ
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
u16 float_to_u16(float float_data,unsigned char h_l)
|
||||
{
|
||||
u16 temp = 0XFFFF;
|
||||
if(h_l == U16_DATA_L)
|
||||
{
|
||||
temp = *((u32 *)&float_data) & 0x0000ffff;
|
||||
}
|
||||
else if(h_l == U16_DATA_H)
|
||||
{
|
||||
temp = ((*((u32 *)&float_data)) >> 16) & 0x0000ffff;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: u32TOu16
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>ȡu32<33>ĸ<EFBFBD>16<31><36><EFBFBD><EFBFBD>16λ
|
||||
* <20><><EFBFBD><EFBFBD>: u32_data : Ҫת<D2AA><D7AA><EFBFBD><EFBFBD>u32
|
||||
h_l : <20><><EFBFBD>ظ<EFBFBD>16λ<36><CEBB><EFBFBD>ǵ<EFBFBD>16λ
|
||||
* <20><><EFBFBD><EFBFBD>: <20><><EFBFBD>ض<EFBFBD>Ӧ<EFBFBD>ĸ<EFBFBD>16λ<36><CEBB><EFBFBD>ǵ<EFBFBD>16λ
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
u16 u32_to_u16(u32 u32_data,unsigned char h_l)
|
||||
{
|
||||
u16 temp = 0XFFFF;
|
||||
if(h_l == U16_DATA_L)
|
||||
{
|
||||
temp = u32_data & 0x0000ffff;
|
||||
}
|
||||
else if(h_l == U16_DATA_H)
|
||||
{
|
||||
temp = (u32_data >> 16) & 0x0000ffff;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
void u32_to_u8(u32 u32_data,u8 *p_data,u8 endian)
|
||||
{
|
||||
if(BIG_ENDIAN == endian)
|
||||
{
|
||||
for(u8 i = 0;i < 4;i++)
|
||||
{
|
||||
p_data[i] = (u32_data >> (8 * (3 - i))) & 0xff;
|
||||
}
|
||||
}
|
||||
else if(LITTLE_ENDIAN == endian)
|
||||
{
|
||||
for(u8 i = 0;i < 4;i++)
|
||||
{
|
||||
p_data[i] = (u32_data >> (8 * i)) & 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void u16_to_u8(u16 u16_data,u8 *p_data,u8 endian)
|
||||
{
|
||||
if(BIG_ENDIAN == endian)
|
||||
{
|
||||
for(u8 i = 0;i < 2;i++)
|
||||
{
|
||||
p_data[i] = (u16_data >> (8 * (1 - i))) & 0xff;
|
||||
}
|
||||
}
|
||||
else if(LITTLE_ENDIAN == endian)
|
||||
{
|
||||
for(u8 i = 0;i < 2;i++)
|
||||
{
|
||||
p_data[i] = (u16_data >> (8 * i)) & 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float u32_to_float(u32 u32_data,unsigned char h_l)
|
||||
{
|
||||
float temp = 0;
|
||||
temp = *((float *)&u32_data);
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*<2A><>u8_endian<61><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>p_u8dataת<61><D7AA>Ϊ<EFBFBD><CEAA>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>С<EFBFBD><D0A1><EFBFBD><EFBFBD>*/
|
||||
u32 u8_to_u32(u8 *p_u8data,unsigned char u8_endian)
|
||||
{
|
||||
u32 temp = 0;
|
||||
if(BIG_ENDIAN == u8_endian)
|
||||
{
|
||||
for(u8 i = 0;i < 4;i++)
|
||||
{
|
||||
temp |= p_u8data[i] << ((3 - i) * 8);
|
||||
}
|
||||
}
|
||||
else if(LITTLE_ENDIAN == u8_endian)
|
||||
{
|
||||
for(u8 i = 0;i < 4;i++)
|
||||
{
|
||||
temp |= p_u8data[i] << (i * 8);
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*<2A><>u8_endian<61><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>p_u8dataת<61><D7AA>Ϊ<EFBFBD><CEAA>Ƭ<EFBFBD><C6AC><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>С<EFBFBD><D0A1><EFBFBD><EFBFBD>*/
|
||||
u16 u8_to_u16(u8 *p_u8data,unsigned char u8_endian)
|
||||
{
|
||||
u32 temp = 0;
|
||||
if(BIG_ENDIAN == u8_endian)
|
||||
{
|
||||
for(u8 i = 0;i < 2;i++)
|
||||
{
|
||||
temp |= p_u8data[i] << ((1 - i) * 8);
|
||||
}
|
||||
}
|
||||
else if(LITTLE_ENDIAN == u8_endian)
|
||||
{
|
||||
for(u8 i = 0;i < 2;i++)
|
||||
{
|
||||
temp |= p_u8data[i] << (i * 8);
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*<2A>ֽڶ<D6BD><DAB6><EFBFBD>ת<EFBFBD><D7AA>*/
|
||||
u32 u32_endian_conv(u32 data)
|
||||
{
|
||||
u32 temp = 0;
|
||||
temp = (((data >> 0 ) & 0xff) << 24)
|
||||
| (((data >> 8 ) & 0xff) << 16)
|
||||
| (((data >> 16) & 0xff) << 8 )
|
||||
| (((data >> 24) & 0xff) << 0 );
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*<2A>ֽڶ<D6BD><DAB6><EFBFBD>ת<EFBFBD><D7AA>*/
|
||||
u16 u16_endian_conv(u16 data)
|
||||
{
|
||||
u16 temp = 0;
|
||||
temp = (((data >> 0) & 0xff) << 8)
|
||||
| (((data >> 8) & 0xff) << 0);
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: ModbusReaddata
|
||||
* <20><><EFBFBD><EFBFBD>: MODBUS<55><53><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: Id :MODBUS<55>ӻ<EFBFBD><D3BB><EFBFBD>ַ
|
||||
* addr :д<><D0B4><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD>ʼ<EFBFBD><CABC>ַ
|
||||
* Num :д<>ļĴ<C4BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* send :<3A><><EFBFBD>ڷ<EFBFBD><DAB7>ͺ<EFBFBD><CDBA><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
void modbus_lib_data_read(u8 id,u16 addr,u16 num,void (*send)(u8 *,u16 ))
|
||||
{
|
||||
u8 tx[8];
|
||||
u16 crc16;
|
||||
tx[0] = id;
|
||||
tx[1] = 0x03;//0x03;
|
||||
tx[2] = (addr >> 8) & 0xff;
|
||||
tx[3] = addr & 0xff;
|
||||
tx[4] = (num >> 8) & 0xff;
|
||||
tx[5] = num & 0xff;
|
||||
crc16 = modbus_lib_crc16(tx,6);
|
||||
tx[6] = crc16 & 0xff;
|
||||
tx[7] = (crc16 >> 8) & 0xff;
|
||||
send(tx,8);
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: modbus_multiple_data_write
|
||||
* <20><><EFBFBD><EFBFBD>: MODBUSд<53><D0B4><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: Id :MODBUS<55>ӻ<EFBFBD><D3BB><EFBFBD>ַ
|
||||
* addr :д<><D0B4><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD>ʼ<EFBFBD><CABC>ַ
|
||||
* Num :д<>ļĴ<C4BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* data :д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* send :<3A><><EFBFBD>ڷ<EFBFBD><DAB7>ͺ<EFBFBD><CDBA><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
void modbus_lib_multiple_data_write(u8 id,u16 addr,u16 Num,u16 *data,void (*send)(u8 *,u16 ))
|
||||
{
|
||||
u8 tx[50];
|
||||
u16 crc16,i;
|
||||
tx[0] = id;
|
||||
tx[1] = 0x10;
|
||||
tx[2] = (addr >> 8) & 0xff;
|
||||
tx[3] = addr & 0xff;
|
||||
tx[4] = (Num >> 8) & 0xff;
|
||||
tx[5] = Num & 0xff;
|
||||
tx[6] = Num * 2;
|
||||
for(i = 0;i < tx[6]/2;i++)
|
||||
{
|
||||
tx[7 + 2 * i] = (data[i] >> 8) & 0xff;
|
||||
tx[8 + 2 * i] = data[i] & 0xff;
|
||||
}
|
||||
crc16 = modbus_lib_crc16(tx,7 + 2 * i);
|
||||
tx[7 + 2 * i] = crc16 & 0xff;
|
||||
tx[8 + 2 * i] = (crc16 >> 8) & 0xff;
|
||||
send(tx,9 + 2 * i);
|
||||
}
|
||||
|
||||
void modbus_lib_only_data_write(u8 id,u16 addr,u16 value,void (*send)(u8 *,u16 ))
|
||||
{
|
||||
u8 tx[8];
|
||||
u16 crc16;
|
||||
tx[0] = id;
|
||||
tx[1] = 0x06;
|
||||
tx[2] = (addr >> 8) & 0xff;
|
||||
tx[3] = addr & 0xff;
|
||||
tx[4] = (value >> 8) & 0xff;
|
||||
tx[5] = value & 0xff;
|
||||
crc16 = modbus_lib_crc16(tx,6);
|
||||
tx[6] = crc16 & 0xff;
|
||||
tx[7] = (crc16 >> 8) & 0xff;
|
||||
send(tx,8);
|
||||
}
|
||||
|
||||
|
||||
//01 10 00 02 00 01 02 12 34 CRC
|
||||
//id Func Startaddress RegNumber wdataaddress
|
||||
u8 modbus_lib_analysis(modbus_analysis_data_t *p_modbus, u8 *p_data, u16 len)
|
||||
{
|
||||
u16 crc16;
|
||||
u16 u16_temp;
|
||||
|
||||
if(NULL == p_modbus) return 0;
|
||||
if(len < 8) return 0;
|
||||
|
||||
crc16 = modbus_lib_crc16(p_data, len-2);
|
||||
u16_temp = *(p_data+len-1);
|
||||
u16_temp <<= 8;
|
||||
u16_temp |= *(p_data+len-2);
|
||||
if(u16_temp != crc16) return 0;
|
||||
|
||||
p_modbus->id = *p_data;
|
||||
p_modbus->func = *(p_data+1);
|
||||
|
||||
p_modbus->start_addr = (*(p_data+2))<<8;
|
||||
p_modbus->start_addr |= *(p_data+3);
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD>дһ<D0B4><D2BB><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>*/
|
||||
if(0x06 == p_modbus->func)
|
||||
{
|
||||
p_modbus->write_data_addr = p_data+4;
|
||||
}
|
||||
else if(0x10 == p_modbus->func) /*д<><D0B4><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>*/
|
||||
{
|
||||
p_modbus->reg_number = (*(p_data+4))<<8;
|
||||
p_modbus->reg_number |= *(p_data+5);
|
||||
p_modbus->write_data_addr = p_data+7;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_modbus->reg_number = (*(p_data+4))<<8;
|
||||
p_modbus->reg_number |= *(p_data+5);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
67
leakage_system/usr/protocol/proto_modbus_lib.h
Normal file
67
leakage_system/usr/protocol/proto_modbus_lib.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#ifndef _PROTO_MODBUS_LIB_H_
|
||||
#define _PROTO_MODBUS_LIB_H_
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#define MODBUS_DEFAULT_ID (0x01)
|
||||
#define MODBUS_SENDBUF_LEN (2048U)
|
||||
|
||||
#define U16_DATA_L (0)//<2F><>16λ
|
||||
#define U16_DATA_H (1)//<2F><>16λ
|
||||
|
||||
#define BIG_ENDIAN (0)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#define LITTLE_ENDIAN (1)//С<><D0A1><EFBFBD><EFBFBD>
|
||||
|
||||
typedef struct {
|
||||
u8 send_buffer[MODBUS_SENDBUF_LEN];
|
||||
u16 len;
|
||||
}modbus_communication_send_buf_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 id; /*ModbusID*/
|
||||
u8 func; /*<2A><><EFBFBD>ܺ<EFBFBD>*/
|
||||
u16 start_addr; /*<2A><>ʼ<EFBFBD><CABC>ַ*/
|
||||
u16 reg_number; /*<2A>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
u8 *write_data_addr; /*д<><D0B4><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ַ*/
|
||||
}modbus_analysis_data_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ModbusErrorCode_Success = 0x00,
|
||||
ModbusErrorCode_IllegalFunction = 0x01,
|
||||
ModbusErrorCode_IllegalAddr = 0x02,
|
||||
ModbusErrorCode_IllegalData = 0x03,
|
||||
ModbusErrorCode_DeviceBusy = 0x06
|
||||
}modbus_error_code_e;
|
||||
|
||||
typedef struct proto_Modbus_t proto_Modbus_t;
|
||||
struct proto_Modbus_t
|
||||
{
|
||||
u8 id;
|
||||
u16 (*data_read)(u16); /*Modbus<75><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
modbus_error_code_e (*data_write)(u16,u16); /*Modbusд<73><D0B4><EFBFBD><EFBFBD>*/
|
||||
void (*init)(void); /*<2A><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
void (*task)(void); /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
void (*data_analysis)(u8 *,u16,void *); /*<2A><><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>*/
|
||||
};
|
||||
|
||||
|
||||
u16 float_to_u16(float float_data,u8 h_l);
|
||||
u16 u32_to_u16(u32 u32_Data,u8 h_l);
|
||||
u32 u8_to_u32(u8 *p_u8_Data,u8 endian);
|
||||
u16 u8_to_u16(u8 *p_u8_Data,u8 endian);
|
||||
|
||||
void u32_to_u8(u32 u32_Data,u8 *p_data,u8 endian);
|
||||
void u16_to_u8(u16 u16_Data,u8 *p_data,u8 endian);
|
||||
|
||||
u32 u32_endian_conv(u32 data);
|
||||
u16 u16_endian_conv(u16 data);
|
||||
|
||||
u16 modbus_lib_crc16(u8 *p_data, u16 len);
|
||||
void modbus_lib_data_read(u8 Id,u16 Addr,u16 Num,void (*UsartSendBuffer)(u8 *,u16 ));
|
||||
void modbus_lib_multiple_data_write(u8 ID,u16 Addr,u16 Num,u16 *Data,void (*UsartSendBuffer)(u8 *,u16 ));
|
||||
void modbus_lib_only_data_write(u8 ID,u16 Addr,u16 Value,void (*UsartSendBuffer)(u8 *,u16 ));
|
||||
u8 modbus_lib_analysis(modbus_analysis_data_t *p_modbus, u8 *p_data, u16 len);
|
||||
|
||||
#endif
|
||||
286
leakage_system/usr/protocol/proto_modbus_master_leakage.c
Normal file
286
leakage_system/usr/protocol/proto_modbus_master_leakage.c
Normal file
@@ -0,0 +1,286 @@
|
||||
#include "proto_modbus_master_leakage.h"
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
#include "app.h"
|
||||
#include "app_timer.h"
|
||||
#include "bsp_Uart.h"
|
||||
#include "bsp_Flash.h"
|
||||
#include "app_leakage.h"
|
||||
|
||||
#include "proto_print.h"
|
||||
#include "proto_modbus_lib.h"
|
||||
|
||||
#define PROTO_LEAKAGE_READ_DATA_NUM (14) /*<2A><>ȡһ<C8A1><D2BB><EFBFBD>豸<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
#define PROTO_LEAKAGE_GET_CURR_DATA_START_ADDR (0x0000)
|
||||
|
||||
static void proto_leakage_init(proto_leakage_t *p_leakage);
|
||||
|
||||
static void proto_leakage_tx_task(proto_leakage_t *p_leakage);
|
||||
static void proto_leakage_rx_task(u8 *p_data,u16 len,void *other_data);
|
||||
|
||||
static void proto_leakage_com1_uart_send(u8 *p_data,u16 len);
|
||||
static void proto_leakage_com2_uart_send(u8 *p_data,u16 len);
|
||||
static void proto_leakage_com3_uart_send(u8 *p_data,u16 len);
|
||||
static void proto_leakage_com4_uart_send(u8 *p_data,u16 len);
|
||||
|
||||
proto_leakage_t modbus_leakage[APP_COM_NUM] =
|
||||
{
|
||||
/*COM1*/
|
||||
{
|
||||
.init = proto_leakage_init,
|
||||
.tx_task = proto_leakage_tx_task,
|
||||
.uart_send = proto_leakage_com1_uart_send,
|
||||
},
|
||||
/*COM2*/
|
||||
{
|
||||
.init = proto_leakage_init,
|
||||
.tx_task = proto_leakage_tx_task,
|
||||
.uart_send = proto_leakage_com2_uart_send,
|
||||
},
|
||||
/*COM3*/
|
||||
{
|
||||
.init = proto_leakage_init,
|
||||
.tx_task = proto_leakage_tx_task,
|
||||
.uart_send = proto_leakage_com3_uart_send,
|
||||
},
|
||||
/*COM4*/
|
||||
{
|
||||
.init = proto_leakage_init,
|
||||
.tx_task = proto_leakage_tx_task,
|
||||
.uart_send = proto_leakage_com4_uart_send,
|
||||
}
|
||||
};
|
||||
|
||||
static void proto_leakage_init(proto_leakage_t *p_leakage)
|
||||
{
|
||||
/*<2A><EFBFBD><F3B6A8B4>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
if(p_leakage == &modbus_leakage[APP_COM1])
|
||||
{
|
||||
com_to_uart[APP_COM1]->rx_data_analysis = proto_leakage_rx_task;
|
||||
}
|
||||
else if(p_leakage == &modbus_leakage[APP_COM2])
|
||||
{
|
||||
com_to_uart[APP_COM2]->rx_data_analysis = proto_leakage_rx_task;
|
||||
}
|
||||
else if(p_leakage == &modbus_leakage[APP_COM3])
|
||||
{
|
||||
com_to_uart[APP_COM3]->rx_data_analysis = proto_leakage_rx_task;
|
||||
}
|
||||
else if(p_leakage == &modbus_leakage[APP_COM4])
|
||||
{
|
||||
com_to_uart[APP_COM3]->rx_data_analysis = proto_leakage_rx_task;
|
||||
}
|
||||
/*<2A><><EFBFBD><EFBFBD>modbus_id<69>Ͷ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>app_com<6F><6D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
}
|
||||
|
||||
|
||||
static void proto_leakage_com1_uart_send(u8 *p_data,u16 len)
|
||||
{
|
||||
com_to_uart[APP_COM1]->send(com_to_uart[APP_COM1],p_data,len);
|
||||
}
|
||||
|
||||
static void proto_leakage_com2_uart_send(u8 *p_data,u16 len)
|
||||
{
|
||||
com_to_uart[APP_COM2]->send(com_to_uart[APP_COM2],p_data,len);
|
||||
}
|
||||
|
||||
static void proto_leakage_com3_uart_send(u8 *p_data,u16 len)
|
||||
{
|
||||
com_to_uart[APP_COM3]->send(com_to_uart[APP_COM3],p_data,len);
|
||||
}
|
||||
|
||||
static void proto_leakage_com4_uart_send(u8 *p_data,u16 len)
|
||||
{
|
||||
com_to_uart[APP_COM4]->send(com_to_uart[APP_COM4],p_data,len);
|
||||
}
|
||||
|
||||
/*<2A>л<EFBFBD><D0BB><EFBFBD>ȡ<EFBFBD><C8A1>©Һ<C2A9>ӿ<EFBFBD>*/
|
||||
static void proto_leakage_switch(proto_leakage_t *p_leakage)
|
||||
{
|
||||
p_leakage->sensor_index++;
|
||||
if(p_leakage->sensor_index >= p_leakage->sensor_num)
|
||||
{
|
||||
p_leakage->sensor_index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void proto_leakage_tx_curr_data_get(proto_leakage_t *p_leakage)
|
||||
{
|
||||
u16 addr = PROTO_LEAKAGE_GET_CURR_DATA_START_ADDR;
|
||||
u8 len = 14;
|
||||
u8 id = p_leakage->sensor[p_leakage->sensor_index].comm.id;
|
||||
modbus_lib_data_read(id,addr,len,p_leakage->uart_send);
|
||||
}
|
||||
|
||||
static void proto_leakage_tx_task(proto_leakage_t *p_leakage)
|
||||
{
|
||||
|
||||
proto_sensor_class_t *p_sensor;
|
||||
|
||||
p_sensor = &p_leakage->sensor[p_leakage->sensor_index];
|
||||
|
||||
if(0 == (p_sensor->comm.sensor_state_code & (0x00000001 << PROTO_LEAKAGE_STATE_CODE_TIME_OUT)))
|
||||
{
|
||||
if((++p_sensor->comm.tx_time_out_count) > 20)/*500ms<6D><73>ѯ 10<31><30>ͨѶ<CDA8><D1B6>ʱ*/
|
||||
{
|
||||
p_sensor->comm.sensor_state_code |= (0x00000001 << PROTO_LEAKAGE_STATE_CODE_TIME_OUT);
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
//memset(&gas_data[p_sensor->sensor_index],0,sizeof(gas_data_t));
|
||||
}
|
||||
}
|
||||
|
||||
switch(p_sensor->comm.state)
|
||||
{
|
||||
case PROTO_LEAKAGE_COMM_STATE_CURR_DATA_GET:
|
||||
{
|
||||
proto_leakage_tx_curr_data_get(p_leakage);
|
||||
}break;
|
||||
default:
|
||||
{
|
||||
|
||||
}break;
|
||||
}
|
||||
p_sensor->comm.state_send_time++;
|
||||
if(p_sensor->comm.state_send_time >= 3) /*<2A><><EFBFBD><EFBFBD><EFBFBD>쳣*/
|
||||
{
|
||||
p_sensor->comm.sensor_state_code |= (0x00000001 << p_sensor->comm.sensor_state_code);/*<2A><>¼<EFBFBD>쳣״̬*/
|
||||
p_sensor->comm.state_send_time = 0;
|
||||
p_sensor->comm.sensor_state_code = PROTO_LEAKAGE_COMM_STATE_DEFAULT;
|
||||
proto_leakage_switch(p_leakage); /*<2A>л<EFBFBD><D0BB>豸*/
|
||||
}
|
||||
}
|
||||
|
||||
static void proto_leakage_rx_task(u8 *p_data,u16 len,void *other_data)
|
||||
{
|
||||
u8 send_flag = 0;
|
||||
u8 modbus_id,cmd;
|
||||
u16 check_crc16,modbus_crc16;
|
||||
u16 *p_u16_temp;
|
||||
u16 i,ch;
|
||||
u8 *p_rx_valid,temp_value;
|
||||
|
||||
proto_sensor_class_t *p_sensor;
|
||||
proto_leakage_t *p_leakage = NULL;
|
||||
|
||||
/***********************<2A><><EFBFBD><EFBFBD>©Һ<C2A9><D2BA><EFBFBD><EFBFBD>**************************/
|
||||
if(other_data == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD>©Һmodbus<75><73><EFBFBD><EFBFBD>*/
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
if( (bsp_uart_t *)other_data == com_to_uart[i] )
|
||||
{
|
||||
p_leakage = &modbus_leakage[i];
|
||||
}
|
||||
}
|
||||
if(p_leakage == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
p_sensor = &p_leakage->sensor[p_leakage->sensor_index];
|
||||
|
||||
/***********************modbus<75><73><EFBFBD><EFBFBD>**************************/
|
||||
if(p_sensor->comm.id != p_data[0])
|
||||
{
|
||||
p_data = &p_data[1];
|
||||
len -= 1;
|
||||
}
|
||||
modbus_id = *p_data;
|
||||
cmd = *(p_data+1);
|
||||
check_crc16 = p_data[len-2] << 8 | p_data[len-1];
|
||||
|
||||
if(modbus_id != p_sensor->comm.id) return ;
|
||||
if(cmd != 0x03 && cmd != 0x06 && cmd != 0x10 && cmd != 0x41) return ;
|
||||
|
||||
modbus_crc16 = modbus_lib_crc16(p_data,len-2);
|
||||
modbus_crc16 = (modbus_crc16 >> 8) | (modbus_crc16 << 8);
|
||||
|
||||
if(check_crc16 != modbus_crc16) return ;
|
||||
|
||||
if(cmd == 0x41)
|
||||
{
|
||||
p_rx_valid = &p_data[6];
|
||||
}
|
||||
|
||||
p_rx_valid = &p_data[3];
|
||||
|
||||
|
||||
p_sensor->comm.tx_time_out_count = 0;
|
||||
p_sensor->comm.sensor_state_code &= (~(0x00000001 << PROTO_LEAKAGE_STATE_CODE_TIME_OUT));
|
||||
|
||||
switch(p_sensor->comm.state)
|
||||
{
|
||||
case PROTO_LEAKAGE_COMM_STATE_INIT:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_LEAKAGE_COMM_STATE_CURR_DATA_GET:
|
||||
{
|
||||
/*<2A><><EFBFBD>㵱ǰ<E3B5B1>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>*/
|
||||
u8 sensor_index = p_leakage->sensor_index;
|
||||
u16 ch_addr_offset[4] = {0,4,8,11}; /*©Һ<C2A9><D2BA><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ַƫ<D6B7><C6AB>*/
|
||||
u16 temp;
|
||||
|
||||
if(sensor_index >= APP_LEAKAGE_SUB_DEVICE_NUM)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/*ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
for(ch = 0;ch < APP_LEAKAGE_SUB_DEVICE_CH_NUM;ch++)
|
||||
{
|
||||
leakage.sub_device_data[sensor_index].ch_data[ch].state = 0;
|
||||
leakage.sub_device_data[sensor_index].ch_data[ch].state = 0;
|
||||
}
|
||||
|
||||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0x0003*/
|
||||
temp_value = (p_rx_valid[6] << 8) | p_rx_valid[7];
|
||||
leakage.sub_device_data[sensor_index].heartbeat = temp_value & 0xFF;
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>0x0007*/
|
||||
temp_value = (p_rx_valid[14] << 8) | p_rx_valid[15];
|
||||
leakage.sub_device_data[sensor_index].test_mode = temp_value & 0xFF;
|
||||
|
||||
/*©Һ<C2A9><D2BA><EFBFBD><EFBFBD>*/
|
||||
for(i=0;i<4;i++)
|
||||
{
|
||||
ch = i;
|
||||
temp = ch_addr_offset[i];
|
||||
temp_value = (p_rx_valid[temp + 0] << 8) | p_rx_valid[temp + 1];
|
||||
if(temp_value == 1)
|
||||
{
|
||||
leakage.sub_device_data[sensor_index].ch_data[ch].state |=
|
||||
APP_LEAKAGE_SUB_DEVICE_STATE_LEAKAGE;
|
||||
}
|
||||
|
||||
temp_value = (p_rx_valid[temp + 2] << 8) | p_rx_valid[temp + 3];
|
||||
if(temp_value == 1)
|
||||
{
|
||||
leakage.sub_device_data[sensor_index].ch_data[ch].state |=
|
||||
APP_LEAKAGE_SUB_DEVICE_STATE_OPEN;
|
||||
}
|
||||
|
||||
temp_value = (p_rx_valid[temp + 4] << 8) | p_rx_valid[temp + 5];
|
||||
leakage.sub_device_data[sensor_index].ch_data[ch].distance = temp_value *0.01;
|
||||
}
|
||||
}break;
|
||||
}
|
||||
if(send_flag)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_sensor->comm.state = PROTO_LEAKAGE_COMM_STATE_DEFAULT;
|
||||
p_sensor->comm.sensor_state_code &= (~(0x00000001 << p_sensor->comm.state));/*<2A><><EFBFBD><EFBFBD><EFBFBD>쳣״̬*/
|
||||
p_sensor->comm.state_send_time = 0;
|
||||
proto_leakage_switch(p_leakage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
47
leakage_system/usr/protocol/proto_modbus_master_leakage.h
Normal file
47
leakage_system/usr/protocol/proto_modbus_master_leakage.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef _PROTO_MODBUS_MASTER_LEAKAGE_H_
|
||||
#define _PROTO_MODBUS_MASTER_LEAKAGE_H_
|
||||
#include "main.h"
|
||||
#include "app_com.h"
|
||||
|
||||
#define COMM_SENSOR_NUM_MAX (32) /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨѶ<CDA8><D1B6><EFBFBD><EFBFBD>*/
|
||||
|
||||
|
||||
/*ͨѶ״̬*/
|
||||
#define PROTO_LEAKAGE_COMM_STATE_INIT (0U) /*<2A><>ʼ<EFBFBD><CABC> */
|
||||
#define PROTO_LEAKAGE_COMM_STATE_CURR_DATA_GET (1U) /*<2A><>ȡʵʱ<CAB5><CAB1><EFBFBD><EFBFBD>*/
|
||||
|
||||
#define PROTO_LEAKAGE_COMM_STATE_DEFAULT PROTO_LEAKAGE_COMM_STATE_CURR_DATA_GET
|
||||
|
||||
#define PROTO_LEAKAGE_STATE_CODE_TIME_OUT (30U) /*ͨѶ<CDA8><D1B6>ʱ*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
struct /*ͨѶ<CDA8><D1B6><EFBFBD>ز<EFBFBD><D8B2><EFBFBD>*/
|
||||
{
|
||||
u8 id; /*modbusͨѶid*/
|
||||
u8 state; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>ͨѶ״̬*/
|
||||
u8 leakage_data_index; /*©Һ<C2A9>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
u16 state_send_time; /*<2A><>ǰͨѶ״̬<D7B4><CCAC><EFBFBD>ʹ<EFBFBD><CDB4><EFBFBD>*/
|
||||
u16 tx_time_out_count; /*Э<><D0AD>ָ<EFBFBD><EFBFBD>ʹ<EFBFBD><CDB4><EFBFBD>*/
|
||||
u32 sensor_state_code; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4>룬Ϊ<EBA3AC><CEAA>ӦλΪ1<CEAA><31><EFBFBD><EFBFBD><EFBFBD>쳣*/
|
||||
}comm;
|
||||
|
||||
}proto_sensor_class_t;
|
||||
|
||||
typedef struct proto_leakage_t proto_leakage_t;
|
||||
|
||||
struct proto_leakage_t
|
||||
{
|
||||
u16 sensor_index; /*<2A><>ǰ<EFBFBD><C7B0><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨѶ*/
|
||||
u16 sensor_num; /*<2A>ܹ<EFBFBD>ͨѶ<CDA8>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
|
||||
proto_sensor_class_t sensor[APP_LEAKAGE_SUB_DEVICE_NUM]; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬*/
|
||||
|
||||
void (*init)(proto_leakage_t *);
|
||||
void (*tx_task)(proto_leakage_t *);
|
||||
void (*uart_send)(u8 *,u16);
|
||||
};
|
||||
|
||||
extern proto_leakage_t modbus_leakage[APP_COM_NUM];
|
||||
|
||||
#endif
|
||||
326
leakage_system/usr/protocol/proto_modbus_slave_ex.c
Normal file
326
leakage_system/usr/protocol/proto_modbus_slave_ex.c
Normal file
@@ -0,0 +1,326 @@
|
||||
/*<2A><><EFBFBD><EFBFBD>ͨѶ modbus<75>ӻ<EFBFBD>*/
|
||||
#include "proto_modbus_slave_ex.h"
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "app_timer.h"
|
||||
#include "bsp_relay.h"
|
||||
#include "bsp_Uart.h"
|
||||
#include "bsp_Flash.h"
|
||||
|
||||
#include "proto_print.h"
|
||||
#include "proto_modbus_master_leakage.h"
|
||||
|
||||
static modbus_analysis_data_t modbus_analysis_data;//ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9>
|
||||
static modbus_communication_send_buf_t send_struct;//<2F><><EFBFBD>ͽṹ<CDBD><E1B9B9>
|
||||
|
||||
static void proto_modbus_communication_data_analysis(u8 *pData, u16 len, void *data);
|
||||
static void proto_modbus_communication_data_send(u8 *pData, u16 len);
|
||||
static void proto_modbus_init(void);
|
||||
static void proto_modbus_task(void);
|
||||
static modbus_error_code_e proto_modbus_data_write(u16 Addr, u16 Value);
|
||||
static u16 proto_modbus_data_read(u16 Addr);
|
||||
|
||||
proto_Modbus_t modbus_slave_ex=
|
||||
{
|
||||
.id = 0x01,
|
||||
.data_read = proto_modbus_data_read,
|
||||
.data_write = proto_modbus_data_write,
|
||||
.data_analysis = proto_modbus_communication_data_analysis,
|
||||
.init = proto_modbus_init,
|
||||
.task = proto_modbus_task,
|
||||
};
|
||||
|
||||
static proto_Modbus_t *p_modbus = &modbus_slave_ex;
|
||||
static bsp_uart_t * p_rx_uart;
|
||||
|
||||
static void proto_modbus_communication_data_send(u8 *p_data, u16 len)
|
||||
{
|
||||
if(p_rx_uart != NULL)
|
||||
{
|
||||
p_rx_uart->send(p_rx_uart,p_data,len);
|
||||
}
|
||||
}
|
||||
|
||||
static void proto_modbus_init(void)
|
||||
{
|
||||
p_modbus->id = Usr_Flash.FlashData.modbus_id;
|
||||
com_uart1.rx_data_analysis = proto_modbus_communication_data_analysis;
|
||||
com_uart4.rx_data_analysis = proto_modbus_communication_data_analysis;
|
||||
}
|
||||
static void proto_modbus_task(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void proto_modbus_communication_data_analysis(u8 *pData, u16 len,void *other_data)
|
||||
{
|
||||
modbus_error_code_e error_code;
|
||||
u16 inx;
|
||||
u16 TempAddr, TempData, crc_16;
|
||||
|
||||
|
||||
if (0 == modbus_lib_analysis(&modbus_analysis_data, pData, len))//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> У<><D0A3><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ
|
||||
return;
|
||||
if (p_modbus->id != modbus_analysis_data.id && modbus_analysis_data.id != 0xe8)//<2F>ж<EFBFBD>ID<49>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ
|
||||
return;
|
||||
|
||||
error_code = ModbusErrorCode_Success;
|
||||
|
||||
|
||||
/* //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>Ƿ<C7B7><F1B3ACB3><EFBFBD>Χ
|
||||
if ((modbus_analysis_data.start_addr >= MODBUS_REG_LEN) || (modbus_analysis_data.reg_number >= MODBUS_REG_LEN) ||
|
||||
(modbus_analysis_data.start_addr + modbus_analysis_data.reg_number >= MODBUS_REG_LEN))
|
||||
{
|
||||
ErrorCode = ModbusErrorCode_IllegalAddr;
|
||||
goto Error;
|
||||
}
|
||||
*/
|
||||
p_rx_uart = (bsp_uart_t*)other_data;
|
||||
switch (modbus_analysis_data.func)
|
||||
{
|
||||
case 0x03:
|
||||
case 0x04:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = 2 * modbus_analysis_data.reg_number;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = proto_modbus_data_read(TempAddr);
|
||||
send_struct.send_buffer[3 + 2 * inx] = (TempData >> 8) & 0xff;
|
||||
send_struct.send_buffer[4 + 2 * inx] = TempData & 0xff;
|
||||
TempAddr++;
|
||||
}
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 3 + send_struct.send_buffer[2]);
|
||||
send_struct.send_buffer[3 + send_struct.send_buffer[2]] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[4 + send_struct.send_buffer[2]] = (crc_16 >> 8) & 0xff;
|
||||
send_struct.len = 5 + send_struct.send_buffer[2];
|
||||
}goto Success;
|
||||
/*<2A><><EFBFBD><EFBFBD>Э<EFBFBD><D0AD>*/
|
||||
case 0x41:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[4] = (2 * modbus_analysis_data.reg_number) >> 8;
|
||||
send_struct.send_buffer[5] = (2 * modbus_analysis_data.reg_number) & 0xff;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = proto_modbus_data_read(TempAddr);
|
||||
send_struct.send_buffer[6 + 2 * inx] = (TempData >> 8) & 0xff;
|
||||
send_struct.send_buffer[7 + 2 * inx] = TempData & 0xff;
|
||||
TempAddr++;
|
||||
}
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 6 + 2 * modbus_analysis_data.reg_number);
|
||||
send_struct.send_buffer[6 + 2 * modbus_analysis_data.reg_number] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[7 + 2 * modbus_analysis_data.reg_number] = (crc_16 >> 8) & 0xff;
|
||||
send_struct.len = 8 + 2 * modbus_analysis_data.reg_number;
|
||||
}goto Success;
|
||||
|
||||
case 0x06:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
TempData = (modbus_analysis_data.write_data_addr[0] << 8) | modbus_analysis_data.write_data_addr[1];
|
||||
error_code = proto_modbus_data_write(TempAddr, TempData);
|
||||
if (error_code)
|
||||
{
|
||||
goto Error;
|
||||
}
|
||||
|
||||
send_struct.len = 8;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[4] = modbus_analysis_data.write_data_addr[0];
|
||||
send_struct.send_buffer[5] = modbus_analysis_data.write_data_addr[1];
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 6);
|
||||
send_struct.send_buffer[6] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[7] = (crc_16 >> 8) & 0xff;
|
||||
}break;
|
||||
|
||||
case 0x10:
|
||||
{
|
||||
TempAddr = modbus_analysis_data.start_addr;
|
||||
for (inx = 0; inx < modbus_analysis_data.reg_number; inx++)
|
||||
{
|
||||
TempData = modbus_analysis_data.write_data_addr[2 * inx];
|
||||
TempData = (TempData << 8) | modbus_analysis_data.write_data_addr[2 * inx + 1];
|
||||
error_code = proto_modbus_data_write(TempAddr, TempData);
|
||||
TempAddr++;
|
||||
if (error_code)
|
||||
{
|
||||
goto Error;
|
||||
}
|
||||
}
|
||||
send_struct.len = 8;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func;
|
||||
send_struct.send_buffer[2] = modbus_analysis_data.start_addr >> 8;
|
||||
send_struct.send_buffer[3] = modbus_analysis_data.start_addr & 0xff;
|
||||
send_struct.send_buffer[4] = modbus_analysis_data.reg_number >> 8;
|
||||
send_struct.send_buffer[5] = modbus_analysis_data.reg_number & 0xff;
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 6);
|
||||
send_struct.send_buffer[6] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[7] = (crc_16 >> 8) & 0xff;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
error_code = ModbusErrorCode_IllegalFunction;
|
||||
}
|
||||
goto Error;
|
||||
}
|
||||
|
||||
Success:
|
||||
proto_modbus_communication_data_send(send_struct.send_buffer, send_struct.len);
|
||||
return;
|
||||
|
||||
Error:
|
||||
send_struct.len = 5;
|
||||
send_struct.send_buffer[0] = modbus_analysis_data.id;
|
||||
send_struct.send_buffer[1] = modbus_analysis_data.func | 0x80;
|
||||
send_struct.send_buffer[2] = error_code;
|
||||
crc_16 = modbus_lib_crc16(send_struct.send_buffer, 3);
|
||||
send_struct.send_buffer[3] = crc_16 & 0xff;
|
||||
send_struct.send_buffer[4] = (crc_16 >> 8) & 0xff;
|
||||
proto_modbus_communication_data_send(send_struct.send_buffer, send_struct.len);
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: proto_modbus_data_write
|
||||
* <20><><EFBFBD><EFBFBD>: Modbusд<73>Ĵ<EFBFBD><C4B4><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: Addr: <20><>ַ
|
||||
Value:<3A><><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
static modbus_error_code_e proto_modbus_data_write(u16 addr, u16 data)
|
||||
{
|
||||
modbus_error_code_e error_code;
|
||||
u8 temp_point,cali_point;
|
||||
|
||||
error_code = ModbusErrorCode_Success;
|
||||
|
||||
switch(addr)
|
||||
{
|
||||
case 1 ... 4:
|
||||
{
|
||||
relay.set(addr-1,data);
|
||||
}break;
|
||||
|
||||
/*sn*/
|
||||
case 20000 ... 20004:
|
||||
{
|
||||
Usr_Flash.FlashData.sn[addr - 20000] = data;
|
||||
Usr_Flash.Write();
|
||||
}break;
|
||||
/*modbus_id*/
|
||||
case 20005:
|
||||
{
|
||||
Usr_Flash.FlashData.modbus_id = data;
|
||||
modbus_slave_ex.id = Usr_Flash.FlashData.modbus_id;
|
||||
Usr_Flash.Write();
|
||||
}break;
|
||||
/*modbus_read_reg_num*/
|
||||
case 20006:
|
||||
{
|
||||
if(data > 1000)
|
||||
{
|
||||
error_code = ModbusErrorCode_IllegalData;
|
||||
}
|
||||
else
|
||||
{
|
||||
Usr_Flash.FlashData.modbus_read_reg_num = data;
|
||||
Usr_Flash.Write();
|
||||
}
|
||||
}break;
|
||||
case 20007:
|
||||
{
|
||||
if(data > 16)
|
||||
{
|
||||
error_code = ModbusErrorCode_IllegalData;
|
||||
}
|
||||
else
|
||||
{
|
||||
Usr_Flash.FlashData.modbus_read_sensor_num = data;
|
||||
Usr_Flash.Write();
|
||||
}
|
||||
}break;
|
||||
/*FC<46><43>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
case 252:
|
||||
{
|
||||
print.set(data);
|
||||
}break;
|
||||
}
|
||||
return error_code;
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* <20><><EFBFBD><EFBFBD>: proto_modbus_data_read
|
||||
* <20><><EFBFBD><EFBFBD>: Modbus<75><73><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: Addr: <20><>ַ
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>ַ<EFBFBD><D6B7>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* <20><><EFBFBD><EFBFBD>: <20><>
|
||||
******************************************/
|
||||
static u16 proto_modbus_data_read(u16 addr)
|
||||
{
|
||||
u16 data = 0;
|
||||
u16 *p_data;
|
||||
u16 num,offset;
|
||||
|
||||
switch(addr)
|
||||
{
|
||||
case 1 ... 4:
|
||||
{
|
||||
data = relay.state[addr - 1];
|
||||
}break;
|
||||
// /*ʵʱ<CAB5><CAB1><EFBFBD><EFBFBD>*/
|
||||
// case 0 ... 639:
|
||||
// {
|
||||
// u16 size;
|
||||
// size = sizeof(gas_data_t);
|
||||
// /* TDLASĬ<53>϶<EFBFBD>ȡ*/
|
||||
// if( size== (2 * Usr_Flash.FlashData.modbus_read_reg_num))
|
||||
// {
|
||||
// p_data = (u16 *)&gas_data;
|
||||
// data = p_data[addr];
|
||||
// }
|
||||
// else if((2 * Usr_Flash.FlashData.modbus_read_reg_num )< sizeof(gas_data_t))/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
// {
|
||||
// num = addr / Usr_Flash.FlashData.modbus_read_reg_num;
|
||||
// offset = addr % Usr_Flash.FlashData.modbus_read_reg_num;
|
||||
// p_data = (u16 *)&gas_data[num];
|
||||
// data = p_data[offset];
|
||||
// }
|
||||
//
|
||||
// }break;
|
||||
//
|
||||
// /*sn*/
|
||||
// case 20000 ... 20004:
|
||||
// {
|
||||
// data = Usr_Flash.FlashData.sn[addr - 20000];
|
||||
// }break;
|
||||
// /*modbus_id*/
|
||||
// case 20005:
|
||||
// {
|
||||
// data = Usr_Flash.FlashData.modbus_id;
|
||||
// }break;
|
||||
// /*modbus_read_reg_num*/
|
||||
// case 20006:
|
||||
// {
|
||||
// data = Usr_Flash.FlashData.modbus_read_reg_num;
|
||||
// }break;
|
||||
// case 20007:
|
||||
// {
|
||||
// data = Usr_Flash.FlashData.modbus_read_sensor_num;
|
||||
// }break;
|
||||
default:data = 0;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
9
leakage_system/usr/protocol/proto_modbus_slave_ex.h
Normal file
9
leakage_system/usr/protocol/proto_modbus_slave_ex.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _PROTO_MODBUS_SLAVE_EX_H_
|
||||
#define _PROTO_MODBUS_SLAVE_EX_H_
|
||||
#include "main.h"
|
||||
#include "gas_data.h"
|
||||
#include "proto_modbus_lib.h"
|
||||
|
||||
extern proto_Modbus_t modbus_slave_ex;
|
||||
|
||||
#endif
|
||||
64
leakage_system/usr/protocol/proto_print.c
Normal file
64
leakage_system/usr/protocol/proto_print.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "proto_print.h"
|
||||
|
||||
#include "stdio.h"
|
||||
#include "bsp_Uart.h"
|
||||
|
||||
u16 proto_print_data_type;
|
||||
|
||||
void proto_print_datatype_set(u8 type);
|
||||
static void proto_print_task(void);
|
||||
|
||||
proto_print_t print =
|
||||
{
|
||||
.task = proto_print_task,
|
||||
.set = proto_print_datatype_set,
|
||||
};
|
||||
|
||||
proto_print_t *p_print = &print;
|
||||
|
||||
static void proto_print_datatype_set(u8 type)
|
||||
{
|
||||
if(type > PROTO_PRINT_MAX)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if(proto_print_data_type == type)
|
||||
{
|
||||
proto_print_data_type = PROTO_PRINT_NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
proto_print_data_type = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void proto_print_task(void)
|
||||
{
|
||||
switch(proto_print_data_type)
|
||||
{
|
||||
case PROTO_PRINT_NULL:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_PRINT_CURDATA:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_PRINT_WAVE:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_PRINT_CALIB:
|
||||
{
|
||||
|
||||
}break;
|
||||
default:
|
||||
{
|
||||
|
||||
}break;
|
||||
}
|
||||
}
|
||||
|
||||
21
leakage_system/usr/protocol/proto_print.h
Normal file
21
leakage_system/usr/protocol/proto_print.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef _PROTO_PRINT_H_
|
||||
#define _PROTO_PRINT_H_
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#define PROTO_PRINT_MAX 3
|
||||
|
||||
#define PROTO_PRINT_NULL 0
|
||||
#define PROTO_PRINT_CURDATA 1
|
||||
#define PROTO_PRINT_WAVE 2
|
||||
#define PROTO_PRINT_CALIB 3
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void (*task)(void);
|
||||
void (*set)(u8);
|
||||
}proto_print_t;
|
||||
|
||||
extern proto_print_t print;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user