#include "includes.h"

/*

该文件内容为485控制文件，
*/

BLV_BUS_Manage_G BUS485_Info;
BLV_ACTIVE_Manage_G Act485_Info;
BLV_POLL_Manage_G Poll485_Info;

uint8_t Device_485_Processing(uint32_t dev_addr,uint32_t sram_addr,uint16_t len)
{
	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Device_485_Processing ----");
	
	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,RX_Buffer:" , len);
	for(uint16_t i = 0;i < len;i++)
	{
		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(sram_addr + i));
	}
	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
	
	return 0;
}

/*******************************************************************************
* Function Name  : Add_BUS_Device_To_List 
* Description    : 添加BUS设备到链表
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void Add_BUS_Device_To_List(uint8_t type,uint8_t addr,uint8_t *dev_data,uint16_t data_len,void (*polling_cf)(uint32_t ),uint8_t (*processing_cf)(uint32_t ,uint32_t ,uint16_t ))
{
	/*向设备链表添加BUS485设备*/
	uint8_t	check_val = 0;
	uint16_t write_len = data_len + Dev_Privately;	//设备私有数据长度加上公共数据
	uint32_t temp = 0;
	uint32_t list_addr = SRAM_Read_DW(SRAM_BUS_Device_List_Addr);	//获取设备信息保存地址
	if((list_addr < SRAM_Device_List_Start_Addr) || (list_addr > SRAM_Device_List_End_Addr))	list_addr = SRAM_Device_List_Start_Addr;
	
	/*清空数据*/
	for(uint16_t i = 0;i<SRAM_Device_List_Size;i++)
	{
		SRAM_Write_Byte(0x00,list_addr+i);	
	}
	
	SRAM_Write_Byte(type,list_addr+Dev_Type);			//设备类型
	SRAM_Write_Byte(addr,list_addr+Dev_Addr);			//设备地址 - 指硬件地址
	SRAM_Write_Byte(0x03,list_addr+Dev_port);			//设置设备发送串口号
	SRAM_Write_Byte(0x00,list_addr+Dev_Check);			//校验值清零
	SRAM_Write_Word(write_len,list_addr+Dev_DataLen);	//整包数据长度
	SRAM_Write_Byte(0x03,list_addr+Dev_Retrynum);		//设置设备数据重发次数
	SRAM_Write_Word(0x03E8,list_addr+Dev_WaitTime);		//设置设备数据发送等待时间
	
	/*添加485设备的回调函数*/
	temp = (uint32_t)polling_cf;
	SRAM_Write_DW(temp,list_addr+Dev_Polling_CF);
	
	temp = (uint32_t)processing_cf;
	SRAM_Write_DW(temp,list_addr+Dev_Processing_CF);
	
	/*添加设备私有变量*/
	SRAM_DMA_Write_Buff(dev_data,data_len,list_addr+Dev_Privately);
	
	/*计算校验值*/
	check_val = Dev_CheckSum(list_addr,write_len);
	SRAM_Write_Byte(check_val,list_addr+Dev_Check);		//校验值重新写入
	
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,dev Buffer:" , write_len);
//	for(uint16_t i = 0;i < write_len;i++)
//	{
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(list_addr + i));
//	}
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
	
	/*设备添加完毕,设备链表地址偏移*/
	list_addr += SRAM_Device_List_Size;
	SRAM_Write_DW(list_addr,SRAM_BUS_Device_List_Addr);
}

void Add_BUS_Device_To_List2(Device_Public_Information_G *dev_info,uint8_t *dev_data,uint16_t data_len)
{
	/*向设备链表添加BUS485设备*/
	uint8_t	check_val = 0;
	dev_info->data_len = data_len + Dev_Privately;	//设备私有数据长度加上公共数据
	uint32_t list_addr = SRAM_Read_DW(SRAM_BUS_Device_List_Addr);	//获取设备信息保存地址
	if((list_addr < SRAM_Device_List_Start_Addr) || (list_addr > SRAM_Device_List_End_Addr))	list_addr = SRAM_Device_List_Start_Addr;
	
	/*清空数据*/
	for(uint16_t i = 0;i<SRAM_Device_List_Size;i++)
	{
		SRAM_Write_Byte(0x00,list_addr+i);	
	}
			/*添加公有数据*/
	SRAM_DMA_Write_Buff((uint8_t *)dev_info,Dev_Privately,list_addr);  
	
	/*添加485设备的回调函数*/
	SRAM_Write_DW(dev_info->polling_cf,list_addr+Dev_Polling_CF);
	SRAM_Write_DW(dev_info->processing_cf,list_addr+Dev_Processing_CF);
	
	/*添加设备私有变量*/
	SRAM_DMA_Write_Buff(dev_data,data_len,list_addr+Dev_Privately);
	
	/*计算校验值*/
	check_val = Dev_CheckSum(list_addr,dev_info->data_len);
	SRAM_Write_Byte(check_val,list_addr+Dev_Check);		//校验值重新写入
	
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,dev Buffer:" , write_len);
//	for(uint16_t i = 0;i < write_len;i++)
//	{
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(list_addr + i));
//	}
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
	
	/*设备添加完毕,设备链表地址偏移*/
	list_addr += SRAM_Device_List_Size;
	SRAM_Write_DW(list_addr,SRAM_BUS_Device_List_Addr);
}

/*******************************************************************************
* Function Name  : Add_POLL_Device_To_List 
* Description    : 添加轮询设备到链表
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void Add_POLL_Device_To_List(uint8_t type,uint8_t addr,uint8_t *dev_data,uint16_t data_len,void (*polling_cf)(uint32_t ),uint8_t (*processing_cf)(uint32_t ,uint32_t ,uint16_t ))
{
	/*向设备链表添加POLL485设备*/
	uint8_t	check_val = 0;
	uint16_t write_len = data_len + Dev_Privately;	//设备私有数据长度加上公共数据
	uint32_t temp = 0;
	uint32_t list_addr = SRAM_Read_DW(SRAM_POLL_Device_List_Addr);	//获取设备信息保存地址
	uint32_t Start_addr = SRAM_Read_DW(SRAM_BUS_Device_List_Addr);	//获取轮询设备信息起始地址
	if((Start_addr < SRAM_Device_List_Start_Addr) || (Start_addr > SRAM_Device_List_End_Addr))	
	{
		Start_addr = SRAM_Device_List_Start_Addr;
		SRAM_Write_DW(Start_addr,SRAM_BUS_Device_List_Addr);
	}
	if( (list_addr < Start_addr) || (list_addr > SRAM_Device_List_End_Addr))	list_addr = Start_addr;
	
	SRAM_Write_Byte(type,list_addr+Dev_Type);			//设备类型
	SRAM_Write_Byte(addr,list_addr+Dev_Addr);			//设备地址 - 指硬件地址
	SRAM_Write_Byte(0x02,list_addr+Dev_port);			//设置设备发送串口号 - 轮询端口
	SRAM_Write_DW((uint32_t)512000,list_addr+Dev_baud);			
	SRAM_Write_Byte(0x00,list_addr+Dev_Check);			//校验值清零
	SRAM_Write_Word(write_len,list_addr+Dev_DataLen);	//整包数据长度
	SRAM_Write_Byte(0x03,list_addr+Dev_Retrynum);		//设置设备数据重发次数
	SRAM_Write_Word(0x03E8,list_addr+Dev_WaitTime);		//设置设备数据发送等待时间
	
	/*添加485设备的回调函数*/
	temp = (uint32_t)polling_cf;
	SRAM_Write_DW(temp,list_addr+Dev_Polling_CF);
	
	temp = (uint32_t)processing_cf;
	SRAM_Write_DW(temp,list_addr+Dev_Processing_CF);
	
	/*添加设备私有变量*/
	SRAM_DMA_Write_Buff(dev_data,data_len,list_addr+Dev_Privately);
	
	/*计算校验值*/
	check_val = Dev_CheckSum(list_addr,write_len);
	SRAM_Write_Byte(check_val,list_addr+Dev_Check);		//校验值重新写入
	
//	Dbg_Print(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,dev Buffer:" , write_len);
//	for(uint16_t i = 0;i < write_len;i++)
//	{
//		DBG_log("%02X " , SRAM_Read_Byte(list_addr + i));
//	}
//	DBG_log("\r\n");
	
	/*设备添加完毕,设备链表地址偏移*/
	list_addr += SRAM_Device_List_Size;
	SRAM_Write_DW(list_addr,SRAM_POLL_Device_List_Addr);
}

/*******************************************************************************
* Function Name  : Add_POLL_Device_To_List 
* Description    : 添加轮询设备到链表
* Input          : 
	Device_Public_Information_G *dev_info	: 添加的设备公共数据
	dev_data		：设备的私有数据
	addr			: 设备私有数据长度

* Return         : None
*******************************************************************************/
void Add_POLL_Device_To_List2(Device_Public_Information_G *dev_info,uint8_t *dev_data,uint16_t data_len)
{
	/*向设备链表添加POLL485设备*/

	dev_info->data_len = data_len + Dev_Privately;	//设备私有数据长度加上公共数据

	uint32_t list_addr = SRAM_Read_DW(SRAM_POLL_Device_List_Addr);	//获取设备信息保存地址
	uint32_t Start_addr = SRAM_Read_DW(SRAM_BUS_Device_List_Addr);	//获取轮询设备信息起始地址
	if((Start_addr < SRAM_Device_List_Start_Addr) || (Start_addr > SRAM_Device_List_End_Addr))	
	{
		Start_addr = SRAM_Device_List_Start_Addr;
		SRAM_Write_DW(Start_addr,SRAM_BUS_Device_List_Addr);
	}
	if( (list_addr < Start_addr) || (list_addr > SRAM_Device_List_End_Addr))	list_addr = Start_addr;
	
	/*清空数据*/
	for(uint16_t i = 0;i<SRAM_Device_List_Size;i++)
	{
		SRAM_Write_Byte(0x00,list_addr+i);	
	}
	
			/*添加公有数据*/
	SRAM_DMA_Write_Buff((uint8_t *)dev_info,Dev_Privately,list_addr);  
	
	/*添加485设备的回调函数*/
	SRAM_Write_DW(dev_info->polling_cf,list_addr+Dev_Polling_CF);
	
	SRAM_Write_DW(dev_info->processing_cf,list_addr+Dev_Processing_CF);
	
	/*添加设备私有变量*/
	SRAM_DMA_Write_Buff(dev_data,data_len,list_addr+Dev_Privately);
	
	/*计算校验值*/
	dev_info->check = Dev_CheckSum(list_addr,dev_info->data_len);
	SRAM_Write_Byte(dev_info->check,list_addr+Dev_Check);		//校验值重新写入
	
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,dev Buffer:" , write_len);
//	for(uint16_t i = 0;i < write_len;i++)
//	{
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(list_addr + i));
//	}
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
	
	/*设备添加完毕,设备链表地址偏移*/
	list_addr += SRAM_Device_List_Size;
	SRAM_Write_DW(list_addr,SRAM_POLL_Device_List_Addr);
}

/*******************************************************************************
* Function Name  : Add_POLL_Device_To_List 
* Description    : 添加轮询设备到链表
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void Add_ACTIVE_Device_To_List(uint8_t type,uint8_t addr,uint8_t *dev_data,uint16_t data_len,void (*polling_cf)(uint32_t ),uint8_t (*processing_cf)(uint32_t ,uint32_t ,uint16_t ))
{
	/*向设备链表添加ACTIVE485设备*/
	uint8_t	check_val = 0;
	uint16_t write_len = data_len + Dev_Privately;	//设备私有数据长度加上公共数据
	uint32_t temp = 0;
	uint32_t list_addr = SRAM_Read_DW(SRAM_ACTIVE_Device_List_Addr);	//获取设备信息保存地址
	uint32_t Start_addr = SRAM_Read_DW(SRAM_POLL_Device_List_Addr);	//获取轮询设备信息起始地址
	
	if((Start_addr < SRAM_Device_List_Start_Addr) || (Start_addr > SRAM_Device_List_End_Addr))	
	{
		Start_addr = SRAM_Device_List_Start_Addr;
		SRAM_Write_DW(Start_addr,SRAM_POLL_Device_List_Addr);
	}
	if((list_addr < Start_addr) || (list_addr > SRAM_Device_List_End_Addr))	list_addr = Start_addr;
	
	/*清空数据*/
	for(uint16_t i = 0;i<SRAM_Device_List_Size;i++)
	{
		SRAM_Write_Byte(0x00,list_addr+i);	
	}
	
	SRAM_Write_Byte(type,list_addr+Dev_Type);			//设备类型
	SRAM_Write_Byte(addr,list_addr+Dev_Addr);			//设备地址 - 指硬件地址
	SRAM_Write_Byte(0x01,list_addr+Dev_port);			//设置设备发送串口号 - 轮询端口
	SRAM_Write_Byte(0x00,list_addr+Dev_Check);			//校验值清零
	SRAM_Write_Word(write_len,list_addr+Dev_DataLen);	//整包数据长度
	SRAM_Write_Byte(0x03,list_addr+Dev_Retrynum);		//设置设备数据重发次数
	SRAM_Write_Word(0x03E8,list_addr+Dev_WaitTime);		//设置设备数据发送等待时间
	
	/*添加485设备的回调函数*/
	temp = (uint32_t)polling_cf;
	SRAM_Write_DW(temp,list_addr+Dev_Polling_CF);
	
	temp = (uint32_t)processing_cf;
	SRAM_Write_DW(temp,list_addr+Dev_Processing_CF);
	
	/*添加设备私有变量*/
	SRAM_DMA_Write_Buff(dev_data,data_len,list_addr+Dev_Privately);
	
	/*计算校验值*/
	check_val = Dev_CheckSum(list_addr,write_len);
	SRAM_Write_Byte(check_val,list_addr+Dev_Check);		//校验值重新写入
	
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,dev Buffer:" , write_len);
//	for(uint16_t i = 0;i < write_len;i++)
//	{
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(list_addr + i));
//	}
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
	
	/*设备添加完毕,设备链表地址偏移*/
	list_addr += SRAM_Device_List_Size;
	SRAM_Write_DW(list_addr,SRAM_POLL_Device_List_Addr);
}

/*******************************************************************************
* Function Name  : Add_ACT_Device_To_List2 
* Description    : 添加主动设备到链表
* Input          : 
	Device_Public_Information_G *dev_info	: 添加的设备公共数据
	dev_data		：设备的私有数据
	addr			: 设备私有数据长度

* Return         : None
*******************************************************************************/
void Add_ACT_Device_To_List2(Device_Public_Information_G *dev_info,uint8_t *dev_data,uint16_t data_len)
{
	/*向设备链表添加POLL485设备*/
	dev_info->check = 0x00;
	dev_info->data_len = data_len + Dev_Privately;	//设备私有数据长度加上公共数据

	uint32_t list_addr = SRAM_Read_DW(SRAM_ACTIVE_Device_List_Addr);	//获取设备信息保存地址
	uint32_t Start_addr = SRAM_Read_DW(SRAM_POLL_Device_List_Addr);	//获取轮询设备信息起始地址
	
	if((Start_addr < SRAM_Device_List_Start_Addr) || (Start_addr > SRAM_Device_List_End_Addr))	
	{
		Start_addr = SRAM_Device_List_Start_Addr;
		SRAM_Write_DW(Start_addr,SRAM_POLL_Device_List_Addr);
	}
	if((list_addr < Start_addr) || (list_addr > SRAM_Device_List_End_Addr))	list_addr = Start_addr;
	
	/*清空数据*/
	for(uint16_t i = 0;i<SRAM_Device_List_Size;i++)
	{
		SRAM_Write_Byte(0x00,list_addr+i);	
	}
		/*添加公有数据*/
	SRAM_DMA_Write_Buff((uint8_t *)dev_info,Dev_Privately,list_addr);   
	
	/*添加485设备的回调函数*/
	SRAM_Write_DW(dev_info->polling_cf,list_addr+Dev_Polling_CF);
	
	SRAM_Write_DW(dev_info->processing_cf,list_addr+Dev_Processing_CF);
	
	/*添加设备私有变量*/
	SRAM_DMA_Write_Buff(dev_data,data_len,list_addr+Dev_Privately);
	
	/*计算校验值*/
	dev_info->check = Dev_CheckSum(list_addr,dev_info->data_len);
	SRAM_Write_Byte(dev_info->check,list_addr+Dev_Check);		//校验值重新写入
	
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,dev Buffer:" , write_len);
//	for(uint16_t i = 0;i < write_len;i++)
//	{
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(list_addr + i));
//	}
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
	
	/*设备添加完毕,设备链表地址偏移*/
	list_addr += SRAM_Device_List_Size;
	SRAM_Write_DW(list_addr,SRAM_ACTIVE_Device_List_Addr);
}




/*******************************************************************************
* Function Name  : BLV_BUS_Polling_Task 
* Description    : BUS轮询任务
*******************************************************************************/
void BLV_BUS_Polling_Task2(void)
{
	static uint32_t BLV_BUS_Wait = 0;
	
	uint16_t data_len = 0;
	uint8_t rev = 0;
	
	if(BUS485_Info.device_num == 0x00)	return ;
	
	switch(BUS485_Info.BUS_Start)
	{
		case B_IDLE:
			
			BUS485_Info.BUS_Start = B_Polling;
			break;
		case B_Polling:
			if(SRAM_Read_Byte(BUS485_Info.Last_list_addr + Dev_port) == Bus_port)
			{
				/*校验数据*/
				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS Data_Check");
				if(Device_Data_Check(BUS485_Info.Last_list_addr) == 0)
				{
					BUS485_Info.n_list_read_addr = BUS485_Info.Last_list_addr;
					/*读取设备信息*/
					//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS Read Data_Check");
					
//					BUS485_Info.n_dev_type = SRAM_Read_Byte(BUS485_Info.n_list_read_addr + Dev_Type);
//					BUS485_Info.n_dev_addr = SRAM_Read_Byte(BUS485_Info.n_list_read_addr + Dev_Addr);
//					BUS485_Info.n_dev_datalen = SRAM_Read_Word(BUS485_Info.n_list_read_addr + Dev_DataLen);
//					BUS485_Info.n_polling_cf = SRAM_Read_DW(BUS485_Info.n_list_read_addr + Dev_Polling_CF);
//					BUS485_Info.n_processing_cf = SRAM_Read_DW(BUS485_Info.n_list_read_addr + Dev_Processing_CF);
//					BUS485_Info.n_dev_waittime = SRAM_Read_Word(BUS485_Info.n_list_read_addr + Dev_WaitTime);
//					BUS485_Info.n_retry_num = SRAM_Read_DW(BUS485_Info.n_list_read_addr + Dev_Retrynum);
					
					Device_Public_Information_G dev_info;
					SRAM_Read_Buff((uint8_t *)&dev_info,sizeof(Device_Public_Information_G),BUS485_Info.n_list_read_addr);
					BUS485_Info.n_dev_type = dev_info.type;
					BUS485_Info.n_dev_addr = dev_info.addr;
					BUS485_Info.n_dev_datalen = dev_info.data_len;
					BUS485_Info.n_polling_cf = dev_info.polling_cf;
					BUS485_Info.n_processing_cf = dev_info.processing_cf;
					BUS485_Info.n_dev_waittime = dev_info.wait_time;
					BUS485_Info.n_retry_num = dev_info.retry_num;
					
//					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port type:%ld",BUS485_Info.n_dev_type);
//					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port addr:%ld",BUS485_Info.n_dev_addr);
//					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port data_len:%ld",BUS485_Info.n_dev_datalen);
//					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port polling_cf:%ld",BUS485_Info.n_polling_cf);
//					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port processing_cf:%ld",BUS485_Info.n_processing_cf);
//					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port wait_time:%ld",BUS485_Info.n_dev_waittime);
//					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port retry_num:%ld",BUS485_Info.n_retry_num);
					
					//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port polling_cf:%ld",BUS485_Info.baud);
					
					BUS485_Info.Retry_Flag = 0x01;
					
					/*调用设备轮询发送函数*/
					//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_BUS_dev Polling");
					
					if(BUS485_Info.baud != dev_info.baud)	//设备波特率与当前通讯波特率不符
					{
						/*切换波特率*/
						//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_Port Baud:%ld,Change Baud:%ld\r\n",BUS485_Info.baud,dev_info.baud);
						BUS485_Info.baud = dev_info.baud;
						if(BUS485_Info.BaudRateCfg != NULL)	BUS485_Info.BaudRateCfg(BUS485_Info.baud);
						
						BUS485_Info.BUS_Start = Baud_Wait;
						BUS485_Info.change_tick = SysTick_1ms;
						
					}else{		//波特率不必切换
						if(NULL != BUS485_Info.n_polling_cf) rev =  ((fun4_prt)BUS485_Info.n_polling_cf)(BUS485_Info.n_list_read_addr);
						
						if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
						{
							BUS485_Info.BUS_Start = Change_Dev;
							/*BUS485总线上有不需要发送轮询数据的设备，需要跳过等待回复阶段直接切换设备，因此重发标志位不需要置位 - 2022-05-04*/
							BUS485_Info.Retry_Flag = 0x00;
							BUS485_Info.n_retry_num = 0x00;
						}else {				//数据发送成功，等待回复
							BLV_BUS_Wait = SysTick_1ms;
							BUS485_Info.BUS_Start = Wait_Reply;
						}
//						BLV_BUS_Wait = SysTick_1ms;
//						BUS485_Info.BUS_Start = Wait_Reply;
					}		

					
				}else {
					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"[BLV_BUS_Polling_Task2]BLV_BUS_dev Check Fail:%08X",BUS485_Info.Last_list_addr);
					BUS485_Info.BUS_Start = Change_Dev;
				}
			}else {
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_BUS_dev Type Fail");
				BUS485_Info.BUS_Start = Change_Dev;
			}
			
			
			break;
		case Change_Dev:
			BUS485_Info.Last_list_addr += SRAM_Device_List_Size;		//下一个设备
			if(BUS485_Info.Last_list_addr >= SRAM_Read_DW(SRAM_BUS_Device_List_Addr))	BUS485_Info.Last_list_addr = SRAM_Device_List_Start_Addr;
			//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS Change_Dev");
			BUS485_Info.BUS_Start = B_Polling;
			break;
		case B_Retry:
			if((BUS485_Info.Retry_Flag == 0x01) && (BUS485_Info.n_retry_num != 0x00))			//重发标志未清零，表示没发送成功
			{
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS Retransmitting Data:%d-%d-%08X...",BUS485_Info.n_dev_type,BUS485_Info.n_dev_addr,BUS485_Info.n_list_read_addr);
				
				//修改时间:2022-07-12
				if(NULL != BUS485_Info.n_polling_cf) rev =  ((fun4_prt)BUS485_Info.n_polling_cf)(BUS485_Info.n_list_read_addr);		//继续调用设备发送函数
				
				if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
				{
					BUS485_Info.BUS_Start = Change_Dev;
					/*BUS485总线上有不需要发送轮询数据的设备，需要跳过等待回复阶段直接切换设备，因此重发标志位不需要置位 - 2022-05-04*/
					BUS485_Info.Retry_Flag = 0x00;	
					BUS485_Info.n_retry_num = 0x00;
				}else {				//数据发送成功，等待回复
					BLV_BUS_Wait = SysTick_1ms;		//重新计时
					BUS485_Info.n_retry_num--;
					BUS485_Info.BUS_Start = Wait_Reply;
				}
				
				
			}else if((BUS485_Info.Retry_Flag == 0x01) && (BUS485_Info.n_retry_num == 0x00))
			{
				BUS485_Info.BUS_Start = Change_Dev;		//发送失败，切换下个设备
				//BUS485_Info.BUS_Start = B_IDLE;				//发送失败，切换下个设备
			}else {			//2021-06-29 新增
				BUS485_Info.BUS_Start = Change_Dev;		//发送失败，切换下个设备
			}
			break;
		case Wait_Reply:
			/*接收处理*/
			if(g_Uart[USART_3].RX_Buffer_WriteAddr != g_Uart[USART_3].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_3].RX_Buffer_ReadAddr);

				if((BUS485_Info.n_processing_cf!=0x00000000) && (BUS485_Info.n_processing_cf!=0xFFFFFFFF))	{
					BUS485_Info.Retry_Flag = ((fun2_prt)BUS485_Info.n_processing_cf)(BUS485_Info.n_list_read_addr,g_Uart[USART_3].RX_Buffer_ReadAddr + 2,data_len);
				}
				
				if(BUS485_Info.Retry_Flag == 0x00)	{
					//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Reply SUCC\r\n");
					BUS485_Info.send_wait = SysTick_1ms;
					BUS485_Info.BUS_Start = B_Wait;		//回复成功，等待总线空闲，便切换为下一个设备
				}
				
				if(BUS485_Info.port_mode == Port_Monitoring_mode)	//如果当前处于监控模式下,将数据发送至PC端
				{
					Udp_Internal_SeriaNet_Uploading(Bus_port,BUS485_Info.baud,g_Uart[USART_3].RX_Buffer_ReadAddr);					//上报
				}
				
				g_Uart[USART_3].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_3].RX_Buffer_ReadAddr > SRAM_UART3_RecvBuffer_End_Addr)	{
					g_Uart[USART_3].RX_Buffer_ReadAddr = SRAM_UART3_RecvBuffer_Start_Addr;
				}
			}
			/*接收超时 - 进入重发*/
			if(SysTick_1ms - BLV_BUS_Wait > BUS485_Info.n_dev_waittime)	BUS485_Info.BUS_Start = B_Retry;
			break;
		case B_Wait:
			if(SysTick_1ms - BUS485_Info.send_wait > BLV_BUS485_WaitLdle_Time)	{
				
				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS B_Wait");
				BUS485_Info.BUS_Start = Change_Dev;
			}
			//BUS485_Info.BUS_Start = Change_Dev;
			break;
			/*2021-11-24 : 调整C5IO波特率不准，发送数据前根据当前设备通讯波特率，切换发送波特率，切换完波特率后，等待10ms在进行发送数据
			2022-07-19 : 调整切换波特率时，先通讯一次，接收回来的数据不做处理直接丢弃
			*/
			
		case Baud_Wait:		//切换波特率等待时间
			if(SysTick_1ms - BUS485_Info.change_tick > BLV_BUS485_ChangeBaudWaitTime)	
			{
				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_BUS State -- Baud_Wait");
				
				if(NULL != BUS485_Info.n_polling_cf) rev =  ((fun4_prt)BUS485_Info.n_polling_cf)(BUS485_Info.n_list_read_addr);		//继续调用设备发送函数
				
				BUS485_Info.change_tick = SysTick_1ms;
				BUS485_Info.BUS_Start = Baud_Comm;
			}
			
			break;
		case Baud_Comm:
			/*接收处理*/
			if(g_Uart[USART_3].RX_Buffer_WriteAddr != g_Uart[USART_3].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_3].RX_Buffer_ReadAddr);

				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_BUS State -- Baud_Comm1");
				
				if((BUS485_Info.n_processing_cf!=0x00000000) && (BUS485_Info.n_processing_cf!=0xFFFFFFFF))	{
					BUS485_Info.Retry_Flag = ((fun2_prt)BUS485_Info.n_processing_cf)(BUS485_Info.n_list_read_addr,g_Uart[USART_3].RX_Buffer_ReadAddr + 2,data_len);
				}
				if(BUS485_Info.Retry_Flag == 0x00)	//回复成功，立即开始下一次通讯
				{
					//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_BUS State -- Baud_Comm3");
					BUS485_Info.BUS_Start = Baud_SendWait;
					BUS485_Info.change_tick = SysTick_1ms;
				}
				
				
				if(BUS485_Info.port_mode == Port_Monitoring_mode)	//如果当前处于监控模式下,将数据发送至PC端
				{
					Udp_Internal_SeriaNet_Uploading(Bus_port,BUS485_Info.baud,g_Uart[USART_3].RX_Buffer_ReadAddr);					//上报
				}
				
				g_Uart[USART_3].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_3].RX_Buffer_ReadAddr > SRAM_UART3_RecvBuffer_End_Addr)	{
					g_Uart[USART_3].RX_Buffer_ReadAddr = SRAM_UART3_RecvBuffer_Start_Addr;
				}
			}
			/*接收等待超时*/
			if(SysTick_1ms - BUS485_Info.change_tick > BUS485_Info.n_dev_waittime)		{
				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_BUS State -- Baud_Comm2");
				BUS485_Info.change_tick = SysTick_1ms;
				BUS485_Info.BUS_Start = Baud_SendWait;
			}
			break;
		case Baud_SendWait:
			if(SysTick_1ms - BUS485_Info.change_tick > BLV_BUS485_ChangeBaudSendWaitTime)		{
				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_BUS State -- Baud_Comm2");
				BUS485_Info.change_tick = SysTick_1ms;
				BUS485_Info.BUS_Start = B_Send;
			}
			break;
		case B_Send:		//发送函数
			//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_BUS State -- send");
		
			//if((BUS485_Info.n_polling_cf!=0x00000000) && (BUS485_Info.n_polling_cf!=0xFFFFFFFF))	Convert_To_Fun_Prt(BUS485_Info.n_polling_cf,BUS485_Info.n_list_read_addr);
			
			//修改时间:2022-07-12
			if(NULL != BUS485_Info.n_polling_cf) rev =  ((fun4_prt)BUS485_Info.n_polling_cf)(BUS485_Info.n_list_read_addr);		//继续调用设备发送函数
			
			if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
			{
				BUS485_Info.BUS_Start = Change_Dev;
				/*BUS485总线上有不需要发送轮询数据的设备，需要跳过等待回复阶段直接切换设备，因此重发标志位不需要置位 - 2022-05-04*/
				BUS485_Info.Retry_Flag = 0x00;	
				BUS485_Info.n_retry_num = 0x00;
			}else {				//数据发送成功，等待回复
				BLV_BUS_Wait = SysTick_1ms;		//重新计时
				BUS485_Info.BUS_Start = Wait_Reply;
			}
		
			break;
		default:
			BUS485_Info.BUS_Start = Change_Dev;
			break;

	}
	
	/*当前不是处于正常模式下，超时回归正常模式*/
	if(BUS485_Info.port_mode != Port_Normal_Mode)
	{
		if(SysTick_1s - BUS485_Info.mode_tick > BUS485_Info.mode_outtime)
		{
			Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Reply SUCC");
			BUS485_Info.mode_tick = SysTick_1s;
			BUS485_Info.port_mode = Port_Normal_Mode;		//正常模式
		}
	}

}


/*******************************************************************************
* Function Name  : BLV_BUS_Processing_Task  -- 没调用
* Description    : BUS数据处理任务
*******************************************************************************/
void BLV_BUS_Processing_Task(void)
{
	uint16_t data_len = 0;
	if(g_Uart[USART_3].RX_Buffer_WriteAddr != g_Uart[USART_3].RX_Buffer_ReadAddr)
	{
		data_len = SRAM_Read_Word(g_Uart[USART_3].RX_Buffer_ReadAddr);
		
		/*数据处理 -- 将数据传入数据解析函数*/
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,RX_Buffer:" , data_len);
//		for(uint16_t i = 0;i < data_len;i++)
//		{
//			Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(g_Uart[USART_3].RX_Buffer_ReadAddr + i + 2));
//		}
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
		
		Convert_To_Fun2_Prt(BUS485_Info.n_processing_cf,BUS485_Info.n_list_read_addr,g_Uart[USART_3].RX_Buffer_ReadAddr + 2,data_len);
		BUS485_Info.Retry_Flag = 0;
		
		
		g_Uart[USART_3].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
		if(g_Uart[USART_3].RX_Buffer_ReadAddr > SRAM_UART3_RecvBuffer_End_Addr)	g_Uart[USART_3].RX_Buffer_ReadAddr = SRAM_UART3_RecvBuffer_Start_Addr;
		
	}

}

/*******************************************轮询端口********************************************************************************/

/*******************************************************************************
* Function Name  : BLV_PollPort_Task 
* Description    : PollPort轮询任务
*******************************************************************************/
void BLV_PollPort_Task(void)
{
	static uint32_t BLV_POLL_Wait = 0;
	uint16_t data_len = 0;
	uint8_t rev = 0;
#if (USE_CORE_TYPE == 1)    //使用C1F核心板
	if(Poll485_Info.device_num == 0x00)	return ;
	
	switch(Poll485_Info.POLL_Start)
	{
		case B_IDLE:
			
			Poll485_Info.POLL_Start = B_Polling;
			break;
		case B_Polling:
			if(SRAM_Read_Byte(Poll485_Info.Last_list_addr + Dev_port) == Polling_Port)
			{
				/*校验数据*/
				if(Device_Data_Check(Poll485_Info.Last_list_addr) == 0)
				{
					Poll485_Info.n_list_read_addr = Poll485_Info.Last_list_addr;
					/*波特率切换*/
					if(Poll485_Info.baud != SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_baud))
					{
						Poll485_Info.baud = SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_baud);
						Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_POLL_dev change baud:%08X",Poll485_Info.baud);
						Poll485_Info.BaudRateCfg( Poll485_Info.baud );
						Poll485_Info.POLL_Start = Baud_Wait;
						Poll485_Info.change_tick = SysTick_1ms;
					}else {
						/*读取设备信息*/
						Device_Public_Information_G dev_info;
						SRAM_Read_Buff((uint8_t *)&dev_info,sizeof(Device_Public_Information_G),Poll485_Info.n_list_read_addr);
						
						Poll485_Info.n_dev_type = dev_info.type; //SRAM_Read_Byte(Poll485_Info.n_list_read_addr + Dev_Type);
						Poll485_Info.n_dev_addr = dev_info.addr; // SRAM_Read_Byte(Poll485_Info.n_list_read_addr + Dev_Addr);
						Poll485_Info.n_dev_datalen = dev_info.data_len; //SRAM_Read_Word(Poll485_Info.n_list_read_addr + Dev_DataLen);
						Poll485_Info.n_polling_cf = dev_info.polling_cf; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Polling_CF);
						Poll485_Info.n_processing_cf = dev_info.processing_cf; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Processing_CF);
						Poll485_Info.n_dev_waittime = dev_info.wait_time; //SRAM_Read_Word(Poll485_Info.n_list_read_addr + Dev_WaitTime);
						
						Poll485_Info.n_retry_num = dev_info.retry_num; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Retrynum);
						Poll485_Info.Retry_Flag = 0x01;
						
						/*2021 09 17 ：修改增加发送函数返回值判断，RS485OCCUPYNOTIME表示数据没有发送数据，跳过该设备*/
						
						if((Poll485_Info.n_polling_cf!=0x00000000) && (Poll485_Info.n_polling_cf!=0xFFFFFFFF))	rev = ((fun4_prt)Poll485_Info.n_polling_cf)(Poll485_Info.n_list_read_addr);
						
						if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
						{
							Poll485_Info.POLL_Start = Change_Dev;
						}else {				//数据发送成功，等待回复
							BLV_POLL_Wait = SysTick_1ms;
							Poll485_Info.POLL_Start = Wait_Reply;
						}
					}
				}else {
					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_POLL_dev Check Fail:%08X",Poll485_Info.Last_list_addr);
					Poll485_Info.POLL_Start = Change_Dev;
				}
			}else {
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_POLL_dev Type Fail:%08X , %d",Poll485_Info.Last_list_addr,SRAM_Read_Byte(Poll485_Info.Last_list_addr + Dev_Type));
				Poll485_Info.POLL_Start = Change_Dev;
			}
			
			
			break;
		case Baud_Wait:
			if(SysTick_1ms - Poll485_Info.change_tick > BLV_POLL485_ChangeBaudWaitTime)
			{
				/*读取设备信息*/
				
				Device_Public_Information_G dev_info;
				SRAM_Read_Buff((uint8_t *)&dev_info,sizeof(Device_Public_Information_G),Poll485_Info.n_list_read_addr);
				
				Poll485_Info.n_dev_type = dev_info.type; //SRAM_Read_Byte(Poll485_Info.n_list_read_addr + Dev_Type);
				Poll485_Info.n_dev_addr = dev_info.addr; // SRAM_Read_Byte(Poll485_Info.n_list_read_addr + Dev_Addr);
				Poll485_Info.n_dev_datalen = dev_info.data_len; //SRAM_Read_Word(Poll485_Info.n_list_read_addr + Dev_DataLen);
				Poll485_Info.n_polling_cf = dev_info.polling_cf; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Polling_CF);
				Poll485_Info.n_processing_cf = dev_info.processing_cf; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Processing_CF);
				Poll485_Info.n_dev_waittime = dev_info.wait_time; //SRAM_Read_Word(Poll485_Info.n_list_read_addr + Dev_WaitTime);
				
				Poll485_Info.n_retry_num = dev_info.retry_num; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Retrynum);
										
				Poll485_Info.Retry_Flag = 0x01;
				
				/*2021 09 17 ：修改增加发送函数返回值判断，0xF0表示数据没有发送数据，跳过该设备*/
				
				if((Poll485_Info.n_polling_cf!=0x00000000) && (Poll485_Info.n_polling_cf!=0xFFFFFFFF))	rev = ((fun4_prt)Poll485_Info.n_polling_cf)(Poll485_Info.n_list_read_addr);
				
				if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
				{
					Poll485_Info.POLL_Start = Change_Dev;
				}else {				//数据发送成功，等待回复
					BLV_POLL_Wait = SysTick_1ms;
					Poll485_Info.POLL_Start = Wait_Reply;
				}
			}
			break;
		case Change_Dev:
			Poll485_Info.Last_list_addr += SRAM_Device_List_Size;		//下一个设备
			if(Poll485_Info.Last_list_addr >= SRAM_Read_DW(SRAM_POLL_Device_List_Addr))	Poll485_Info.Last_list_addr = SRAM_Read_DW(SRAM_BUS_Device_List_Addr);
			
			Poll485_Info.POLL_Start = B_Polling;
			break;
		case B_Retry:
			if((Poll485_Info.Retry_Flag == 0x01) && (Poll485_Info.n_retry_num != 0x00))			//重发标志未清零，表示没发送成功
			{
				//Convert_To_Fun_Prt(Poll485_Info.n_polling_cf,Poll485_Info.n_list_read_addr);		//继续调用设备发送函数
				
				/*2021 09 17 ：修改增加发送函数返回值判断，0xF0表示数据没有发送数据，跳过该设备*/
				if((Poll485_Info.n_polling_cf!=0x00000000) && (Poll485_Info.n_polling_cf!=0xFFFFFFFF))	rev = ((fun4_prt)Poll485_Info.n_polling_cf)(Poll485_Info.n_list_read_addr);
				
				if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
				{
					Poll485_Info.POLL_Start = Change_Dev;
				}else {				//数据发送成功，等待回复
					BLV_POLL_Wait = SysTick_1ms;		//重新计时
					Poll485_Info.n_retry_num--;
					Poll485_Info.POLL_Start = Wait_Reply;
				}
			}else if((Poll485_Info.Retry_Flag == 0x01) && (Poll485_Info.n_retry_num == 0x00))
			{
				Poll485_Info.POLL_Start = Change_Dev;		//发送失败，切换下个设备
			}else {
				Poll485_Info.POLL_Start = Change_Dev;
			}
			break;
		case Wait_Reply:
			/*接收处理*/
			if(g_Uart[USART_0].RX_Buffer_WriteAddr != g_Uart[USART_0].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_0].RX_Buffer_ReadAddr);

				if((Poll485_Info.n_processing_cf!=0x00000000) && (Poll485_Info.n_processing_cf!=0xFFFFFFFF))	{
					Poll485_Info.Retry_Flag = ((fun2_prt )Poll485_Info.n_processing_cf)(Poll485_Info.n_list_read_addr,g_Uart[USART_0].RX_Buffer_ReadAddr + 2,data_len);
				}
				
				if(Poll485_Info.Retry_Flag == 0x00)	{
					//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Reply SUCC\r\n");
					Poll485_Info.send_wait = SysTick_1ms;
					Poll485_Info.POLL_Start = B_Wait;		//回复成功，等待总线空闲，便切换为下一个设备
				}
				
				if(Poll485_Info.port_mode == Port_Monitoring_mode)	//如果当前处于监控模式下,将数据发送至PC端
				{
					Udp_Internal_SeriaNet_Uploading(Polling_Port,Poll485_Info.baud,g_Uart[USART_1].RX_Buffer_ReadAddr);					//上报
				}
				
				g_Uart[USART_0].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_0].RX_Buffer_ReadAddr > SRAM_UART0_RecvBuffer_End_Addr)	{
					g_Uart[USART_0].RX_Buffer_ReadAddr = SRAM_UART0_RecvBuffer_Start_Addr;
				}
			}
			/*接收超时 - 进入重发*/
			if(SysTick_1ms - BLV_POLL_Wait > Poll485_Info.n_dev_waittime)	Poll485_Info.POLL_Start = B_Retry;
			break;
		case B_Wait:	//发送成功等待时间
			if(SysTick_1ms - BLV_POLL_Wait > Poll485_Info.n_dev_waittime)	Poll485_Info.POLL_Start = Change_Dev;
			break;
		default:
			Poll485_Info.POLL_Start = Change_Dev;
			break;
	}
	
	/*当前不是处于正常模式下，超时回归正常模式*/
	if(Poll485_Info.port_mode != Port_Normal_Mode)
	{
		if(SysTick_1s - Poll485_Info.mode_tick > Poll485_Info.mode_outtime)
		{
			Poll485_Info.mode_tick = SysTick_1s;
			Poll485_Info.port_mode = Port_Normal_Mode;		//正常模式
		}
	}
#elif (USE_CORE_TYPE == 2)   //使用C1核心板
	if(Poll485_Info.device_num == 0x00)	return ;
	
	switch(Poll485_Info.POLL_Start)
	{
		case B_IDLE:
			
			Poll485_Info.POLL_Start = B_Polling;
			break;
		case B_Polling:
			if(SRAM_Read_Byte(Poll485_Info.Last_list_addr + Dev_port) == Polling_Port)
			{
				/*校验数据*/
				if(Device_Data_Check(Poll485_Info.Last_list_addr) == 0)
				{
					Poll485_Info.n_list_read_addr = Poll485_Info.Last_list_addr;
					/*波特率切换*/
					if(Poll485_Info.baud != SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_baud))
					{
						Poll485_Info.baud = SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_baud);
						Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_POLL_dev change baud:%08X\r\n",Poll485_Info.baud);
						Poll485_Info.BaudRateCfg( Poll485_Info.baud );
						Poll485_Info.POLL_Start = Baud_Wait;
						Poll485_Info.change_tick = SysTick_1ms;
					}else {
						/*读取设备信息*/

						Device_Public_Information_G dev_info;
						SRAM_Read_Buff((uint8_t *)&dev_info,sizeof(Device_Public_Information_G),Poll485_Info.n_list_read_addr);
						
						Poll485_Info.n_dev_type = dev_info.type; //SRAM_Read_Byte(Poll485_Info.n_list_read_addr + Dev_Type);
						Poll485_Info.n_dev_addr = dev_info.addr; // SRAM_Read_Byte(Poll485_Info.n_list_read_addr + Dev_Addr);
						Poll485_Info.n_dev_datalen = dev_info.data_len; //SRAM_Read_Word(Poll485_Info.n_list_read_addr + Dev_DataLen);
						Poll485_Info.n_polling_cf = dev_info.polling_cf; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Polling_CF);
						Poll485_Info.n_processing_cf = dev_info.processing_cf; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Processing_CF);
						Poll485_Info.n_dev_waittime = dev_info.wait_time; //SRAM_Read_Word(Poll485_Info.n_list_read_addr + Dev_WaitTime);
						
						Poll485_Info.n_retry_num = dev_info.retry_num; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Retrynum);
						
						Poll485_Info.Retry_Flag = 0x01;
						
						
						/*2021 09 17 ：修改增加发送函数返回值判断，0xF0表示数据没有发送数据，跳过该设备*/
						
						if((Poll485_Info.n_polling_cf!=0x00000000) && (Poll485_Info.n_polling_cf!=0xFFFFFFFF))	rev = ((fun4_prt)Poll485_Info.n_polling_cf)(Poll485_Info.n_list_read_addr);
						
						if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
						{
							Poll485_Info.POLL_Start = Change_Dev;
						}else {				//数据发送成功，等待回复
							BLV_POLL_Wait = SysTick_1ms;
							Poll485_Info.POLL_Start = Wait_Reply;
						}
					}
				}else {
					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_POLL_dev Check Fail:%08X",Poll485_Info.Last_list_addr);
					Poll485_Info.POLL_Start = Change_Dev;
				}
			}else {
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_POLL_dev Type Fail:%08X , %d",Poll485_Info.Last_list_addr,SRAM_Read_Byte(Poll485_Info.Last_list_addr + Dev_Type));
				Poll485_Info.POLL_Start = Change_Dev;
			}
			
			
			break;
		case Baud_Wait:
			if(SysTick_1ms - Poll485_Info.change_tick > BLV_POLL485_ChangeBaudWaitTime)
			{
				/*读取设备信息*/
				
				Device_Public_Information_G dev_info;
				SRAM_Read_Buff((uint8_t *)&dev_info,sizeof(Device_Public_Information_G),Poll485_Info.n_list_read_addr);
				
				Poll485_Info.n_dev_type = dev_info.type; //SRAM_Read_Byte(Poll485_Info.n_list_read_addr + Dev_Type);
				Poll485_Info.n_dev_addr = dev_info.addr; // SRAM_Read_Byte(Poll485_Info.n_list_read_addr + Dev_Addr);
				Poll485_Info.n_dev_datalen = dev_info.data_len; //SRAM_Read_Word(Poll485_Info.n_list_read_addr + Dev_DataLen);
				Poll485_Info.n_polling_cf = dev_info.polling_cf; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Polling_CF);
				Poll485_Info.n_processing_cf = dev_info.processing_cf; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Processing_CF);
				Poll485_Info.n_dev_waittime = dev_info.wait_time; //SRAM_Read_Word(Poll485_Info.n_list_read_addr + Dev_WaitTime);
				
				Poll485_Info.n_retry_num = dev_info.retry_num; //SRAM_Read_DW(Poll485_Info.n_list_read_addr + Dev_Retrynum);
										
				Poll485_Info.Retry_Flag = 0x01;
				
				/*2021 09 17 ：修改增加发送函数返回值判断，0xF0表示数据没有发送数据，跳过该设备*/
				
				if((Poll485_Info.n_polling_cf!=0x00000000) && (Poll485_Info.n_polling_cf!=0xFFFFFFFF))	rev = ((fun4_prt)Poll485_Info.n_polling_cf)(Poll485_Info.n_list_read_addr);
				
				if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
				{
					Poll485_Info.POLL_Start = Change_Dev;
				}else {				//数据发送成功，等待回复
					BLV_POLL_Wait = SysTick_1ms;
					Poll485_Info.POLL_Start = Wait_Reply;
				}
			}
			break;
		case Change_Dev:
			Poll485_Info.Last_list_addr += SRAM_Device_List_Size;		//下一个设备
			if(Poll485_Info.Last_list_addr >= SRAM_Read_DW(SRAM_POLL_Device_List_Addr))	Poll485_Info.Last_list_addr = SRAM_Read_DW(SRAM_BUS_Device_List_Addr);
			
			Poll485_Info.POLL_Start = B_Polling;
			break;
		case B_Retry:
			if((Poll485_Info.Retry_Flag == 0x01) && (Poll485_Info.n_retry_num != 0x00))			//重发标志未清零，表示没发送成功
			{
				//Convert_To_Fun_Prt(Poll485_Info.n_polling_cf,Poll485_Info.n_list_read_addr);		//继续调用设备发送函数
				
				/*2021 09 17 ：修改增加发送函数返回值判断，0xF0表示数据没有发送数据，跳过该设备*/
				if((Poll485_Info.n_polling_cf!=0x00000000) && (Poll485_Info.n_polling_cf!=0xFFFFFFFF))	rev = ((fun4_prt)Poll485_Info.n_polling_cf)(Poll485_Info.n_list_read_addr);
				
				if(rev == RS485OCCUPYNOTIME)		//切换到下一个设备
				{
					Poll485_Info.POLL_Start = Change_Dev;
				}else {				//数据发送成功，等待回复
					BLV_POLL_Wait = SysTick_1ms;		//重新计时
					Poll485_Info.n_retry_num--;
					Poll485_Info.POLL_Start = Wait_Reply;
				}
			}else if((Poll485_Info.Retry_Flag == 0x01) && (Poll485_Info.n_retry_num == 0x00))
			{
				Poll485_Info.POLL_Start = Change_Dev;		//发送失败，切换下个设备
			}else {
				Poll485_Info.POLL_Start = Change_Dev;
			}
			break;
		case Wait_Reply:
			/*接收处理*/
			if(g_Uart[USART_1].RX_Buffer_WriteAddr != g_Uart[USART_1].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_1].RX_Buffer_ReadAddr);

				if((Poll485_Info.n_processing_cf!=0x00000000) && (Poll485_Info.n_processing_cf!=0xFFFFFFFF))	{
					Poll485_Info.Retry_Flag = ((fun2_prt)Poll485_Info.n_processing_cf)(Poll485_Info.n_list_read_addr,g_Uart[USART_1].RX_Buffer_ReadAddr + 2,data_len);
				}
				
				if(Poll485_Info.Retry_Flag == 0x00)	{
					//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Reply SUCC\r\n");
					Poll485_Info.send_wait = SysTick_1ms;
					Poll485_Info.POLL_Start = B_Wait;		//回复成功，等待总线空闲，便切换为下一个设备
				}
				
				if(Poll485_Info.port_mode == Port_Monitoring_mode)	//如果当前处于监控模式下,将数据发送至PC端
				{
					Udp_Internal_SeriaNet_Uploading(Polling_Port,Poll485_Info.baud,g_Uart[USART_1].RX_Buffer_ReadAddr);					//上报
				}
				
				g_Uart[USART_1].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_1].RX_Buffer_ReadAddr > SRAM_UART1_RecvBuffer_End_Addr)	{
					g_Uart[USART_1].RX_Buffer_ReadAddr = SRAM_UART1_RecvBuffer_Start_Addr;
				}
			}
			/*接收超时 - 进入重发*/
			if(SysTick_1ms - BLV_POLL_Wait > Poll485_Info.n_dev_waittime)	Poll485_Info.POLL_Start = B_Retry;
			break;
		case B_Wait:	//发送成功等待时间
			if(SysTick_1ms - BLV_POLL_Wait > Poll485_Info.n_dev_waittime)	Poll485_Info.POLL_Start = Change_Dev;
			break;

		default:
			Poll485_Info.POLL_Start = Change_Dev;
			break;
	}
	
	/*当前不是处于正常模式下，超时回归正常模式*/
	if(Poll485_Info.port_mode != Port_Normal_Mode)
	{
		if(SysTick_1s - Poll485_Info.mode_tick > Poll485_Info.mode_outtime)
		{
			Poll485_Info.mode_tick = SysTick_1s;
			Poll485_Info.port_mode = Port_Normal_Mode;		//正常模式
		}
	}
#endif  //USE_CORE_TYPE == CORE_TYPE_C1F		
	
}

/*******************************************主动端口********************************************************************************/
/*
主动设备
在没有接收到数据的情况下，会一直遍历设备
当遍历到设备需要发送数据时，便开始发送数据
当前设备发送数据时接收到其他设备数据或是乱码，先处理，处理完后再继续发送；

*/


uint8_t BLV_Active_Dev_Scan(uint32_t dev_addr)
{
	//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Active Device addr:%02X ----\n",SRAM_Read_Byte(dev_addr + Dev_Addr));
	uint8_t temp_buff[12] = {0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB};
	MCU485_SendString(Active_Port,temp_buff,10);
	return 0;
}

uint8_t BLV_Active_Dev_Processing(uint32_t dev_addr,uint32_t data_addr,uint16_t data_len)
{
	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Device addr:%02X ---",SRAM_Read_Byte(dev_addr + Dev_Addr));
	
	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,RX_Buffer:", data_len);
	for(uint16_t i = 0;i < data_len;i++)
	{
		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(data_addr + i));
	}
	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
	
	return 1;
}

/*******************************************************************************
* Function Name  : BLV_ActivePort_Task 
* Description    : ActivePort 主动端口任务
*******************************************************************************/
void BLV_ActivePort_Task(void)
{
	static uint32_t BLV_Act_Wait = 0;
	uint16_t data_len_1 = 0;
	
	switch(Act485_Info.Act_Start)
	{
		case B_IDLE:		//空闲状态
			/*接收处理*/
			if(g_Uart[USART_2].RX_Buffer_WriteAddr != g_Uart[USART_2].RX_Buffer_ReadAddr)
			{
				data_len_1 = SRAM_Read_Word(g_Uart[USART_2].RX_Buffer_ReadAddr);

				if((Act485_Info.n_processing_cf!=0x00000000) && (Act485_Info.n_processing_cf!=0xFFFFFFFF))	{
					Act485_Info.Retry_Flag = ((fun2_prt)Act485_Info.n_processing_cf)(Act485_Info.Last_list_addr,g_Uart[USART_2].RX_Buffer_ReadAddr + 2,data_len_1);
				}else {
					Act485_Info.Retry_Flag = 0x01;		//当前设备函数无处理函数，切换下一个设备		2026-01-09
				}
				
				if(Act485_Info.Retry_Flag == 0x00)	{
				
					if(Act485_Info.Last_list_addr == Act485_Info.n_list_read_addr) 
					{
						Dbg_Println(DBG_BIT_SYS_STATUS_bit,"A Reply SUCC");
						Act485_Info.Send_Flag = 0x00;
						Act485_Info.Act_Start = B_Send;		//回复成功，等待总线空闲，便切换为下一个设备
					}else{
						//当前数据不是回复数据 ， 切换回发送数据设备信息
						Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Not Dev Data");
						Act485_Info.Act_Start = Read_Dev;
					}
					
					Act485_Info.process_num = 0;
					
					if(Act485_Info.port_mode == Port_Monitoring_mode)	//如果当前处于监控模式下,将数据发送至PC端
					{
						Udp_Internal_SeriaNet_Uploading(Active_Port,Act485_Info.baud,g_Uart[USART_2].RX_Buffer_ReadAddr);					//上报
					}
					
					/*本包设备处理完毕，接收读取缓冲区地址偏移*/
					g_Uart[USART_2].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
					if(g_Uart[USART_2].RX_Buffer_ReadAddr > SRAM_UART2_RecvBuffer_End_Addr)	{
						g_Uart[USART_2].RX_Buffer_ReadAddr = SRAM_UART2_RecvBuffer_Start_Addr;
					}
				}else {
					Act485_Info.process_num ++;
					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Data parsing failed :%d" , Act485_Info.process_num);
					Act485_Info.Act_Start = Change_Dev;//不是当前设备的数据，换下一个处理
				}
				
				if(Act485_Info.process_num >= Act485_Info.device_num)
				{
					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"wipe cache partition:%d",Act485_Info.device_num);
					Act485_Info.Act_Start = B_IDLE;
					Act485_Info.process_num = 0;	//清楚处理计数
					Act485_Info.Retry_Flag = 0;		//清楚回复标志
					
					
					if(Act485_Info.port_mode == Port_Monitoring_mode)	//如果当前处于监控模式下,将数据发送至PC端
					{
						Udp_Internal_SeriaNet_Uploading(Active_Port,Act485_Info.baud,g_Uart[USART_2].RX_Buffer_ReadAddr);					//上报
					}
					g_Uart[USART_2].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
					if(g_Uart[USART_2].RX_Buffer_ReadAddr > SRAM_UART2_RecvBuffer_End_Addr)	{
						g_Uart[USART_2].RX_Buffer_ReadAddr = SRAM_UART2_RecvBuffer_Start_Addr;
					}
				}
			}else{
				if(SysTick_1ms - BLV_Act_Wait > Act485_Info.send_wait)
				{
					
					if((Act485_Info.Send_Flag == RS485OCCUPYTIME) && (Act485_Info.Last_list_addr == Act485_Info.n_list_read_addr))	
					{
						Act485_Info.Send_Flag = RS485OCCUPYNOTIME;		//本包数据发送完成
						Act485_Info.Act_Start = B_Send;		//继续回归发送状态，发送函数内部会判断是否还需要发送
					}else {
						/*
							1、当前没数据发送 ， 切换设备
							2、当前是否
						*/
						Act485_Info.Act_Start = Change_Dev;
						
					}
				}
			}
			
			break;
		case B_Send:		//数据发送状态
			
			//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_ActivePort_Task - B_Send:%08X\r\n" , Act485_Info.n_polling_cf);
		
			Act485_Info.n_list_read_addr = Act485_Info.Last_list_addr;

			/*调用设备发送函数*/

			if((Act485_Info.n_polling_cf!=0x00000000) && (Act485_Info.n_polling_cf!=0xFFFFFFFF))	{
				Act485_Info.Send_Flag = ((fun4_prt)Act485_Info.n_polling_cf)(Act485_Info.n_list_read_addr);
				
			}
			
			Act485_Info.Retry_Flag = 0x00;
			BLV_Act_Wait = SysTick_1ms;
			
			if(Act485_Info.Send_Flag == RS485OCCUPYTIME)	
			{
				Act485_Info.send_wait = Act485_Info.n_dev_waittime;
//				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_ActivePort_Task - Send Data\r\n");
			} else{
//				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_ActivePort_Task - Not Data\r\n");
				Act485_Info.send_wait = 5;		//切换设备时间
			}
			
			Act485_Info.Act_Start = B_IDLE;

			break;
		case Change_Dev:	//切换设备
			if(Act485_Info.list_read_addr != 0x00)
			{
				Act485_Info.Last_list_addr = Act485_Info.list_read_addr;
				Act485_Info.list_read_addr = 0x00;
//				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_ActivePort_Task - list_read_addr:%08X\r\n",Act485_Info.Last_list_addr);
			}else{
				Act485_Info.Last_list_addr += SRAM_Device_List_Size;
			}
			
			if((Act485_Info.Last_list_addr >= SRAM_Read_DW(SRAM_ACTIVE_Device_List_Addr)) || (Act485_Info.Last_list_addr >= SRAM_Device_List_End_Addr))	Act485_Info.Last_list_addr = SRAM_Read_DW(SRAM_POLL_Device_List_Addr);
			
			//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_ActivePort_Task - Change_Dev : %08X\r\n",Act485_Info.Last_list_addr);
		
			/*校验数据 并读取设备数据*/
			if(Device_Data_Check(Act485_Info.Last_list_addr) == 0)	
			{
				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_ActivePort_Task - Read_Dev SUCC\r\n");
				
				/*读取设备信息*/
				Device_Public_Information_G dev_info;
				SRAM_Read_Buff((uint8_t *)&dev_info,sizeof(Device_Public_Information_G),Act485_Info.Last_list_addr);
				
				Act485_Info.n_polling_cf = dev_info.polling_cf; //SRAM_Read_DW(Act485_Info.Last_list_addr + Dev_Polling_CF);
				Act485_Info.n_processing_cf = dev_info.processing_cf; //SRAM_Read_DW(Act485_Info.Last_list_addr + Dev_Processing_CF);
				Act485_Info.n_dev_waittime = dev_info.wait_time; //SRAM_Read_Word(Act485_Info.Last_list_addr + Dev_WaitTime);
				Act485_Info.n_retry_num = dev_info.retry_num; //SRAM_Read_DW(Act485_Info.Last_list_addr + Dev_Retrynum);
				
				if(Act485_Info.Retry_Flag == 0x00)	Act485_Info.Act_Start = B_Send;
				else	Act485_Info.Act_Start = B_IDLE;
			}else{
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%s - Read_Dev Fail: %08X",__func__,Act485_Info.Last_list_addr);
//				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Read_Dev Data:");
//				/*将数据发出来*/
//				for(uint16_t k = 0;k<SRAM_Device_List_Size;k++)
//				{
//					Dbg_NoTick_Print(DBG_BIT_SYS_STATUS_bit,"%02X ",SRAM_Read_Byte(Act485_Info.Last_list_addr + k));
//				}
//				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
			}				
			break;
		case Read_Dev:
			//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_ActivePort_Task - Read_Dev\r\n");
			if(Device_Data_Check(Act485_Info.n_list_read_addr) == 0)	
			{
				/*读取设备信息*/
				Device_Public_Information_G dev_info;
				SRAM_Read_Buff((uint8_t *)&dev_info,sizeof(Device_Public_Information_G),Act485_Info.n_list_read_addr);
								
				Act485_Info.n_polling_cf = dev_info.polling_cf; //SRAM_Read_DW(Act485_Info.Last_list_addr + Dev_Polling_CF);
				Act485_Info.n_processing_cf = dev_info.processing_cf; //SRAM_Read_DW(Act485_Info.Last_list_addr + Dev_Processing_CF);
				Act485_Info.n_dev_waittime = dev_info.wait_time; //SRAM_Read_Word(Act485_Info.Last_list_addr + Dev_WaitTime);
				Act485_Info.n_retry_num = dev_info.retry_num; //SRAM_Read_DW(Act485_Info.Last_list_addr + Dev_Retrynum);
				
				Act485_Info.Last_list_addr = Act485_Info.n_list_read_addr;	//下一个设备指针归位
				Act485_Info.Act_Start = B_IDLE;	//继续等待
			}else {
				Act485_Info.Act_Start = Change_Dev;
			}
			break;
		default:
			Act485_Info.Act_Start = B_IDLE;
			break;
	
	}
	
	/*当前不是处于正常模式下，超时回归正常模式*/
	if(Act485_Info.port_mode != Port_Normal_Mode)
	{
		if(SysTick_1s - Act485_Info.mode_tick > Act485_Info.mode_outtime)
		{
			Act485_Info.mode_tick = SysTick_1s;
			Act485_Info.port_mode = Port_Normal_Mode;		//正常模式
		}
	}
}

void BLV_Active_Set_List_Addr(uint32_t addr)
{
	Act485_Info.list_read_addr = addr;
	Act485_Info.Act_Start = Change_Dev;
}

/*查找设备*/
uint32_t Find_Device_List_Information(uint8_t dev_type,uint8_t addr)
{
	uint32_t read_addr = SRAM_Device_List_Start_Addr;
	uint32_t end_addr = SRAM_Read_DW(SRAM_NORMAL_Device_List_Addr);
    //Dbg_Println(DBG_OPT_DEVICE_STATUS,"Find Device:%08x",end_addr);
	if((end_addr < SRAM_Device_List_Start_Addr) || (end_addr > SRAM_Device_List_End_Addr))	end_addr = SRAM_Device_List_End_Addr;
	
	for(uint32_t i=SRAM_Device_List_Start_Addr;i<end_addr;)
	{
		if((SRAM_Read_Byte(read_addr + Dev_Type) == dev_type) && (SRAM_Read_Byte(read_addr + Dev_Addr) == addr))
		{
			if(Device_Data_Check(read_addr) == 0)
			{
				return read_addr;
			}
		}
		read_addr += SRAM_Device_List_Size;
		i+= SRAM_Device_List_Size;
		if(read_addr >= end_addr)	
		{
			return 0x00;
		}
	}
	
	return 0x00;
}

/*查询设备总数*/
uint8_t Find_The_Number_Of_Device_In_The_List(void)
{
	uint32_t read_addr = SRAM_Device_List_Start_Addr;
	uint32_t end_addr = SRAM_Read_DW(SRAM_ACTIVE_Device_List_Addr);		//设备链表结束地址
	uint8_t temp_num = 0;
	uint32_t temp_len = 0;
	
	temp_len = end_addr - read_addr;
	temp_num = (temp_len / SRAM_Device_List_Size) & 0xFF;
	
	return temp_num;
}

/*获取设备全部在线状态*/
uint8_t Gets_the_state_of_all_devices(uint8_t *data_buff,uint8_t num)
{
	uint8_t dev_type = 0,dev_addr = 0,dev_online = 0;
	uint32_t read_addr = SRAM_Device_List_Start_Addr;
	uint32_t end_addr = SRAM_Read_DW(SRAM_ACTIVE_Device_List_Addr);
	if((end_addr < SRAM_Device_List_Start_Addr) || (end_addr > SRAM_Device_List_End_Addr))	end_addr = SRAM_Device_List_End_Addr;
	
	for(uint8_t i=0;i<num;i++)
	{
		if(Device_Data_Check(read_addr) == 0)
		{
			dev_type = SRAM_Read_Byte(read_addr + Dev_Type);
			dev_addr = SRAM_Read_Byte(read_addr + Dev_Addr);
			/*返回在线状态*/
			switch(dev_type)
			{
				case DEV_C5IO_Type:
					dev_online = Get_BUS_C5IO_Online_Status(read_addr);
					break;
				case DEV_C5MUSIC_Type:
					dev_online = Get_BUS_C5MUSIC_Online_Status(read_addr);
					break;
				case DEV_RS485_TEMP:
					dev_online = Get_BLV485_TEMP_Online_Status(read_addr);//Get_BLV485_TEMP_Online_Status(read_addr);
					break;
				case DEV_RS485_CARD:
					dev_online = Get_BLV485_CARD_Online_Status(read_addr);
					break;
				case DEV_SWI_TYPE:
					dev_online = Get_Switch_Online_Status(read_addr);
					break;
				case Dev_Rs458_RotaryCtrl:
					dev_online = Get_Rotary_Switch_Online_Status(read_addr);
					break;
			}
			data_buff[i*3+0] = dev_type;
			data_buff[i*3+1] = dev_addr;
			data_buff[i*3+2] = dev_online;
		}else {
			return 0xF0;	//读取失败	
		}
		
		read_addr += SRAM_Device_List_Size;
		if(read_addr >= end_addr)	
		{
			return 0x00;
		}
	}
	
	return 0x00;
}

/*2022.12.26 林开锐修改开始*/
/*******************************************************************************
  * @brief      写设备故障状态到SRAM
  * @param  
  *		device_type		设备类型
  *		device_addr		设备地址
  *		fault_type		故障类型
  *		fault_state		故障状态
  * @retval     None
  * @attention	一种故障  1个字节485设备类型 1个字节设备地址 2个字节设备回路（低字节在前） 1个字节故障类型 1个字节故障内容 组成
*******************************************************************************/
void Write_Device_Fault_State(uint8_t device_type,uint8_t device_addr,uint8_t fault_type,uint8_t fault_state)
{
	uint8_t data[6];	//用于保存设备故障信
	uint32_t write_addr = 0x00,read_addr = 0x00;	//设备故障状态写入地址
	uint8_t len = 0;
	
	//读取设备故障 缓冲区读写地址
	write_addr = SRAM_Read_DW(SRAM_DEVICE_ONLINE_STATE_WRITE_ADDR);
	read_addr = SRAM_Read_DW(SRAM_DEVICE_ONLINE_STATE_READ_ADDR);
	
	//如果读取地址任何一个出错，读写地址直接复位
	if( (write_addr < SRAM_DEVICE_ONLINE_STATE_START_ADDR) || (write_addr > SRAM_DEVICE_ONLINE_STATE_END_ADDR) \
		|| (read_addr < SRAM_DEVICE_ONLINE_STATE_START_ADDR) || (read_addr > SRAM_DEVICE_ONLINE_STATE_END_ADDR) )	
	{
		write_addr = SRAM_DEVICE_ONLINE_STATE_START_ADDR;
		read_addr = SRAM_DEVICE_ONLINE_STATE_START_ADDR;
		SRAM_Write_DW(write_addr,SRAM_DEVICE_ONLINE_STATE_WRITE_ADDR);
		SRAM_Write_DW(read_addr,SRAM_DEVICE_ONLINE_STATE_READ_ADDR);
		SRAM_Write_DW(read_addr,SRAM_DEVICE_ONLINE_STATE_TEMP_ADDR);
	}
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"%s SRAM addr:%08X",__func__, write_addr);
	
	memset(data,0x00,6);	//设备故障信息清0
	
	data[0] = device_type;	//设备类型
	data[1] = device_addr;	//设备地址
	data[4] = fault_type;	//故障类型
	data[5] = fault_state;	//故障状态
	
	if( (write_addr + 0x06) > SRAM_DEVICE_ONLINE_STATE_END_ADDR )
	{
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"%s overstep_1 %08X!!!",__func__,write_addr);
		len = SRAM_DEVICE_ONLINE_STATE_END_ADDR - write_addr;
		SRAM_DMA_Write_Buff(data,len,write_addr);
		write_addr = SRAM_DEVICE_ONLINE_STATE_START_ADDR;
		SRAM_DMA_Write_Buff(&data[len],(6-len),write_addr);
		write_addr += (6-len);
	}else {
		SRAM_DMA_Write_Buff(data, 6, write_addr);	//写入设备故障信息
		write_addr += 0x06;	//地址偏移
	}
	
	if(write_addr > SRAM_DEVICE_ONLINE_STATE_END_ADDR)
	{
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"%s overstep:%08X",__func__,write_addr);
		write_addr = SRAM_DEVICE_ONLINE_STATE_START_ADDR;
	}
	
	SRAM_Write_DW(write_addr,SRAM_DEVICE_ONLINE_STATE_WRITE_ADDR);	//更新设备故障状态写入地址
	//Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"Write Fault Data:%02X %02X %02X %02X %02X %02X\r\n",data[0],data[1],data[2],data[3],data[4],data[5]);
}
/*2022.12.26 林开锐修改结束*/


/*******************************************************************************
  * @brief      写设备回路故障状态到SRAM
  * @param  
  *		device_type		设备类型
  *		device_addr		设备地址
  *		fault_type		故障类型
  *		fault_state		故障状态
  * @retval     None
  * @attention	一种故障  1个字节485设备类型 1个字节设备地址 2个字节设备回路（低字节在前） 1个字节故障类型 1个字节故障内容 组成
*******************************************************************************/
void Write_Device_Loop_Fault_State(uint8_t device_type,uint8_t device_addr,uint8_t fault_type,uint8_t fault_state,uint16_t loop)
{
	uint8_t data[6];	//用于保存设备故障信
	uint32_t write_addr = 0x00,read_addr = 0x00;	//设备故障状态写入地址
	uint8_t len = 0;
	
	//读取设备故障 缓冲区读写地址
	write_addr = SRAM_Read_DW(SRAM_DEVICE_ONLINE_STATE_WRITE_ADDR);
	read_addr = SRAM_Read_DW(SRAM_DEVICE_ONLINE_STATE_READ_ADDR);
	
	//如果读取地址任何一个出错，读写地址直接复位
	if( (write_addr < SRAM_DEVICE_ONLINE_STATE_START_ADDR) || (write_addr > SRAM_DEVICE_ONLINE_STATE_END_ADDR) \
		|| (read_addr < SRAM_DEVICE_ONLINE_STATE_START_ADDR) || (read_addr > SRAM_DEVICE_ONLINE_STATE_END_ADDR) )	
	{
		write_addr = SRAM_DEVICE_ONLINE_STATE_START_ADDR;
		read_addr = SRAM_DEVICE_ONLINE_STATE_START_ADDR;
		SRAM_Write_DW(write_addr,SRAM_DEVICE_ONLINE_STATE_WRITE_ADDR);
		SRAM_Write_DW(read_addr,SRAM_DEVICE_ONLINE_STATE_READ_ADDR);
		SRAM_Write_DW(read_addr,SRAM_DEVICE_ONLINE_STATE_TEMP_ADDR);
	}
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"%s SRAM addr:%08X",__func__, write_addr);
	
	memset(data,0x00,6);	//设备故障信息清0
	
	data[0] = device_type;	//设备类型
	data[1] = device_addr;	//设备地址
	data[2] = (loop & 0xFF);
	data[3] = ((loop >> 8) & 0xFF);
	data[4] = fault_type;	//故障类型
	data[5] = fault_state;	//故障状态
	
	if( (write_addr + 0x06) > SRAM_DEVICE_ONLINE_STATE_END_ADDR )
	{
		Dbg_Println(DBG_BIT_NET_STATUS_bit,"%s overstep_1 %08X!!!",__func__,write_addr);
		len = SRAM_DEVICE_ONLINE_STATE_END_ADDR - write_addr;
		SRAM_DMA_Write_Buff(data,len,write_addr);
		write_addr = SRAM_DEVICE_ONLINE_STATE_START_ADDR;
		SRAM_DMA_Write_Buff(&data[len],(6-len),write_addr);
		write_addr += (6-len);
	}else {
		SRAM_DMA_Write_Buff(data, 6, write_addr);	//写入设备故障信息
		write_addr += 0x06;	//地址偏移
	}
	
	if(write_addr > SRAM_DEVICE_ONLINE_STATE_END_ADDR)
	{
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"%s overstep:%08X",__func__,write_addr);
		write_addr = SRAM_DEVICE_ONLINE_STATE_START_ADDR;
	}
	
	SRAM_Write_DW(write_addr,SRAM_DEVICE_ONLINE_STATE_WRITE_ADDR);	//更新设备故障状态写入地址
}


/*******************************************************************************
* Function Name  : Act485Port_Passthrough_Task 
* Description    : ActivePort 主动端口透传任务
*******************************************************************************/
void Act485Port_Passthrough_Task(void)
{
	uint16_t data_len = 0;
	switch(Act485_Info.pass_state)
	{
		case B_IDLE:	//空闲状态 - 判断是否有数据上报或下发
			/*接收处理*/
			if(g_Uart[USART_2].RX_Buffer_WriteAddr != g_Uart[USART_2].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_2].RX_Buffer_ReadAddr);

				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d" , data_len);
				Udp_Internal_SeriaNet_Uploading(Active_Port,Act485_Info.baud,g_Uart[USART_2].RX_Buffer_ReadAddr);					//上报
				
				g_Uart[USART_2].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_2].RX_Buffer_ReadAddr > SRAM_UART2_RecvBuffer_End_Addr)	g_Uart[USART_2].RX_Buffer_ReadAddr = SRAM_UART2_RecvBuffer_Start_Addr;
				
			}
			
			if(g_Uart[USART_2].TX_Buffer_WriteAddr != g_Uart[USART_2].TX_Buffer_ReadAddr)
			{
				/*读取发送缓冲区 - 下发*/
				data_len = SRAM_Read_Word(g_Uart[USART_2].TX_Buffer_ReadAddr);
				Act485_Info.pass_outtime = SRAM_Read_Byte(g_Uart[USART_2].TX_Buffer_ReadAddr + 2);		//单位：S
				if(data_len > Passthrough_DataLen_Max)  data_len = Passthrough_DataLen_Max;				//透传数据长度
				Act485_Info.pass_tick = SysTick_1s;
				
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d" , data_len);
				MCU485_SendSRAMData(Active_Port,g_Uart[USART_2].TX_Buffer_ReadAddr + 3,data_len);		//下发
				
				Act485_Info.pass_state = Wait_Reply;
				
				g_Uart[USART_2].TX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_2].TX_Buffer_ReadAddr > SRAM_UART2_SendBuffer_End_Addr)	g_Uart[USART_2].TX_Buffer_ReadAddr = SRAM_UART2_SendBuffer_Start_Addr;
			}
			
			if(SysTick_1s - Act485_Info.mode_tick > Act485_Info.mode_outtime)
			{
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Act485_Info Port_Normal_Mode mode_outtime");
				Act485_Info.mode_tick = SysTick_1s;
				Act485_Info.port_mode = Port_Normal_Mode;		//正常模式
				
				if(Act485_Info.baud != Active_Baud)		//设置波特率
				{
					Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Act485_Info.baud:%d",Act485_Info.baud);
					Act485_Info.baud = Active_Baud;
					Act485_Info.BaudRateCfg(Act485_Info.baud);
				}
			}
			break;
		case Wait_Reply:	//等待回复 - 回复超时的话 - 上报给主机错误
			/*接收处理*/
			if(g_Uart[USART_2].RX_Buffer_WriteAddr != g_Uart[USART_2].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_2].RX_Buffer_ReadAddr);
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d ,RX_Buffer:" , data_len);

				
				Udp_Internal_SeriaNet_Uploading(Active_Port,Act485_Info.baud,g_Uart[USART_2].RX_Buffer_ReadAddr);
				
				Act485_Info.pass_state = B_IDLE; 		//进入空闲状态
				
				g_Uart[USART_2].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_2].RX_Buffer_ReadAddr > SRAM_UART2_RecvBuffer_End_Addr)	g_Uart[USART_2].RX_Buffer_ReadAddr = SRAM_UART2_RecvBuffer_Start_Addr;
				
			}
		
			if(SysTick_1s - Act485_Info.pass_tick > Act485_Info.pass_outtime)
			{
				//回复超时
				Udp_Internal_SeriaNet_Response_Timeout();
				
				Act485_Info.pass_state = B_IDLE; 		//进入空闲状态
			}
		
			break;
		default:
			Act485_Info.pass_state = B_IDLE; 		//进入空闲状态
			break;
	
	}
	
}

/*******************************************************************************
* Function Name  : Poll485Port_Passthrough_Task 
* Description    : PollPort 轮询端口透传任务
*******************************************************************************/
void Poll485Port_Passthrough_Task(void)
{
	uint16_t data_len = 0;
#if (USE_CORE_TYPE == 1)    //使用C1F核心板
	switch(Poll485_Info.pass_state)
	{
		case B_IDLE:	//空闲状态 - 判断是否有数据上报或下发
			/*接收处理*/
			if(g_Uart[USART_0].RX_Buffer_WriteAddr != g_Uart[USART_0].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_0].RX_Buffer_ReadAddr);

				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d ,RX_Buffer:" , data_len);
				Udp_Internal_SeriaNet_Uploading(Polling_Port,Poll485_Info.baud,g_Uart[USART_0].RX_Buffer_ReadAddr);					//上报
				
				g_Uart[USART_0].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_0].RX_Buffer_ReadAddr > SRAM_UART0_RecvBuffer_End_Addr)	g_Uart[USART_0].RX_Buffer_ReadAddr = SRAM_UART0_RecvBuffer_Start_Addr;
				
			}
			
			if(g_Uart[USART_0].TX_Buffer_WriteAddr != g_Uart[USART_0].TX_Buffer_ReadAddr)
			{
				/*读取发送缓冲区 - 下发*/
				data_len = SRAM_Read_Word(g_Uart[USART_0].TX_Buffer_ReadAddr);
				Poll485_Info.pass_outtime = SRAM_Read_Byte(g_Uart[USART_0].TX_Buffer_ReadAddr + 2);		//单位：S
				if(data_len > Passthrough_DataLen_Max)  data_len = Passthrough_DataLen_Max;				//透传数据长度
				Poll485_Info.pass_tick = SysTick_1s;
				
				MCU485_SendSRAMData(Polling_Port,g_Uart[USART_0].TX_Buffer_ReadAddr + 3,data_len);		//下发
				
				Poll485_Info.pass_state = Wait_Reply;
				
				g_Uart[USART_0].TX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_0].TX_Buffer_ReadAddr > SRAM_UART0_SendBuffer_End_Addr)	g_Uart[USART_0].TX_Buffer_ReadAddr = SRAM_UART0_SendBuffer_Start_Addr;
			}
			
			if(SysTick_1s - Poll485_Info.mode_tick > Poll485_Info.mode_outtime)
			{
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Poll485_Info Port_Normal_Mode mode_outtime");
				Poll485_Info.mode_tick = SysTick_1s;
				Poll485_Info.port_mode = Port_Normal_Mode;		//正常模式
				
				if(Poll485_Info.baud != Polling_Baud)		//设置波特率
				{
					Poll485_Info.baud = Polling_Baud;
					Poll485_Info.BaudRateCfg(Poll485_Info.baud);
				}
				
			}
			break;
		case Wait_Reply:	//等待回复 - 回复超时的话 - 上报给主机错误
			/*接收处理*/
			if(g_Uart[USART_0].RX_Buffer_WriteAddr != g_Uart[USART_0].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_0].RX_Buffer_ReadAddr);
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d ,RX_Buffer:" , data_len);

				
				Udp_Internal_SeriaNet_Uploading(Polling_Port,Act485_Info.baud,g_Uart[USART_0].RX_Buffer_ReadAddr);
				
				Poll485_Info.pass_state = B_IDLE; 		//进入空闲状态
				
				g_Uart[USART_0].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_0].RX_Buffer_ReadAddr > SRAM_UART0_RecvBuffer_End_Addr)	g_Uart[USART_0].RX_Buffer_ReadAddr = SRAM_UART0_RecvBuffer_Start_Addr;
				
			}
			
			if(g_Uart[USART_0].TX_Buffer_WriteAddr != g_Uart[USART_0].TX_Buffer_ReadAddr)
			{
				/*读取发送缓冲区 - 下发*/
				data_len = SRAM_Read_Word(g_Uart[USART_0].TX_Buffer_ReadAddr);
				Poll485_Info.pass_outtime = SRAM_Read_Byte(g_Uart[USART_0].TX_Buffer_ReadAddr + 2);		//单位：S
				if(data_len > Passthrough_DataLen_Max)  data_len = Passthrough_DataLen_Max;				//透传数据长度
				Poll485_Info.pass_tick = SysTick_1s;
				
				MCU485_SendSRAMData(Polling_Port,g_Uart[USART_0].TX_Buffer_ReadAddr + 3,data_len);		//下发
				
				Poll485_Info.pass_state = Wait_Reply;
				
				g_Uart[USART_0].TX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_0].TX_Buffer_ReadAddr > SRAM_UART0_SendBuffer_End_Addr)	g_Uart[USART_0].TX_Buffer_ReadAddr = SRAM_UART0_SendBuffer_Start_Addr;
			}
		
			if(SysTick_1s - Poll485_Info.pass_tick > Poll485_Info.pass_outtime)
			{
				//回复超时
				Udp_Internal_SeriaNet_Response_Timeout();
				
				Poll485_Info.pass_state = B_IDLE; 		//进入空闲状态
			}
		
			break;
		default:
			Poll485_Info.pass_state = B_IDLE; 		//进入空闲状态
			break;
	
	}
#elif (USE_CORE_TYPE == 2)   //使用C1核心板
	switch(Poll485_Info.pass_state)
	{
		case B_IDLE:	//空闲状态 - 判断是否有数据上报或下发
			/*接收处理*/
			if(g_Uart[USART_1].RX_Buffer_WriteAddr != g_Uart[USART_1].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_1].RX_Buffer_ReadAddr);

				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d ,RX_Buffer:" , data_len);
				Udp_Internal_SeriaNet_Uploading(Polling_Port,Poll485_Info.baud,g_Uart[USART_1].RX_Buffer_ReadAddr);					//上报
				
				g_Uart[USART_1].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_1].RX_Buffer_ReadAddr > SRAM_UART1_RecvBuffer_End_Addr)	g_Uart[USART_1].RX_Buffer_ReadAddr = SRAM_UART1_RecvBuffer_Start_Addr;
				
			}
			
			if(g_Uart[USART_1].TX_Buffer_WriteAddr != g_Uart[USART_1].TX_Buffer_ReadAddr)
			{
				/*读取发送缓冲区 - 下发*/
				data_len = SRAM_Read_Word(g_Uart[USART_1].TX_Buffer_ReadAddr);
				Poll485_Info.pass_outtime = SRAM_Read_Byte(g_Uart[USART_1].TX_Buffer_ReadAddr + 2);		//单位：S
				if(data_len > Passthrough_DataLen_Max)  data_len = Passthrough_DataLen_Max;				//透传数据长度
				Poll485_Info.pass_tick = SysTick_1s;
				
				MCU485_SendSRAMData(Polling_Port,g_Uart[USART_1].TX_Buffer_ReadAddr + 3,data_len);		//下发
				
				Poll485_Info.pass_state = Wait_Reply;
				
				g_Uart[USART_1].TX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_1].TX_Buffer_ReadAddr > SRAM_UART1_SendBuffer_End_Addr)	g_Uart[USART_1].TX_Buffer_ReadAddr = SRAM_UART1_SendBuffer_Start_Addr;
			}
			
			if(SysTick_1s - Poll485_Info.mode_tick > Poll485_Info.mode_outtime)
			{
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Poll485_Info Port_Normal_Mode mode_outtime");
				Poll485_Info.mode_tick = SysTick_1s;
				Poll485_Info.port_mode = Port_Normal_Mode;		//正常模式
				
				if(Poll485_Info.baud != Polling_Baud)		//设置波特率
				{
					Poll485_Info.baud = Polling_Baud;
					Poll485_Info.BaudRateCfg(Poll485_Info.baud);
				}
				
			}
			break;
		case Wait_Reply:	//等待回复 - 回复超时的话 - 上报给主机错误
			/*接收处理*/
			if(g_Uart[USART_1].RX_Buffer_WriteAddr != g_Uart[USART_1].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_1].RX_Buffer_ReadAddr);
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d ,RX_Buffer:" , data_len);

				
				Udp_Internal_SeriaNet_Uploading(Polling_Port,Act485_Info.baud,g_Uart[USART_1].RX_Buffer_ReadAddr);
				
				Poll485_Info.pass_state = B_IDLE; 		//进入空闲状态
				
				g_Uart[USART_1].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_1].RX_Buffer_ReadAddr > SRAM_UART1_RecvBuffer_End_Addr)	g_Uart[USART_1].RX_Buffer_ReadAddr = SRAM_UART1_RecvBuffer_Start_Addr;
				
			}
			
			if(g_Uart[USART_1].TX_Buffer_WriteAddr != g_Uart[USART_1].TX_Buffer_ReadAddr)
			{
				/*读取发送缓冲区 - 下发*/
				data_len = SRAM_Read_Word(g_Uart[USART_1].TX_Buffer_ReadAddr);
				Poll485_Info.pass_outtime = SRAM_Read_Byte(g_Uart[USART_1].TX_Buffer_ReadAddr + 2);		//单位：S
				if(data_len > Passthrough_DataLen_Max)  data_len = Passthrough_DataLen_Max;				//透传数据长度
				Poll485_Info.pass_tick = SysTick_1s;
				
				MCU485_SendSRAMData(Polling_Port,g_Uart[USART_1].TX_Buffer_ReadAddr + 3,data_len);		//下发
				
				Poll485_Info.pass_state = Wait_Reply;
				
				g_Uart[USART_1].TX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_1].TX_Buffer_ReadAddr > SRAM_UART1_SendBuffer_End_Addr)	g_Uart[USART_1].TX_Buffer_ReadAddr = SRAM_UART1_SendBuffer_Start_Addr;
			}
		
			if(SysTick_1s - Poll485_Info.pass_tick > Poll485_Info.pass_outtime)
			{
				//回复超时
				Udp_Internal_SeriaNet_Response_Timeout();
				
				Poll485_Info.pass_state = B_IDLE; 		//进入空闲状态
			}
		
			break;
		default:
			Poll485_Info.pass_state = B_IDLE; 		//进入空闲状态
			break;
	
	}
#endif  //USE_CORE_TYPE == CORE_TYPE_C1F		
	
}

/*******************************************************************************
* Function Name  : BUS485Port_Passthrough_Task 
* Description    : BUSPort BUS端口透传任务
*******************************************************************************/
void BUS485Port_Passthrough_Task(void)
{
	uint16_t data_len = 0;
	switch(BUS485_Info.pass_state)
	{
		case B_IDLE:	//空闲状态 - 判断是否有数据上报或下发
			/*接收处理*/
			if(g_Uart[USART_3].RX_Buffer_WriteAddr != g_Uart[USART_3].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_3].RX_Buffer_ReadAddr);

				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d ,RX_Buffer:" , data_len);
				Udp_Internal_SeriaNet_Uploading(Bus_port,BUS485_Info.baud,g_Uart[USART_3].RX_Buffer_ReadAddr);					//上报
				
				g_Uart[USART_3].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_3].RX_Buffer_ReadAddr > SRAM_UART3_RecvBuffer_End_Addr)	g_Uart[USART_3].RX_Buffer_ReadAddr = SRAM_UART3_RecvBuffer_Start_Addr;
				
			}
			
			if(g_Uart[USART_3].TX_Buffer_WriteAddr != g_Uart[USART_3].TX_Buffer_ReadAddr)
			{
				/*读取发送缓冲区 - 下发*/
				data_len = SRAM_Read_Word(g_Uart[USART_3].TX_Buffer_ReadAddr);
				BUS485_Info.pass_outtime = SRAM_Read_Byte(g_Uart[USART_3].TX_Buffer_ReadAddr + 2);		//单位：S
				if(data_len > Passthrough_DataLen_Max)  data_len = Passthrough_DataLen_Max;				//透传数据长度
				BUS485_Info.pass_tick = SysTick_1s;
				
				MCU485_SendSRAMData(Bus_port,g_Uart[USART_3].TX_Buffer_ReadAddr + 3,data_len);		//下发
				
				BUS485_Info.pass_state = Wait_Reply;
				
				g_Uart[USART_3].TX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_3].TX_Buffer_ReadAddr > SRAM_UART3_SendBuffer_End_Addr)	g_Uart[USART_3].TX_Buffer_ReadAddr = SRAM_UART3_SendBuffer_Start_Addr;
			}
			
			if(SysTick_1s - BUS485_Info.mode_tick > BUS485_Info.mode_outtime)
			{
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS485_Info Port_Normal_Mode mode_outtime");
				BUS485_Info.mode_tick = SysTick_1s;
				BUS485_Info.port_mode = Port_Normal_Mode;		//正常模式
				BUS485_Info.baud = Bus_Baud;
			}
			break;
		case Wait_Reply:	//等待回复 - 回复超时的话 - 上报给主机错误
			/*接收处理*/
			if(g_Uart[USART_3].RX_Buffer_WriteAddr != g_Uart[USART_3].RX_Buffer_ReadAddr)
			{
				data_len = SRAM_Read_Word(g_Uart[USART_3].RX_Buffer_ReadAddr);
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"UART2 data_len :%d ,RX_Buffer:" , data_len);

				
				Udp_Internal_SeriaNet_Uploading(Bus_port,BUS485_Info.baud,g_Uart[USART_3].RX_Buffer_ReadAddr);
				
				BUS485_Info.pass_state = B_IDLE; 		//进入空闲状态
				
				g_Uart[USART_3].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
				if(g_Uart[USART_3].RX_Buffer_ReadAddr > SRAM_UART3_RecvBuffer_End_Addr)	g_Uart[USART_3].RX_Buffer_ReadAddr = SRAM_UART3_RecvBuffer_Start_Addr;
				
			}
		
			if(SysTick_1s - BUS485_Info.pass_tick > BUS485_Info.pass_outtime)
			{
				//回复超时
				Udp_Internal_SeriaNet_Response_Timeout();
				
				BUS485_Info.pass_state = B_IDLE; 		//进入空闲状态
			}
		
			break;
		default:
			BUS485_Info.pass_state = B_IDLE; 		//进入空闲状态
			break;
	
	}
	
}

/*******************************************************************************
* Function Name  : BLV_ActivePort_ModeTask 
* Description    : ActivePort Active端口模式
*******************************************************************************/
void BLV_ActivePort_ModeTask(void)
{
	switch(Act485_Info.port_mode)
	{
		case Port_Passthrough_mode:
			Act485Port_Passthrough_Task();		//透传模式
			break;
		case Port_Normal_Mode:
		case Port_Monitoring_mode:
			
			BLV_ActivePort_Task();				//正常模式 以及正常模式下监控模式
			
			break;
	}
}

/*******************************************************************************
* Function Name  : BLV_PollPort_ModeTask 
* Description    : PollPort Poll端口模式
*******************************************************************************/
void BLV_PollPort_ModeTask(void)
{
	uint16_t data_len = 0,rev = 0;
#if (USE_CORE_TYPE == 1)    //使用C1F核心板
	if((g_pc_test.test_flag == 0x03) || (g_pc_test.test_flag == 0x13)) 
	{
		if(g_Uart[USART_0].RX_Buffer_WriteAddr != g_Uart[USART_0].RX_Buffer_ReadAddr)
		{
			data_len = SRAM_Read_Word(g_Uart[USART_0].RX_Buffer_ReadAddr);
			
			rev = BLV_PC_TEST_TOUR_DATACheck(g_Uart[USART_0].RX_Buffer_ReadAddr+2,data_len);
			if(rev == 0x00)	{
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Poll Port 测试巡回数据\r\n");
				g_pc_test.tour_succ++;		//是巡回数据
			}else {
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"不是巡回数据!\r\n");
			}
			g_Uart[USART_0].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_0].RX_Buffer_ReadAddr > SRAM_UART0_RecvBuffer_End_Addr)	g_Uart[USART_0].RX_Buffer_ReadAddr = SRAM_UART0_RecvBuffer_Start_Addr;
			
		}
		return;			//测试中，暂停任务处理
	}
#elif (USE_CORE_TYPE == 2)   //使用C1核心板
	if((g_pc_test.test_flag == 0x03) || (g_pc_test.test_flag == 0x13)) 
	{
		if(g_Uart[USART_1].RX_Buffer_WriteAddr != g_Uart[USART_1].RX_Buffer_ReadAddr)
		{
			data_len = SRAM_Read_Word(g_Uart[USART_1].RX_Buffer_ReadAddr);
			
			rev = BLV_PC_TEST_TOUR_DATACheck(g_Uart[USART_1].RX_Buffer_ReadAddr+2,data_len);
			if(rev == 0x00)	{
				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Poll Port 是巡回数据");
				
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Poll Port 巡回数据");
				g_pc_test.tour_succ++;		
			}else {
				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"不是巡回数据!\r\n");
			}
			
			g_Uart[USART_1].RX_Buffer_ReadAddr += SRAM_Uart_Buffer_Size;
			if(g_Uart[USART_1].RX_Buffer_ReadAddr > SRAM_UART1_RecvBuffer_End_Addr)	g_Uart[USART_1].RX_Buffer_ReadAddr = SRAM_UART1_RecvBuffer_Start_Addr;
			
		}
		return;			//测试中，暂停任务处理
	}
#endif  //USE_CORE_TYPE == CORE_TYPE_C1F		
	
	
	switch(Poll485_Info.port_mode)
	{
		case Port_Passthrough_mode:
			Poll485Port_Passthrough_Task();		//透传模式
			break;
		case Port_Normal_Mode:
		case Port_Monitoring_mode:
			
			BLV_PollPort_Task();				//正常模式 以及正常模式下监控模式
			
			break;
	}
}

/*******************************************************************************
* Function Name  : BLV_BUS485Port_ModeTask 
* Description    : BUS端口模式
*******************************************************************************/
void BLV_BUS485Port_ModeTask(void)
{
	switch(BUS485_Info.port_mode)
	{
		case Port_Passthrough_mode:
			BUS485Port_Passthrough_Task();		//透传模式
			break;
		case Port_Normal_Mode:
		case Port_Monitoring_mode:
			
			BLV_BUS_Polling_Task2();				//正常模式 以及正常模式下监控模式
			
			break;
	}
}


/*******************************************************************************
* Function Name  : BLV_Communication_Record 
* Description    : BUS通讯记录
* Input          : 
	dev_record	:通讯记录结构体
	option		:记录选项
		0x01:记录通讯次数
		0x02:记录通讯结果
	state		:
		选项为记录通讯次数时，该参数无效
		选项为记录通讯结果时，该参数为0x00：通讯失败，0x01：通讯成功
*******************************************************************************/
void BLV_Communication_Record(BLV_COMM_RECORD_G *dev_record,uint8_t option,uint8_t state)
{
	switch(option)
	{
		case 0x01:
			if(dev_record->num >= BLV_COMM_RecordNum*8)	
			{
//				Dbg_Println(DBG_BIT_SYS_STATUS_bit,"通讯记录已满");
				dev_record->full_flag = 0x01;
				dev_record->num = 0;	
			}
			
			dev_record->num++;
			dev_record->continue_fail_num++;
			//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"通讯记录数:%d",dev_record->num);
						
			dev_record->record[(dev_record->num-1)/8] &= ~((0x01 << ((dev_record->num-1)%8)));
			//Dbg_Print_Buff(DBG_BIT_SYS_STATUS_bit,"通讯BUFF：",dev_record->record,BLV_COMM_RecordNum);
			break;
		case 0x02:
			if(state == 0x01)
			{
				//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"通讯成功数:%d",dev_record->num);
				dev_record->continue_fail_num = 0;
				dev_record->record[(dev_record->num-1)/8] |= (0x01 << ((dev_record->num-1)%8));
			}
			break;
	}
}

/*******************************************************************************
* Function Name  : BUS_Adjust_Communication_Baud_Rate 
* Description    : BUS通讯调整波特率 - 每次在范围内调整一定值
*******************************************************************************/
void BUS_Adjust_Communication_Baud_Rate(uint32_t baud_addr)
{	
	uint32_t baud_data = SRAM_Read_DW(baud_addr);
	baud_data += BLV_BUS_BAUD_ADJUST_SIZE;
	
	if(baud_data > BLV_BUS_BAUD_ADJUST_MAX)
	{
		baud_data = BLV_BUS_BAUD_ADJUST_MIN;
	}
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"调整Baud:%ld",baud_data);
	
	SRAM_Write_DW(baud_data,baud_addr);
}


void BUS_Adjust_Communication_Baud_Rate2(Device_Public_Information_G *dev_info)
{	
	uint32_t baud_data = dev_info->baud;
	baud_data += BLV_BUS_BAUD_ADJUST_SIZE;
	
	if(baud_data > BLV_BUS_BAUD_ADJUST_MAX)
	{
		baud_data = BLV_BUS_BAUD_ADJUST_MIN;
	}
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"调整Baud:%ld",baud_data);
	
	dev_info->baud = baud_data;
}


/*******************************************************************************
* Function Name  : BLV_Communication_Record_Conversion 
* Description    : 通讯记录转换函数 -- 判断是否要调整波特率并调整波特率
* Return 		 ：
	0x00：未切换波特率
	0x01：当前通讯记录未达计算数
	0x02：连续失败次数已达上限，切换通讯波特率
	0x03：通讯失败百分比已达上限，切换通讯波特率
*******************************************************************************/
uint8_t BLV_Communication_Baud_Adjust(BLV_COMM_RECORD_G *dev_record,uint32_t baud_addr)
{
	uint8_t precent = 0,temp = 0;
	uint16_t fail_num = 0,sum = 0;
	
	if(dev_record->continue_fail_num >= BLV_CONTINUE_FAIL_MAX)		//连续失败次数已达上线
	{
		//通讯记录清零
		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Continuous Failure:%d NUM:%d",dev_record->continue_fail_num,dev_record->num);
		
		dev_record->continue_fail_num = 0;
		dev_record->num = 0;
		dev_record->full_flag = 0;
		memset(dev_record->record,0,BLV_COMM_RecordNum);
		//调整波特率
		BUS_Adjust_Communication_Baud_Rate(baud_addr);
		
		return 0x02;
	}
	
	/*当前记录总数*/
	if(dev_record->full_flag == 0x01)	sum = BLV_COMM_RecordNum*8;		//当前记录已满
	else sum = dev_record->num;			//当前没有记录未满
	
	if(sum <= BLV_COMM_Precent_Num)	
	{
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"通讯记录未达到计算数:%d",sum);
		return 0x01;
	}
	
	for(uint8_t i=0;i<sum;i++)
	{
		temp = (dev_record->record[i/8] >> (i%8)) & 0x01;
		if(temp == 0x00)	fail_num++;		//失败次数加一
	}
	
	precent = (fail_num*100)/sum;
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"通讯失败百分比：%d,通讯总数：%d",fail_num,sum);
//	Dbg_Print_Buff(DBG_BIT_SYS_STATUS_bit,"通讯BUFF：",dev_record.record,BLV_COMM_RecordNum);
	
	if(precent > BLV_COMM_Fail_Precent_Max)
	{
		//通讯记录清零
		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_COMM Fail_num:%d NUM:%d - %d",fail_num,sum,precent);
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"百分比已达上限,通讯记录清零!");
		dev_record->continue_fail_num = 0;
		dev_record->num = 0;
		dev_record->full_flag = 0;
		memset(dev_record->record,0,BLV_COMM_RecordNum);
		//调整波特率
		BUS_Adjust_Communication_Baud_Rate(baud_addr);
		return 0x03;
	}
	return 0x00;
}

uint8_t BLV_Communication_Baud_Adjust2(BLV_COMM_RECORD_G *dev_record,Device_Public_Information_G *dev_info)
{
	uint8_t precent = 0,temp = 0;
	uint16_t fail_num = 0,sum = 0;
	
	/*当前记录总数*/
	if(dev_record->full_flag == 0x01)	sum = BLV_COMM_RecordNum*8;		//当前记录已满
	else sum = dev_record->num;			//当前没有记录未满
	
	if(dev_record->continue_fail_num >= BLV_CONTINUE_FAIL_MAX)		//连续失败次数已达上线
	{
		//通讯记录清零
		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"Continuous Failure:%d NUM:%d",dev_record->continue_fail_num,dev_record->num);
		
		//调整波特率
		BUS_Adjust_Communication_Baud_Rate2(dev_info);
		LOG_Device_COMM_Adjust_Baud_Record(dev_info->type,dev_info->addr,dev_info->baud,0x01,dev_record->continue_fail_num,sum,dev_record->num);
		
		//清空统计数据
		dev_record->continue_fail_num = 0;
		dev_record->num = 0;
		dev_record->full_flag = 0;
		memset(dev_record->record,0,BLV_COMM_RecordNum);
		
		return 0x02;
	}
	
	if(sum <= BLV_COMM_Precent_Num)	
	{
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"通讯记录未达到计算数:%d",sum);
		return 0x01;
	}
	
	for(uint8_t i=0;i<sum;i++)
	{
		temp = (dev_record->record[i/8] >> (i%8)) & 0x01;
		if(temp == 0x00)	fail_num++;		//失败次数加一
	}
	
	precent = (fail_num*100)/sum;
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"通讯失败百分比：%d,通讯总数：%d",fail_num,sum);
//	Dbg_Print_Buff(DBG_BIT_SYS_STATUS_bit,"通讯BUFF：",dev_record.record,BLV_COMM_RecordNum);
	
	if(precent > BLV_COMM_Fail_Precent_Max)
	{
		//通讯记录清零
		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BUS_COMM Fail_num:%d NUM:%d - %d",fail_num,sum,precent);
		
		//调整波特率
		BUS_Adjust_Communication_Baud_Rate2(dev_info);
		LOG_Device_COMM_Adjust_Baud_Record(dev_info->type,dev_info->addr,dev_info->baud,0x02,fail_num,sum,dev_record->num);
		
		//清空统计数据
		dev_record->continue_fail_num = 0;
		dev_record->num = 0;
		dev_record->full_flag = 0;
		memset(dev_record->record,0,BLV_COMM_RecordNum);
		return 0x03;
	}
	return 0x00;
}

/*******************************************************************************
* Function Name  : Get_BLV_Communication_Succ_Rate 
* Description    : 获取通讯失败数(当前已统计的通讯中的失败)
* Return 		 ：失败百分比
*******************************************************************************/
uint16_t Get_BLV_Communication_Fail_Rate(BLV_COMM_RECORD_G *dev_record)
{
	uint8_t temp = 0;
	uint16_t fail_num = 0,sum = 0,precent = 0;
	
	if(dev_record->full_flag == 0x01)	sum = BLV_COMM_RecordNum*8;		//当前记录已满
	else sum = dev_record->num;			//当前没有记录未满
	
	for(uint16_t i=0;i<sum;i++)
	{
		temp = (dev_record->record[i/8] >> (i%8)) & 0x01;
		if(temp == 0x00)	fail_num++;		//失败次数加一
	}
	
	precent = (fail_num*100)/sum;
	
	return precent;
}
