/**
  ******************************************************************************
  * @file    BLV_485_Dev_LCDFun.c
  * @author  BLW RCU  development Team
  * @version 1.0
  * @date    2023年10月07日
  * @brief  
  ******************************************************************************
  */
  
#include "includes.h"

#if RS485_LCD_1602_Flag

/*设备驱动宏定义开始*/
#define RS485_DEV_PRO_FUN_01    DevExistJudgge(RS485_LCD_1602_Flag, BLW_RS485_LCD1602_Data_Init) 
/*设备驱动宏定义结束*/


void BLW_RS485_LCD1602_Data_Init(Device_Public_Information_G *BUS_Public, RS485_LCD_INFO *Rs485LCD, uint16_t LoopNum);


/*本地函数调用结束*/

/*******************************************************************************
* @name  :  LCD驱动总表
* @para  : 
*   DataLen :数据长度
*   Data：   数据数组
*
*******************************************************************************/
void BLW_RS485_LCD_Data_Init(uint16_t DataLen, uint8_t *Data)
{
	Device_Public_Information_G BUS_Public;
	RS485_LCD_INFO Rs485LCD;

	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485LCD,0,sizeof(RS485_LCD_INFO));
	
	BUS_Public.addr = Data[1];		//设备485地址
	BUS_Public.type = Data[0];		//设备类型
	BUS_Public.baud = Data[3] + (Data[4]<<8) + (Data[5]<<16) + (Data[6]<<24);		    //设备发送波特率115200
	BUS_Public.retry_num = 0x03;	//设备数据重发次数
	BUS_Public.wait_time = 0x0064;	//设备数据发送等待回复时间 - 100ms
	BUS_Public.Protocol = Data[7];	//设备协议
	
	Rs485LCD.DevOffline = Rs485LCD.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
	switch(Data[7])
	{
		case	 ENUM_RS485_DEV_PRO_01: if(NULL!=RS485_DEV_PRO_FUN_01) RS485_DEV_PRO_FUN_01(&BUS_Public, &Rs485LCD, Data[10]);break; //
		default:
			break;
	}
	
	
	switch(Data[2])
	{
		case Active_Port:   //主动端口
			BUS_Public.port = Active_Port;		//设备发送端口
			Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO));
			Act485_Info.device_num += 1;
			break;  
		case Polling_Port:   //轮询端口
			BUS_Public.port = Polling_Port;		//设备发送端口
			Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO));   //将填充后的数据添加到链表
			Poll485_Info.device_num += 1;	
			break;
		case Bus_port:    //总线端口
			BUS_Public.port = Bus_port;		//设备发送端口		
			Add_BUS_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO));
			BUS485_Info.device_num += 1;
			break;
		default:
			break;
	}
}

/*******************************************************************************
* Function Name  : BLW_RS485_LED_For_Logic_Init 
* Description    : 调光初始化信息来自逻辑文件
*******************************************************************************/
void BLW_RS485_LCD_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	RS485_LCD_INFO Rs485LCD;

	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485LCD,0,sizeof(RS485_LCD_INFO));
	
	BUS_Public.addr = dev_info->addr;		//设备485地址
	BUS_Public.type = dev_info->type;		//设备类型
	BUS_Public.baud = dev_info->baud;;		//设备发送波特率9600
	BUS_Public.retry_num = 0x03;	//设备数据重发次数
	BUS_Public.wait_time = 0x0064;	//设备数据发送等待回复时间 - 100ms
	BUS_Public.Protocol = dev_info->version;
	

	Rs485LCD.DevOffline = Rs485LCD.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
	switch(BUS_Public.Protocol)		//协议版本
	{
		case	 ENUM_RS485_DEV_PRO_01: if(NULL!=RS485_DEV_PRO_FUN_01) RS485_DEV_PRO_FUN_01(&BUS_Public, &Rs485LCD, dev_info->output_num);break; //
		default:
			break;
	}
	
	switch(dev_info->port)
	{
		case Active_Port:   //主动端口
			BUS_Public.port = Active_Port;		//设备发送端口
			Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO));
			Act485_Info.device_num += 1;
			break;  
		case Polling_Port:   //轮询端口
			BUS_Public.port = Polling_Port;		//设备发送端口
			Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO));   //将填充后的数据添加到链表
			Poll485_Info.device_num += 1;	
			break;
		case Bus_port:    //总线端口
			BUS_Public.port = Bus_port;		//设备发送端口		
			Add_BUS_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO));
			BUS485_Info.device_num += 1;
			break;
		default:
			break;
	}
	
}

/******************************************************************************
* Function:  void Lcd1602_SetGlobal_Send()
* Parameter: RS485_LCD_INFO
* Return:    NULL
* Description: 设置全局信息
*******************************************************************************/
void Lcd1602_SetGlobal_Send(RS485_LCD_INFO *Rs485LCD)
{
	uint8_t lens = 0x17;
	uint8_t buff[lens];
	memset(buff,0,lens);
	uint32_t temp_unix = 0;
	
	temp_unix = RTC_Conversion_To_Unix(&RTC_Raw_Data);
	
	buff[PKT_ADD_FM] = 0x00;
	
	if(Rs485LCD->DevSendCnt == 0x00)
	{
		Rs485LCD->DevSendSN++;
		if(Rs485LCD->DevSendSN > 0x0F)	Rs485LCD->DevSendSN = 0x00;
		buff[PKT_TYPE] |= (Rs485LCD->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		buff[PKT_TYPE] |= (Rs485LCD->DevSendSN & 0x0F);
		buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	
	buff[PKT_DevType] = DEV_LCDTYPE;
	buff[PKT_ADD_TO] = 0x01;
	buff[PKT_LEN] = lens;
	buff[PKT_CMD] = LCD_CMD_GLOBAL;
	buff[7] = Rs485LCD->G_Set_Flag;
	buff[8] = Rs485LCD->CardState;
	buff[9] = Rs485LCD->RoomState;
	buff[10] = Rs485LCD->NetState;
	buff[11] = Rs485LCD->OffLineDevType;      //离线设备类型
	buff[12] = Rs485LCD->OffLineDevAddr;      //离线设备地址
	buff[13] = temp_unix&0xff;
	buff[14] = (temp_unix>>8)&0xff;
	buff[15] = (temp_unix>>16)&0xff;
	buff[16] = (temp_unix>>24)&0xff;
	buff[17] = Rs485LCD->ProductCode&0xff;
	buff[18] = (Rs485LCD->ProductCode>>8)&0xff;
	buff[19] = Rs485LCD->LockState;
	buff[20] = Rs485LCD->InputType;
	buff[21] = Rs485LCD->InputAddr;
	buff[22] = Rs485LCD->InputLoop;
	
	buff[PKT_CHKSUM] = CheckSum(buff, buff[PKT_LEN]);

	MCU485_SendString(USART_3,buff, buff[PKT_LEN]);

}

/******************************************************************************
* Function:  void Lcd1602_SetNet_Send()
* Parameter: RS485_LCD_INFO
* Return:    NULL
* Description: 设置网络信息
*******************************************************************************/
void Lcd1602_SetNet_Send(RS485_LCD_INFO *Rs485LCD)
{
	uint8_t lens = 0x1D;
	uint8_t buff[lens];
	memset(buff,0,lens);
	
	buff[PKT_ADD_FM] = 0x00;
	
	if(Rs485LCD->DevSendCnt == 0x00)
	{
		Rs485LCD->DevSendSN++;
		if(Rs485LCD->DevSendSN > 0x0F)	Rs485LCD->DevSendSN = 0x00;
		buff[PKT_TYPE] |= (Rs485LCD->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		buff[PKT_TYPE] |= (Rs485LCD->DevSendSN & 0x0F);
		buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	
	buff[PKT_DevType] = DEV_LCDTYPE;
	buff[PKT_ADD_TO] = 0x01;
	buff[PKT_LEN] = lens;
	buff[PKT_CMD] = LCD_CMD_NETINF;
	
	buff[7] = Rs485LCD->Net_Set_Flag;
	buff[8] = server_info.dhcp_en;
	memcpy(&buff[9],Rs485LCD->Ip,4);
	memcpy(&buff[13],Rs485LCD->Mask,4);
	memcpy(&buff[17],Rs485LCD->Gateway,4);
	memcpy(&buff[21],Rs485LCD->Dns,4);
	memcpy(&buff[25],Rs485LCD->Serverip,4);
	
	buff[PKT_CHKSUM] = CheckSum(buff, buff[PKT_LEN]);

	MCU485_SendString(USART_3,buff, buff[PKT_LEN]);

}

/******************************************************************************
* Function:  void Lcd1602_SetHw_Send()
* Parameter: RS485_LCD_INFO
* Return:    NULL
* Description: 设置设备信息
*******************************************************************************/
void Lcd1602_SetHw_Send(RS485_LCD_INFO *Rs485LCD)
{
	uint8_t lens = 0x23;
	uint8_t buff[lens];
	memset(buff,0,lens);
	
	buff[PKT_ADD_FM] = 0x00;
	
	if(Rs485LCD->DevSendCnt == 0x00)
	{
		Rs485LCD->DevSendSN++;
		if(Rs485LCD->DevSendSN > 0x0F)	Rs485LCD->DevSendSN = 0x00;
		buff[PKT_TYPE] |= (Rs485LCD->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		buff[PKT_TYPE] |= (Rs485LCD->DevSendSN & 0x0F);
		buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	
	buff[PKT_DevType] = DEV_LCDTYPE;
	buff[PKT_ADD_TO] = 0x01;
	buff[PKT_LEN] = lens;
	buff[PKT_CMD] = LCD_CMD_DEVINF;
	
	memcpy(&buff[7],Rs485LCD->Fw_ver,20);
    buff[27] = Rs485LCD->Cfg_ver[0];
	buff[28] = Rs485LCD->Cfg_ver[1];
	memcpy(&buff[29],Rs485LCD->Mac,6);
	
	buff[PKT_CHKSUM] = CheckSum(buff, buff[PKT_LEN]);

	MCU485_SendString(USART_3,buff, buff[PKT_LEN]);

}

/******************************************************************************
* Function:  void Lcd1602_Poll_Send()
* Parameter: RS485_LCD_INFO
* Return:    NULL
* Description: 询问状态改变
*******************************************************************************/
void Lcd1602_Poll_Send(RS485_LCD_INFO *Rs485LCD)
{
	uint8_t lens = 0x07;
	uint8_t buff[lens];
	memset(buff,0,lens);
	
	buff[PKT_ADD_FM] = 0x00;
	
	if(Rs485LCD->DevSendCnt == 0x00)
	{
		Rs485LCD->DevSendSN++;
		if(Rs485LCD->DevSendSN > 0x0F)	Rs485LCD->DevSendSN = 0x00;
		buff[PKT_TYPE] |= (Rs485LCD->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		buff[PKT_TYPE] |= (Rs485LCD->DevSendSN & 0x0F);
		buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	
	buff[PKT_DevType] = DEV_LCDTYPE;
	buff[PKT_ADD_TO] = 0x01;
	buff[PKT_LEN] = lens;
	buff[PKT_CMD] = LCD_CMD_INQUIRY;
	
	buff[PKT_CHKSUM] = CheckSum(buff, buff[PKT_LEN]);

	MCU485_SendString(USART_3,buff, buff[PKT_LEN]);
}

/******************************************************************************
* Function:  void LCD1602_CtrlDevice()
* Parameter: devtype - 设备类型
*            devaddr - 设备地址
*            state   - 控制设备回路状态
* Return:    NULL
* Description: 询问状态改变
*******************************************************************************/
void LCD1602_CtrlDevice(uint8_t devtype,uint8_t devaddr,uint32_t state)
{
	uint32_t listaddr = 0;
	
	Device_Public_Information_G BUS_Public;   //共用
	NOR_HVOUT_INFO DevHVoutInfo;       //继电器局部变量
	
	listaddr = Find_AllDevice_List_Information(devtype,devaddr);
	
	if(NULL != listaddr)
	{
		SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),listaddr);	//拷贝所有共有数据	
		switch(devtype)
		{
			case Dev_Host_HVout:
				SRAM_DMA_Read_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),listaddr+Dev_Privately);
//				if(state==0xffffffff)
//				{
//					UINT64DATACONVER(DevHVoutInfo.DevHVoutState) |= state;
//				}
//				else if(state==0x00)
//				{
//					UINT64DATACONVER(DevHVoutInfo.DevHVoutState) &= state;
//				}
//				else
//				{
//					UINT64DATACONVER(DevHVoutInfo.DevHVoutState) = 0x00;
//					UINT64DATACONVER(DevHVoutInfo.DevHVoutState) |= state;
//				}
				
				for(uint8_t i=0;i<DevHVoutInfo.HVoutLoopValidNum;i++)
				{
					if( ( state & (0x01 << i) ) != 0x00 )
					{
						DevHVoutInfo.DevHVoutState[i] = 0x01;
					}else {
						DevHVoutInfo.DevHVoutState[i] = 0x00;
					}
				}
				
				BUS_Public.check = 0x00;
				BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevHVoutInfo, sizeof(NOR_HVOUT_INFO));
				SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),listaddr);/*将数据保存*/
				SRAM_DMA_Write_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),listaddr+Dev_Privately);
				
				break;
			default:
				break;
		}
	}
	
}



/*******************************************************************************
* Function Name  : BLW_LCD1602_CycleCtrl
* Parameter:     设备链表地址
* Return:  RS485OCCUPYTIME - 占用发送      RS485OCCUPYNOTIME - 不占用发送   
* Description    : LCD1602轮询兼控制发送函数
*******************************************************************************/
uint8_t BLW_LCD1602_CycleCtrl(uint32_t dev_addr) 
{
	uint8_t Ret = RS485OCCUPYNOTIME;
	Device_Public_Information_G BUS_Public;
	RS485_LCD_INFO Rs485LCD;
	uint8_t KeepFlag = 0x00;
	
	uint32_t temp = 0x00;
	uint8_t temp_buff[4] = {0};
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO),dev_addr+Dev_Privately);
	
	/*插卡状态判断*/
	if(Rs485LCD.CardState != DevActionGlobal.DevActionU64Cond.EleState)
	{
		Rs485LCD.CardState = DevActionGlobal.DevActionU64Cond.EleState;
		Rs485LCD.G_Set_Flag |= (0x01<<0);
	}
	
	/*房态判断*/
	SRAM_Read_Buff(temp_buff,4,(FLASH_Register_Start_ADDRESS + Register_RoomRent_OFFSET));
	temp = temp_buff[0] + (temp_buff[1]<<8) + (temp_buff[2]<<16) + (temp_buff[3]<<24);
	if(Rs485LCD.RoomState != (temp&0xff))
	{
		Rs485LCD.RoomState = temp&0xff;
		Rs485LCD.G_Set_Flag |= (0x01<<1);
	}
	
	/*网络状态判断*/
	if(Rs485LCD.NetState != server_info.online_state)
	{
		Rs485LCD.NetState = server_info.online_state;
		Rs485LCD.G_Set_Flag |= (0x01<<2);
	}
	
	/*设备离线标记判断*/
	if((Rs485LCD.OffLineDevType != DevActionGlobal.OffLineDevType)||\
		(Rs485LCD.OffLineDevAddr != DevActionGlobal.OffLineDevAddr))
	{
		Rs485LCD.OffLineDevType = DevActionGlobal.OffLineDevType;
		Rs485LCD.OffLineDevAddr = DevActionGlobal.OffLineDevAddr;
		Rs485LCD.G_Set_Flag |= (0x01<<3);
	}
	
	/*输入控制判断*/
	if((Rs485LCD.InputType != DevActionGlobal.InputType) || (Rs485LCD.InputAddr != DevActionGlobal.InputAddr) ||\
		(Rs485LCD.InputLoop != DevActionGlobal.InputLoop))
	{
		Rs485LCD.InputType = DevActionGlobal.InputType;
		Rs485LCD.InputAddr = DevActionGlobal.InputAddr;
		Rs485LCD.InputLoop = DevActionGlobal.InputLoop;
		Rs485LCD.G_Set_Flag |= (0x01<<4);
	}
	
	/*UTC时间判断*/
	if((SysTick_1s - Rs485LCD.UTC_Tick > 600) || (server_info.sync_tick == 0x02))
	{
		Rs485LCD.UTC_Tick = SysTick_1s;
		Rs485LCD.G_Set_Flag |= (0x01<<5);
		server_info.sync_tick = 0x00;
	}
	
	/*项目编码判断*/
	Flash_Read(temp_buff,4,(FLASH_Register_Start_ADDRESS + Register_ProjectCode_OFFSET));
	temp = temp_buff[0] + (temp_buff[1]<<8) + (temp_buff[2]<<16) + (temp_buff[3]<<24);
	if(Rs485LCD.ProductCode != temp)
	{
		Rs485LCD.ProductCode = temp&0x0ffff;
		Rs485LCD.G_Set_Flag |= (0x01<<6);
	}
	
	/*锁定状态*/
	if(Rs485LCD.LockState != Get_Authorize_Lock_Status())
	{
		Rs485LCD.LockState = Get_Authorize_Lock_Status();
		Rs485LCD.G_Set_Flag |= (0x01<<7);
	}
	
	/*IP比较*/
	if(memcmp(Rs485LCD.Ip,net_info.local_ip,4)!=0x00)      
	{
		memcpy(Rs485LCD.Ip,net_info.local_ip,4);
		Rs485LCD.Net_Set_Flag |= (0x01<<0);
	}
	
	/*子网掩码比较*/
	if(memcmp(Rs485LCD.Mask,net_info.mask,4)!=0x00)      
	{
		memcpy(Rs485LCD.Mask,net_info.mask,4);
		Rs485LCD.Net_Set_Flag |= (0x01<<1);
	}
	
	/*默认网关比较*/
	if(memcmp(Rs485LCD.Gateway,net_info.gwip_addr,4)!=0x00)      
	{
		memcpy(Rs485LCD.Gateway,net_info.gwip_addr,4);
		Rs485LCD.Net_Set_Flag |= (0x01<<2);
	}
	
	/*DNS比较*/
	if(memcmp(Rs485LCD.Dns,DNS_SERVER_IP,4)!=0x00)      
	{
		memcpy(Rs485LCD.Dns,DNS_SERVER_IP,4);
		Rs485LCD.Net_Set_Flag |= (0x01<<3);
	}
	
	/*服务器IP比较*/
	if(memcmp(Rs485LCD.Serverip,server_info.dis_ip,4)!=0x00)      
	{
		memcpy(Rs485LCD.Serverip,server_info.dis_ip,4);
		Rs485LCD.Net_Set_Flag |= (0x01<<4);
	}
	
	/*固件版本比较*/
	if(memcmp(Rs485LCD.Fw_ver,SoftwareVer,20)!=0x00)
	{
		memcpy(Rs485LCD.Fw_ver,SoftwareVer,20);
		Rs485LCD.Hw_Set_Flag |= (0x01<<0);
	}
	
	/*配置版本比较*/
	SRAM_Read_Buff(temp_buff,4,SRAM_Register_Start_ADDRESS + Register_ConfigVersion_OFFSET);
	if(memcmp(Rs485LCD.Cfg_ver,temp_buff,4)!=0x00)
	{
		memcpy(Rs485LCD.Cfg_ver,temp_buff,4);
		Rs485LCD.Hw_Set_Flag |= (0x01<<1);
	}
	
	/*MAC比较*/
	if(memcmp(Rs485LCD.Mac,MACAddr,6)!=0x00)
	{
		memcpy(Rs485LCD.Mac,MACAddr,6);
		Rs485LCD.Hw_Set_Flag |= (0x01<<2);
	}
	
	
	if(Rs485LCD.G_Set_Flag != 0x00)
	{
		if(0x00 != Rs485LCD.CtrlCnt)
		{
			Rs485LCD.CtrlCnt--;
			Lcd1602_SetGlobal_Send(&Rs485LCD);               //设置全局信息发送
			Ret = RS485OCCUPYTIME;
		}
		else
		{
			Rs485LCD.G_Set_Flag = 0x00;
		}
	}
	else if(Rs485LCD.Net_Set_Flag != 0x00)
	{
		if(0x00 != Rs485LCD.CtrlCnt)
		{
			Rs485LCD.CtrlCnt--;
			Lcd1602_SetNet_Send(&Rs485LCD);                 //设置网络信息发送
			Ret = RS485OCCUPYTIME;
		}
		else
		{
			Rs485LCD.Net_Set_Flag = 0x00;
		}
	}
	else if(Rs485LCD.Hw_Set_Flag != 0x00)
	{
		if(0x00 != Rs485LCD.CtrlCnt)
		{
			Rs485LCD.CtrlCnt--;
			Lcd1602_SetHw_Send(&Rs485LCD);                 //设置设备信息发送
			Ret = RS485OCCUPYTIME;
		}
		else
		{
			Rs485LCD.Hw_Set_Flag = 0x00;
		}
	}
	else
	{
		Lcd1602_Poll_Send(&Rs485LCD);                     //轮询状态
		Ret = RS485OCCUPYTIME;
	}
	
	
	if(RS485OCCUPYTIME == Ret)
	{
		KeepFlag = 0x01;
		if(Rs485LCD.DevSendCnt > LCD_RETRY_TIME)
		{
			if(Rs485LCD.DevOffline != DEV_IS_OFFLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485 LCD DEV_IS_OFFLINE");
				LOG_Device_Online_Record(DEV_LCDTYPE,BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线
			}
			Rs485LCD.DevOffline = DEV_IS_OFFLINE;   //离线
			if(Rs485LCD.DevOffline != Rs485LCD.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				Rs485LCD.DevOfflineLast = Rs485LCD.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
		}
		else
		{
			Rs485LCD.DevSendCnt++;   //
		}
//		Rs485LCD.inquire_tick = SysTick_1ms;
		
		/*通讯统计*/
		BLV_Communication_Record(&Rs485LCD.comm_record,0x01,0x00);
	}
	
	if(0x01 == KeepFlag)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485LCD, sizeof(RS485_LCD_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO),dev_addr+Dev_Privately);
	}
	
	return Ret;
}

#define RECDATALENMAX   0x30   //接收数据最大长度
#define IsMyData        0x00
#define IsNotMyData     0x01
/*******************************************************************************
* Function Name  : BLW_Rs485_LCD1602_Check 
* Description    : LCD1602接收数据处理函数
* Input          : dev_addr:设备信息地址   data_addr:数据地址     len: 数据长度
* Return         : 	0x00：处理成功  0x01：处理失败			
*******************************************************************************/
uint8_t BLW_Rs485_LCD1602_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{

	uint8_t rev = IsNotMyData;
	Device_Public_Information_G BUS_Public;
	RS485_LCD_INFO Rs485LCD;
	uint8_t data[RECDATALENMAX];
	
	uint8_t tempdata[4] = {0};
	uint32_t relay_L = 0;    //低32位
	uint32_t relay_H = 0;    //高32位
	uint8_t wflash_flag = 0;
	

	if(len > RECDATALENMAX)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"接收数据长度超出范围!!");
		return rev;     //长度
	}
	
	SRAM_DMA_Read_Buff(data,len,data_addr);   //拷贝482数据
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	
	if((data[0] != 0x01) || (DEV_LCDTYPE != data[2]))   	   //地址不对
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"LCD1602设备地址不对或者类型不对");
		return rev;				//校验文件头
	}

	
	if(data[5] == CheckSum_Overlook_Check(data, len, 5))   //校验通过
	{
		SRAM_DMA_Read_Buff((uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO),dev_addr+Dev_Privately);
		
		switch(data[6])  
		{
			case LCD_CMD_GLOBAL_R:      
			case LCD_CMD_NETINF_R:		
			case LCD_CMD_DEVINF_R:		
			case LCD_CMD_INQUIRY_R:
				rev = IsMyData;
				Rs485LCD.CtrlCnt = LCD_RETRY_TIME;
				
				if(Rs485LCD.DevOffline != DEV_IS_ONLINE)
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485LCD DEV_IS_ONLINE");
					LOG_Device_Online_Record(DEV_LCDTYPE,BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
				}
				Rs485LCD.DevSendCnt = 0x00;
				Rs485LCD.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(Rs485LCD.DevOffline != Rs485LCD.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485LCD.DevOfflineLast = Rs485LCD.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
//				BLV_Communication_Record(&Rs485LCD.comm_record,0x02,0x01);		//记录通讯成功
				break;			
		}
		switch(data[6])
		{
			case LCD_CMD_GLOBAL_R:
				Rs485LCD.G_Set_Flag = 0x00;
				/*日志记录*/
//				LOG_Device_COMM_Control_Reply_Record(BUS_Public.port,BUS_Public.baud,data,len);
				break;
			case LCD_CMD_NETINF_R:
				Rs485LCD.Net_Set_Flag = 0x00;
				/*日志记录*/
//				LOG_Device_COMM_Control_Reply_Record(BUS_Public.port,BUS_Public.baud,data,len);
				break;
			case LCD_CMD_DEVINF_R:
				Rs485LCD.Hw_Set_Flag = 0x00;				
				break;
			case LCD_CMD_INQUIRY_R:
				
				if(data[7]!=0x00)
				{
					Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Rs485LCD 0x60 Ack 0x61 Buff:",data,len);
				}
							
				if((SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_NetInfo_EN_OFFSET + 1)==0x01)&&\
					(SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_NetInfo_EN_OFFSET) == 0x02))    //只有在使用固定IP才能设置
				{
					if(((data[7]>>0)&0x01)==0x01)    //设置IP地址
					{
						Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Rs485LCD Set IP:",&data[8],4);
						SRAM_Read_Buff(tempdata,4,(SRAM_Register_Start_ADDRESS+Register_NetIP_OFFSET));
						if(memcmp(&data[8],tempdata,4)!=0x00)
						{
							SRAM_Write_Buff(&data[8],4,(SRAM_Register_Start_ADDRESS+Register_NetIP_OFFSET));
							wflash_flag = 0x01;
						}
						server_info.init_flag = 0x00;
					}
					
					if(((data[7]>>1)&0x01)==0x01)    //设置默认网关
					{
						Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Rs485LCD Set Gateway:",&data[12],4);
						SRAM_Read_Buff(tempdata,4,(SRAM_Register_Start_ADDRESS+Register_NetGateway_OFFSET));
						if(memcmp(&data[12],tempdata,4)!=0x00)
						{
							SRAM_Write_Buff(&data[12],4,(SRAM_Register_Start_ADDRESS+Register_NetGateway_OFFSET));
							wflash_flag = 0x01;
						}
						server_info.init_flag = 0x00;
					}
					
					if(((data[7]>>2)&0x01)==0x01)    //设置子网掩码
					{
						
						Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Rs485LCD Set Mask:",&data[16],4);
						SRAM_Read_Buff(tempdata,4,(SRAM_Register_Start_ADDRESS+Register_NetMask_OFFSET));
						if(memcmp(&data[16],tempdata,4)!=0x00)
						{
							SRAM_Write_Buff(&data[16],4,(SRAM_Register_Start_ADDRESS+Register_NetMask_OFFSET));
							wflash_flag = 0x01;
						}
						server_info.init_flag = 0x00;
					}
					
					if(((data[7]>>3)&0x01)==0x01)    //设置DNS
					{
						Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Rs485LCD Set DNS:",&data[20],4);
						SRAM_Read_Buff(tempdata,4,(SRAM_Register_Start_ADDRESS+Register_DNSServerIP_OFFSET));
						if(memcmp(&data[20],tempdata,4)!=0x00)
						{
							SRAM_Write_Buff(&data[20],4,(SRAM_Register_Start_ADDRESS+Register_DNSServerIP_OFFSET));
							wflash_flag = 0x01;
						}
						server_info.init_flag = 0x00;
					}
					
					
				}
				
				if(SRAM_Read_Byte(SRAM_Register_Start_ADDRESS + Register_NetInfo_EN_OFFSET + 2)==0x01)    //只有采用本地服务器时才能设置
				{
					if(((data[7]>>4)&0x01)==0x01)    //设置服务器IP
					{
						Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"Rs485LCD Set Server IP:",&data[24],4);
						SRAM_Read_Buff(tempdata,4,(SRAM_Register_Start_ADDRESS+Register_WebServerIP_OFFSET));
						if(memcmp(&data[24],tempdata,4)!=0x00)
						{
							SRAM_Write_Buff(&data[24],4,(SRAM_Register_Start_ADDRESS+Register_WebServerIP_OFFSET));
							wflash_flag = 0x01;
						}
						server_info.init_flag = 0x00;
					}
				}
				
				if(wflash_flag==0x01)
				{
					Retain_Flash_Register_Data();
				}
				
				
				if(((data[7]>>5)&0x01)==0x01)    //继电器控制
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485LCD Relay Ctrl");
					relay_L = data[28] + (data[29]<<8) + (data[30]<<16) + (data[31]<<24);
					relay_H = data[32] + (data[33]<<8) + (data[34]<<16) + (data[35]<<24);
					if((relay_L==0xffffffff)&&(relay_H==0x0f))       //全开
					{
						LCD1602_CtrlDevice(Dev_Host_HVout,0x00,relay_L);
					}
					else if((relay_L==0x00)&&(relay_H==0x00))        //全关
					{
						LCD1602_CtrlDevice(Dev_Host_HVout,0x00,relay_L);
					}
					else                                             //单一开
					{
						LCD1602_CtrlDevice(Dev_Host_HVout,0x00,relay_L);
					}
				}
				
				
				break;
		}
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485LCD, sizeof(RS485_LCD_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485LCD,sizeof(RS485_LCD_INFO),dev_addr+Dev_Privately);
			
	}
	
	return rev;
}





void BLW_RS485_LCD1602_Data_Init(Device_Public_Information_G *BUS_Public, RS485_LCD_INFO *Rs485LCD, uint16_t LoopNum)
{
	BUS_Public->polling_cf = (uint32_t)&BLW_LCD1602_CycleCtrl;
	BUS_Public->processing_cf = (uint32_t)&BLW_Rs485_LCD1602_Check;
	
	Rs485LCD->CtrlCnt = LCD_RETRY_TIME;
}


#endif

