iT邦幫忙

0

UART接收端資料移漏

  • 分享至 

  • xImage

各位好:
最近在研究MCU與感測器結合應用,使用UART進行資料接收,但發現接收端好像來不急接收感測器端吐出來的資料,目前設定的情況是我使用Uart中斷接收的方式,buadrate設定為115200,感測器由0開始計數,但接收端的部份中間固定有幾比資料會遺失,請問能否協助解答?
UART DataBuff[0] = 0x2
UART DataBuff[0] = 0x5
UART DataBuff[0] = 0x8
UART DataBuff[0] = 0x11
UART DataBuff[0] = 0x14
UART DataBuff[0] = 0x17
UART DataBuff[0] = 0x20
UART DataBuff[0] = 0x23

void HAL_UART_RxCpltCallback(UART_HandleTypeDef*UartHandle)
{


		RxLine++;                      
	    DataBuff[RxLine-1]=RxBuff[0];  

	    if(DataBuff[RxLine-1]==0xff && DataBuff[RxLine-2]==0xff && DataBuff[RxLine-3]==0xff && DataBuff[RxLine-4]==0xff) 
	    {
	      // printf("tail_confuon!!! \r\n");
	       if(RxLine == 8)    
	       {
	    	   printf("RXLen=%d\r\n",RxLine);
	    	   for(int i=0;i<RxLine;i++)
	    	   {
	    		   printf("UART DataBuff[%d] = 0x%d\r\n",i,DataBuff[i]);
	    	   }
	    	   memset(DataBuff,0,sizeof(DataBuff)); 
	    	   RxLine=0;  
	       }

	       else if(RxLine > 8)
	       {
	    	  // printf("RxLine_overload!!! \r\n");
	    	   memset(DataBuff,0,sizeof(DataBuff));  
	    	   RxLine=0;  
	       }
	    }
        
	    RxBuff[0]=0;
	    HAL_UART_Receive_IT(&huart2, (uint8_t *)RxBuff, 1); 
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
YC
iT邦研究生 2 級 ‧ 2022-08-10 10:11:46

我沒用過ARM這麼高階的MCU,只用過AVR。
你需要先確定MCU的datasheet,裡面支援115200的baud rate嗎?裡面有提到容錯率多少?
另外,datasheet有沒有提到需要外接 crystal 震盪器?

我記得 datasheet 裡會有一整章表格跟公式,在討論電路跟設定對 baud rate 的穩定跟影響。

補充:
你可以直接寫一個while在main中,
連續讀rx的值,看值是不是連續。

我要發表回答

立即登入回答