appReceiver ()函数
static void appReceiver()
{
uint32 segNumber=0; // 数据包序列号
int16 perRssiBuf[RSSI_AVG_WINDOW_SIZE] = {0}; // Ring buffer for RSSI 存储RSSI的环形缓冲区
uint8 perRssiBufCounter = 0; // Counter to keep track of the 计数器用于RSSI缓冲区统计
// oldest newest byte in RSSI
// ring buffer
perRxStats_t rxStats = {0,0,0,0};
int16 rssi;
uint8 resetStats=FALSE;
int8 strPer[5]; //用于串口发送之前的数据处理---掉包率
int8 strRssi[2]; //用于串口发送之前的数据处理---接收的包的个数
int8 strCount[4]; //用于串口发送之前的数据处理---rssi值
int32 nPer; //存放掉包率
int32 nRssi; //存放前32个rssi值的平均值
int32 nCount; //存放接收的包的个数
initUART(); // 初始化串口
#ifdef INCLUDE_PA
uint8 gain;
// Select gain (for modules with CC2590/91 only)
gain =appSelectGain();
halRfSetGain(gain);
#endif
// Initialize BasicRF
basicRfConfig.myAddr = RX_ADDR;
if(basicRfInit(&basicRfConfig)==FAILED) /* 初始化 BasicRF 结构体数据*/
{
HAL_ASSERT(FALSE);
}
basicRfReceiveOn();
//halLcdClear();
//halLcdWriteCharString(0,HAL_LCD_LINE_1, "Mode:Receiver");
//halLcdWriteCharString(0,HAL_LCD_LINE_3, "Ready");
/* 主循环 */
UartTX_Send_String("PER_test: ",strlen("PER_test: "));
// Main loop
while (TRUE)
{
while(!basicRfPacketIsReady()); // 等待新的数据包
if(basicRfReceive((uint8*)&rxPacket, MAX_PAYLOAD_LENGTH, &rssi)>0) {
halLedSet(3);//P1_4
// Change byte order from network to host order
UINT32_NTOH(rxPacket.seqNumber); // 改变接收序号的字节顺序
segNumber = rxPacket.seqNumber;
// If statistics is reset set expected sequence number to
// received sequence number 若统计被复位,设置期望收到的数据包序号为已经收到的数据包序号
if(resetStats)
{
rxStats.expectedSeqNum = segNumber;
resetStats=FALSE;
}
// Subtract old RSSI value from sum
rxStats.rssiSum -= perRssiBuf[perRssiBufCounter]; // 从sum中减去旧的RSSI值
// Store new RSSI value in ring buffer, will add it to sum later
perRssiBuf[perRssiBufCounter] = rssi; // 存储新的RSSI值到环形缓冲区,之后它将被加入sum
rxStats.rssiSum += perRssiBuf[perRssiBufCounter]; // 增加新的RSSI值到sum
if(++perRssiBufCounter == RSSI_AVG_WINDOW_SIZE) {
perRssiBufCounter = 0; // Wrap ring buffer counter
}
// 检查接收到的数据包是否是所期望收到的数据包
if(rxStats.expectedSeqNum == segNumber) // 是所期望收到的数据包
{
rxStats.expectedSeqNum++;
}
// 不是所期望收到的数据包(大于期望收到的数据包的序号)认为丢包
else if(rxStats.expectedSeqNum < segNumber)
{
rxStats.lostPkts += segNumber - rxStats.expectedSeqNum;
rxStats.expectedSeqNum = segNumber + 1;
}
// If the sequence number is lower than the previous one, we will assume a
// new data burst has started and we will reset our statistics variables.
else // (小于期望收到的数据包的序号)
{
// Update our expectations assuming this is a new burst 认为是一个新的测试开始,复位统计变量
rxStats.expectedSeqNum = segNumber + 1;
rxStats.rcvdPkts = 0;
rxStats.lostPkts = 0;
}
rxStats.rcvdPkts++;
// reset statistics if button 1 is pressed
nCount = (int32)rxStats.rcvdPkts;
if(nCount > 1000)
{
if(halButtonPushed()==HAL_BUTTON_1){
resetStats = TRUE;
rxStats.rcvdPkts = 1;
rxStats.lostPkts = 0;
}
}
//串口输出
strCount[0]=nCount/100+'0';
strCount[1]=nCount%100/10+'0';
strCount[2]=nCount%10+'0';
strCount[3]='\0';
UartTX_Send_String("RECV:", strlen("RECV:"));
UartTX_Send_String(strCount, 4);
UartTX_Send_String(" ", strlen(" "));
nPer = (int32)((rxStats.lostPkts*1000)/(rxStats.lostPkts+rxStats.rcvdPkts));
strPer[0]=nPer/100+'0';
strPer[1]=nPer%100/10+'0';
strPer[2]='.';
strPer[3]=nPer%10+'0';
strPer[4]='%';
UartTX_Send_String("PER:",strlen("PER:"));
UartTX_Send_String(strPer,5);
UartTX_Send_String(" ",strlen(" "));
nRssi=(0-(int32)rxStats.rssiSum/32);
strRssi[0]=nRssi/10+'0';
strRssi[1]=nRssi%10+'0';
UartTX_Send_String(" RSSI:-",strlen(" RSSI:-"));
UartTX_Send_String(strRssi,2);
UartTX_Send_String("\r\n",strlen("\r\n"));
halLedClear(3);
halMcuWaitMs(300);
// Update LCD
// PER in units per 1000
/* utilLcdDisplayValue(HAL_LCD_LINE_1, "PER: ", (int32)((rxStats.lostPkts*1000)/(rxStats.lostPkts+rxStats.rcvdPkts)), " /1000");
utilLcdDisplayValue(HAL_LCD_LINE_2, "RSSI: ", (int32)rxStats.rssiSum/32, "dBm");
#ifndef SRF04EB
utilLcdDisplayValue(HAL_LCD_LINE_3, "Recv'd: ", (int32)rxStats.rcvdPkts, NULL);
#endif
halLedClear(3);*/
}
}
}
|