
#include "ScreenMaster485.h"
#include "usart.h"			
#include "Dwin.h"
#include "cmsis_os.h"

////////////////////////////////////////////////////////////////////////////////// 	

void TestUsart3_Receive(void)
{
	if((usart3_rx_flag)&&(USART3_RX_BUF[0]==0x12))
	{
		usart3_rx_flag=0;
		ScreenMaster485_Send_Data(USART3_RX_BUF,rx3_len);
	}
	
	
	
}
//RS485发送len个字节.
//buf:发送区首地址
//len:发送的字节数(为了和本代码的接收匹配,这里建议不要超过64个字节)
uint8_t screen_buf[512];
volatile uint8_t screen_delay=0;
void ScreenMaster485_Send_Data(uint8_t *buf,uint8_t len)
{
//	int retry;
	
	do
	{
		osDelay(1);
	}while(huart3.gState != HAL_UART_STATE_READY);
	do
	{
		osDelay(1);
	}while(screen_delay);	
	usart3_rx_flag=0;
	ScreenMaster485_TX_Set(1);			//设置为发送模式
  //	HAL_UART_Transmit(&huart3,buf,len,200);//串口3发送数据  
	memcpy(screen_buf,buf,len);
	if(HAL_UART_Transmit_IT(&huart3,screen_buf,len)!=HAL_OK)
	{
		ScreenMaster485_TX_Set(0);			//设置为接收模式	
	}
}

//RS485模式控制.
//en:0,接收;1,发送.
void ScreenMaster485_TX_Set(uint8_t en)
{
	if(en==1)HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15,GPIO_PIN_RESET);
	else HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15,GPIO_PIN_SET);
}
