N-sim
Emulation and simulation of
Wireless Sensor Networks



   Home


   Project Page


   Download


   CVS



   Installation


   Configuration


   Plug-ins




 Hosted by
SourceForge.net Logo

/home/brennan/n-sim/Vaike/linux/system-addons/drivers/raw_magnetic.c

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright 2007. Los Alamos National Security, LLC. This material
00016  * was produced under U.S. Government contract DE-AC52-06NA25396 for
00017  * Los Alamos National Laboratory (LANL), which is operated by Los
00018  * Alamos National Security, LLC, for the Department of Energy. The
00019  * U.S. Government has rights to use, reproduce, and distribute this
00020  * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
00021  * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL
00022  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
00023  * produce derivative works, such modified software should be clearly
00024  * marked, so as not to confuse it with the version available from LANL.
00025  *
00026  * Additionally, this program is free software; you can redistribute
00027  * it and/or modify it under the terms of the GNU General Public
00028  * License as published by the Free Software Foundation; version 2 of
00029  * the License. Accordingly, this program is distributed in the hope
00030  * it will be useful, but WITHOUT ANY WARRANTY; without even the
00031  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00032  * PURPOSE. See the GNU General Public License for more details.
00033  */
00034 
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <unistd.h>
00039 #include <string.h>
00040 #include <fcntl.h>
00041 #include <errno.h>
00042 #include <termios.h>
00043 #include <sys/ioctl.h>
00044 
00045 /*
00046 #ifdef __cplusplus
00047     #define __CPPARGS ...
00048 #else
00049     #define __CPPARGS
00050 #endif
00051 
00052 
00053 int buffer_depth=100;
00054 unsigned base_address,IER,IIR,LCR,MCR,LSR,MSR,DIR;
00055 volatile unsigned buffer_count=0, buffer_overflow=0;
00056 volatile char serial_buffer[100];
00057 volatile char *head_pointer=serial_buffer;
00058 volatile char *tail_pointer=serial_buffer;
00059 */
00060 struct reading {
00061   union data_set{
00062     int integer;
00063     unsigned char input[2];
00064   } x,y,z;
00065 };
00066 
00067 
00068 
00069 static void set_DTR(int fd)
00070 {
00071   int status;
00072   ioctl(fd, TIOCMGET, &status);
00073   status |= TIOCM_DTR;
00074   ioctl(fd, TIOCMSET, &status);
00075 }
00076 
00077 static void clear_DTR(int fd)
00078 {
00079   int status;
00080   ioctl(fd, TIOCMGET, &status);
00081   status &= ~TIOCM_DTR;
00082   ioctl(fd, TIOCMSET, &status);
00083 }
00084 
00085 static void set_RTS(int fd)
00086 {
00087   int status;
00088   ioctl(fd, TIOCMGET, &status);
00089   status |= TIOCM_RTS;
00090   ioctl(fd, TIOCMSET, &status);
00091 
00092 }
00093 
00094 static void clear_RTS(int fd)
00095 {
00096   int status;
00097   ioctl(fd, TIOCMGET, &status);
00098   status &= ~TIOCM_RTS;
00099   ioctl(fd, TIOCMSET, &status);
00100 }
00101 
00102 
00103 
00104 static int get_data_set(int fd, struct reading *data, unsigned char sensor)
00105 {
00106   unsigned char scrap;
00107   int degree_counter;
00108   int error = 0;
00109   char sensor_num[1];
00110 
00111   sensor_num[0] = sensor+48;
00112 
00113   set_RTS(fd);          // RS485 Transmitter enabled.
00114   clear_DTR(fd);        // RS485 Receiver disabled.
00115   
00116   write(fd, "*", 1);
00117   usleep(5000);
00118   write(fd, "0", 1);
00119   usleep(5000);
00120   write(fd, sensor_num, 1);
00121   usleep(5000);
00122   write(fd, "P", 1);    // Polled output mode
00123   usleep(5000);
00124   write(fd, "\r", 1);   // <cr>
00125   
00126   usleep(2000);
00127 
00128   set_DTR(fd);         // RS485 Receiver enabled.
00129   clear_RTS(fd);       // RS485 Transmitter disabled.
00130   
00131   usleep(100000);
00132   data->x.integer=0;            // initialize x, y, z to zero
00133   data->y.integer=0;
00134   data->z.integer=0;
00135 
00136   if ((error = read(fd, &data->x.input[1], 1)) <= 0)
00137     return error;
00138   if ((error = read(fd, &data->x.input[0], 1)) <= 0)
00139     return error;
00140   if ((error = read(fd, &data->y.input[1], 1)) <= 0)
00141     return error;
00142   if ((error = read(fd, &data->y.input[0], 1)) <= 0)
00143     return error;
00144   if ((error = read(fd, &data->z.input[1], 1)) <= 0)
00145     return error;
00146   if ((error = read(fd, &data->z.input[0], 1)) <= 0)
00147     return error;
00148   error = read(fd, &scrap, 1);          // Read the <cr>.
00149   
00150   return error;
00151 }
00152 
00153 
00154 
00155 static int open_magnetometer(char *device, int baudrate)
00156 {
00157   int fd = -1;
00158   struct termios options;
00159 
00160   if ((fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
00161     char temp[24];
00162     sprintf(temp, "open %s", device);
00163     perror(temp);
00164     return fd;
00165   }
00166 
00167   fcntl(fd, F_SETFL, 0);
00168   tcgetattr(fd, &options);
00169 
00170   cfsetispeed(&options, baudrate);
00171   cfsetospeed(&options, baudrate);
00172   options.c_cflag |= (CLOCAL | CREAD);
00173 
00174   tcsetattr(fd, TCSANOW, &options);
00175   return fd;
00176 }
00177 
00178 void initialize_magnetometer(int fd)
00179 {
00180   write(fd, "\033", 1);   // ESC key (33 octal=1B hex)
00181   usleep(5000);
00182   write(fd, "\033", 1);   // ESC key (33 octal=1B hex)
00183   usleep(5000);
00184   write(fd, "\033", 1);   // ESC key (33 octal=1B hex)
00185   usleep(5000);
00186   write(fd, "\r", 1);   // <cr>
00187   usleep(5000);
00188   write(fd, "*", 1);
00189   usleep(5000);
00190   write(fd, "9", 1);
00191   usleep(5000);
00192   write(fd, "9", 1);
00193   usleep(5000);
00194   write(fd, "W", 1);    // Write Enable
00195   usleep(5000);
00196   write(fd, "E", 1);
00197   usleep(5000);
00198   write(fd, "\r", 1);   // <cr>
00199   usleep(55000);
00200   write(fd, "*", 1);
00201   usleep(5000);
00202   write(fd, "9", 1);
00203   usleep(5000);
00204   write(fd, "9", 1);
00205   usleep(5000);
00206   write(fd, "B", 1);    // Binary output mode
00207   usleep(5000);
00208   write(fd, "\r", 1);   // <cr>
00209   usleep(5000);
00210 }
00211 
00212 
00213 
00214 int main(int argc, char *argv[])
00215 {
00216   int i, fd, baudrate = B9600;
00217   unsigned count;
00218   char *device = "/dev/ttyS0";
00219 
00220   if (argc != 3 || argc != 5) {
00221     fprintf(stderr, "Usage: %s [-d /dev/ttySx] [-b 9600 | 19200]\n", argv[0]);
00222     exit(-1);
00223   }
00224   for (i = 0; i < argc; i++) {
00225     if (strncmp(argv[i], "-d", 2) == 0)
00226       device = argv[++i];
00227     if (strncmp(argv[i], "-b", 2) == 0) {
00228       char *baud = argv[++i];
00229       if (strncmp(baud, "9600", 4) == 0)
00230         baudrate = B9600;
00231       if (strncmp(baud, "19200", 5) == 0)
00232         baudrate = B19200;
00233     }
00234   }
00235 
00236   fd = open_magnetometer(device, baudrate);
00237   initialize_magnetometer(fd);
00238 
00239   set_RTS(fd);          // RS485 Transmitter enabled.
00240   set_DTR(fd);          // RS485 Receiver enabled.
00241   usleep(5000);
00242   clear_DTR(fd);        // RS485 Receiver disabled.
00243 
00244   printf("\n\n *********************** Hit 'Q', or <ESC>, to Quit.!! ");
00245   printf("********************\n ");
00246 
00247   for(;;){
00248     struct reading data;
00249     unsigned char sensor;
00250     int choice;
00251 
00252     printf("\n\nEnter the sensor number (0-9), or 'Q' to quit:");
00253     choice = getchar();
00254 
00255     if(choice==27 || choice=='Q' || choice=='q')
00256       break;
00257 
00258     printf("%c",choice);
00259     if(choice>='0' && choice<='9')
00260       sensor = choice-48;  //convert ASCII to number
00261     else{
00262       printf("\nSensor must be from  0 through 9.");
00263       continue;
00264     }
00265     if((count = get_data_set(fd, &data, sensor)) !=1){
00266       printf("\nx-axis=%6d", data.x.integer);
00267       printf("\ny-axis=%6d", data.y.integer);
00268       printf("\nz-axis=%6d", data.z.integer);
00269       printf("\nbytes received=%6d", count);
00270     }
00271     else{
00272       printf("\nSensor #%d is not responding.", sensor);
00273       printf("\nPress 'Q' to quit.\n");
00274       printf("\nbytes received=%6d",count);
00275     }
00276   }
00277   close(fd);
00278   return 0;
00279 }


© 2007, Los Alamos National Security, LLC.