00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
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);
00114 clear_DTR(fd);
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);
00123 usleep(5000);
00124 write(fd, "\r", 1);
00125
00126 usleep(2000);
00127
00128 set_DTR(fd);
00129 clear_RTS(fd);
00130
00131 usleep(100000);
00132 data->x.integer=0;
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);
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);
00181 usleep(5000);
00182 write(fd, "\033", 1);
00183 usleep(5000);
00184 write(fd, "\033", 1);
00185 usleep(5000);
00186 write(fd, "\r", 1);
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);
00195 usleep(5000);
00196 write(fd, "E", 1);
00197 usleep(5000);
00198 write(fd, "\r", 1);
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);
00207 usleep(5000);
00208 write(fd, "\r", 1);
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);
00240 set_DTR(fd);
00241 usleep(5000);
00242 clear_DTR(fd);
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;
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 }