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

📄 motetest.c.bak

📁 主要用于无线传感网络的编写的书籍.对于初学者有着很大的用处
💻 BAK
字号:
/*
 * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 
 * By downloading, copying, installing or using the software you 
 * agree to this license. If you do not agree to this license, do 
 * not download,install, copy or use the software.
 *
 * Intel Open Source License
 *
 * Copyright (c) 2003 Intel Corporation
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * (1) Redistributions of source code must retain the above copyright
 * notice, this list of conditions, the author names, and the 
 * following disclaimer.
 * (2) Redistributions in binary form must reproduce the above 
 * copyright notice, this list of conditions and the following 
 * disclaimer in the documentation and/or other materials provided 
 * with the distribution.
 * (3) Neither the name of the Intel Corporation nor the names of its
 * contributors may be used to endorse or promote products derived 
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL 
 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

/************************************************************
 * Sample code for Stargate-Mote serial communication
 * Vijay Raghunathan, August 2003
 * Modified by Jaidev Prabhu for non-blocking fixes, Feb 2004
 ************************************************************/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdlib.h>

/* Serial port information for talking to the mote */
#define TOS_PACKET_LENGTH 36 		/* Default TOS packet length */
#define SERIAL_PORT "/dev/tts/2" 	/* Default serial port to use */
//#define SERIAL_PORT "/dev/ttyS2" 	/* Default serial port to use */
#define MICA1_BAUDRATE B19200		/* MICA1 baudrate */
#define MICA2_BAUDRATE B57600		/* MICA2 baudrate */
#define DEFAULT_BAUDRATE B57600		/* Default is MICA2 */

/* Functions */
void usage (char *);
void parse_opts (int, char **); /* Parse command line options */
void open_serial (void);	/* Open a serial channel to mote */
void read_packet (void);	/* Read a packet from the mote */
void create_packet(char *);     /* Create TOS packet */
void write_packet(void);	/* Write a packet to the mote */
void print_packet (char *);	/* Print mote packet to stdout */

/* Global variables */
int inout_stream;
char input_buffer[TOS_PACKET_LENGTH];
char output_buffer[TOS_PACKET_LENGTH];
char *serial_port = SERIAL_PORT;
char mote = 2, charflag = 0;

void usage(char *name) {
  fprintf(stderr,"Usage: %s [-h] [-c] [-m 1|2] [-p /dev/ttySXXX]\n",name);
  fprintf(stderr,"       -c              : Print packet data as char (default is int)\n");
  fprintf(stderr,"       -m 1            : Use BAUDRATE settings for MICA1\n");
  fprintf(stderr,"       -m 2            : Use BAUDRATE settings for MICA2 (default)\n");
  fprintf(stderr,"       -p /dev/ttySXXX : Talk to mote on /dev/ttySXXX (default is /dev/ttyS2)\n");
  fprintf(stderr,"       -h              : Print usage information\n");
  exit(-1);
}

void parse_opts(int argc,char **argv) {
  int opt;

  while ((opt = getopt(argc,argv,"hcm:p:")) != EOF) {
    switch(opt) {
    case 'h':
      usage(argv[0]);
      break;
    case 'c':
      charflag = 1;
      break;
    case 'm':
      mote = atoi(optarg);
      if ((mote != 1) && (mote != 2))
	usage(argv[0]);
      break;
    case 'p':
      serial_port = optarg;
      break;
    default:
      usage(argv[0]);
    }
  }
}

int main(int argc, char ** argv) {
  int index = 0, count = 10;

  /* Parse command line arguments */
  parse_opts(argc,argv);

  /* Open serial channel to mote */
  open_serial();

  while (index < count) {

    /* Create the TOS packet. */
    create_packet("The first (TOS_PACKET_LENGTH-7) characters of this string form the outgoing TOS packet data");
    
    /* Write the output buffer to the mote */
    write_packet();
    
    /* Read packet from mote into input buffer */
    /* Note: The read blocks till a mote packet is received. 
       An alternative is to use "poll" to check for incoming data */
    read_packet(); 
    
    /* Dump packet onto stdout */
    print_packet(input_buffer);

    index++;
  }

  exit(0);

}

/* Open serial channel to mote */ 
void open_serial() {
  struct termios tio;
  inout_stream = open(serial_port, O_RDWR|O_NOCTTY);
  if (inout_stream < 0) {
    fprintf(stderr,"Cannot open serial device %s\n", serial_port);
    exit(-1);
  }

  /* Serial port setting */
  bzero(&tio, sizeof(tio));
  tio.c_iflag &= ~(IGNPAR | BRKINT | PARMRK | ISTRIP 
                   | INLCR | IGNCR | ICRNL | IXON);
  
  tio.c_cflag &= ~(CSIZE | PARENB);
  if (mote == 1)
    tio.c_cflag |= MICA1_BAUDRATE | CS8 | CLOCAL | CREAD;
  else if (mote == 2)
    tio.c_cflag |= MICA2_BAUDRATE | CS8 | CLOCAL | CREAD;
  else
    tio.c_cflag |= DEFAULT_BAUDRATE | CS8 | CLOCAL | CREAD;

  /* Raw output_file */
  tio.c_oflag &= ~OPOST;

  tio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  
  /* Flush buffer; parameters take effect immediately */
  tcflush(inout_stream, TCIFLUSH);
  tcsetattr(inout_stream, TCSANOW, &tio);

  /* Enable non-blocking IO */
  fcntl(inout_stream, F_SETFL, O_NONBLOCK);

}

/* Read TOS packet from mote */
void read_packet() {
  int count;
  bzero(input_buffer, TOS_PACKET_LENGTH);
  //Find start of packet
  while(input_buffer[0] != (char)(0x7E)){
    while(1 != read(inout_stream, input_buffer, 1)){};
  } 
  count = 1;
  //Read packet contents
  while(count < TOS_PACKET_LENGTH)
    count += read(inout_stream, input_buffer + count, TOS_PACKET_LENGTH - count); 	
}

/* Print packet to stdout */
void print_packet(char *packet) {
  int i;
  printf("Raw TOS packet contents: ");
  for(i = 0; i < TOS_PACKET_LENGTH; i ++) {
    if (charflag)
      printf("%c ", (char) (packet[i] & 0xff));
    else
      printf("%d ", (char) (packet[i] & 0xff));
  }
  printf("\n");
}

/* Create TOS packet to send to mote */
void create_packet(char *message) {
  /* Setup packet header information */
  output_buffer[0] = 0xff; 	//Destination address
  output_buffer[1] = 0xff; 	//Destination address
  output_buffer[2] = 0x0a; 	//AM type
  output_buffer[3] = 0x7d; 	//AM group
  output_buffer[4] = TOS_PACKET_LENGTH-7;//Payload length

  /* Fill packet data */
  output_buffer[5] = '\0';
  strncat(output_buffer,message, TOS_PACKET_LENGTH-7);

  /* CRC bytes */
  output_buffer[TOS_PACKET_LENGTH-2] = 0xff;
  output_buffer[TOS_PACKET_LENGTH-1] = 0xff;

}

/* Send packet to mote */
void write_packet() {
  int i;
  i = write (inout_stream, output_buffer, TOS_PACKET_LENGTH);
  if (i != TOS_PACKET_LENGTH)
    fprintf(stderr,"UART send screwed up!\n");
  else
    fprintf(stderr,"Packet sent to mote over UART.\n");
}

⌨️ 快捷键说明

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