使用的CH573串口1接收數(shù)據(jù),每接收一字節(jié)數(shù)據(jù)要調(diào)用void uart_receive_input(unsigned char value)函數(shù)將輸入放入緩存區(qū)處理,字節(jié)數(shù)少的話處理起來(lái)沒(méi)問(wèn)題,但是如果接收的是一個(gè)256字節(jié)的數(shù)據(jù)包,接受的數(shù)據(jù)好像不完整,校驗(yàn)會(huì)不通過(guò),請(qǐng)問(wèn)要接收256字節(jié)的數(shù)據(jù)包,甚至512字節(jié),每個(gè)字節(jié)還要調(diào)用函數(shù)放入緩存區(qū),串口終端應(yīng)該怎樣寫(xiě)才能接收完整。
第一個(gè)函數(shù)是單字節(jié)暫存,第二個(gè)函數(shù)是多字節(jié)暫存,附上我的串口中斷函數(shù),請(qǐng)指出不足,謝謝。
void?uart_receive_input(unsigned?char?value)//單字節(jié)暫存 { ???? ????if(1?==?rx_buf_out?-?rx_buf_in)?{ ????????//串口接收緩存已滿 ????}else?if((rx_buf_in?>?rx_buf_out)?&&?((rx_buf_in?-?rx_buf_out)?>=?sizeof(wifi_uart_rx_buf)))?{ ????????//串口接收緩存已滿 ????}else?{ ????????//串口接收緩存未滿 ????????if(rx_buf_in?>=?(unsigned?char?*)(wifi_uart_rx_buf?+?sizeof(wifi_uart_rx_buf)))?{ ????????????rx_buf_in?=?(unsigned?char?*)(wifi_uart_rx_buf); ????????} ???????? ????????*rx_buf_in?++?=?value; ????} }
void?uart_receive_buff_input(unsigned?char?value[],?unsigned?short?data_len)//多字節(jié)暫存 { ???? ????unsigned?short?i?=?0; ????for(i?=?0;?i?<?data_len;?i++)?{ ????????uart_receive_input(value[i]); ????} }
__attribute__((interrupt("WCH-Interrupt-fast"))) __attribute__((section(".highcode"))) void?UART1_IRQHandler(?void?)????????//?UART1?中斷 { ????UINT8V?i?=?0; ????UINT8V?Recv; ????switch?(?UART1_GetITFlag()?) ????{ ????????case?UART_II_LINE_STAT?:????????//?線路狀態(tài)錯(cuò)誤 ????????{ ????????????UART1_GetLinSTA(); ????????????break; ????????} ????????case?UART_II_RECV_RDY?:??????????//?數(shù)據(jù)達(dá)到設(shè)置觸發(fā)點(diǎn) ????????????for?(?i?=?0?;?i?!=?trigB_UART_Wifi;?i++?) ????????????{ ????????????????Recv?=?R8_UART1_RBR; ????????????????uart_receive_input(Recv); ????????????} ????????break; ????????case?UART_II_RECV_TOUT?:?????????//?接收超時(shí),暫時(shí)一幀數(shù)據(jù)接收完成 ????????????i?=?UART1_RecvString(?Wifi_RxBuff?); ????????????uart_receive_buff_input(Wifi_RxBuff,i); ????????????memset(Wifi_RxBuff,0,i); ????????break; ????????case?UART_II_THR_EMPTY?:?????????//?發(fā)送緩存區(qū)空,可繼續(xù)發(fā)送 ????????break; ????????case?UART_II_MODEM_CHG?:?????????//?只支持串口0 ????????break; ????????default?: ????????break; ????} }