#ifndef __TERMINALSLAVE485_H
#define __TERMINALSLAVE485_H
#include "stm32f7xx_hal.h"
								  
////////////////////////////////////////////////////////////////////////////////// 	

#define USART_REC_LEN  			600  	//定义最大接收字节数 600  升级时一包数据256*2个数据
#define EN_USART1_RX 			1		//使能（1）/禁止（0）串口1接收
#define FRAME_HEAD 0x3A    //起始字符，英文状态下的“:”
#define FRAME_END 0x0D0A

//终端功能码定义
#define Read_data 	0x04	//读数据
#define Read_para		0x03	//读参数
#define Set_para 		0x10	//设置参数
#define Update			0x41	//升级
#define Read_Version 0x2B //读取版本号
#define Control_IO  0x0F  //控制状态IO
#define TEST_State  0x30  //测试通信状态
#define Data_alibration 0x05//数据标定
#define PlatformTransfer  0x06
#define TERMINAL_CONTROL_CMD_RESPOND     0x30
#define TERMINAL_CONTROL_GET_SENSOR_D1   0x91
#define TERMINAL_CONTROL_GET_SENSOR_D2   0x92

//李伟修改    20210610
#define TERMINAL_CONTROL_CMD_REMOTE_CAL     0x39   //远程标定命令--根据5月份的远程标定会议文件进行修改。固定长度为64字节

//李伟修改    20210702
#define TERMINAL_CONTROL_Seal_By_ElecFence     0x38   //出电子围栏进行施封



#define TERMINAL_CONTROL_GET_ELEC_SEAL_STATE    0x40   //李伟增加  20210610  电子铅封状态
#define TERMINAL_CONTROL_GET_CABINET01_STATE    0x47   //李伟增加  20210610  1仓传感器数据
#define TERMINAL_CONTROL_GET_CABINET02_STATE    0x48   //李伟增加  20210610  2仓传感器数据
#define TERMINAL_CONTROL_GET_CABINET03_STATE    0x49   //李伟增加  20210610  3仓传感器数据
#define TERMINAL_CONTROL_GET_CABINET04_STATE    0x4a   //李伟增加  20210610  4仓传感器数据
#define TERMINAL_CONTROL_GET_CABINET05_STATE    0x4b   //李伟增加  20210610  5仓传感器数据
#define TERMINAL_CONTROL_GET_CABINET06_STATE    0x4c   //李伟增加  20210610  6仓传感器数据
#define TERMINAL_CONTROL_GET_CABINET07_STATE    0x4d   //李伟增加  20210610  7仓传感器数据
#define TERMINAL_CONTROL_GET_CABINET08_STATE    0x4e   //李伟增加  20210610  8仓传感器数据


#define TERMINAL_CONTROL_GET_Store01_ALL_Sensor    0x93    
#define TERMINAL_CONTROL_GET_Store02_ALL_Sensor    0x94    
#define TERMINAL_CONTROL_GET_Store03_ALL_Sensor    0x95    
#define TERMINAL_CONTROL_GET_Store04_ALL_Sensor    0x96    
#define TERMINAL_CONTROL_GET_Store05_ALL_Sensor    0x97    
#define TERMINAL_CONTROL_GET_Store06_ALL_Sensor    0x98    
#define TERMINAL_CONTROL_GET_Store07_ALL_Sensor    0x99    
#define TERMINAL_CONTROL_GET_Store08_ALL_Sensor    0x9A    
#define TERMINAL_CONTROL_GET_Whole_Car_Sensor    	 0x9B    

#define LenthOf_Reply0x93    	 159     //1+6+148+2+2 


//取自国标的3条modbus命令
#define JL_COMMON_CMD_ReadReg03     0x0003
#define JL_COMMON_CMD_WriteReg06    0x0006
#define JL_COMMON_CMD_WriteRegs10   0x0010

#define Broadcast_data 0x10

//usart Rx data
typedef struct
{
	uint8_t Frame_head;//帧头
	uint8_t Frame_addr;//外设地址
	uint8_t Frame_funcode;//功能码
	uint16_t Frame_message_id;//消息ID
	
//	u16 Frame_regaddr;//读取的寄存器
//	u16 Frame_regcnt;//读取寄存器的个数
	uint16_t Frame_length;//数据长度
	uint16_t Carbration_len;//标定数据长度
	uint16_t Carbration_sum;//标定数据总包
	uint16_t Carbration_child;//子包
	uint8_t  *Frame_data;//数据区  后续不知道需不需要扩展
	uint16_t Frame_LRC;//校验
	uint16_t Frame_end;//结束符
}Data_frame_TypeDef;
extern Data_frame_TypeDef data_frame,Tx_data_frame;
//typedef struct {
//	uint8_t ID ;//仓的ID  47 .。。
//	uint8_t length ;//数据总长度
//	uint8_t Communication_para[8];//通讯参数
//	uint8_t Genel_para[56];//常规参数
//}All_para_TypeDef;
//extern All_para_TypeDef All_para;
typedef struct Oil_Data_frame
{
	uint32_t Level_ADvalue;					//油位高度AD值----更改为第1个温度数据
	
//	
//	uint32_t Level_temperature;			//液体温度
//	uint32_t Temperature;						//环境温度
	
	uint32_t Liquid_Temp;			//液体温度---实际上为浮点数，u32格式传输，4字节 替换为第2个温度数据
	uint32_t EnvTemperature;		//环境温度	---实际上为浮点数，u32格式传输，4字节 替换为第3个温度数据
	 
	//20201219李伟同龚天平、张毅商量后更改；将加油量替换为倾角数据
	uint32_t Add_oilvalue;		//加油量---实际上为浮点数，u32格式传输，4字节 
	
	////20210227根据戴永峰的协议修改——原来漏油量的位置变更为倾角2
	uint32_t Dec_oilvalue;		//漏油量---实际上为浮点数，u32格式传输，4字节
	
	////油箱油量替换为定期上报的密度数据   李伟修改   20210301  同张毅商定后修改
	uint32_t Oil_boxvalue;		//油箱油量------更改为密度数据，上位机平台要按照浮点数解析
	
	
	
	uint32_t LevelPercent_or_PressValue;//液位百分比或者压力值
	uint32_t Level_value;					 //液位高度---实际上为浮点数，u32格式传输，4字节
}Oil_Data_frame	;

typedef struct IO_Data_frame
{
	uint8_t IO_emergencyflag;			 		//重要数据标识  bit7  0/1
	uint8_t IO_allnum;			//IO总数  m 1-255
	uint8_t IO_normaldata;						//正常或者异常数据  0/1
	uint8_t IO_statenum;					//IO状态组数   n(1~8)  n=(m/32)+1
	uint32_t IO_state[8];					//IO状态  4*n
	uint32_t IO_normalstate[8];				//正常异常状态
	
	uint32_t Temp_IO_state[8];					//IO临时状态  4*n
	uint32_t Temp_IO_normalstate[8];				//正常异常临时状态
	uint8_t  Updata_TempVar[8];
	uint32_t IO_unload_switch;			//卸尽开关监测 有油位置1，无油位置0
	uint8_t IO_Discharge_state;		//新版卸尽信号输入监测
	
}IO_Data_frame	;

//电子铅封数据帧
typedef struct Seal_Data_frame
{
	uint8_t Sealing_up_datetype;//上传数据类型   常规数据、解封施封等破封  0：默认 1:解封   2 ：施封   3：破封
	uint8_t Sealing_emergencyflag;					//重要数据标识  bit7  0/1
	uint8_t Sealing_type;			//电子铅封数据类型
	uint16_t Sesling_All_num;						//仓数量
	//1~n仓
	uint8_t Sesling_ID[8];					//仓号
	uint8_t Sealing_state_oiltype[8];//罐仓状态与油品
	uint8_t Sealing_Mode[8];					//铅封状态与装油模式
	uint8_t Sealing_IO_state[8];      //装油模式与阀门逻辑是否正常
	//1~n仓
	uint16_t Sealing_num;//解封仓数量
	uint8_t Sealing_num_id[8];//解封仓号
	uint8_t Sealing_Random_code[3];//解封码
	uint8_t Sealing_Ask_code[3];//匹配码
	
	uint8_t TerminalOnLineFlag;
}Seal_Data_frame	;


typedef struct Version_Data_frame
{
	uint8_t *Version_comname;						//公司名称 SHJL
	uint8_t *Version_allnum;							//产品代码  02
	uint8_t *Version_normaldata;					//硬件版本号
	uint8_t *Version_statenum;						//软件版本号 99.33.22长度不超过8字节
	uint8_t *Version_state;							//设备ID 
	uint8_t *Version_normalstate;				//客户代码  
}Version_Data_frame	;

typedef struct IO_Data_Reg
{
	uint16_t IO_Addr;							//2字节外设地址 30寄存器
	uint16_t IO_bound;							//2字节 波特率	31寄存器
	uint16_t IO_Parity;						//2字节奇偶校验	32寄存器
	uint16_t Bacup;								//预留
}IO_Data_Reg	;

extern uint8_t Oil_Data_Reg[64];
extern  uint8_t CarLicCodeArray[31][16];
extern Oil_Data_frame Oil_Data;


extern  uint8_t Data_V20_From_Server[68];//来自平台的0x64数据帧

extern  struct All_para  All_Para;
typedef struct Calibration_data
{
	uint16_t ID ;//仓的ID  47 .。。
	uint16_t  All_pack;//总包数
	uint16_t Child_pack_len[10];//子包长度
	uint8_t Oil_Carlibration_data[500];//标定数据
	uint16_t Old_ID;//上一次的ID
}Calibration_data;

typedef struct Save_Calibration_Inof
{
	uint8_t id[4];//仓号
	uint16_t ALL_pack[4];//总包数
	uint16_t Length[4];//总长度
}Save_Calibration_Inof;

typedef struct Update_data
{
	uint16_t All_pack_no;//总包数
	uint16_t pack_no;//子包序号
	uint32_t Sum_all;//总校验和
	uint32_t Sum_pack;//子包校验和
	uint8_t *data;//数据
	
}Update_data;


typedef struct Return_data
{
	uint8_t *data;
	uint16_t length;
	
}Return_data;



//Daiyf ??
typedef struct _T2C_SetupData
{
  	uint16_t   year;
	uint8_t    month;
	uint8_t    day;
  	uint8_t    hour;
	uint8_t    minute;
	uint8_t    second;
	uint8_t    m_second;
	float      longitude;
	float      latitude;
	float      Capacity_Vt;
	float      Capacity_V20;
	float      liquid_level;
	float      temperature;
	float      inclination_angle_x;
	float      inclination_angle_y;
	float      pressure;
	float      density;
	float      Reserved_parameters_1;
	float      Reserved_parameters_2;
	float      Reserved_parameters_3;
	float      Reserved_parameters_4;
 
}sT2C_SetupData;


/*
前16字节为固定帧头，接下来8字节为保留待扩展，接下来8个参数32字节为整车模拟量信息（倾角、速度等），
最后为4仓的模拟量参数与校验信息。
总长度：16+8+32+4*4*11+4=236字节。(废除F3之后容量将会加倍，最大长度按照800字节设计)
*/
//李伟修改  20210928   1-4,5-8仓模拟量数据
typedef struct _T2C_MoniLiang_Data
{
  uint8_t   	FrameHead[16];
	uint8_t    	ResData[8];
	float      	MoniLiang_DataOf_Car[8];
	float      	MoniLiang_DataOfCang_1to4[11*4];
	uint8_t    	chkLRC_Data[2];
	uint8_t    	chkCRCModbus_Data[2];
 
}sT2C_MoniLiang_Data;


typedef struct _T2C_ALL_Sensor_Data_Each_Cang
{//每仓传感器
  uint8_t   	FrameHead[16];	
	uint32_t    ElecSealState;
	uint32_t    IO_Valve_Data_Every_Cang[21];
	float      	MoniLiang_Data_Every_Cang[19];
	uint8_t    	chkCRCModbus_Data[2];
 
}sT2C_ALL_Sensor_Data_Each_Cang;

typedef struct _T2C_ALL_Sensor_Data_WholeCar
{//整车传感器数据  倾角  备电等
	uint8_t   	FrameHead[16];
  	float 	   	tiltXYZ[3];	
	uint32_t    BeiDian_State_OnOff;//备电开关状态
	float    		BeiDianDianLiang;
	uint32_t    YouQiHuiShou_State_OnOff;//开关状态
	uint32_t    YouQiHuiShou_State_Photo;//光电状态
	uint32_t    ResPara[13];//备用参数
	uint8_t    	chkCRCModbus_Data[2];//暂未使用
 
}sT2C_ALL_Sensor_Data_WholeCar;


typedef struct _T2C_IO_Valve_Data   //开关量数据---针对内蒙古项目
{
  uint8_t   	FrameHead[16];
	uint8_t    	ResData[8];
	float      	IO_Valve_DataOf_Car[8];
	float      	IO_Valve_DataOfCang_1to4[11*4];
	uint8_t    	chkLRC_Data[2];
	uint8_t    	chkCRCModbus_Data[2];
 
}sT2C_IO_Valve_Data;

/* zhuzl 20211220 增加发送帧的结构定义*/
#pragma pack (1)
typedef struct _NEIMENG_FRAME
{
	//uint8_t  Frame_41;
	uint32_t Frame_Identification;
	uint16_t Frame_Len;
	uint16_t Serial_Num;
	uint16_t Total_Packages;
	uint16_t Current_Package;
	uint16_t Message_Num;
	uint32_t Version;
	uint8_t  Frame_Head[16];
	uint8_t  Time[6]; //年、月、日、时、分、秒
	uint8_t  Is_Retransmit;
	uint8_t	 Reserver1;
	uint8_t  Data[768];
}sNEIMENG_FRAME;
#pragma pack ()
//zhuzl add end



//李伟修改  20210610  远程标定命令结构体
/*
总长度为64字节；后期自己研发终端可以扩大，预计1k字节可以实现。
帧头2字节0x 3901
地址码 4字节，比如0x95510001
功能码2字节   只能是0x0003、0x0006或者0x0010
寄存器起始地址2字节：合法范围为0x0000~0xffff
数据个数2字节：最多读取0xffff字节；实际取决于终端限制，目前为256字节
保留字4字节
有效载荷44字节
保留字2字节
校验2字节
*/
typedef struct _T2C_RemoteCaliData
{
  	uint16_t   FrameHead;   		//帧头 0-1
	uint32_t   AddrExtModule; 	//4字节扩展地址   2--5
	uint16_t   FunctionCode;		//功能码2字节   只能是0x0003、0x0006或者0x0010    6--7
  	uint16_t   AddrRegStart;		//寄存器起始地址2字节：合法范围为0x0000~0xffff    8--9
	uint16_t   NumberOfRegs;		//数据个数2字节：最多读取0xffff字节；实际取决于终端限制，目前为256字节  10--11
	uint32_t   ReserveData001;	//保留字4字节 12--15
	uint8_t    PayLoadData[192];	//有效载荷具体有三种：44，或者76字节，或者108字节；字节 16--59
	uint16_t   ReserveData002;	//保留字2字节    60--61
	uint16_t   CheckSum;				//校验2字节      62--63
 
 
}sT2C_RemoteCaliDat;

typedef struct _T2C_SealByElec_Fence
{
  uint8_t   FrameHead;   		//帧头 0-1
	uint8_t    FrmLength; 	//数据长度
	uint8_t   FunctionCode;		//功能码1字节   只能是0x03
  uint8_t   TypeOf_Seal;//离开油库  离开加油站   速度达到xx
	uint8_t   Array_res000[56];		//数据个数2字节：最多读取0xffff字节；实际取决于终端限制，目前为256字节  10--11
 //李伟修改   20211208
	uint8_t   TpyeOfUnSeal;   		//解封类型  01=正常解封   02=强制解封
	uint8_t   Array_NumOfCab_To_UnSeal[8];//
 
}sT2C_SealByElec_Fence;


extern 	sT2C_SetupData *pT2C_SetupData;
extern 	sT2C_SetupData T2C_SetupData[8];

extern  sT2C_MoniLiang_Data T2C_MoniLiang_Data_Cang1to4,T2C_MoniLiang_Data_Cang5to8;
extern 	sT2C_MoniLiang_Data *pT2C_MoniLiang_Data;


extern 	sT2C_SetupData *pT2C_SetupData_Con2Server;


extern sT2C_SealByElec_Fence *pT2C_SealByElec_Fence;
extern sT2C_SealByElec_Fence T2C_SealByElec_Fence001;


extern sT2C_ALL_Sensor_Data_Each_Cang* pT2C_ALL_Sensor_Data;
extern sT2C_ALL_Sensor_Data_Each_Cang T2C_ALL_Sensor_Data01,T2C_ALL_Sensor_Data02,T2C_ALL_Sensor_Data03,T2C_ALL_Sensor_Data04;
extern sT2C_ALL_Sensor_Data_Each_Cang T2C_ALL_Sensor_Data05,T2C_ALL_Sensor_Data06,T2C_ALL_Sensor_Data07,T2C_ALL_Sensor_Data08;


extern sT2C_ALL_Sensor_Data_WholeCar* pT2C_ALL_Sensor_WholeCar;
extern sT2C_ALL_Sensor_Data_WholeCar T2C_ALL_Sensor_WholeCar01;


typedef struct Data_From_Server_0x6401
{
	int16_t Year ;//
	int16_t  Month;//
	int16_t Date;//
	int16_t Hour;//
	int16_t Minute;//
	int16_t Second;
	float Longi;
	float Lati;
	float Vt;
	float V20;
	float Yewei;
	float Temper;
	float QingjiaoX;
	float QingjiaoY;
	float Pressure;
	float Density;
	float res001;
	float res002;
	uint8_t res001_u8;//控制屏幕显示本地计算的数据还是来自服务器的数据，比如Vt,V20等
	uint8_t res002_u8;
	uint8_t res003_u8;
	uint8_t res004_u8;
	
}Data_From_Server_0x6401;


extern Data_From_Server_0x6401  Data_From_Server_0x6401_001;
extern char Version[30];
extern uint8_t Oil_Carlibration_data[500];
extern uint8_t UP_date_flag ;
//extern uint8_t  USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.末字节为换行符 
extern struct Save_Calibration_Inof Save_Calibration_inof;
extern Seal_Data_frame Sealing_Data;
extern IO_Data_frame IO_Date;


extern Data_From_Server_0x6401  Data_From_Server_0x6401_00;



extern  uint8_t CarLicensePlate[64];

extern  uint32_t addr_ext_controllBoard;

//如果想串口中断接收，请不要注释以下宏定义
void uart_init1(uint32_t bound);
void Data_analysis(void);
uint8_t * Oil_Tx_Readvalue(uint8_t exaddr,uint16_t ID,uint16_t length);
Return_data * IO_Tx_Readvalue(uint16_t ID,uint16_t length);//读取IO阀门开关的应答;
//u8 * Version_Tx_Readvalue(u16 ID,u16 length);
Return_data * Version_Tx_Readvalue(uint16_t ID,uint16_t length);
Return_data * Oil_Tx_Readpara(uint16_t ID,uint16_t length);
uint8_t * IO_Tx_Readpara(uint16_t ID,uint16_t length);//读取寄存器的参数值
uint8_t  Set_Io_Paravalue(uint16_t ID,uint16_t length1,uint8_t * data);
uint8_t  Set_Oil_Paravalue(uint16_t ID,uint16_t length1,uint8_t * data);
void Tx_data_to_ask(Data_frame_TypeDef temp_data);

uint8_t Set_Carlibration_data(uint16_t ID,uint8_t pack,uint16_t length1,uint8_t*data);
//Return_data * Read_Carlibration_data(u16 ID,u8 pack);
Return_data * Read_Carlibration_data(uint16_t ID,uint8_t pack,uint16_t length);

Return_data * Seal_Tx_Readvalue(uint16_t ID,uint16_t length);//读取电子铅封的应答

uint8_t Get_Ask_code_value(uint16_t ID,uint16_t length1,uint8_t*data);//获取平台下发的解封码 

uint8_t Cang_ID(uint16_t ID);

void WriteCarLicensePlate(void); //保存车牌号

void TerminalSlave485_Send_Data(uint8_t *buf,uint16_t len);
void TerminalSlave485_TX_Set(uint8_t en);
void TestUsart1_Receive(void);
//int  	Process_CMD_0x30(void);//温度、密度等调试数据下发

int 	Process_CMD_0x30_ManualFix(void);//电子围栏自动施封
int 	Process_CMD_0x38_AutoSeal_By_ElecFence(void);//电子围栏自动施封
int 	Process_CMD_0x39_RemoteCali(uint8_t dataMode);//远程标定
void FormatEachStoreData(uint8_t storeNum);
void FormatWholeCarData(void);
#endif
