// Mario Behn, behn@mirsl.ecs.umass.edu // 0.1 runs with four sonics on /dev/ttyUSB0 to /dev/ttyUSB3 // 0.2 include temperature logger on /dev/ttyS1 // 0.3 include barometer on /dev/ttyUSB4 // Header files #include #include #include #include #include #include #include #include #include #include #include #include #include // Symbols #define _GNU_SOURCE #define _POSIX_SOURCE 1 #define BAUDRATE1 B38400 #define BAUDRATE2 B2400 #define BAUDRATE3 B9600 #define MODEMDEVICE "/dev/ttyUSB0" // Sensor 1 (Sonic) #define MODEMDEVICE1 "/dev/ttyUSB1" // Sensor 2 (Sonic) #define MODEMDEVICE2 "/dev/ttyUSB2" // Sensor 3 (Sonic) #define MODEMDEVICE3 "/dev/ttyUSB3" // Sensor 4 (Sonic) #define MODEMDEVICE4 "/dev/ttyUSB4" // Sensor 5 (Barometer) #define MODEMDEVICE8 "/dev/ttyS1" // Temperatursensors // Global Variables FILE *myfile; FILE *myfile1; FILE *myfile2; FILE *myfile3; FILE *myfile4; FILE *myfile8; int fd; int fd1; int fd2; int fd3; int fd4; int fd8; int select_result; struct termios oldtio, newtio; struct termios oldtio1, newtio1; struct termios oldtio2, newtio2; struct termios oldtio3, newtio3; struct termios oldtio4, newtio4; struct termios oldtio8, newtio8; // ***************************************************************** // Signal Handler on CTRL-C void catch_int (int sig_num) { pid_t my_pid = getpid (); fclose (myfile); close (fd); tcsetattr (fd, TCSANOW, &oldtio); fclose (myfile1); close (fd1); tcsetattr (fd1, TCSANOW, &oldtio1); fclose (myfile2); close (fd2); tcsetattr (fd2, TCSANOW, &oldtio2); fclose (myfile3); close (fd3); tcsetattr (fd3, TCSANOW, &oldtio3); fclose (myfile4); close (fd4); tcsetattr (fd4, TCSANOW, &oldtio4); fclose (myfile8); close (fd8); tcsetattr (fd8, TCSANOW, &oldtio8); printf ("Ending data retrieval program \n"); kill (my_pid, SIGTERM); } // ***************************************************************** // ***************************************************************** // Creates Timestamp as Filename struct tm * init_filename (char fileName[]) { time_t t; struct tm *date; t = time ((time_t *) NULL); // date = gmtime (&t); date = localtime (&t); sprintf (fileName, "%04d%02d%02d%02d%02d%02d.dat", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec); return date; } // ***************************************************************** // ***************************************************************** main (int argc, char *argv[], char *envp[]) { int c, res; fd_set set; char buf[255]; int i = 0; int ch; int flags; int old_tm_mday; char buffer[30]; char test[30]; char path[60] = "../raw_data/"; char timestamp[60]; char filename[60]; char filename1[60]; char filename2[60]; char filename3[60]; char filename4[60]; char filename8[60]; struct tm my_time; struct timeval tv; time_t curtime; // Timestamp>>Filename init_filename (timestamp); // Timestamp>>Filename // Register the Signal Handler signal (SIGINT, catch_int); // Register the Signal Handler // Open 1st sensor device for read and write fd = open (MODEMDEVICE, O_RDWR | O_NOCTTY); // file descriptor for select if (fd < 0) { perror (MODEMDEVICE); exit (-1); } // Open 2nd sensor device for read and write fd1 = open (MODEMDEVICE1, O_RDWR | O_NOCTTY); // file descriptor for select if (fd1 < 0){ perror (MODEMDEVICE1); exit (-1); } // Open 3rd sensor device for read and write fd2 = open (MODEMDEVICE2, O_RDWR | O_NOCTTY); // file descriptor for select if (fd2 < 0){ perror (MODEMDEVICE2); exit (-1); } // Open 4th sensor device for read and write fd3 = open (MODEMDEVICE3, O_RDWR | O_NOCTTY); // file descriptor for select if (fd3 < 0){ perror (MODEMDEVICE3); exit (-1); } // Open 5th sensor device for read and write fd4 = open (MODEMDEVICE4, O_RDWR | O_NOCTTY); // file descriptor for select if (fd4 < 0){ perror (MODEMDEVICE4); exit (-1); } // Open temp logger for read and write fd8 = open (MODEMDEVICE8, O_RDWR | O_NOCTTY); // file descriptor for select if (fd8 < 0) { perror (MODEMDEVICE8); exit (-1); } // Clear FD // FD_ZERO (&set); // Add 1st Sonic FD // FD_SET (fd, &set); // Buffer serial port settings from before program start // Serial Port 1 tcgetattr (fd, &oldtio); /* save current serial port settings */ bzero (&newtio, sizeof (newtio));/* clear struct for new port settings */ // Serial Port 2 tcgetattr (fd1, &oldtio1); /* save current serial port settings */ bzero (&newtio1, sizeof (newtio1));/* clear struct for new port settings */ // Serial Port 3 tcgetattr (fd2, &oldtio2); /* save current serial port settings */ bzero (&newtio2, sizeof (newtio2));/* clear struct for new port settings */ // Serial Port 4 tcgetattr (fd3, &oldtio3); /* save current serial port settings */ bzero (&newtio3, sizeof (newtio3));/* clear struct for new port settings */ // Serial Port 4 tcgetattr (fd4, &oldtio4); /* save current serial port settings */ bzero (&newtio4, sizeof (newtio4));/* clear struct for new port settings */ // Serial Port 9 (onboard serial port 2) tcgetattr (fd8, &oldtio8); /* save current serial port settings */ bzero (&newtio8, sizeof (newtio8));/* clear struct for new port settings */ // Setup terminal line control parameter newtio.c_cflag = BAUDRATE1 | CRTSCTS | CS8 | CLOCAL | CREAD; newtio1.c_cflag = BAUDRATE1 | CRTSCTS | CS8 | CLOCAL | CREAD; newtio2.c_cflag = BAUDRATE1 | CRTSCTS | CS8 | CLOCAL | CREAD; newtio3.c_cflag = BAUDRATE1 | CRTSCTS | CS8 | CLOCAL | CREAD; newtio4.c_cflag = BAUDRATE3 | CRTSCTS | CS8 | CLOCAL | CREAD; newtio8.c_cflag = BAUDRATE2 | CRTSCTS | CS8 | CLOCAL | CREAD; newtio8.c_iflag = IGNPAR | IGNCR; newtio8.c_oflag = 0; /* set input mode (non-canonical, no echo,...) */ newtio8.c_lflag = 0; newtio8.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio8.c_cc[VMIN] = 9; /* blocking read until 9 chars received */ newtio.c_iflag = IGNPAR | ICRNL; newtio1.c_iflag = IGNPAR | ICRNL; newtio2.c_iflag = IGNPAR | ICRNL; newtio3.c_iflag = IGNPAR | ICRNL; newtio4.c_iflag = IGNPAR | ICRNL; newtio.c_oflag = 0; newtio1.c_oflag = 0; newtio2.c_oflag = 0; newtio3.c_oflag = 0; newtio4.c_oflag = 0; newtio.c_lflag = ICANON; newtio1.c_lflag = ICANON; newtio2.c_lflag = ICANON; newtio3.c_lflag = ICANON; newtio4.c_lflag = ICANON; // Initialize all control characters // Serial Port 1 newtio.c_cc[VINTR] = 0; /* Ctrl-c */ newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */ newtio.c_cc[VERASE] = 0; /* del */ newtio.c_cc[VKILL] = 0; /* @ */ newtio.c_cc[VEOF] = 4; /* Ctrl-d */ newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ newtio.c_cc[VSWTC] = 0; /* '\0' */ newtio.c_cc[VSTART] = 0; /* Ctrl-q */ newtio.c_cc[VSTOP] = 0; /* Ctrl-s */ newtio.c_cc[VSUSP] = 0; /* Ctrl-z */ newtio.c_cc[VEOL] = 0; /* '\0' */ newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */ newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */ newtio.c_cc[VWERASE] = 0; /* Ctrl-w */ newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */ newtio.c_cc[VEOL2] = 0; /* '\0' */ // Serial Port 2 newtio1.c_cc[VINTR] = 0; /* Ctrl-c */ newtio1.c_cc[VQUIT] = 0; /* Ctrl-\ */ newtio1.c_cc[VERASE] = 0; /* del */ newtio1.c_cc[VKILL] = 0; /* @ */ newtio1.c_cc[VEOF] = 4; /* Ctrl-d */ newtio1.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio1.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ newtio1.c_cc[VSWTC] = 0; /* '\0' */ newtio1.c_cc[VSTART] = 0; /* Ctrl-q */ newtio1.c_cc[VSTOP] = 0; /* Ctrl-s */ newtio1.c_cc[VSUSP] = 0; /* Ctrl-z */ newtio1.c_cc[VEOL] = 0; /* '\0' */ newtio1.c_cc[VREPRINT] = 0; /* Ctrl-r */ newtio1.c_cc[VDISCARD] = 0; /* Ctrl-u */ newtio1.c_cc[VWERASE] = 0; /* Ctrl-w */ newtio1.c_cc[VLNEXT] = 0; /* Ctrl-v */ newtio1.c_cc[VEOL2] = 0; /* '\0' */ // Serial Port 3 newtio2.c_cc[VINTR] = 0; /* Ctrl-c */ newtio2.c_cc[VQUIT] = 0; /* Ctrl-\ */ newtio2.c_cc[VERASE] = 0; /* del */ newtio2.c_cc[VKILL] = 0; /* @ */ newtio2.c_cc[VEOF] = 4; /* Ctrl-d */ newtio2.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio2.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ newtio2.c_cc[VSWTC] = 0; /* '\0' */ newtio2.c_cc[VSTART] = 0; /* Ctrl-q */ newtio2.c_cc[VSTOP] = 0; /* Ctrl-s */ newtio2.c_cc[VSUSP] = 0; /* Ctrl-z */ newtio2.c_cc[VEOL] = 0; /* '\0' */ newtio2.c_cc[VREPRINT] = 0; /* Ctrl-r */ newtio2.c_cc[VDISCARD] = 0; /* Ctrl-u */ newtio2.c_cc[VWERASE] = 0; /* Ctrl-w */ newtio2.c_cc[VLNEXT] = 0; /* Ctrl-v */ newtio2.c_cc[VEOL2] = 0; /* '\0' */ // Serial Port 4 newtio3.c_cc[VINTR] = 0; /* Ctrl-c */ newtio3.c_cc[VQUIT] = 0; /* Ctrl-\ */ newtio3.c_cc[VERASE] = 0; /* del */ newtio3.c_cc[VKILL] = 0; /* @ */ newtio3.c_cc[VEOF] = 4; /* Ctrl-d */ newtio3.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio3.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ newtio3.c_cc[VSWTC] = 0; /* '\0' */ newtio3.c_cc[VSTART] = 0; /* Ctrl-q */ newtio3.c_cc[VSTOP] = 0; /* Ctrl-s */ newtio3.c_cc[VSUSP] = 0; /* Ctrl-z */ newtio3.c_cc[VEOL] = 0; /* '\0' */ newtio3.c_cc[VREPRINT] = 0; /* Ctrl-r */ newtio3.c_cc[VDISCARD] = 0; /* Ctrl-u */ newtio3.c_cc[VWERASE] = 0; /* Ctrl-w */ newtio3.c_cc[VLNEXT] = 0; /* Ctrl-v */ newtio3.c_cc[VEOL2] = 0; /* '\0' */ // Serial Port 4 newtio4.c_cc[VINTR] = 0; /* Ctrl-c */ newtio4.c_cc[VQUIT] = 0; /* Ctrl-\ */ newtio4.c_cc[VERASE] = 0; /* del */ newtio4.c_cc[VKILL] = 0; /* @ */ newtio4.c_cc[VEOF] = 4; /* Ctrl-d */ newtio4.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio4.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ newtio4.c_cc[VSWTC] = 0; /* '\0' */ newtio4.c_cc[VSTART] = 0; /* Ctrl-q */ newtio4.c_cc[VSTOP] = 0; /* Ctrl-s */ newtio4.c_cc[VSUSP] = 0; /* Ctrl-z */ newtio4.c_cc[VEOL] = 0; /* '\0' */ newtio4.c_cc[VREPRINT] = 0; /* Ctrl-r */ newtio4.c_cc[VDISCARD] = 0; /* Ctrl-u */ newtio4.c_cc[VWERASE] = 0; /* Ctrl-w */ newtio4.c_cc[VLNEXT] = 0; /* Ctrl-v */ newtio4.c_cc[VEOL2] = 0; /* '\0' */ // Clean FD and Write Settings into port 1 /* tcflush (fd, TCIFLUSH); tcsetattr (fd, TCSANOW, &newtio); // Clean FD and Write Settings into port 2 /* tcflush (fd1, TCIFLUSH); tcsetattr (fd1, TCSANOW, &newtio1); // Clean FD and Write Settings into port 3 /* tcflush (fd2, TCIFLUSH); tcsetattr (fd2, TCSANOW, &newtio2); // Clean FD and Write Settings into port 4 /* tcflush (fd3, TCIFLUSH); tcsetattr (fd3, TCSANOW, &newtio3); // Clean FD and Write Settings into port 4 /* tcflush (fd4, TCIFLUSH); tcsetattr (fd4, TCSANOW, &newtio4); // Clean FD and Write Settings into port 9 /* tcflush (fd8, TCIFLUSH); tcsetattr (fd8, TCSANOW, &newtio8); // Open file for 1st sonic sprintf (filename, "%sSONIC_1_%s", path, timestamp); myfile = fopen (filename, "w+"); // Open file for 2nd sonic sprintf (filename1, "%sSONIC_2_%s", path, timestamp); myfile1 = fopen (filename1, "w+"); // Open file for 3rd sonic sprintf (filename2, "%sSONIC_3_%s", path, timestamp); myfile2 = fopen (filename2, "w+"); // Open file for 4th sonic sprintf (filename3, "%sSONIC_4_%s", path, timestamp); myfile3 = fopen (filename3, "w+"); // Open file for 4th sonic sprintf (filename4, "%sBAROMETER_1_%s", path, timestamp); myfile4 = fopen (filename4, "w+"); // Open file for Temperature sprintf (filename8, "%sTEMPERATURE_%s", path, timestamp); myfile8 = fopen (filename8, "w+"); // Clear FD FD_ZERO (&set); // Add 1st Sonic FD FD_SET (fd, &set); // Add 2nd Sonic FD FD_SET (fd1, &set); // Add 3rd Sonic FD FD_SET (fd2, &set); // Add 4th Sonic FD FD_SET (fd3, &set); // Add Barometer FD FD_SET (fd4, &set); // Add Temperature FD FD_SET (fd8, &set); // Read a dummy read, to avoid upstarting incomplete data for (i=0;i<=19;i++) { // Wait for the FD which has data select_result = select (FD_SETSIZE, &set, NULL, NULL, NULL); // printf("active %i /n",select_result); // Data available from 1st sonic if (FD_ISSET (fd, &set)) { // printf("SONIC 1 \n"); res = read (fd, buf, 255); // Add 1st Sonic FD FD_SET (fd, &set); // Add 2nd Sonic FD FD_SET (fd1, &set); // Add 3rd Sonic FD FD_SET (fd2, &set); // Add 4th Sonic FD FD_SET (fd3, &set); // Add Barometer FD FD_SET (fd4, &set); // Add Temperature FD FD_SET (fd8, &set); } else if (FD_ISSET (fd1, &set)) { // printf("SONIC 2 \n"); res = read (fd1, buf, 255); // Add 1st Sonic FD FD_SET (fd, &set); // Add 2nd Sonic FD FD_SET (fd1, &set); // Add 3rd Sonic FD FD_SET (fd2, &set); // Add 4th Sonic FD FD_SET (fd3, &set); // Add Barometer FD FD_SET (fd4, &set); // Add Temperature FD FD_SET (fd8, &set); } else if (FD_ISSET (fd2, &set)) { // printf("SONIC 3 \n"); res = read (fd2, buf, 255); // Add 1st Sonic FD FD_SET (fd, &set); // Add 2nd Sonic FD FD_SET (fd1, &set); // Add 3rd Sonic FD FD_SET (fd2, &set); // Add 4th Sonic FD FD_SET (fd3, &set); // Add Barometer FD FD_SET (fd4, &set); // Add Temperature FD FD_SET (fd8, &set); } else if (FD_ISSET (fd3, &set)) { // printf("SONIC 4 \n"); res = read (fd3, buf, 255); // Add 1st Sonic FD FD_SET (fd, &set); // Add 2nd Sonic FD FD_SET (fd1, &set); // Add 3rd Sonic FD FD_SET (fd2, &set); // Add 4th Sonic FD FD_SET (fd3, &set); // Add Barometer FD FD_SET (fd4, &set); // Add Temperature FD FD_SET (fd8, &set); } else if (FD_ISSET (fd3, &set)) { // printf("BAROMETER 1 \n"); res = read (fd4, buf, 255); // Add 1st Sonic FD FD_SET (fd, &set); // Add 2nd Sonic FD FD_SET (fd1, &set); // Add 3rd Sonic FD FD_SET (fd2, &set); // Add 4th Sonic FD FD_SET (fd3, &set); // Add Barometer FD FD_SET (fd4, &set); // Add Temperature FD FD_SET (fd8, &set); } else if (FD_ISSET (fd8, &set)) { // printf("TEMPERATURE \n"); res = read (fd8, buf, 255); // Add 1st Sonic FD FD_SET (fd, &set); // Add 2nd Sonic FD FD_SET (fd1, &set); // Add 3rd Sonic FD FD_SET (fd2, &set); // Add 4th Sonic FD FD_SET (fd3, &set); // Add Barometer FD FD_SET (fd4, &set); // Add Temperature FD FD_SET (fd8, &set); } } // Infinite data recording loop // for(i=0;i<=20;i++) for (;;) { // Wait for the FD which has data select_result = select (FD_SETSIZE, &set, NULL, NULL, NULL); // Create timestamp right away gettimeofday (&tv, NULL); curtime = tv.tv_sec; if (localtime (&curtime)->tm_mday!=old_tm_mday){ // insert File closing, new file name generation and opening new file here fclose (myfile); fclose (myfile1); fclose (myfile2); fclose (myfile3); fclose (myfile4); fclose (myfile8); init_filename (timestamp); // Timestamp>>Filename sprintf (filename, "%sSONIC_1_%s", path, timestamp), myfile = fopen (filename, "w+"); // Open file for 1st sonic sprintf (filename1, "%sSONIC_2_%s", path, timestamp), myfile1 = fopen (filename1, "w+"); // Open file for 2nd sonic sprintf (filename2, "%sSONIC_3_%s", path, timestamp), myfile2 = fopen (filename2, "w+"); // Open file for 3rd sonic sprintf (filename3, "%sSONIC_4_%s", path, timestamp), myfile3 = fopen (filename3, "w+"); // Open file for 4th sonic sprintf (filename4, "%sBAROMETER_1_%s", path, timestamp), myfile4 = fopen (filename4, "w+"); // Open file for 1st Barometer sprintf (filename8, "%sTEMPERATURE_%s", path, timestamp), myfile8 = fopen (filename8, "w+"); // Open file for the temperature logger } old_tm_mday = localtime(&curtime)->tm_mday; strftime (buffer, 30, "20%g %m %d %H %M %S.", localtime (&curtime)); sprintf (test, "%s%06ld ", buffer, tv.tv_usec); // ********************************************************************************** // Data available from 1st sonic if (FD_ISSET (fd, &set)){ // Read the data from the serial port res = read (fd, buf, 255); // Set end of string buf[res] = 0; // Write timestamp to file fputs (test, myfile); // Write data to file fputs (buf, myfile); } // Data available from 2nd sonic else if (FD_ISSET (fd1, &set)){ // Read the data from the serial port res = read (fd1, buf, 255); // Set end of string buf[res] = 0; // Write timestamp to file fputs (test, myfile1); // Write data to file fputs (buf, myfile1); } // Data available from 3rd sonic else if (FD_ISSET (fd2, &set)){ // Read the data from the serial port res = read (fd2, buf, 255); // Set end of string buf[res] = 0; // Write timestamp to file fputs (test, myfile2); // Write data to file fputs (buf, myfile2); } // Data available from 4th sonic else if (FD_ISSET (fd3, &set)){ // Read the data from the serial port res = read (fd3, buf, 255); // Set end of string buf[res] = 0; // Write timestamp to file fputs (test, myfile3); // Write data to file fputs (buf, myfile3); } // Data available from 1st barometer else if (FD_ISSET (fd4, &set)){ // Read the data from the serial port res = read (fd4, buf, 255); // Set end of string buf[res] = 0; // Write timestamp to file fputs (test, myfile4); // Write data to file fputs (buf, myfile4); } // Data available from temperature sensor else if (FD_ISSET (fd8, &set)){ // Read the data from the serial port res = read (fd8, buf, 255); // Set end of string buf[res] = 0; // Write timestamp to file //fputs (test, myfile8); // Write data to file //fputs (buf, myfile8); fprintf(myfile8," %s %s",buf, test); } // ********************************************************************************** // Add 1st Sonic FD FD_SET (fd, &set); // Add 2nd Sonic FD FD_SET (fd1, &set); // Add 3rd Sonic FD FD_SET (fd2, &set); // Add 4th Sonic FD FD_SET (fd3, &set); // Add Barometer FD FD_SET (fd4, &set); // Add Temperature FD FD_SET (fd8, &set); } // Restore the old serial port settings fclose (myfile); close (fd); tcsetattr (fd, TCSANOW, &oldtio); fclose (myfile1); close (fd1); tcsetattr (fd, TCSANOW, &oldtio1); fclose (myfile2); close (fd2); tcsetattr (fd2, TCSANOW, &oldtio2); fclose (myfile3); close (fd3); tcsetattr (fd3, TCSANOW, &oldtio3); fclose (myfile4); close (fd4); tcsetattr (fd4, TCSANOW, &oldtio4); fclose (myfile8); close (fd8); tcsetattr (fd8, TCSANOW, &oldtio8); }