  /*
  ******************************************************************************
  修改信息：
	2022.09.06		林开锐
		用于主机上电时，丢掉第一包A9IO数据
  ******************************************************************************
  */

#include "BLV_485_Dev_A9IO_SwiFun.h"
#include <string.h>
#include "BLV_485_Protocol.h"
#include "SPI_SRAM.h"
#include "usart.h"
#include "CH57x_common.h"
#include "check_fun.h"
#include "rw_logging.h"
#include "SRAM_Variable_Address.h"
#include "DBG.h"
#include "Log_api.h"

//uint8_t Dev_Swi_InType_Get(uint32_t DevAddr, uint16_t DevInputLoop, uint16_t DevInputType);
//void Dev_Swi_Output_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t DevAddr, uint16_t DevOutputLoop, uint16_t DevOutputType);
//uint16_t Dev_Swi_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop);

uint8_t BLW_A9IO_SwitchCycleDis(uint32_t DevAdd);  
uint8_t BLW_Rs485_A9IO_Swi_Check(uint32_t DevAdd ,uint32_t Data_addr, uint16_t DataLen); 
#define RS485_A9_IO_QUERY(buf,x)  (((buf[x/4+7]&(0x03<<((x%4)*2)))>>(x%4)*2))

//#define RS485_TOUCH_INPUT_QUERY(data,x)  (((data&(0x03<<((x%4)*2)))>>(x%4)*2))   //每次判断两位
#define REPEATSENDTIMEMAX			0x01  	//本设备重发次数上限
#define BLWTOUCHCTRLTIMEOUT			100		//本设备控制超时时间
#define BLWTOUCHASKTIMEOUT			10		//本设备控制回复超时时间

#define SwitchSnMax                 0x05         //最大Sn号
#define SwitchSnMin                 0x00         //最小Sn号

//#define BLW_TOUCH_LOOPIN_NUM		25		//本设备RS485开关输入回路数    6
//#define BLW_TOUCH_LOOPOUT_NUM		25		//本设备开关输出回路数        7
//#define BLW_SWT_ASK_LEN_M			(((BLW_TOUCH_LOOPOUT_NUM+7)/4)+3)
//#define BLW_SWT_BUF_LEN				0x05      //发送数组长度为5  如果开关在16个按键以内
//#define BLW_SWT_BUF_LEN_M			(((BLW_TOUCH_LOOPOUT_NUM+7)/8)+3)

#define Dev_A9IOSwitch_RecvData_Len_Max	0x18

#define Dev_A9IOSwitch_Input_NUm		24		//A9IO 输入回路上限是24个

#define   A9IOTYPE           14	

/*******************************************************************************
* Fail Description	: 485设备 - T1触摸开关驱动文件
* 
*******************************************************************************/

void BLV_485_Dev_A9IO_Switch_Init(Device_Public_Information_G *BUS_Public, RS485_SWI_INFO *Rs485SwiInfo)
{
	Rs485SwiInfo->SwtRelayLedCtrlFlag = 0x02;		//上电初始化，读取A9IO 当前状态
	Rs485SwiInfo->SwtRelayLedCtrlCnt = REPEATSENDTIMEMAX;
	
	BUS_Public->polling_cf = (uint32_t)&BLW_A9IO_SwitchCycleDis;
	BUS_Public->processing_cf = (uint32_t)&BLW_Rs485_A9IO_Swi_Check;
	
//	BUS_Public.DevFunInfo.Dev_Input_Type_Get = Dev_Swi_InType_Get;
//	BUS_Public.DevFunInfo.Dev_Output_Ctrl = Dev_Swi_Output_Ctrl;
//	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Dev_Swi_Loop_State;
	
	/*6键开关，7号输出为背光*/
//	Rs485SwiInfo.SwtInputValidNum = DevSwiInputnum;		//初始化开关输入输出数
//	Rs485SwiInfo.SwtOutputValidNum = DevSwiOutputnum;
//	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"增加A9IO开关 地址: %d",BUS_Public->addr);										
	
//	Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO));
}


void BLW_A9IO_Rs485_Swi_Pro(Device_Public_Information_G* BUS_Public, uint8_t *data, RS485_SWI_INFO  *Rs485SwiInfo)
{
	uint8_t value_buf = 0;
	uint8_t i;
//	uint8_t temp = 0,temp2 = 0,d_addr = 0;
	
	/*选择开关日志数据保存地址*/
//	d_addr = SRAM_Read_Byte(dev_addr + Dev_Addr);
	
	if(Rs485SwiInfo->DevOffline == DEV_IS_OFFLINE)
	{
		//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dev_A9IOSwitch LogInfo_Device_Online...........");
		LOG_Device_Online_Record(DEV_SWI_TYPE,BUS_Public->addr,LogInfo_Device_Online);	//记录设备在线
	}
	
	BLV_Communication_Record(&Rs485SwiInfo->comm_record,0x02,0x01);		//记录通讯成功
//	Rs485SwiInfo->DevSendCnt = 0x00;             		//收到回复就清计数
//	Rs485SwiInfo->DevOffline = DEV_IS_ONLINE;    		//设备在线
	
//	temp = SRAM_Read_Byte(data_addr + 1);
	
//	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A9IO switch Input Num:%d\r\n", Rs485SwiInfo->SwtInputValidNum);
	for(i = 0; i < Rs485SwiInfo->SwtInputValidNum; i++)//18路按键状态赋值
	{
//		if(RS485_A9_IO_KEY_FLAG(data, i))  //如果指定标志位被置一 
		//点按直接赋值，长按需要上一个状态不是长按，才有效。长按松开需要上一个状态不是长按松开才有效。
		{
			value_buf |= RS485_A9_IO_QUERY(data, i);    //下标从5开始
			
			
			switch(value_buf)
			{
				case 0x01:
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"switch%d Dev%02X KeyPress Coord:%d",BUS_Public->addr, i, BUS_Public->DevCoord);
					DevActionGlobal.Devi = BUS_Public->DevCoord;     //直接跳到指定的下标去处理
					Rs485SwiInfo->DevReadBuf[i] = KeyPress;          //地址从1开始
					Rs485SwiInfo->DevReadBuf_last[i] = Rs485SwiInfo->DevReadBuf[i];	//保存当前键值，用于无卡取电逻辑使用 2025-02-27		
					break;
				case 0x03:
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"switch%d Dev%02X KeyRelease Coord:%d",BUS_Public->addr, i, BUS_Public->DevCoord);
					DevActionGlobal.Devi = BUS_Public->DevCoord;     //直接跳到指定的下标去处理
					Rs485SwiInfo->DevReadBuf[i] = KeyRelease;
					Rs485SwiInfo->DevReadBuf_last[i] = Rs485SwiInfo->DevReadBuf[i];	//保存当前键值，用于无卡取电逻辑使用 2025-02-27		
					break;
				case 0x02:
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"switch%d Dev%02X KeyHold  Coord:%d",BUS_Public->addr, i, BUS_Public->DevCoord);
					DevActionGlobal.Devi = BUS_Public->DevCoord;     //直接跳到指定的下标去处理
					Rs485SwiInfo->DevReadBuf[i] = KeyHold;
					Rs485SwiInfo->DevReadBuf_last[i] = Rs485SwiInfo->DevReadBuf[i];	//保存当前键值，用于无卡取电逻辑使用 2025-02-27		
					break;
				default:
	//				Rs485SwiInfo.DevReadBuf[28*data[0]+i+1] = keyInvalid;			
					break;
			}
			switch(value_buf)
			{
				case 0x01:
				case 0x02:
				case 0x03:  
//					Ret = 0x01;
//					Rs485SwiInfo->A9IORepeatFlag = 0x00;  //拿到键值就清重发标志
					break;
				default:
//					Rs485SwiInfo->A9IORepeatFlag = 0x01;  //没有拿到键值置重发标志
					break;
			}
			value_buf = 0;		
		}
	}	
}

void BLW_A9IO_Rs485_IO_State_Pro(Device_Public_Information_G* BUS_Public, uint8_t *data, RS485_SWI_INFO  *Rs485SwiInfo)
{
	uint8_t value_buf = 0;
	uint8_t i;
	
	if(Rs485SwiInfo->DevOffline == DEV_IS_OFFLINE)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dev_A9IOSwitch LogInfo_Device_Online...........");
		LOG_Device_Online_Record(DEV_SWI_TYPE,BUS_Public->addr,LogInfo_Device_Online);	//记录设备在线
	}
	
	BLV_Communication_Record(&Rs485SwiInfo->comm_record,0x02,0x01);		//记录通讯成功
	
	for(i = 0; i < Rs485SwiInfo->SwtInputValidNum; i++)//18路按键状态赋值
	{
		if(i >= Dev_A9IOSwitch_Input_NUm) break;
			
		value_buf = (Rs485SwiInfo->KeyBitValue >> i) & 0x01;
		
		switch(value_buf)
		{
			case 0x01:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A9IO %d Loop%02X KeyHold",BUS_Public->addr, i);
				DevActionGlobal.Devi = BUS_Public->DevCoord;     	//直接跳到指定的下标去处理
				//这里不产生实际动作，只是读取一下状态，用于无卡取电逻辑判断
				Rs485SwiInfo->DevReadBuf_last[i] = KeyHold;			//保存当前键值，用于无卡取电逻辑使用 2025-02-27		
				break;
			case 0x00:
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A9IO %d Loop%02X KeyRelease",BUS_Public->addr, i);
				DevActionGlobal.Devi = BUS_Public->DevCoord;     	//直接跳到指定的下标去处理
				//这里不产生实际动作，只是读取一下状态，用于无卡取电逻辑判断
				Rs485SwiInfo->DevReadBuf_last[i] = KeyRelease;		//保存当前键值，用于无卡取电逻辑使用 2025-02-27		
				break;
			default:		
				break;
		}

	}	
}

#define RECDATALENMAX   12   //接收数据最大长度
#define IsNotMyData   0x01
#define IsMyData      0x00
/*******************************************************************************
* Function Name  : BLW_Rs485_A9IO_Swi_Check 
* Description    : BLW触摸开关485接收处理函数

* Return         : 
	0x01：数据处理失败
	0x00：数据处理成功
*******************************************************************************/
uint8_t BLW_Rs485_A9IO_Swi_Check(uint32_t DevAdd ,uint32_t Data_addr, uint16_t DataLen) 
{
	uint8_t Crc_buf = 0;
	uint8_t ret = IsNotMyData;
	uint8_t data[RECDATALENMAX];
//	static uint8_t A9IOSetFlag = 0;	//A9IO稳定标志							//2022.08.22	林开锐改	用于红外上电时，丢掉第一包数据		2025-02-28 取消，不明白为什么要增加这个
	Device_Public_Information_G BUS_Public;   //共用	
	RS485_SWI_INFO Rs485SwiInfo;       //开关局部变量
	
	if(DataLen > RECDATALENMAX)
	{
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"接收数据长度超出范围!!\r\n");
		return ret;     //长度
	}
	
	SRAM_DMA_Read_Buff(data,DataLen,Data_addr);   //拷贝485数据
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAdd);		
		
	
	if((data[0] != BUS_Public.addr)||(DataLen < 0x05))   //匹配开关地址数据
	{
//		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"不是A9IO开关的数据，DevAdd:%d,len:%d\r\n",data[0],DataLen);
		return ret;    //不是就直接返回
	}
	
//	if(A9IOSetFlag == 0)
//	{
//		A9IOSetFlag = 1;													//2022.08.22	林开锐改	用于红外上电时，丢掉第一包数据	2025-02-28 取消，不明白为什么要增加这个
//		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"A9IO数据丢包");
//		return ret;	//返回失败
//	}

	Crc_buf = CheckSum_Overlook_Check(data, DataLen, 5);  //和校验data[DataLen-1];
	 
	if(Crc_buf == data[5])	//校验通过
	{
//		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A9IO 校验通过 DevAdd:%d,len:%d\r\n",data[0],DataLen);
		SRAM_DMA_Read_Buff((uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO),DevAdd+Dev_Privately);
//		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"是A9IO开关的数据，DevAdd:%d,len:%d\r\n",data[0],DataLen);
		
		switch(data[6])
		{
			case 0x33:
			case 0x31:
			case 0x36:
				ret = IsMyData;
				Rs485SwiInfo.A9IORepeatFlag = 0x00;   //清重发标志
				Rs485SwiInfo.DevSendCnt = 0x00;                                     //收到回复就清计数
				Rs485SwiInfo.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(Rs485SwiInfo.DevOffline != Rs485SwiInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485SwiInfo.DevOfflineLast = Rs485SwiInfo.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
				break;
		}
		switch(data[6])
		{
			case 0x33:         //按键键值
				BLW_A9IO_Rs485_Swi_Pro(&BUS_Public, data, &Rs485SwiInfo);  //校验通过，接收处理
				break;
			case 0x31:         //反馈灯控制回复
				//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"接收到A9IO开关反馈灯的数据，DevAdd:%d,len:%d\r\n",data[0],DataLen);
				
				Rs485SwiInfo.SwtRelayLedCtrlFlag = 0x00;
				LOG_Device_COMM_Control_Reply_Record(BUS_Public.port,BUS_Public.baud,data,DataLen);
				break;
			case 0x36:         //按键bit位
				if(0x0B == DataLen)
				{
					Rs485SwiInfo.KeyBitValue = data[8] + (data[9]<<8) + (data[10]<<16);
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"读取到A9IO IO状态:%x\r\n",Rs485SwiInfo.KeyBitValue);
					
					BLW_A9IO_Rs485_IO_State_Pro(&BUS_Public, data, &Rs485SwiInfo);
					Rs485SwiInfo.SwtRelayLedCtrlFlag = 0x00;
				}
				
				
				break;
		}
		
		/*日志记录*/
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485SwiInfo, sizeof(RS485_SWI_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAdd);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO),DevAdd+Dev_Privately);
		
	}

	return ret;
}


#define A9IOCTRLLEN     12         //指示灯控制报文
/**
	* @name A9IO开关指示灯控制函数
	* @para
	*   BUS_Public      公用数据
  *   Rs485SwiInfo    私有数据
	* @return 无
	*/
void BLW_A9IO_Switch_ctrl(Device_Public_Information_G *BUS_Public, RS485_SWI_INFO *Rs485SwiInfo)
{
	                            //己方地址 发送类型  设备类型  收方地址 LEN          校验和  CMD  4个灯
	uint8_t SendBuf[A9IOCTRLLEN] = {0x00,   0x00,   A9IOTYPE,   0x00,  A9IOCTRLLEN,  0x18,  0x21, 0x00, 0x00,  0x00,  0x00, 0x00};   //
	uint8_t i;

	SendBuf[1] |= BLV_SN_Get(&Rs485SwiInfo->SwitchSn, SwitchSnMin, SwitchSnMax);
												 
	SendBuf[3] = BUS_Public->addr;   
	for(i = 0; i < Rs485SwiInfo->SwtOutputValidNum; i++)
	{
		if(0x00 != Rs485SwiInfo->DevSendBuf[i])  //不为0
		{
			SendBuf[7+i/4] |= 0x02<<((i%4)*2);         //开
		}
		else
		{
			SendBuf[7+i/4] |= 0x01<<((i%4)*2);         //关
		}
	}
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A9IOCTRLLEN, 5);	
	/*发送函数*/
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s",__func__);
	MCU485_SendString(BUS_Public->port,SendBuf,A9IOCTRLLEN);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,SendBuf,A9IOCTRLLEN);
	
}
#define A9IOCYCLELEN     7         //A9_IO开关轮询报文
/**
	* @name A9IO开关轮询函数
	* @para
	*   BUS_Public      公用数据
  *   Rs485SwiInfo    私有数据
	* @return 无
	*/
void BLW_Switch_A9_IO_Cycle(Device_Public_Information_G *BUS_Public, RS485_SWI_INFO *Rs485SwiInfo)
{
	                             //己方地址 发送类型  设备类型  收方地址 LEN          校验和  CMD  
	uint8_t SendBuf[A9IOCYCLELEN] = {0x00,   0x00,   A9IOTYPE,   0x00,  A9IOCYCLELEN,  0x18,  0x23,};   //
	
	if(0x01 == Rs485SwiInfo->A9IORepeatFlag)
	{
		SendBuf[1] |= Rs485SwiInfo->SwitchSn;  
		SendBuf[1] |= (0x01<<6);
	}		 
	else
	{
		SendBuf[1] |= BLV_SN_Get(&Rs485SwiInfo->SwitchSn, SwitchSnMin, SwitchSnMax);
	}
	SendBuf[3] = BUS_Public->addr; //CfgDevAdd->DevAddr;   	
	
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A9IOCYCLELEN, 5);	
	/*发送函数*/
//	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A9IO switch send data---\r\n");
	MCU485_SendString(BUS_Public->port,SendBuf, A9IOCYCLELEN);
	
}

/**
	* @name A9IO开关 - 读取IO状态
	* @para
	*   BUS_Public      公用数据
  *   Rs485SwiInfo    私有数据
	* @return 无
	*/
void BLW_Switch_A9_ReadIO_State(Device_Public_Information_G *BUS_Public, RS485_SWI_INFO *Rs485SwiInfo)
{
	                             //己方地址 发送类型  设备类型  收方地址 LEN          校验和  CMD  
	uint8_t SendBuf[A9IOCYCLELEN] = {0x00,   0x00,   A9IOTYPE,   0x00,  A9IOCYCLELEN,  0x18,  0x26,};
	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s...",__func__);
	
	if(0x01 == Rs485SwiInfo->A9IORepeatFlag)
	{
		SendBuf[1] |= Rs485SwiInfo->SwitchSn;  
		SendBuf[1] |= (0x01<<6);
	}		 
	else
	{
		SendBuf[1] |= BLV_SN_Get(&Rs485SwiInfo->SwitchSn, SwitchSnMin, SwitchSnMax);
	}
	SendBuf[3] = BUS_Public->addr; //CfgDevAdd->DevAddr;   	
	
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A9IOCYCLELEN, 5);	
	/*发送函数*/
//	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A9IO switch send data---\r\n");
	MCU485_SendString(BUS_Public->port,SendBuf, A9IOCYCLELEN);
	
}

/*******************************************************************************
* Function Name  : BLW_A9IO_SwitchCycleDis 
* Description    : BLW触摸开关485数据发送处理函数，包括重发

* Return         : 
	0x01：有数据需要发送
	0x00：没有数据需要发送
*******************************************************************************/
uint8_t BLW_A9IO_SwitchCycleDis(uint32_t DevAdd)  
{
	Device_Public_Information_G BUS_Public;
	RS485_SWI_INFO Rs485SwiInfo;       //开关局部变量

	uint8_t i;
	uint8_t Ret = RS485OCCUPYTIME; //每次都有数据要发送
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAdd);			
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO),DevAdd+Dev_Privately);/*拷贝设备私有信息*/
	
	if(Rs485SwiInfo.SwtRelayLedCtrlFlag != 0x02){
		/*判断设备是否有数据需要发送 - 正常通讯中*/
		for(i = 0; i < Rs485SwiInfo.SwtOutputValidNum; i++)
		{
			if(Rs485SwiInfo.DevSendBuf_last[i] != Rs485SwiInfo.DevSendBuf[i])
			{
				Rs485SwiInfo.SwtRelayLedCtrlFlag = 0x01;
				Rs485SwiInfo.SwtRelayLedCtrlCnt = REPEATSENDTIMEMAX;
				//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dev_A9IOSwitch 产生反馈灯控制标志");
				
				memcpy(Rs485SwiInfo.DevSendBuf_last, Rs485SwiInfo.DevSendBuf, Rs485SwiInfo.SwtOutputValidNum); 
				break; 
			}
		}
		
		if(0x01 == Rs485SwiInfo.SwtRelayLedCtrlFlag)
		{
			if(0x00 != Rs485SwiInfo.SwtRelayLedCtrlCnt)
			{
				Rs485SwiInfo.SwtRelayLedCtrlCnt--; 
			}else{
				Rs485SwiInfo.SwtRelayLedCtrlFlag = 0x00;
			}
			BLW_A9IO_Switch_ctrl(&BUS_Public, &Rs485SwiInfo);
		}else{
			BLW_Switch_A9_IO_Cycle(&BUS_Public, &Rs485SwiInfo);
		}
	}else{
		/*当前处于上电初始化状态，读取当前A9IO电平状态*/
		if(0x00 != Rs485SwiInfo.SwtRelayLedCtrlCnt)
		{
			Rs485SwiInfo.SwtRelayLedCtrlCnt--; 
		}else{
			Rs485SwiInfo.SwtRelayLedCtrlFlag = 0x00;
		}
		BLW_Switch_A9_ReadIO_State(&BUS_Public, &Rs485SwiInfo);
	}
	
	if(Rs485SwiInfo.DevSendCnt > REPEATSENDTIMEMAX)
	{
		if(Rs485SwiInfo.DevOffline != DEV_IS_OFFLINE)
		{
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dev_A9IOSwitch LogInfo_Device_Offline...........");
			LOG_Device_Online_Record(DEV_SWI_TYPE, BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线
		}

		Rs485SwiInfo.DevOffline = DEV_IS_OFFLINE;   //判为离线
		if(Rs485SwiInfo.DevOffline != Rs485SwiInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
		{
			Rs485SwiInfo.DevOfflineLast = Rs485SwiInfo.DevOffline;	//更新上一次在线状态
			Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
		}
	}
	else
	{
		Rs485SwiInfo.DevSendCnt++;   //发送次数累计
	}
	/*通讯统计记录*/
	BLV_Communication_Record(&Rs485SwiInfo.comm_record,0x01,0x00);
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485SwiInfo, sizeof(RS485_SWI_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAdd);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&Rs485SwiInfo,sizeof(RS485_SWI_INFO),DevAdd+Dev_Privately);
			
	return Ret;
}



