⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packet_test.c

📁 主要用于无线传感网络的编写的书籍.对于初学者有着很大的用处
💻 C
字号:
#include "../layers/physical/serial_connect.h"#include "../debug/debug.h"#include "../layers/network/packet.h"#include <stdio.h>   /* Standard input/output definitions */#include <string.h>  /* String function definitions */#include <unistd.h>  /* UNIX standard function definitions */#include <fcntl.h>   /* File control definitions */#include <errno.h>   /* Error number definitions */#include <termios.h> /* POSIX terminal control definitions *//******************************************************************* * To test this file, install the "sender" application on the mote * that you'll have connected to the serial port. * * To test layers other than the network layer, comment the network * layer section, and uncomment one of the others * * Currently, I use argv[1] as the serial port, so you should run  * this program like: *   ptest /dev/ttyS0 *******************************************************************/int main(int argc, char *argv[]) {  int sfd;  networkPkt p;  frame f;  int insync = 0;  char temp = '\0';  int i;  setupDebug(DBG_ALL, CRITICAL);  if (sfd = openSerialPort(argv[1]) < 0) {    exit(1);  }  /* test physical   while (1) {    while (1) {      if (insync == FALSE) {        dbg(DBG_USR, AVERAGE, "not insync, resyncronizing\n");        while (read(sfd, &temp, 1) == 1) {          if (temp == SYNC_BYTE) {             printf("\n %02x", (temp & 0xFF));            break;          }        }        dbg(DBG_USR, FRIVOLOUS, "now we're in sync, on with the show\n");        insync = TRUE;      }      if (read(sfd, &temp, 1) == 1) {        printf(" %02x", (temp & 0xFF));        if (temp == SYNC_BYTE) {          insync = FALSE;          break;        }      }      else { // error reading byte from serial port, resyncronize        dbg(DBG_USR, AVERAGE, "error reading byte: readFrame resyncronizes\n");        insync = FALSE;      }    }    // if we got here, we read an entire frame    printf("\n");  }  */    /* test link  for (i=0; i<50; i++) {    if (readFrame(sfd, &f) == FAILURE) {      printf("COULD NOT READ FRAME!!!!");    }    else {      printf("Frame read successfully, printing...\n");      printFrame(DBG_ALL, CRITICAL, &f);      printf("Trying frame in handleFrame....\n");      if (handleFrame(&f) != GOODPACKET) {        printf("GOT DROPPACKET FROM HANDLEFRAME!!\n");      }      else {        printf("frame passes handleFrame function\n");      }    }  }  */  /* test packets */  p.hdr.address = 0xFFFF;  p.hdr.port = 12;  p.hdr.group = 1;  p.hdr.length = 3;  p.data[0] = 0xFF;  p.data[1] = 0xFF;  p.data[2] = 0xFF;    if (writeNetworkPkt(sfd, &p) != SUCCESS) {      printf("COULD NOT WRITE NETWORK PACKET!!!\n");    }    else {      printf("Packet successfully written\n");    }    if (readNetworkPkt(sfd, &p) != SUCCESS) {      printf("COULD NOT READ PACKET!!!!\n");    }    else {      printf("Packet successfully read\n");      printPacket(DBG_USR, CRITICAL, stdout, &p);    }  closeSerialPort(sfd);  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -