#include "config.h"

volatile u8 Multiplex_Port_RX_Flag = 0;//Multiplex_Port接受任务标志位，1允许运行
volatile u8 Multiplex_Port_TX_Flag = 0;//
volatile u8 Multiplex_Port_RX_Buff[1000] = {0x01,0x03,0x00,0x02,0x00,0x09,0x00,0x00};;
volatile u8 Multiplex_Port_TX_Buff[1000] = {0x01,0x03,0x00,0x02,0x00,0x09,0x00,0x00};;
volatile u8 Multiplex_Port_DMA_RX_Len  = 23;
/*_____ D E F I N I T I O N __________________________________________________*/

u16 BigToLittle( u16 BigData) 
{  
  return (BigData << 8 | BigData >> 8);  
}
//u32 BigToLittle( u32 BigData) 
//{  
//	union
//	{
//	  u32 Temp;
//		u8 Buff[4];
//	  }Dat;
//	
//	if(sizeof(BigData) == 2)
//    return (BigData << 8 | BigData >> 8);
//  else  if(sizeof(BigData) == 4)
//	{
//		Dat.Buff[0] = (BigData >> 24) & 0xff;
//		Dat.Buff[1] = (BigData >> 16) & 0xff;
//		Dat.Buff[2] = (BigData >> 8) & 0xff;
//		Dat.Buff[3] = (BigData >> 0) & 0xff;
//		return (Dat.Temp);
//	  }
//}
/****************************************************
函数名：Multiplex_Port_IO_Configuration
功能  ：Multiplex_Port卡通讯部分用到的IO初始化
参数  ：无
返回值：无
****************************************************/
void Multiplex_Port_IO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;	//定义GOIP配置结构体

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_Init(GPIOC, &GPIO_InitStructure); 
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_13 | GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
  GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化
	Multiplex_Port_RS485_RxEN1;
	Multiplex_Port_RS485_RxEN2;
}
/****************************************************
函数名：Multiplex_Port_NVMultiplex_Port_Configuration
功能  ：Multiplex_Port卡通讯部分用到的中断向量配置
参数  ：无
返回值：无
****************************************************/
void Multiplex_Port_NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;	//定义中断配置结构体
	
	NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream6_IRQn;  //USART6_TX_DMA
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	
	NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;		//USART??
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;		//
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		//
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ????
	NVIC_Init(&NVIC_InitStructure);	//??NVIC_InitStruct???????????NVIC???USART1
}


/****************************************************
函数名：Multiplex_Port_NVMultiplex_Port_Configuration
功能  ：Multiplex_Port卡通讯部分用到的串口配置
参数  ：无
返回值：无
****************************************************/
void Multiplex_Port_USART_Configuration(u32 Baud,u8 Check)
{
  USART_InitTypeDef USART_InitStructure;
	
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
	USART_InitStructure.USART_BaudRate =Baud;// Baud;//?????
	if(Check == Check_EVEN)
	{
		USART_InitStructure.USART_WordLength = USART_WordLength_9b;
		USART_InitStructure.USART_Parity = USART_Parity_Even;
		}
	else if(Check == Check_ODD)
	{
		USART_InitStructure.USART_WordLength = USART_WordLength_9b;
		USART_InitStructure.USART_Parity = USART_Parity_Odd;
		}
	else if(Check == Check_NONE)
	{
		USART_InitStructure.USART_WordLength = USART_WordLength_8b;
		USART_InitStructure.USART_Parity = USART_Parity_No;
		}
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_Init(USART6, &USART_InitStructure);
	USART_Cmd(USART6, ENABLE);
	
	USART_ITConfig(USART6, USART_IT_IDLE, ENABLE);  
  USART_ClearFlag(USART6, USART_FLAG_IDLE); 
	
	USART_DMACmd(USART6, USART_DMAReq_Tx | USART_DMAReq_Rx,ENABLE);
	
}
/*************************************************
函数名  ：DMA_Configuration（）
功能    ：DMA初始化
传入参数：无
返回值  ：无
**************************************************/
void Multiplex_Port_DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;
 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);  
	
	/*******************发送TX1*********************/
	DMA_DeInit(DMA2_Stream6);		
	DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;   
	//DMA_InitStructure.DMA_BufferSize = UART_DMA_SEND_LEN;   
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;  
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;  
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;  
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;  
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART6->DR; 	
	DMA_InitStructure.DMA_Channel = DMA_Channel_5; 
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Multiplex_Port_TX_Buff; 	
	
	DMA_Init(DMA2_Stream6, &DMA_InitStructure); 		 
	DMA_ITConfig(DMA2_Stream6, DMA_IT_TC, ENABLE);	
	/*******************RX*********************/
	DMA_DeInit(DMA2_Stream1);		
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;   
	DMA_InitStructure.DMA_BufferSize = 1000;   
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;  
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;  
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;  
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;  
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; 
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART6->DR; 	
	DMA_InitStructure.DMA_Channel = DMA_Channel_5; 
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Multiplex_Port_RX_Buff; 	
	
	DMA_Init(DMA2_Stream1, &DMA_InitStructure); 		
	DMA_Cmd(DMA2_Stream1, ENABLE);
	
	
}
//F*****************************************************************************
//* NAME:    Multiplex_Port_TXDataCheck
//* PURPOSE: Multiplex_Port卡键盘通信校验函数
//* PARAMS: pData需要检验的数组地址，len数据长度
//* return:	 无
//******************************************************************************
u8 Multiplex_Port_TXDataCheck(volatile u8* pData, u8 len)
{
	u8 result;
	if(len == 0) len = 16;	
	result = *pData++;
	do
		{
		result ^= *pData++;
		}while(--len);	

	return	result & 0x7f;
}
void Multiplex_Port_DMATX_IRQHandler(void)
{  
	u16 i = 0;
	DMA_ClearFlag(DMA2_Stream6, DMA_IT_TCIF6);  
	Multiplex_Port_TX_DMAStop();  //关闭DMA传输
	while(! USART_GetFlagStatus(USART6, USART_FLAG_TC))NULL;
	Multiplex_Port_RS485_RxEN1;
	Multiplex_Port_RS485_RxEN2;
	DMA_ClearFlag(DMA2_Stream1, DMA_IT_TCIF1);
	Multiplex_Port_RX_DMAStart();   //打开DMA传输(Gas就收)长度在选择发送队列时设置
	Multiplex_Port_TX_Flag = 1;
}
//F*****************************************************************************
//* NAME:    Multiplex_Port_DMARX_IRQHandler
//* PURPOSE: DMA传输完成中断，Gas ModBus
//* PARAMS: 无
//* return:	 无
//******************************************************************************

void Multiplex_Port_DMARX_IRQHandler(void)
{  
  u8 i = 0;
	u16 CRC_Temp = 0;
	if(USART_GetITStatus(USART6, USART_IT_IDLE) != RESET)//????????? 
	{
		USART_ClearFlag(USART6, USART_IT_IDLE);  
		i = USART6->SR;
    i = USART6->DR;
		TXHData.MD_MultiplexRXIDLETimer.ON_OFF = 1;
		TXHData.MD_Multiplex_Rec_NummberB = 1000 - DMA2_Stream1->NDTR;
	}
}
//F*****************************************************************************
//* NAME:    Price_DataCheck
//* PURMDE: 单价牌检验算法
//* PARAMS: pData需要检验的数组地址，len数据长度
//* return:	 无
//******************************************************************************
u8 Price_DataCheck(volatile u8* pData, u16 len)
{
	u8 result = 0;
	do
		{
		result += *pData++;
		}while(--len);	

	return	result;
}
void StringToHex(u8 *Str,u8 *H,u8 Len)
{
  u8 i = 0;
	for(i = 0;i < Len;i += 2)
	{
		if(*(Str + i) > 0x39)
	    *(H + (i / 2)) = (*(Str + i) - 0x37) << 4;
		else
			*(H + (i / 2)) = (*(Str + i) - 0x30) << 4;
		
		if(*(Str + i + 1) > 0x39)
	    *(H + (i / 2)) |= (*(Str + i + 1) - 0x37) & 0x0f;
		else
			*(H + (i / 2)) |= (*(Str + i + 1) - 0x30) & 0x0f;
	  }
  }
u16 mystrstr(u8 *s1,u8 *s2)
{
  int len2;
	u16 i= 0;
  if(!(len2=strlen(s2)))//此种情况下s2不能指向空，否则strlen无法测出长度，这条语句错误
    return 1;
  for(;*s1;++s1)
  {
		i ++;
   if(*s1==*s2 && strncmp(s1,s2,len2)==0)
      return i;
    }
  return NULL;
}

void Multiplex_Port_Choose(u8 Port)
{
  switch(Port)
  {
	  case 0:
		{
			GPIO_ResetBits(GPIOC,GPIO_Pin_13);//B
		  GPIO_ResetBits(GPIOC,GPIO_Pin_8);//A
		  }break;
		case 1:
		{
		  GPIO_ResetBits(GPIOC,GPIO_Pin_13);//B
		  GPIO_SetBits(GPIOC,GPIO_Pin_8);//A
		  }break;
		case 2:
		{
		  GPIO_SetBits(GPIOC,GPIO_Pin_13);//B
		  GPIO_ResetBits(GPIOC,GPIO_Pin_8);//A
		  }break;
		case 3:
		{
		  GPIO_SetBits(GPIOC,GPIO_Pin_13);//B
		  GPIO_SetBits(GPIOC,GPIO_Pin_8);//A
		  }break;
	}		
}
//F*****************************************************************************
//* NAME:    Multiplex_Port_Init
//* PURPOSE: Multiplex_Port通讯初始化
//* PARAMS: 无
//* return:	 无
//******************************************************************************

void Multiplex_Port_Init(void)
{
	u8 i = 0;
  Multiplex_Port_IO_Configuration();                //流量计相关IO初始化
	Multiplex_Port_USART_Configuration(115200,Check_NONE); //相关串口初始化
	Multiplex_Port_DMA_Configuration();               //流量计相关DMA初始化
  Multiplex_Port_NVIC_Configuration();         //流量计相关中断初始化
  
	TXHData.MD_MultiplexRXIDLETimer.Init = 0;          //清除初始化标志
	TXHData.MD_MultiplexRXIDLETimer.Flag = 0;          //清除到时标志
	TXHData.MD_MultiplexRXIDLETimer.ON_OFF = 0;        //打开
	TXHData.MD_MultiplexRXIDLETimer.Cycle = 0;         //设置为循环模式，发送时启动
	TXHData.MD_MultiplexRXIDLETimer.TimerCountMax = 5;//周期1000（1s）
	
	TXHData.MD_MultiplexTimer.Init = 0;          //清除初始化标志
	TXHData.MD_MultiplexTimer.Flag = 0;          //清除到时标志
	TXHData.MD_MultiplexTimer.ON_OFF = 1;        //打开
	TXHData.MD_MultiplexTimer.Cycle = 1;         //设置为循环模式，发送时启动
	TXHData.MD_MultiplexTimer.TimerCountMax = 1000;//周期1000（1s）
	//TXHData.MD_Multiplex_Port_Data.PortData[0].POSType = 1;
//	TXHData.MD_Multiplex_Port_Data.DeviceNumber =1;
	if(TXHData.MD_Multiplex_Port_Data.DeviceNumber == 0)
	{
		if(TXHData.HardwareVersion >= 26)//版本26之后增加GPS模块
		{
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[0].Init = 0;          //清除初始化标志
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[0].Flag = 0;          //清除到时标志
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[0].ON_OFF = 1;        //打开
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[0].Cycle = 1;         //设置为循环模式，发送时启动
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[0].TimerCountMax = 10*1000;//周期1000（1s）
			TXHData.MD_Multiplex_Port_Data.PortData[0].MBCountTime = 10*100;
			TXHData.MD_Multiplex_Port_Data.PortData[0].POSType = Multiplex_GPS;
			TXHData.MD_Multiplex_Port_Data.PortData[0].BaudRate = 9600;
			TXHData.MD_Multiplex_Port_Data.PortData[0].StopBits = 1;
			TXHData.MD_Multiplex_Port_Data.DeviceNumber += 1;//GPS模块放在随后的一个位置
			}
		}
	else
	{
		for(i = 0;i < TXHData.MD_Multiplex_Port_Data.DeviceNumber;i ++)
		{
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[i].Init = 0;          //清除初始化标志
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[i].Flag = 0;          //清除到时标志
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[i].ON_OFF = 1;        //打开
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[i].Cycle = 1;         //设置为循环模式，发送时启动
			TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[i].TimerCountMax = TXHData.MD_Multiplex_Port_Data.PortData[i].MBCountTime * 10;//周期1000（1s）
			switch(TXHData.MD_Multiplex_Port_Data.PortData[i].POSType)
			{
				case Multiplex_Print:TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[i] = PT_Null;break;
				case Multiplex_YWY  :
					TXHData.MD_MultiplexRXIDLETimer.TimerCountMax = 50;//周期1000（1s）
					TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[i] = YWY_MDConnectCmdReadData;
					TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[i].TimerCountMax = TXHData.MD_Multiplex_Port_Data.PortData[i].MBCountTime * 100;break;
				case Multiplex_Price:TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[i] = Price_MDConnectCmdReadParameter;
				     TXHData.MD_Multiplex_Port_Info.Handshake_Top[i] = 2;
					   TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[i].TimerCountMax = TXHData.MD_Multiplex_Port_Data.PortData[i].MBCountTime * 100;break;
				}
			}
		}
}

void Multiplex_PortTXTask(u8 PosTypeTemp,u8 MD_TX_CMD)
{
	u16 i = 0,j = 0,k = 0,MD_Port_Num = 0,DeviceNumber = 0,MD_ConnectTX_Len = 0;
  u8 GunNum = 0,GunAddr = 0,SunNum = 0;
	u64 TempBCD = 0,Tempu64 = 0;
	u8 Data_201[7] = "i20100";
	u8 Data_20E[7] = "i20E00";
	
	Multiplex_Port_TX_Flag = 0;
	if(TXHData.MD_Multiplex_Port_Info.MBCountCutNum[PosTypeTemp] >= 4)
	{
		TXHData.MD_Multiplex_Port_Info.MBCountCutNum[PosTypeTemp] = 5;
		TXHData.MD_Multiplex_Port_Info.Handshake[PosTypeTemp] = 0;
		TXHData.MD_Multiplex_Port_Data.State[PosTypeTemp].Flag.Online = 0;
		switch(TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].POSType)
		{
			case Multiplex_Print:TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[PosTypeTemp] = PT_Null;break;
			case Multiplex_YWY  :TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[PosTypeTemp] = YWY_MDConnectCmdReadData;break;
			case Multiplex_Price:TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[PosTypeTemp] = Price_MDConnectCmdReadParameter;TXHData.MD_Multiplex_Port_Info.Handshake[PosTypeTemp] = 0;break;
			}
		}
	switch(TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].POSType)
	{
		case Multiplex_Print:
		{
			if(TXHData.MD_Multiplex_Port_Info.PrintType != PT_Null)
			{
				Multiplex_Port_Choose(0);
			  Multiplex_Port_USART_Configuration(TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].BaudRate,TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].Parity_Even);
				switch(TXHData.MD_Multiplex_Port_Info.PrintType)
				{
					case PT_LastRecord:
					{
						DeviceNumber = TXHData.MD_Multiplex_Port_Info.PrintPlaceNumber;
						GunNum = TXHData.PrintLastRecord[DeviceNumber].GunNumber;
            TXHData.PrintRecord.FData = TXHData.PrintLastRecord[DeviceNumber].FData;
						TXHData.PrintRecord.Price = TXHData.PrintLastRecord[DeviceNumber].Price;	
						TXHData.PrintRecord.RTIME = TXHData.PrintLastRecord[DeviceNumber].RTIME;
						MD_Port_Num = DeviceNumber / 16;
						}break;
					case PT_Record:
					{
						GunNum = TXHData.PrintRecord.GunNumber;
						for(MD_Port_Num = 0;MD_Port_Num < TXHData.MD_Port_Num_Top;MD_Port_Num ++)
						{
							for(DeviceNumber = 0;DeviceNumber < TXHData.MD_Port_Data[MD_Port_Num].PortData.DeviceNumber;DeviceNumber ++)
							{
								if(TXHData.MD_Port_Data[MD_Port_Num].GunData[DeviceNumber].GunNummber == GunNum)
									break;
								}
							if(DeviceNumber < TXHData.MD_Port_Data[MD_Port_Num].PortData.DeviceNumber) 
								break;
							}
						}break;
				  }
				
				Print_Num(0,6,MD_Connect_u64ToBCD(GunNum),0);
				Print_Num(0,7,TXHData.PrintRecord.FData.MoneyCNT,TXHData.MD_Port_Data[MD_Port_Num].PortData.GunDec.Dec.MoneyCNT);
				Print_Num(0,8,TXHData.PrintRecord.FData.OilCNT,TXHData.MD_Port_Data[MD_Port_Num].PortData.GunDec.Dec.OilCNT);
				Print_Num(0,9,TXHData.PrintRecord.Price,TXHData.MD_Port_Data[MD_Port_Num].PortData.GunDec.Dec.Price);
			
				TXHData.PrintInfo.PrintBuff[11][30-18]  = '2';TXHData.PrintInfo.PrintBuff[11][30-17]  = '0';
				TXHData.PrintInfo.PrintBuff[11][30-16] = ((TXHData.PrintRecord.RTIME.Year>>4)&0x0f)+'0';TXHData.PrintInfo.PrintBuff[11][30-15] = ((TXHData.PrintRecord.RTIME.Year)&0x0f)+'0';
				TXHData.PrintInfo.PrintBuff[11][30-14]  = '-';
				TXHData.PrintInfo.PrintBuff[11][30-13]  = ((TXHData.PrintRecord.RTIME.Month>>4)&0x0f)+'0';TXHData.PrintInfo.PrintBuff[11][30-12] = ((TXHData.PrintRecord.RTIME.Month)&0x0f)+'0';
				TXHData.PrintInfo.PrintBuff[11][30-11]  = '-';
				TXHData.PrintInfo.PrintBuff[11][30-10]  = ((TXHData.PrintRecord.RTIME.Day>>4)&0x0f)+'0';TXHData.PrintInfo.PrintBuff[11][30-9] = ((TXHData.PrintRecord.RTIME.Day)&0x0f)+'0';

				TXHData.PrintInfo.PrintBuff[11][30-7]  = ((TXHData.PrintRecord.RTIME.Hour>>4)&0x0f)+'0';TXHData.PrintInfo.PrintBuff[11][30-6] = ((TXHData.PrintRecord.RTIME.Hour)&0x0f)+'0';
				TXHData.PrintInfo.PrintBuff[11][30-5]  = ':';
				TXHData.PrintInfo.PrintBuff[11][30-4]  = ((TXHData.PrintRecord.RTIME.Min>>4)&0x0f)+'0';TXHData.PrintInfo.PrintBuff[11][30-3] = ((TXHData.PrintRecord.RTIME.Min)&0x0f)+'0';
				TXHData.PrintInfo.PrintBuff[11][30-2]  = ':';
				TXHData.PrintInfo.PrintBuff[11][30-1]  = ((TXHData.PrintRecord.RTIME.Sec>>4)&0x0f)+'0';TXHData.PrintInfo.PrintBuff[11][30] = (TXHData.PrintRecord.RTIME.Sec & 0x0f)+'0';
				Print_Num(0,12,MD_Connect_u64ToBCD(TXHData.PrintRecord.OrderNumber),0);
				for(i = 0;i < 650;i++)
					Multiplex_Port_TX_Buff[i] = 0x20;
				Multiplex_Port_TX_Buff[0] = LF;
				Multiplex_Port_TX_Buff[1] = LF;
				for(i = 0;i< 20;i++)
				{
					for(j = 0;j< 32;j++)
						if(TXHData.PrintInfo.PrintBuff[i][j] != 0x20)
							break;
					if(j < 31)
					{
						for(j = 0;j< 32;j++)
							Multiplex_Port_TX_Buff[k*32+j] = TXHData.PrintInfo.PrintBuff[i][j];
						k ++;
						}
					}
				Multiplex_Port_TX_Buff[k*32+32+0] = LF;
				Multiplex_Port_TX_Buff[k*32+32+1] = LF;
				Multiplex_Port_TX_Buff[k*32+32+2] = LF;
				Multiplex_Port_TX_Buff[k*32+32+3] = LF;
				Multiplex_Port_TX_Buff[k*32+32+4] = LF;
				Multiplex_Port_TX_Buff[k*32+32+5] = LF;
				MD_ConnectTX_Len = k*32+32+5;
			  TXHData.MD_Multiplex_Port_Info.PrintType = PT_Null;
			  }
			}break;
		case Multiplex_YWY:
		{
			Multiplex_Port_Choose(1);
			Multiplex_Port_RS485_TxEN1;
			Multiplex_Port_USART_Configuration(TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].BaudRate,TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].Parity_Even);
			TXHData.MD_Multiplex_Port_Info.MBCountCutNum[GunNum] ++;
			//switch(TXHData.MD_Port_Data[PosTypeTemp].PortData.Device_Type)
			{
				//case YWY_Bluesky:
				{
					switch (MD_TX_CMD)
					{
						case YWY_MDConnectCmdReadData://读数据
						{
							for(i = 0;i < 7;i ++)
								Multiplex_Port_TX_Buff[i] = Data_201[i];
							MD_ConnectTX_Len = 7;
							}
							break;
						case YWY_MDConnectCmdWriteApacity://下载罐容表
						{
							for(i = 0;i < 7;i ++)
								Multiplex_Port_TX_Buff[i] = Data_20E[i];
							MD_ConnectTX_Len = 7;
							}
							break;
						}
					}//break;
				}
		  }break;
		case Multiplex_Price:
		{
			Multiplex_Port_Choose(2);
			Multiplex_Port_RS485_TxEN2;
			TXHData.MD_Multiplex_Port_Info.MBCountCutNum[PosTypeTemp] ++;
			Multiplex_Port_USART_Configuration(TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].BaudRate,TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].Parity_Even);
			switch (MD_TX_CMD)
			{
				case Price_MDConnectCmdWriteParameter://写参数
				{
					Multiplex_Port_TX_Buff[0] = 0xFE;
					Multiplex_Port_TX_Buff[1] = 0xFF;
					Multiplex_Port_TX_Buff[2] = 0x04;
					Multiplex_Port_TX_Buff[3] = 0x01;
					Multiplex_Port_TX_Buff[4] = 0x03;
					Tempu64 = Price_DataCheck(&Multiplex_Port_TX_Buff[2],Multiplex_Port_TX_Buff[2]-1);
					Multiplex_Port_TX_Buff[5] = Tempu64;
					Multiplex_Port_TX_Buff[6] = 0xEF;
					MD_ConnectTX_Len = 7;
					}
					break;
				case Price_MDConnectCmdReadParameter://读参数
				{
					Multiplex_Port_TX_Buff[0] = 0xFE;
					Multiplex_Port_TX_Buff[1] = 0xFF;
					Multiplex_Port_TX_Buff[2] = 0x04;
					Multiplex_Port_TX_Buff[3] = 0x01;
					Multiplex_Port_TX_Buff[4] = 0x02;
					Tempu64 = Price_DataCheck(&Multiplex_Port_TX_Buff[2],Multiplex_Port_TX_Buff[2]-1);
					Multiplex_Port_TX_Buff[5] = Tempu64;
					Multiplex_Port_TX_Buff[6] = 0xEF;
					MD_ConnectTX_Len = 7;
					}
					break;
				case Price_MDConnectCmdWriteDecimal://写小数位
				{
					Multiplex_Port_TX_Buff[0] = 0xFE;
					Multiplex_Port_TX_Buff[1] = 0xFF;
					Multiplex_Port_TX_Buff[2] = 16;
					Multiplex_Port_TX_Buff[3] = 0x01;
					Multiplex_Port_TX_Buff[4] = 0x04;
					for(j = 0;j < TXHData.MD_Multiplex_Port_Info.PriceTagData.PriceNumber;j ++)
					{
						Multiplex_Port_TX_Buff[5 + j] = TXHData.MD_Multiplex_Port_Info.PriceTagData.Length - TXHData.MD_Multiplex_Port_Info.PriceTagData.Decimal[j];
						}
					for(j = TXHData.MD_Multiplex_Port_Info.PriceTagData.PriceNumber;j < 17;j ++)
							Multiplex_Port_TX_Buff[5 + j] = 0;
					Tempu64 = Price_DataCheck(&Multiplex_Port_TX_Buff[2],Multiplex_Port_TX_Buff[2]-1);
					Multiplex_Port_TX_Buff[17] = Tempu64;
					Multiplex_Port_TX_Buff[18] = 0xEF;
					MD_ConnectTX_Len = 19;
					}
					break;
				case Price_MDConnectCmdWritePrice://写单价
				{
					Multiplex_Port_TX_Buff[0] = 0xFE;
					Multiplex_Port_TX_Buff[1] = 0xFF;
					Multiplex_Port_TX_Buff[2] = 40;
					Multiplex_Port_TX_Buff[3] = 0x01;
					Multiplex_Port_TX_Buff[4] = 0x01;
					for(i = 5;i < 40; i++)
					  Multiplex_Port_TX_Buff[i] = 0;
					for(i = 0;i < TXHData.MD_Multiplex_Port_Info.PriceTagData.PriceNumber; i++)
					{
					  for(j = 0;j < 3;j ++)
						{
							if(TXHData.MD_Multiplex_Port_Info.PriceTagData.Length/2 > j)
							  Multiplex_Port_TX_Buff[5+ j + (i * 3)] = TXHData.MD_Multiplex_Port_Info.PriceTagData.Price[i] >> ((TXHData.MD_Multiplex_Port_Info.PriceTagData.Length/2-1-j)*8);
							else
								Multiplex_Port_TX_Buff[5+ j + (i * 3)] = 0;
						  }
					  }
					Tempu64 = Price_DataCheck(&Multiplex_Port_TX_Buff[2],Multiplex_Port_TX_Buff[2]-1);
					Multiplex_Port_TX_Buff[41] = Tempu64;
					Multiplex_Port_TX_Buff[42] = 0xEF;
					MD_ConnectTX_Len = 43;
					}
					break;
				case Price_MDConnectCmdReadPrice://读单价
				{
					Multiplex_Port_TX_Buff[0] = 0xFE;
					Multiplex_Port_TX_Buff[1] = 0xFF;
					Multiplex_Port_TX_Buff[2] = 0x04;
					Multiplex_Port_TX_Buff[3] = 0x01;
					Multiplex_Port_TX_Buff[4] = 0x03;
					Tempu64 = Price_DataCheck(&Multiplex_Port_TX_Buff[2],Multiplex_Port_TX_Buff[2]-1);
					Multiplex_Port_TX_Buff[5] = Tempu64;
					Multiplex_Port_TX_Buff[6] = 0xEF;
					MD_ConnectTX_Len = 7;
					}
					break;
				}
		  }break;
		case Multiplex_GPS:
		{
			Multiplex_Port_Choose(3);
			TXHData.MD_Multiplex_Port_Info.MBCountCutNum[GunNum] ++;
			Multiplex_Port_USART_Configuration(TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].BaudRate,TXHData.MD_Multiplex_Port_Data.PortData[PosTypeTemp].Parity_Even);
		  }break;
	  }
	if(MD_ConnectTX_Len)
	{
		Multiplex_Port_TX_DMALenSet(MD_ConnectTX_Len);
		Multiplex_Port_TX_DMAStart();
		}
}

void Multiplex_PortRXTask(void)
{
	volatile u16 i = 0,j = 0,Temp = 0,DeviceNumber = 0;
	volatile u64 TempBCD = 0,Tempu64 = 0;
	volatile u8 MD_ConnectRX_Port = 0;     
	volatile u8 MD_ConnectRX_Len = 0;     //长度
	volatile u8 MD_ConnectRX_pGun = 0;     //地址枪号
	volatile u8 MD_ConnectRX_pGun_Sun = 0; //dart协议子枪号
	volatile u16 MD_ConnectRX_CMD = 0;     //命令
	u8 MD_ConnectRX_Check = 0;            //校验变量
	u8 Buff[50];
	u8 POSType_Temp = 0;
	volatile u32 CRC_temp = 0;
	volatile float TempF = 0;
	volatile u8 Tank_Num;
	u8 Data_201[7] = "i20100";
	u8 Data_20E[7] = "i20E00";
	
	if(TXHData.MD_MultiplexRXIDLETimer.Flag)
	{
		TXHData.MD_MultiplexRXIDLETimer.Flag = 0;
		if(TXHData.MD_Multiplex_Rec_NummberB == 1000 - DMA2_Stream1->NDTR)
		{
			TXHData.MD_Multiplex_Rec_Nummber = 1000 - DMA2_Stream1->NDTR;
			Multiplex_Port_RX_DMAStop();   //关闭DMA接收
			DMA_ClearFlag(DMA2_Stream1, DMA_IT_TCIF1);
			Multiplex_Port_RX_Flag = 1;            //使能Gas接收任务	
			Multiplex_Port_RX_DMALenSet(1000);//重置接受长度
			Multiplex_Port_RX_DMAStart();   //打开DMA接收
		 }
	  }
	if(Multiplex_Port_RX_Flag == 1)
	{
		Multiplex_Port_RX_Flag = 0;
		POSType_Temp = TXHData.MD_Multiplex_Port_Data.PortData[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].POSType;
		switch(POSType_Temp)
		{
			case Multiplex_Print:
			{
				TXHData.MD_MultiplexTimer.Flag = 1;
				TXHData.MD_MultiplexTimer.TimerCount = 0;
				}break;
			case Multiplex_YWY:
			{
				//switch(TXHData.MD_Multiplex_Port_Data.PortData[POSType_Temp].Device_Type)
				{
					//case YWY_Bluesky:
					{
						TempBCD = CheckSum(&Multiplex_Port_RX_Buff[1],TXHData.MD_Multiplex_Rec_Nummber - 6);
						Tempu64 = StrToInt(&Multiplex_Port_RX_Buff[TXHData.MD_Multiplex_Rec_Nummber - 5],4);
						if(Tempu64 == TempBCD)
						{
							TXHData.MD_Multiplex_Port_Info.MBCountCutNum[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] = 0;
							MD_ConnectRX_CMD = Multiplex_Port_RX_Buff[4];
							switch(MD_ConnectRX_CMD)
							{
								case YWY_MDConnectCmdReadData:
								{
									TXHData.TankNumTop = Tempu64 = (TXHData.MD_Multiplex_Rec_Nummber - 24) / 65;
									for(i = 0;i < Tempu64;i ++)
									{
										if(Multiplex_Port_RX_Buff[23 + i * 65] == 0)
										  TXHData.TankState[4][i].State = Multiplex_Port_RX_Buff[20 + i * 65];
										TempF = StrToFloat(&Multiplex_Port_RX_Buff[26 + i * 65], 8);
										if(Multiplex_Port_RX_Buff[26 + i * 65] != 0x3F)
											TXHData.TankData[4][i].Data.OV = TempF;
										else
											TXHData.TankData[4][i].Data.OV = 0;
										TempF = StrToFloat(&Multiplex_Port_RX_Buff[50 + i * 65], 8);
										if(Multiplex_Port_RX_Buff[50 + i * 65] != 0x3F)
											TXHData.TankData[4][i].Data.OH = TempF;
										else
											TXHData.TankData[4][i].Data.OH = 0;
										TempF = StrToFloat(&Multiplex_Port_RX_Buff[58 + i * 65], 8);
										if(Multiplex_Port_RX_Buff[58 + i * 65] != 0x3F)
											TXHData.TankData[4][i].Data.WH = TempF;
										else
											TXHData.TankData[4][i].Data.WH = 0;
										TempF = StrToFloat(&Multiplex_Port_RX_Buff[66 + i * 65], 8);
										if(Multiplex_Port_RX_Buff[66 + i * 65] != 0x3F)
											TXHData.TankData[4][i].Data.TH = TempF;
										else
											TXHData.TankData[4][i].Data.TH = 0;
										TXHData.MD_Multiplex_Port_Data.State[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].Flag.Online = 1;
										//TXHData.MD_Multiplex_Port_Info[MD_ConnectRX_Port].GunInfo[MD_ConnectRX_pGun].RSSI = Multiplex_Port_RX_Buff[TXHData.MD_Multiplex_Rec_Nummber - 1];
										}
									}break;
								case YWY_MDConnectCmdWriteApacity:
								{
									
									}break;
								}
							}
						}//break;
					}
			  TXHData.MD_MultiplexTimer.Flag = 1;	
				TXHData.MD_MultiplexTimer.TimerCount = 0;
			  }break;
			case Multiplex_Price:
			{
				MD_ConnectRX_Len = Multiplex_Port_RX_Buff[2];
				TempBCD = Price_DataCheck(&Multiplex_Port_RX_Buff[2],MD_ConnectRX_Len - 1);
				Tempu64 = Multiplex_Port_RX_Buff[MD_ConnectRX_Len+1];
				if(Tempu64 == TempBCD)
				{
					TXHData.MD_Multiplex_Port_Info.MBCountCutNum[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] = 0;
					TXHData.MD_MultiplexTimer.Flag = 1;
					TXHData.MD_MultiplexTimer.TimerCount = 0;
					MD_ConnectRX_CMD = Multiplex_Port_RX_Buff[4];
					switch (MD_ConnectRX_CMD)
					{
						case Price_MDConnectCmdWriteParameter://写参数
						{
              TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] = Price_MDConnectCmdReadPrice;
							}
							break;
						case Price_MDConnectCmdReadParameter://读参数
						{
							TXHData.MD_Multiplex_Port_Info.Handshake[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] = 1;
							TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] = Price_MDConnectCmdReadPrice;
              TXHData.MD_Multiplex_Port_Info.PriceTagData.State.Flag.Online = 1;
							TXHData.MD_Multiplex_Port_Info.PriceTagData.SingleOrDouble = Multiplex_Port_RX_Buff[7];
							TXHData.MD_Multiplex_Port_Info.PriceTagData.PriceNumber = Multiplex_Port_RX_Buff[8];
							TXHData.MD_Multiplex_Port_Info.PriceTagData.Length = Multiplex_Port_RX_Buff[9];
							TXHData.MD_Multiplex_Port_Info.PriceTagData.Brightness = Multiplex_Port_RX_Buff[11];
							}
							break;
						case Price_MDConnectCmdWritePrice://写单价
						{
             TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] = Price_MDConnectCmdReadPrice;
							}
							break;
						case Price_MDConnectCmdReadPrice://读单价
						{
							if(TXHData.MD_Multiplex_Port_Info.Handshake[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] < TXHData.MD_Multiplex_Port_Info.Handshake_Top[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] )  
							{
								TXHData.MD_Multiplex_Port_Data.State[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].Flag.UpdateDisplay = 1;
								TXHData.MD_Multiplex_Port_Info.Handshake[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] = TXHData.MD_Multiplex_Port_Info.Handshake_Top[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow];
							}
							TXHData.MD_Multiplex_Port_Data.State[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].Flag.Online = 1;
               for(i = 0;i < TXHData.MD_Multiplex_Port_Info.PriceTagData.PriceNumber; i++)
							{
								TXHData.MD_Multiplex_Port_Info.PriceTagData.Price[i] = 0;
								for(j = 0;j < TXHData.MD_Multiplex_Port_Info.PriceTagData.Length/2;j ++)
								{
									TXHData.MD_Multiplex_Port_Info.PriceTagData.Price[i] |= Multiplex_Port_RX_Buff[5 + j + i*TXHData.MD_Multiplex_Port_Info.PriceTagData.Length/2] << (8*(TXHData.MD_Multiplex_Port_Info.PriceTagData.Length/2 - 1 -j));
									}
								}
							}
							break;
						case Price_MDConnectCmdWriteDecimal://写小数位
						{
              TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow] = Price_MDConnectCmdWritePrice;
							}
							break;
						}
					}
				}break;
			case Multiplex_GPS:
			{
				TXHData.MD_MultiplexTimer.Flag = 1;
				TXHData.MD_MultiplexTimer.TimerCount = 0;
				Temp = mystrstr(Multiplex_Port_RX_Buff,"RMC");
				if(Temp && Temp < 800)
				{
					for(i = 0;i < 30;i ++)
					  Multiplex_Port_RX_Buff[i] = Multiplex_Port_RX_Buff[Temp + 14 + i];
					TXHData.USART6_Drive = GPS;
					if(Multiplex_Port_RX_Buff[0] == 'A')//则提取经度和纬度信息
					{
						for(i = 0;i < 11;i ++)// 提取经度类型信息，保存到TXHData结构体中
						{
							if(Multiplex_Port_RX_Buff[2+i] == ',')
								break;
							Buff[i] = Multiplex_Port_RX_Buff[2 + i];
						  }
						TempF = (float)atof(Buff) / 100;
						Tempu64 = TempF;
						TempF -= Tempu64;
						TXHData.GPSData.GPS.Longitude = Tempu64 + TempF * 100 / 60;
						TXHData.GPSData.GPS.LongitudeType = Multiplex_Port_RX_Buff[3 + i];
						Temp = 5 + i;
						for(i = 0;i < 11;i ++)// 提取维度类型信息，保存到TXHData结构体中
						{
							if(Multiplex_Port_RX_Buff[Temp + i] == ',')
								break;
							Buff[i] = Multiplex_Port_RX_Buff[Temp + i];
						  }
						TempF = (float)atof(Buff) / 100;
						Tempu64 = TempF;
						TempF -= Tempu64;
						TXHData.GPSData.GPS.Latitude = Tempu64 + TempF * 100 / 60;
						TXHData.GPSData.GPS.LatitudeType = Multiplex_Port_RX_Buff[Temp + i + 1];
							
						}
					else //则经纬度信息无效，置为0
					{
						TXHData.GPSData.GPS.Latitude = 0;
						TXHData.GPSData.GPS.LatitudeType = 0;
						TXHData.GPSData.GPS.Latitude = 0;
						TXHData.GPSData.GPS.LatitudeType = 0;
						}
					}
				else //则经纬度信息无效，置为0
				{
					TXHData.GPSData.GPS.Latitude = 0;
					TXHData.GPSData.GPS.LatitudeType = 0;
					TXHData.GPSData.GPS.Latitude = 0;
					TXHData.GPSData.GPS.LatitudeType = 0;
					}

				}break;
			}
		for(i = 0;i < 20;i ++)
		  Multiplex_Port_RX_Buff[i] = 0;
		}
}

