uartdebugserver.c

来自「tinyos最新版」· C语言 代码 · 共 137 行

C
137
字号
/* uartDebugServer.c * * This program receives debugging bytes from each node via a UDP socket, * and save them into a log file that can be parsed later by uartDebugParser.c. * If a state-event table is included, it will display debugging message  * on the screen. Otherwise it will only display raw data. *  * To use this program, you need to connect each mote to a PC, and run  * uartByte.c on the PC. On the mote side, you need to define UART_DEBUG_ENABLE * and include uartDebug.h in the component you want to debug. *  * Author: Wei Ye (USC/ISI) * Date: 03/10/2003 * */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/time.h>#include <time.h>// if debug another component, comment out following line// or include a corresponding debug table#include "smacDebugTab.h"#define MYPORT 5322    // the port users will be connecting to#define MAXBUFLEN 100int main(int argc, char ** argv){   int sockfd;   struct sockaddr_in my_addr;    // my address information   struct sockaddr_in their_addr; // connector's address information   int addr_len, numbytes, i;   char buf[MAXBUFLEN];   // time stamping   long startTime;  // starting time in second   time_t timep;   struct timeval sTime;   struct timeval timeStamp;   long totalTime;   uint8_t nodeId, days, hours, minutes, seconds, milisec;   FILE *logFile;   if (argc != 2) {      printf("Usage: uartDebugServer logFile\n");      exit(1);   }      if ((logFile = fopen(argv[1], "w")) == NULL) {      printf("Error: can't open log file.\n");      exit(1);   }         // record starting time   timep = time(NULL);   printf("\nTesting starts at %s\n", ctime(&timep));   fprintf(logFile, "Testing starts at %s\n", ctime(&timep));   gettimeofday(&sTime, NULL);  // record starting time   startTime = sTime.tv_sec;   if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {      perror("socket");      exit(1);   }   my_addr.sin_family = AF_INET;         // host byte order   my_addr.sin_port = htons(MYPORT);     // short, network byte order   my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP   memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct   if (bind(sockfd, (struct sockaddr *)&my_addr,      sizeof(struct sockaddr)) == -1) {      perror("bind");      exit(1);   }   addr_len = sizeof(struct sockaddr);   // recvfrom will block if no packet arrives   while (1) {      if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,	     (struct sockaddr *)&their_addr, &addr_len)) == -1) {         perror("recvfrom");         exit(1);      }         // get time stamp      gettimeofday(&timeStamp, NULL);      totalTime = timeStamp.tv_sec - startTime; // total seconds      seconds = (uint8_t)(totalTime % 60);      totalTime = (totalTime - seconds) / 60;   // total minutes      minutes = (uint8_t)(totalTime % 60);      totalTime = (totalTime - minutes) / 60;   // total hours      //hours = (uint8_t)(totalTime % 24);      //days = (uint8_t)(totalTime - hours) / 24; // total days      hours = (uint8_t)(totalTime);      milisec = (uint16_t)(timeStamp.tv_usec/10000);         //nodeId = *((uint8_t*)(&their_addr.sin_addr.s_addr) + 3) - 40; // for ISI      nodeId = *((uint8_t*)(&their_addr.sin_addr.s_addr) + 3);            printf("Node %d at %02d:%02d:%02d.%02d\n",             nodeId, hours, minutes, seconds, milisec);      //fprintf(logFile, "%d %d %d %d %d\n",      //      nodeId, hours, minutes, seconds, milisec);      fprintf(logFile, "%d\n", nodeId);      fprintf(logFile, "%d\n", numbytes);                   // print out debugging info      for(i = 0; i < numbytes; i++){         uint8_t msgNo = (uint8_t)buf[i];#ifdef STATE_EVENT         if (msgNo >= sizeof(stateEvent)) exit(1);         printf("   %s\n", stateEvent[msgNo]);#else         printf("   %d\n", msgNo);#endif         fprintf(logFile, "%d\n", msgNo);      }      printf("\n");      fprintf(logFile, "\n");      fflush(logFile);   }   close(sockfd);   fclose(logFile);   return 0;}

⌨️ 快捷键说明

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