#ifndef __COLLECTMASTER485_H
#define __COLLECTMASTER485_H
#include "stm32f7xx_hal.h"
#include "TerminalSlave485.h"
////////////////////////////////////////////////////////////////////////////////// 	
//临时定义一下卸尽状态，后续根据485读来的值进行判断 

#define EN_Read_Press          1
#define EN_Read_YeWei          1
#define EN_Read_Temperature    1


#define indexHaiDiFa01 		18
#define indexXieYouFa01 	19
#define indexDaGai01 			20
#define indexXiaoGai01 		21	

#define indexHaiDiFa02 		28
#define indexXieYouFa02 	29
#define indexDaGai02 			20
#define indexXiaoGai02 		21

#define indexHaiDiFa03 		38
#define indexXieYouFa03   39
#define indexDaGai03 			30
#define indexXiaoGai03 		31

#define indexHaiDiFa04 		48
#define indexXieYouFa04   49
#define indexDaGai04 			40
#define indexXiaoGai04 		41	





#define U32_BIT_00                 ((uint32_t)0x00000001U)  
#define U32_BIT_01                 ((uint32_t)0x00000002U)  
#define U32_BIT_02                 ((uint32_t)0x00000004U)  
#define U32_BIT_03                 ((uint32_t)0x00000008U)  
#define U32_BIT_04                 ((uint32_t)0x00000010U)  
#define U32_BIT_05                 ((uint32_t)0x00000020U)  
#define U32_BIT_06                 ((uint32_t)0x00000040U)  
#define U32_BIT_07                 ((uint32_t)0x00000080U)  
#define U32_BIT_08                 ((uint32_t)0x00000100U)  
#define U32_BIT_09                 ((uint32_t)0x00000200U)  
#define U32_BIT_10                 ((uint32_t)0x00000400U)  
#define U32_BIT_11                 ((uint32_t)0x00000800U)  
#define U32_BIT_12                 ((uint32_t)0x00001000U)  
#define U32_BIT_13                 ((uint32_t)0x00002000U)  
#define U32_BIT_14                 ((uint32_t)0x00004000U)  
#define U32_BIT_15                 ((uint32_t)0x00008000U)  


#define U32_BIT_16                 ((uint32_t)0x00010000U) 
#define U32_BIT_17                 ((uint32_t)0x00020000U) 
#define U32_BIT_18                 ((uint32_t)0x00040000U) 
#define U32_BIT_19                 ((uint32_t)0x00080000U) 
#define U32_BIT_20                 ((uint32_t)0x00100000U) 
#define U32_BIT_21                 ((uint32_t)0x00200000U) 
#define U32_BIT_22                 ((uint32_t)0x00400000U) 
#define U32_BIT_23                 ((uint32_t)0x00800000U) 
#define U32_BIT_24                 ((uint32_t)0x01000000U) 
#define U32_BIT_25                 ((uint32_t)0x02000000U) 
#define U32_BIT_26                 ((uint32_t)0x04000000U) 
#define U32_BIT_27                 ((uint32_t)0x08000000U) 
#define U32_BIT_28                 ((uint32_t)0x10000000U) 
#define U32_BIT_29                 ((uint32_t)0x20000000U) 
#define U32_BIT_30                 ((uint32_t)0x40000000U) 
#define U32_BIT_31                 ((uint32_t)0x80000000U) 



//236字节的数组偏移量    16+8=24；接下来为52个浮点数数据=8个整车数据+4个*11个每仓=52
#define MoiLiangFrom_CaijibanOffset    24   


#define ALL_SensorData_From_CaijibanOffset    16
#define FloatData_Num_Caijiban    52 
#define LengthOfData_From_Caijiban_Cang_1to4    236 
#define LengthOfData_From_Caijiban_Cang_01    150   //每仓数据长度长度为帧头16+4*33+crc2=150字节

#define LengthOfData_From_Caijiban_WholeCar    98   //每仓数据长度长度为帧头16+4*20+crc2=98字节

//总长度为33个4字节-----每仓传感器数量
#define ALL_Sensor_Data_Num_Caijiban    33 

//总长度为20个4字节-----整车传感器数量
#define ALL_Sensor_Data_Num_WholeCar    20 


#define NumOf_WenYouPanduan    300   //是否进行了稳油操作   稳油判断

extern uint8_t Pneumatic_PR1 ;
extern uint8_t Pneumatic_PR2 ;  
extern uint8_t Pneumatic_PR3 ;
extern uint8_t Pneumatic_PR4 ;
typedef struct
{
	uint8_t  ErrorCode_Collect;
	uint8_t  ValveState_Num1;  //1仓阀门开关状态   低四位Bit0:关 Bit1:开 高四位Bit0:正常 Bit1:异常
  uint8_t  ValveState_Num2;  //2仓阀门开关状态		           X X X X X X X X
  uint8_t  ValveState_Num3;  //3仓阀门开关状态							 | | | | | | | |__________1仓大盖开关状态  0：关 1：开
	uint8_t  ValveState_Num4;  //4仓阀门开关状态							 | | | | | | |____________2仓大盖开关状态  0：关 1：开
	uint8_t  ValveState_Num5;  //5仓阀门开关状态							 | | | | | |______________3仓大盖开关状态  0：关 1：开
  uint8_t  ValveState_Num6;  //6仓阀门开关状态							 | | | | |________________4仓大盖开关状态  0：关 1：开
  uint8_t  ValveState_Num7;  //7仓阀门开关状态							 | | | |__________________1仓大盖异常状态  0：正常 1：异常
	uint8_t  ValveState_Num8;  //8仓阀门开关状态							 | | |____________________2仓大盖异常状态  0：正常 1：异常
	uint8_t  UnloadState_Num1; //1仓卸尽传感器状态						 | |______________________3仓大盖异常状态  0：正常 1：异常
  uint8_t  UnloadState_Num2; //2仓卸尽传感器状态						 |________________________4仓大盖异常状态  0：正常 1：异常
  uint8_t  UnloadState_Num3; //3仓卸尽传感器状态
	uint8_t  UnloadState_Num4; //4仓卸尽传感器状态
	uint8_t  UnloadState_Num5; //5仓卸尽传感器状态
  uint8_t  UnloadState_Num6; //6仓卸尽传感器状态
  uint8_t  UnloadState_Num7; //7仓卸尽传感器状态
	uint8_t  UnloadState_Num8; //8仓卸尽传感器状态
	uint8_t  Write_StorNum;
	uint8_t  Collect_CommError;//采集器通信错误，排除软件设计有误，有错误默认采集器通信不上或者其他原因
	
}Collect485_TypeDef;
extern Collect485_TypeDef Collect485;

typedef struct
{
	uint8_t  Stor_ID;  //分配的仓号
  uint8_t  rkgID[3]; //人孔盖ID号
  uint8_t  stat[2];  //人孔小盖，人孔盖状态
	
	
  //uint16_t yewei[2]; //液位
	int16_t yewei[2]; //液位   20210120-------整形数表示的液位，单位为0.1mm
	
  uint32_t Press;    //压力值
	uint16_t Volume;   //体积
	
	
	//20210331更改为有符号数据，因为目前的倾角传感器输出为有符号数据 李伟修改
	int16_t QingJiao;//倾角数据  角度范围0--360
	//uint16_t QingJiao;//倾角数据  角度范围0--360
	
 
	float QingJiaoFlo;
	
	
	
  uint8_t  xyf_state;//卸油阀状态
  uint8_t  hdf_state;//海底阀状态
  uint8_t  Oil_sate; //装卸油状态
	float  Temperature;//温度
	float  yewei_flo[2]; //液位   20210305--浮点数表示的液位，单位为mm
	//float  GasPressure;//压力
	//灌仓温度
}STOR_TypeDef;
extern STOR_TypeDef Stor[8];

typedef struct
{
	uint8_t xyf[8];  //卸油阀状态缓存
	uint8_t bottm[8] ;//海底阀状态缓存
	uint8_t rkgx[8];  //人孔小盖状态缓存
	uint8_t rkg[8] ;  //人孔盖状态缓存
}STatetemp_TypeDef; 
extern STatetemp_TypeDef Statetemp,Fg;
typedef struct
{
	uint8_t StartCalibrat_Flag;
	uint16_t Threshold_Angle;
	uint16_t Times;
	uint16_t Reserve_Register;
	uint8_t  Restart_Controller;
	uint8_t  Restart_Collect;
	uint8_t  Restart_ErHeYi;
	uint8_t  Restart_Encoder;
	
	uint8_t  Restart_Function;
	uint8_t  ReturnData;
	uint8_t  DeviceNum;
	uint8_t  FunctionCode;
	uint8_t  DeviceAddr;
	
	uint8_t  SetOilStyleFlag;//在家新增
	uint8_t  OilStyleStore;//在家新增,油品种类仓号
	uint8_t  OilStyle;//在家新增
	uint8_t  ErrorStore;//在家新增,异常仓号
	uint8_t  ErrorStyle;//在家新增,异常种类
	uint8_t  ErrorFlag;//异常标志	
}Calibrat_TypeDef;



///////////////新疆样机项目添加，在屏幕上独立显示温度、液位、倾角等参数
//李伟 20201211
typedef union{
 float flo_Data;
 char farray[4];
 uint32_t u32Dat;
} uni_temper;//uni_temper001;//温度参数，浮点数表示


typedef union{
 float flo_Data;
 char array_u8[4];
 uint32_t u32Dat;
} uni_float;//uni_temper001;//4字节浮点数表示




typedef union{
 float flo_Data;
 uint8_t farray[4];
 uint32_t u32Dat;
} uni_OilDensity;//密度参数，浮点数表示


//李伟修改  20210305   以浮点数格式上发给平台温度与液位数据----开始
typedef union{
 float flo_Data;
 uint8_t farray[4];
 uint32_t u32Dat;
} uni_YeweiToPlatForm;//上发至平台的4字节液位数据，浮点数格式

typedef union{
 float flo_Data;
 uint8_t farray[4];
 uint32_t u32Dat;
} uni_TempToPlatForm;//上发至平台的4字节温度数据，浮点数格式
//李伟修改  20210305   以浮点数格式上发给平台温度与液位数据----结束


typedef union{
 float flo_Data;
 char farray[4];
} uni_QingJiao;//uni_temper001;//倾角参数，浮点数表示

typedef union{
 float flo_Data;
 char farray[4];
 uint32_t u32Dat;
} uni_LiqLev;//uni_LiqLev001;//液位；liquid level

typedef union {
 float flo_Data;
 char farray[4];
}uni_TankVol_Realtime;//uni_TankVol_Realtime001;//实时换算出的罐容


typedef union {
 float flo_Data;
 char farray[4];
}uni_TankVol_Tempe20;//uni_TankVol_Tempe20_001;//温度为20时候的罐容


typedef union {
 float flo_Data;
 char farray[4];
 uint32_t u32_Data;
}uni_Float;//浮点数表示为4字节数组

typedef union {
 float flo_Data;
 char farray[4];
}uni_IncliAngle;//倾角

typedef union {
 float flo_Data;
 char farray[4];
}uni_GasPressure;//气压

typedef union {
 float flo_Data;
 char farray[4];
}uni_OilWeight;//重量

typedef union {
 float flo_Data;
 char farray[4];
}uni_LeiDaYewei;//雷达液位





extern uni_IncliAngle uni_IncliAngle001;
extern uni_TankVol_Tempe20 uni_TankVol_Tempe20_001,uni_TankVol_Tempe20_002,uni_TempRealtime001;
extern uni_TankVol_Realtime uni_TankVol_Realtime001,uni_TankVol_Realtime002,uni_TankVol_Realtime003,uni_TankVol_Realtime004;
extern uni_temper uni_temper001,uni_temper002,uni_temper003,uni_temper004;
extern uni_QingJiao uni_QingJiao001,uni_QingJiao002;
extern uni_LiqLev uni_LiqLev001,uni_LiqLev002,uni_LiqLev003,uni_LiqLev004;//4个仓位的液位高度数据
extern uni_GasPressure uni_GasPressure001;
extern uni_OilWeight uni_OilWeight001;

extern uni_OilDensity uni_OilDensity001;
extern uni_LeiDaYewei uni_LeiDaYewei001;
extern uint8_t CabinetNumForWenYou;

//李伟修改 20210305
extern uni_YeweiToPlatForm uni_YeweiToPlatForm001,uni_YeweiToPlatForm002,uni_YeweiToPlatForm003,uni_YeweiToPlatForm004;
extern uni_TempToPlatForm uni_TempToPlatForm001,uni_TempToPlatForm002;

extern float Zero_Of_LiquidMeter[8];//8个仓的雷达液位计零点参数
extern float Zero_Of_TankCapacity[8];//8个仓的罐容表零点参数


extern uint8_t HaiDiFa001;//8个海底阀的状态

//20210129李伟修改---增加一个全局数组用于保存8个仓的阀门状态（按照平台的位定义协议进行组包）
extern uint8_t Array_Of_Valve[32];

//20210201李伟修改---增加一个全局数组用于上传按下打印键之后的车辆状态
//具体详见<控制器平台通信协议增加20210126李伟修改回传数据格式.xlsx>
extern uint8_t Array_Of_Tank_0x64[68];//


extern float ArrOfTempeSensor_Flo[3];//三点式温度传感器的数据
//extern float ArrYewei[300];
extern float ArrYeweiForWenyou[NumOf_WenYouPanduan+3];//存储300s的液位数据    打印的时候作为是否稳油判断依据
extern float Yewei_max000,Yewei_min000,Yewei_peak000;


extern uint8_t Array_Of_IO_0xA1[64];//

extern uint32_t IO_OnOff_State01,IO_OnOff_State02,IO_OnOff_State03,IO_OnOff_State04;
extern uint32_t IO_Exception_State01,IO_Exception_State02,IO_Exception_State03,IO_Exception_State04;



extern uint32_t IO_OnOff_StateArr[];//
extern uint32_t IO_Exception_StateArr[];

extern uint16_t Usart2_ErrorTimes,chkRetCaiJiBan;

extern uint8_t  Array_IO_OnOff_AllStore[];
extern uint8_t  Array_IO_Exception_AllStore[];


extern uint8_t ValState01,ValState02,ValState03,ValState04,ValState05,ValState06,ValState07,ValState08;

extern Calibrat_TypeDef  PlatformCalibrat;
extern uint8_t StoreNumber,all_Store;


#define Uart2_BUF_SIZE  1024u      /*为UART2、3专门开一个DMA接收存储区 2021-4-12 by Daiyf*/

extern uint8_t USART2_RX_BUF_Collector[Uart2_BUF_SIZE];
extern uint8_t ValStateArr_0x13[64],ValStateArr_PoFeng_0x13[64];//破封时的阀门状态
extern uint8_t ValStateArr_0x20[64],ValStateArr_PoFeng_0x20[64];//破封时的阀门状态


extern uint8_t ValStateArr_0x20_Cang01[64],ValStateArr_PoFeng_0x20_Cang01[64];//破封时的阀门状态
extern uint8_t ValStateArr_0x20_Cang02[64],ValStateArr_PoFeng_0x20_Cang02[64];//破封时的阀门状态
extern uint8_t ValStateArr_0x20_Cang03[64],ValStateArr_PoFeng_0x20_Cang03[64];//破封时的阀门状态
extern uint8_t ValStateArr_0x20_Cang04[64],ValStateArr_PoFeng_0x20_Cang04[64];//破封时的阀门状态



//uint8_t CheckInFrame_Collect485(void);
uint16_t CheckInFrame_Collect485(uint16_t LenRx,uint16_t MsgID);
void TestUsart2_Receive(void);
void CollectMaster485_Send_Data(uint8_t *buf,uint16_t len);	
void CollectMaster485_TX_Set(uint8_t en);
void ReadAllStoreValue(void);
void ReadCollect_QingJiao_TX(void);
void CollectData_Transform_UsefullData(void);//实时数据转换
void Immediate_Processing_Function(void);//新增
void Send_Yewei_offset_To_Collect(void);//
void ValStateConvertTo91(void);
void UpdateWenYouFifo(void);//稳油数据更新

void Get_All_Moniliang_Data_4Cang(uint16_t GroupNum);

//void ReadCollect_ValveState_64Byte(uint8_t CangNum);
void ReadCollect_ValveState_64Byte(uint16_t para001,uint16_t para002,uint16_t para003,uint16_t para004,uint16_t para005);

void Parse_AllSensor_Data_For_SingleStore(sT2C_ALL_Sensor_Data_Each_Cang* pT2C_ALL_Sensor_Data,uint16_t msgID);
void Parse_AllSensor_Data_For_WholeCar(sT2C_ALL_Sensor_Data_WholeCar* pT2C_ALL_Sensor_Data,uint16_t msgID,uint16_t LenOfPara);

void Parse_0x0020_Data_64Byte(void);
void Parse_0x0013_Data(void);

void Translate_IO_to_U32Bit(void);
void Translate_IO_to_U32Bit_V2(void);

#endif
