leakage
This commit is contained in:
300
calib_board/usr/protocol/proto_modbus_lib.c
Normal file
300
calib_board/usr/protocol/proto_modbus_lib.c
Normal file
@@ -0,0 +1,300 @@
|
||||
#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;
|
||||
while(len--)
|
||||
{
|
||||
crc16 = crc16^(*p_data++);
|
||||
for(i=0; i++<8; )
|
||||
{
|
||||
if(crc16&0x0001)
|
||||
{
|
||||
crc16 = (crc16>>1)^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] = 0x41;//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
calib_board/usr/protocol/proto_modbus_lib.h
Normal file
67
calib_board/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
|
||||
412
calib_board/usr/protocol/proto_modbus_master_tdlas.c
Normal file
412
calib_board/usr/protocol/proto_modbus_master_tdlas.c
Normal file
@@ -0,0 +1,412 @@
|
||||
#include "proto_modbus_master_tdlas.h"
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "os_timer.h"
|
||||
|
||||
#include "bsp_Uart.h"
|
||||
#include "bsp_74HC4067.h"
|
||||
#include "bsp_Flash.h"
|
||||
|
||||
#include "proto_print.h"
|
||||
#include "proto_modbus_lib.h"
|
||||
|
||||
#define TDLAS_MODBUS_ID (0xe8)
|
||||
#define TDLAS_DATA_ENDIAN (LITTLE_ENDIAN) /*<2A><><EFBFBD>ô<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>*/
|
||||
|
||||
#define PROTO_TDLAS_GET_CURR_DATA_START_ADDR (20000)
|
||||
#define PROTO_TDLAS_SET_ZERO_CALIB_START_ADDR (12)
|
||||
#define PROTO_TDLAS_SET_SPAN_CALIB_START_ADDR (13)
|
||||
#define PROTO_TDLAS_GET_RESET_START_ADDR (0x0000)
|
||||
#define PROTO_TDLAS_GET_FAC_CALIB_START_ADDR (11010)
|
||||
#define PROTO_TDLAS_GET_FAC_CALIB_PARA_SET_START_ADDR (11000)
|
||||
#define PROTO_TDLAS_GET_FAC_CALIB_DATA_START_ADDR (250)
|
||||
|
||||
|
||||
static void proto_tdlas_init(void);
|
||||
static void proto_tdlas_control_zero_calib(u16 calib_value);
|
||||
|
||||
static void proto_tdlas_control_span_calib(u16 CalibValue);
|
||||
static void proto_tdlas_control_reset(void);
|
||||
static void proto_tdlas_control_fac_calib(u16 calib_value,u8 calib_point,u8 temp_point);
|
||||
static void proto_tdlas_control_fac_calib_para_set(s16 temp,u16 press,u16 humidity);
|
||||
static void proto_tdlas_tx_task(void);
|
||||
static void proto_tdlas_rx_task(u8 *p_data,u16 len,void *other_data);
|
||||
static void proto_tdlas_control_fac_calib_data_get(u8 ch);
|
||||
|
||||
/*<2A><><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>ڽ<EFBFBD><DABD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
static bsp_Uart_t *rx_uart = NULL;
|
||||
|
||||
proto_tdlas_t tdlas=
|
||||
{
|
||||
.modbus_id = TDLAS_MODBUS_ID,
|
||||
|
||||
.init = proto_tdlas_init,
|
||||
.tx_task = proto_tdlas_tx_task,
|
||||
.rx_task = proto_tdlas_rx_task,
|
||||
// .print = proto_tdlas_print_debug_data,
|
||||
|
||||
.control.zero_calib = proto_tdlas_control_zero_calib,
|
||||
.control.span_calib = proto_tdlas_control_span_calib,
|
||||
.control.reset = proto_tdlas_control_reset,
|
||||
.control.fac_calib = proto_tdlas_control_fac_calib,
|
||||
.control.fac_calib_para_set = proto_tdlas_control_fac_calib_para_set,
|
||||
.control.fac_calib_data_get = proto_tdlas_control_fac_calib_data_get,
|
||||
};
|
||||
|
||||
proto_tdlas_t *p_sensor = &tdlas;
|
||||
bsp_Uart_t *p_use_uart = &COM_Uart4;
|
||||
|
||||
static void proto_tdlas_init(void)
|
||||
{
|
||||
COM_Uart4.Rx_DataAnalysis = p_sensor->rx_task;
|
||||
}
|
||||
|
||||
static void proto_tdlas_send(u8 *p_data,u16 len)
|
||||
{
|
||||
COM_Uart4.Send(&COM_Uart4,p_data,len);
|
||||
}
|
||||
|
||||
static void proto_sensor_switch(u8 ch)
|
||||
{
|
||||
UartCH_Config.ch_set(ch);
|
||||
}
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD>У*/
|
||||
static void proto_tdlas_control_zero_calib(u16 calib_value)
|
||||
{
|
||||
u8 i;
|
||||
for(i=0;i<Usr_Flash.FlashData.modbus_read_sensor_num;i++)
|
||||
{
|
||||
p_sensor->sys[i].sys_state = PROTO_TDLAS_SYS_STATE_ZERO_CALIB;
|
||||
p_sensor->set_data.calib_value = calib_value;
|
||||
}
|
||||
}
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD>У */
|
||||
static void proto_tdlas_control_span_calib(u16 CalibValue)
|
||||
{
|
||||
u8 i;
|
||||
for(i=0;i<Usr_Flash.FlashData.modbus_read_sensor_num;i++)
|
||||
{
|
||||
p_sensor->sys[i].sys_state = PROTO_TDLAS_SYS_STATE_SPAN_CALIB;
|
||||
p_sensor->set_data.calib_value = CalibValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*<2A>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
static void proto_tdlas_control_reset(void)
|
||||
{
|
||||
u8 i;
|
||||
for(i=0;i<Usr_Flash.FlashData.modbus_read_sensor_num;i++)
|
||||
{
|
||||
p_sensor->sys[i].sys_state = PROTO_TDLAS_SYS_STATE_RESET;
|
||||
}
|
||||
}
|
||||
|
||||
/*<2A><><EFBFBD>ұ궨*/
|
||||
static void proto_tdlas_control_fac_calib(u16 calib_value,u8 calib_point,u8 temp_point)
|
||||
{
|
||||
u8 i;
|
||||
for(i=0;i<Usr_Flash.FlashData.modbus_read_sensor_num;i++)
|
||||
{
|
||||
p_sensor->sys[i].sys_state = PROTO_TDLAS_SYS_STATE_FAC_CALIB;
|
||||
p_sensor->set_data.calib_value = calib_value;
|
||||
p_sensor->set_data.calib_point = calib_point;
|
||||
p_sensor->set_data.temp_point = temp_point;
|
||||
}
|
||||
}
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
static void proto_tdlas_control_fac_calib_para_set(s16 temp,u16 press,u16 humidity)
|
||||
{
|
||||
u8 i;
|
||||
for(i=0;i<Usr_Flash.FlashData.modbus_read_sensor_num;i++)
|
||||
{
|
||||
p_sensor->sys[i].sys_state = PROTO_TDLAS_SYS_STATE_FAC_CALIB_PARA_SET;
|
||||
p_sensor->set_data.temp = (-1 == temp) ? p_sensor->set_data.temp : temp;
|
||||
p_sensor->set_data.humidity = (0xffff == humidity) ? p_sensor->set_data.humidity : humidity;
|
||||
p_sensor->set_data.press = (0xffff == press) ? p_sensor->set_data.press : press;
|
||||
}
|
||||
}
|
||||
|
||||
/*<2A><>ȡ<EFBFBD>궨<EFBFBD><EAB6A8><EFBFBD><EFBFBD>*/
|
||||
static void proto_tdlas_control_fac_calib_data_get(u8 ch)
|
||||
{
|
||||
if(Usr_Flash.FlashData.modbus_read_sensor_num > ch)
|
||||
{
|
||||
p_sensor->sensor_index = ch;
|
||||
p_sensor->sys[ch].sys_state = PROTO_TDLAS_SYS_STATE_FAC_CALIB_DATA_GET;
|
||||
proto_sensor_switch(ch);
|
||||
}
|
||||
}
|
||||
|
||||
/********<2A><><EFBFBD><EFBFBD>***********/
|
||||
static void proto_tdlas_tx_curr_data_get(void)
|
||||
{
|
||||
u16 addr = PROTO_TDLAS_GET_CURR_DATA_START_ADDR;
|
||||
u8 len = Usr_Flash.FlashData.modbus_read_reg_num;
|
||||
modbus_lib_data_read(p_sensor->modbus_id,addr,len,proto_tdlas_send);
|
||||
}
|
||||
|
||||
static void proto_tdlas_tx_zero_calib(void)
|
||||
{
|
||||
u16 addr = PROTO_TDLAS_SET_ZERO_CALIB_START_ADDR;
|
||||
u8 len = 1;
|
||||
u16 data[1];
|
||||
data[0] = 0;
|
||||
|
||||
modbus_lib_multiple_data_write(p_sensor->modbus_id,addr,len,data,proto_tdlas_send);
|
||||
}
|
||||
|
||||
static void proto_tdlas_tx_span_calib(void)
|
||||
{
|
||||
u16 addr = PROTO_TDLAS_SET_SPAN_CALIB_START_ADDR;
|
||||
u8 len = 1;
|
||||
u16 data[1];
|
||||
data[0] = p_sensor->set_data.calib_value;
|
||||
modbus_lib_multiple_data_write(p_sensor->modbus_id,addr,len,data,proto_tdlas_send);
|
||||
}
|
||||
|
||||
static void proto_tdlas_tx_Reset(void)
|
||||
{
|
||||
u16 addr = PROTO_TDLAS_GET_RESET_START_ADDR;
|
||||
u8 len = 1;
|
||||
u16 data[1];
|
||||
data[0] = 0x00FE;
|
||||
//modbus_lib_multiple_data_write(p_sensor->modbus_id,addr,len,data,proto_tdlas_send);
|
||||
}
|
||||
|
||||
static void proto_tdlas_tx_fac_calib(void)
|
||||
{
|
||||
u16 addr = PROTO_TDLAS_GET_FAC_CALIB_START_ADDR;
|
||||
u8 len = 1;
|
||||
u16 data[1];
|
||||
data[0] = p_sensor->set_data.calib_value;
|
||||
addr = addr + p_sensor->set_data.temp_point * 10 + p_sensor->set_data.calib_point;
|
||||
modbus_lib_multiple_data_write(p_sensor->modbus_id,addr,len,data,proto_tdlas_send);
|
||||
}
|
||||
|
||||
static void proto_tdlas_tx_fac_calib_para_set(void)
|
||||
{
|
||||
u16 addr = PROTO_TDLAS_GET_FAC_CALIB_PARA_SET_START_ADDR;
|
||||
u8 len = 3;
|
||||
u16 data[3];
|
||||
|
||||
data[0] = *((u16 *)(&p_sensor->set_data.temp)); /*<2A>¶<EFBFBD>*/
|
||||
data[1] = p_sensor->set_data.press; /*ѹ<><D1B9>*/
|
||||
data[2] = p_sensor->set_data.humidity; /*ʪ<><CAAA>*/
|
||||
|
||||
modbus_lib_multiple_data_write(p_sensor->modbus_id,addr,len,data,proto_tdlas_send);
|
||||
}
|
||||
|
||||
static void proto_tdlas_tx_fac_calib_data_get(void)
|
||||
{
|
||||
u16 addr = PROTO_TDLAS_GET_FAC_CALIB_DATA_START_ADDR;
|
||||
u8 len = 1;
|
||||
u16 data[1];
|
||||
|
||||
data[0] = 3; /*<2A>¶<EFBFBD>*/
|
||||
modbus_lib_multiple_data_write(p_sensor->modbus_id,addr,len,data,proto_tdlas_send);
|
||||
}
|
||||
|
||||
static void proto_tdlas_tx_task(void)
|
||||
{
|
||||
u8 SendFlag = 0;
|
||||
proto_tdlas_sys_t *p_sensor_sys;
|
||||
p_sensor_sys = &p_sensor->sys[p_sensor->sensor_index];
|
||||
|
||||
if(0 == (p_sensor_sys->state_error_flag & (0x00000001 << PROTO_TDLAS_ERROR_FLAG_TIME_OUT)))
|
||||
{
|
||||
if((++p_sensor_sys->tx_time_out_count) > 8)/*ͨѶ<CDA8><D1B6>ʱ*/
|
||||
{
|
||||
p_sensor_sys->state_error_flag |= (0x00000001 << PROTO_TDLAS_ERROR_FLAG_TIME_OUT);
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
memset(&gas_data[p_sensor->sensor_index],0,sizeof(gas_data_t));
|
||||
}
|
||||
}
|
||||
|
||||
switch(p_sensor_sys->sys_state)
|
||||
{
|
||||
case PROTO_TDLAS_SYS_STATE_INIT:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_CURR_DATA_GET:
|
||||
{
|
||||
proto_tdlas_tx_curr_data_get();
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_ZERO_CALIB:
|
||||
{
|
||||
proto_tdlas_tx_zero_calib();
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_SPAN_CALIB:
|
||||
{
|
||||
proto_tdlas_tx_span_calib();
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_RESET:
|
||||
{
|
||||
proto_tdlas_tx_Reset();
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_FAC_CALIB:
|
||||
{
|
||||
proto_tdlas_tx_fac_calib();
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_FAC_CALIB_PARA_SET:
|
||||
{
|
||||
proto_tdlas_tx_fac_calib_para_set();
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_FAC_CALIB_DATA_GET:
|
||||
{
|
||||
if(0 == p_sensor_sys->send_time)
|
||||
{
|
||||
p_use_uart->relay.flag = 1;
|
||||
proto_tdlas_tx_fac_calib_data_get();
|
||||
}
|
||||
p_sensor_sys->send_time++;
|
||||
if(p_sensor_sys->send_time > 50)
|
||||
{
|
||||
p_use_uart->relay.flag = 0;
|
||||
p_sensor_sys->send_time = 0;
|
||||
p_sensor_sys->sys_state = PROTO_TDLAS_SYS_STATE_CURR_DATA_GET;
|
||||
p_sensor->sensor_index++;
|
||||
if(p_sensor->sensor_index >= Usr_Flash.FlashData.modbus_read_sensor_num)
|
||||
{
|
||||
p_sensor->sensor_index = 0;
|
||||
}
|
||||
proto_sensor_switch(p_sensor->sensor_index);
|
||||
}
|
||||
SendFlag = 1;
|
||||
}break;
|
||||
default:
|
||||
{
|
||||
|
||||
}break;
|
||||
}
|
||||
if(SendFlag)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
p_sensor_sys->send_time++;
|
||||
if(p_sensor_sys->send_time >= 3) /*<2A><><EFBFBD><EFBFBD><EFBFBD>쳣*/
|
||||
{
|
||||
p_sensor_sys->state_error_flag |= (0x00000001 << p_sensor_sys->sys_state);/*<2A><>¼<EFBFBD>쳣״̬*/
|
||||
p_sensor_sys->send_time = 0;
|
||||
p_sensor_sys->sys_state = PROTO_TDLAS_SYS_STATE_CURR_DATA_GET;
|
||||
p_sensor->sensor_index++;
|
||||
if(p_sensor->sensor_index >= Usr_Flash.FlashData.modbus_read_sensor_num)
|
||||
{
|
||||
p_sensor->sensor_index = 0;
|
||||
}
|
||||
proto_sensor_switch(p_sensor->sensor_index);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static void proto_tdlas_rx_task(u8 *p_data,u16 len,void *other_data)
|
||||
{
|
||||
u8 send_time_flag = 0;
|
||||
u8 modbus_id,cmd;
|
||||
u16 check_crc16,modbus_crc16;
|
||||
u16 *p_u16_temp;
|
||||
u16 i;
|
||||
u8 *p_rx_valid;
|
||||
|
||||
proto_tdlas_sys_t *p_sensor_sys;
|
||||
p_sensor_sys = &p_sensor->sys[p_sensor->sensor_index];
|
||||
|
||||
if( p_use_uart->relay.flag == 1)
|
||||
{
|
||||
p_use_uart->relay.uart->Send(p_use_uart->relay.uart,p_data,len);
|
||||
}
|
||||
|
||||
if(p_sensor->modbus_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->modbus_id) return ;
|
||||
if(cmd != 0x04 && 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];
|
||||
}
|
||||
rx_uart = (bsp_Uart_t *)other_data;
|
||||
|
||||
p_sensor_sys->tx_time_out_count = 0;
|
||||
p_sensor_sys->state_error_flag &= (~(0x00000001 << PROTO_TDLAS_ERROR_FLAG_TIME_OUT));
|
||||
|
||||
switch(p_sensor_sys->sys_state)
|
||||
{
|
||||
case PROTO_TDLAS_SYS_STATE_INIT:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_CURR_DATA_GET:
|
||||
{
|
||||
p_u16_temp = (u16 *)&(gas_data[p_sensor->sensor_index]);
|
||||
for(i=0;i<Usr_Flash.FlashData.modbus_read_reg_num;i++)
|
||||
{
|
||||
p_u16_temp[i] = p_rx_valid[i * 2] << 8 | p_rx_valid[i * 2 + 1];
|
||||
}
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_ZERO_CALIB:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_SPAN_CALIB:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_RESET:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_FAC_CALIB:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_FAC_CALIB_PARA_SET:
|
||||
{
|
||||
|
||||
}break;
|
||||
case PROTO_TDLAS_SYS_STATE_FAC_CALIB_DATA_GET:
|
||||
{
|
||||
|
||||
}break;
|
||||
}
|
||||
if(send_time_flag)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_sensor_sys->sys_state = PROTO_TDLAS_SYS_STATE_CURR_DATA_GET;
|
||||
p_sensor_sys->state_error_flag &= (~(0x00000001 << p_sensor_sys->sys_state));/*<2A><><EFBFBD><EFBFBD><EFBFBD>쳣״̬*/
|
||||
p_sensor_sys->send_time = 0;
|
||||
p_sensor->sensor_index++;
|
||||
if(p_sensor->sensor_index >= Usr_Flash.FlashData.modbus_read_sensor_num)
|
||||
{
|
||||
p_sensor->sensor_index = 0;
|
||||
}
|
||||
proto_sensor_switch(p_sensor->sensor_index);
|
||||
return;
|
||||
}
|
||||
}
|
||||
68
calib_board/usr/protocol/proto_modbus_master_tdlas.h
Normal file
68
calib_board/usr/protocol/proto_modbus_master_tdlas.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#ifndef _PROTO_MODBUS_MASTER_SENSOR_H_
|
||||
#define _PROTO_MODBUS_MASTER_SENSOR_H_
|
||||
#include "main.h"
|
||||
#include "gas_data.h"
|
||||
|
||||
/*NIDR״̬*/
|
||||
#define PROTO_TDLAS_SYS_STATE_INIT (0U) /*<2A><>ʼ<EFBFBD><CABC> */
|
||||
#define PROTO_TDLAS_SYS_STATE_CURR_DATA_GET (1U) /*<2A><>ȡʵʱ<CAB5><CAB1><EFBFBD><EFBFBD>*/
|
||||
#define PROTO_TDLAS_SYS_STATE_ZERO_CALIB (2U) /*<2A><><EFBFBD><EFBFBD>У */
|
||||
#define PROTO_TDLAS_SYS_STATE_SPAN_CALIB (3U) /*<2A><><EFBFBD><EFBFBD>У */
|
||||
#define PROTO_TDLAS_SYS_STATE_RESET (4U) /*<2A>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD> */
|
||||
#define PROTO_TDLAS_SYS_STATE_FAC_CALIB (5U) /*<2A><><EFBFBD>ұ궨 */
|
||||
#define PROTO_TDLAS_SYS_STATE_FAC_CALIB_PARA_SET (6U) /*<2A><><EFBFBD>ұ궨ʱ<EAB6A8>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
#define PROTO_TDLAS_SYS_STATE_FAC_CALIB_DATA_GET (7U) /*<2A><>ȡ<EFBFBD>궨<EFBFBD><EAB6A8>Ϣ*/
|
||||
|
||||
#define PROTO_TDLAS_ERROR_FLAG_TIME_OUT (30U) /*ͨѶ<CDA8><D1B6>ʱ*/
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void (*zero_calib)(u16); /*<2A><><EFBFBD><EFBFBD>У*/
|
||||
void (*span_calib)(u16); /*<2A><><EFBFBD>̵<EFBFBD>У*/
|
||||
void (*reset)(void); /*<2A>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
void (*fac_calib)(u16,u8,u8); /*<2A><><EFBFBD>ұ궨ʱ<EAB6A8><CAB1>Ũ<EFBFBD><C5A8>*/
|
||||
void (*fac_calib_para_set)(s16,u16,u16); /*<2A><><EFBFBD>ұ궨ʱ<EAB6A8><CAB1><EFBFBD>õ<EFBFBD><C3B5>¶ȡ<C2B6>ʪ<EFBFBD>ȡ<EFBFBD>ѹ<EFBFBD><D1B9>*/
|
||||
void (*fac_calib_data_get)(u8); /*<2A><><EFBFBD>ұ궨ʱ<EAB6A8><CAB1><EFBFBD>õ<EFBFBD><C3B5>¶ȡ<C2B6>ʪ<EFBFBD>ȡ<EFBFBD>ѹ<EFBFBD><D1B9>*/
|
||||
}proto_tdlas_control_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 sys_state;
|
||||
u16 send_time; /*<2A><><EFBFBD>ʹ<EFBFBD><CDB4><EFBFBD>*/
|
||||
u32 sensor_state; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬*/
|
||||
u8 print_flag; /*ʵʱ<CAB5><CAB1><EFBFBD>ݴ<EFBFBD>ӡ<EFBFBD><D3A1>־λ*/
|
||||
u32 state_error_flag; /*ϵͳ<CFB5><CDB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ*/
|
||||
u16 tx_time_out_count; /*Э<><D0AD>ָ<EFBFBD><EFBFBD>ʹ<EFBFBD><CDB4><EFBFBD>*/
|
||||
}proto_tdlas_sys_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
||||
u8 calib_point;
|
||||
u8 temp_point;
|
||||
u16 calib_value;
|
||||
s16 temp;
|
||||
u16 humidity;
|
||||
u16 press;
|
||||
}proto_tdlas_set_data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 modbus_id;
|
||||
|
||||
u16 sensor_index; /*<2A><>ǰ<EFBFBD><C7B0><EFBFBD>ĸ<EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>*/
|
||||
proto_tdlas_set_data_t set_data;
|
||||
proto_tdlas_sys_t sys[SENSOR_NUM];
|
||||
proto_tdlas_control_t control;
|
||||
|
||||
void (*init)(void);
|
||||
void (*tx_task)(void);
|
||||
void (*rx_task)(u8 *,u16,void *);
|
||||
void (*print)(void);
|
||||
void (*warm_task)(void);
|
||||
}proto_tdlas_t;
|
||||
|
||||
extern proto_tdlas_t tdlas;
|
||||
|
||||
#endif
|
||||
362
calib_board/usr/protocol/proto_modbus_slave_ex.c
Normal file
362
calib_board/usr/protocol/proto_modbus_slave_ex.c
Normal file
@@ -0,0 +1,362 @@
|
||||
/*<2A><><EFBFBD><EFBFBD>ͨѶ modbus<75>ӻ<EFBFBD>*/
|
||||
#include "proto_modbus_slave_ex.h"
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
|
||||
#include "app.h"
|
||||
#include "os_timer.h"
|
||||
|
||||
#include "bsp_Uart.h"
|
||||
#include "bsp_Flash.h"
|
||||
|
||||
#include "proto_print.h"
|
||||
#include "proto_modbus_master_tdlas.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 * rx_uart;
|
||||
|
||||
static void proto_modbus_communication_data_send(u8 *pData, u16 len)
|
||||
{
|
||||
if(&COM_Uart1 == rx_uart)
|
||||
{
|
||||
COM_Uart1.Send(&COM_Uart1,pData,len);
|
||||
}
|
||||
else if(&COM_Uart4 == rx_uart)
|
||||
{
|
||||
COM_Uart4.Send(&COM_Uart4,pData,len);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void proto_modbus_init(void)
|
||||
{
|
||||
p_modbus->id = Usr_Flash.FlashData.modbus_id;
|
||||
COM_Uart1.Rx_DataAnalysis = proto_modbus_communication_data_analysis;
|
||||
COM_Uart4.Rx_DataAnalysis = 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;
|
||||
}
|
||||
*/
|
||||
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)
|
||||
{
|
||||
/*<2A>¶<EFBFBD> ѹ<><D1B9> ʪ<><CAAA>*/
|
||||
case 11000:
|
||||
{
|
||||
tdlas.control.fac_calib_para_set(*((u16 *)(&data)),0xffff,0xffff);
|
||||
}break;
|
||||
case 11001:
|
||||
{
|
||||
tdlas.control.fac_calib_para_set(0xffff,data,0xffff);
|
||||
}break;
|
||||
case 11002:
|
||||
{
|
||||
tdlas.control.fac_calib_para_set(0xffff,0xffff,data);
|
||||
}break;
|
||||
|
||||
/*<2A><><EFBFBD><EFBFBD>У*/
|
||||
case 11003:
|
||||
{
|
||||
tdlas.control.zero_calib(0);
|
||||
}break;
|
||||
/*<2A><><EFBFBD>̵<EFBFBD>У*/
|
||||
case 11004:
|
||||
{
|
||||
tdlas.control.span_calib(data);
|
||||
}break;
|
||||
|
||||
/*<2A><><EFBFBD>ұ궨*/
|
||||
case 11010 ... 11109:
|
||||
{
|
||||
temp_point = (addr - 11010) / 10;
|
||||
cali_point = (addr - 11010) % 10;
|
||||
tdlas.control.fac_calib(data,cali_point,temp_point);
|
||||
}break;
|
||||
|
||||
/*ת<><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>궨<EFBFBD><EAB6A8>Ϣ*/
|
||||
case 11200:
|
||||
{
|
||||
tdlas.control.fac_calib_data_get(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)
|
||||
{
|
||||
/*ʵʱ<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
calib_board/usr/protocol/proto_modbus_slave_ex.h
Normal file
9
calib_board/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
calib_board/usr/protocol/proto_print.c
Normal file
64
calib_board/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
calib_board/usr/protocol/proto_print.h
Normal file
21
calib_board/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