楼主免费送给你:
#define cHead1P 0
#define cHead2P 1
#define cHead3P 2
#define cDataHP 3
#define cDataLP 4
#define cHead4P 5
#define cHead5P 6
#define cHead6P 7
#define cHead1 0xFF
#define cHead2 0xFF
#define cHead3 0xFF
#define cHead4 0x00
#define cHead5 0x00
#define cHead6 0x00
#define cMAX_BUF_SIZE 200
typedef struct
{
uint8_t rxBuf[cMAX_BUF_SIZE];
uint8_t rxFlag;
uint8_t rxLen;
}RxMsg_Type;
typedef struct
{
uint8_t txBuf[cMAX_BUF_SIZE];
uint8_t txIndex;
uint8_t txLen;
}TxMsg_Type;
RxMsg_Type rxMsg;
TxMsg_Type txMsg;
bool sInvRxAnalysis(RxMsg_Type *pvRxMsg, uint16_t *pvRadarVal)
{
if(pvRxMsg->rxBuf[cHead1P] != cHead1 || pvRxMsg->rxBuf[cHead2P] != cHead2 || pvRxMsg->rxBuf[cHead3P] != cHead3 \
|| pvRxMsg->rxBuf[cHead4P] != cHead4 || pvRxMsg->rxBuf[cHead5P] != cHead5 || pvRxMsg->rxBuf[cHead6P] != cHead6)
{
return false;
}
if(pvRxMsg->rxLen != 8)
{
return false;
}
*pvRadarVal = pvRxMsg->rxBuf[cDataHP];
*pvRadarVal <<= 8;
*pvRadarVal += pvRxMsg->rxBuf[cDataLP];
return true;
}
void sInvRecvProcess(RxMsg_Type *pvRxMsg, uint8_t value)
{
static uint16_t index = 0;
switch( index )
{
case 0:
{
if(value == cHead1)
{
pvRxMsg->rxBuf[index++] = value;
}
break;
}
case 1:
{
if(value == cHead2)
{
pvRxMsg->rxBuf[index++] = value;
}
else
{
index = 0;
}
break;
}
case 2:
{
if(value == cHead3)
{
pvRxMsg->rxBuf[index++] = value;
}
else
{
index = 0;
}
break;
}
case 3:
case 4:
{
pvRxMsg->rxBuf[index++] = value;
break;
}
case 5:
{
if(value == cHead4)
{
pvRxMsg->rxBuf[index++] = value;
}
else
{
index = 0;
}
break;
}
case 6:
{
if(value == cHead5)
{
pvRxMsg->rxBuf[index++] = value;
}
else
{
index = 0;
}
break;
}
case 7:
{
if(value == cHead6)
{
pvRxMsg->rxBuf[index++] = value;
pvRxMsg->rxLen = index;
pvRxMsg->rxFlag = 1;
index = 0;
}
else
{
index = 0;
}
break;
}
default:
break;
}
}
void uctsk_com(void *p_arg)
{
uint16_t uwRadarVal;
if(rxMsg.rxFlag)
{
rxMsg.rxFlag = 0;
// 数据解析
if(sInvRxAnalysis(&rxMsg, &uwRadarVal))
{
// 数据处理
switch( uwRadarVal )
{
default:
break;
}
}
}
}
void sUSART2_ISR(void)
{
if(_BIT_TST(USART2->SR, 3))
{
_BIT_CLR(USART2->SR, 3);
}
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //数据接收中断
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
sInvRecvProcess(&rxMsg, USART_ReceiveData(USART2));
}
if(USART_GetITStatus(USART2, USART_IT_TC) != RESET) //数据发送完成中断
{
USART_ITConfig(USART2, USART_IT_TC, DISABLE);
USART_ClearITPendingBit(USART2, USART_IT_TC);
}
}
|