Changeset 169 for tools/ListenToArduino
- Timestamp:
- 02/23/10 09:37:38 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/ListenToArduino/ListenToArduino.cc
r153 r169 1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <sys/time.h> 4 #include <fcntl.h> 5 #include <termios.h> 6 #include <stdio.h> 7 #include <time.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <unistd.h> 11 #include <sys/select.h> 1 #include <fcntl.h> 2 #include <termios.h> 3 #include <stdio.h> 4 5 // For Evidence server 6 #define SERVER_NAME "ARDUINO" // Name to use in DIM 7 #include "Evidence.h" 8 #define NUM_VAL 10 12 9 13 #define bzero(b,len) (memset((b), '\0' ,(len)), (void) 0) 14 15 16 /* 17 Included for Evidence server 18 */ 19 #define SERVER_NAME "ARDUINO" // Name to use in DIM 20 #include "Evidence.h" 21 #define NUM_VAL 10 22 23 /* baudrate settings are defined in <asm/termbits.h>, which is 24 included by <termios.h> */ 25 #define BAUDRATE B9600 26 /* change this definition for the correct port */ 27 #define MODEMDEVICE "/dev/myArduino" 28 #define _POSIX_SOURCE 1 /* POSIX compliant source */ 10 #define MODEMDEVICE "/dev/myArduino" 29 11 30 #define FALSE 0 31 #define TRUE 1 12 int main() { 32 13 33 #define myPath "/ct3data/SlowData/" // will be used later on 34 //#define myPath "" // for testing purposes 35 14 int fd, res; 15 struct termios oldtio,newtio; 16 char Textbuffer[255], Temp[sizeof(Textbuffer)], *Token; 36 17 37 FILE * openOutfile(char *); 38 static int poll_stdin_time(int);18 // Start Evidence server and create services 19 EvidenceServer Srv(SERVER_NAME); 39 20 40 volatile int STOP=FALSE; 21 float Val[NUM_VAL]={0}; 22 DimService TextService(SERVER_NAME"/Textout", Textbuffer); 23 DimService Data(SERVER_NAME"/Data", "F", Val, sizeof(Val)); 41 24 42 int main(int argc, char *argv[]) 43 { 44 int fd, res; 45 struct termios oldtio,newtio; 46 char buf[255]; 25 // Open modem device for reading and writing and not as controlling tty 26 // because we don't want to get killed if linenoise sends CTRL-C. 27 fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); 28 if (fd <0) { 29 Srv.State(Srv.FATAL, "Could not open modem device (%s)", strerror(errno)); 30 } 47 31 48 /* 49 Start Evidence server and create services 50 */ 51 EvidenceServer Srv(SERVER_NAME); 32 tcgetattr(fd,&oldtio); /* save current serial port settings */ 33 memset(&newtio, 0, sizeof(newtio)); /* clear struct for new port settings */ 52 34 53 float Val[NUM_VAL]; 54 char Name[100]; 55 DimService *Service[NUM_VAL]; 35 /* 36 BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. 37 CRTSCTS : output hardware flow control (only used if the cable has 38 all necessary lines. See sect. 7 of Serial-HOWTO) 39 CS8 : 8n1 (8bit,no parity,1 stopbit) 40 CLOCAL : local connection, no modem contol 41 CREAD : enable receiving characters 42 */ 43 newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD; 56 44 45 // CRTSCTS | maybe not needed //DN 090907 46 47 /* 48 IGNPAR : ignore bytes with parity errors 49 ICRNL : map CR to NL (otherwise a CR input on the other computer 50 will not terminate input) 51 otherwise make device raw (no other input processing) 52 */ 53 newtio.c_iflag = IGNPAR ; 54 55 // | ICRNL // i guess this is not needed here. DN 090907 56 57 // Raw output. 58 newtio.c_oflag = 0; 59 60 /* 61 ICANON : enable canonical input 62 disable all echo functionality, and don't send signals to calling program 63 */ 64 newtio.c_lflag = ICANON; 65 66 /* 67 initialize all control characters 68 default values can be found in /usr/include/termios.h, and are given 69 in the comments, but we don't need them here 70 */ 71 newtio.c_cc[VINTR] = 0; /* Ctrl-c */ 72 newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */ 73 newtio.c_cc[VERASE] = 0; /* del */ 74 newtio.c_cc[VKILL] = 0; /* @ */ 75 newtio.c_cc[VEOF] = 4; /* Ctrl-d */ 76 newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ 77 newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ 78 newtio.c_cc[VSWTC] = 0; /* '\0' */ 79 newtio.c_cc[VSTART] = 0; /* Ctrl-q */ 80 newtio.c_cc[VSTOP] = 0; /* Ctrl-s */ 81 newtio.c_cc[VSUSP] = 0; /* Ctrl-z */ 82 newtio.c_cc[VEOL] = 0; /* '\0' */ 83 newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */ 84 newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */ 85 newtio.c_cc[VWERASE] = 0; /* Ctrl-w */ 86 newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */ 87 newtio.c_cc[VEOL2] = 0; /* '\0' */ 88 89 // now clean the modem line and activate the settings for the port 90 tcflush(fd, TCIFLUSH); 91 tcsetattr(fd,TCSANOW,&newtio); 92 93 while (!Srv.ExitRequest) { 94 95 /* read blocks program execution until a line terminating character is 96 input, even if more than 255 chars are input. If the number 97 of characters read is smaller than the number of chars available, 98 subsequent reads will return the remaining chars. res will be set 99 to the actual number of characters actually read */ 100 res = read(fd, Textbuffer, sizeof(Textbuffer)); 101 102 // Set end of string marker anbd replace all non-printable characters 103 Textbuffer[res]=0; 104 for (unsigned int i=0; i<strlen(Textbuffer); i++) if (isprint(Textbuffer[i])==0) Textbuffer[i] = ' '; 105 106 printf("%s\n", Textbuffer); 107 TextService.updateService(); 108 if (strncmp (Textbuffer,"[Temp,",6) != 0) continue; 109 110 // Update DIM services 111 memcpy(Temp, Textbuffer, sizeof(Textbuffer)); 112 Token = strtok(Temp, ", "); 57 113 for (int i=0; i<NUM_VAL; i++) { 58 Val[i] = -99;59 snprintf(Name, sizeof(Name), SERVER_NAME"/VAL%.2d", i);60 Service[i] = new DimService (Name, Val[i]);61 }114 Token = strtok(NULL, ", "); 115 if (Token != NULL) Val[i] = atof(Token); 116 } 117 Data.updateService(); 62 118 63 /* 64 Open modem device for reading and writing and not as controlling tty 65 because we don't want to get killed if linenoise sends CTRL-C. 66 */ 67 fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); 68 if (fd <0) {perror(MODEMDEVICE); exit(-1); } 69 70 tcgetattr(fd,&oldtio); /* save current serial port settings */ 71 bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */ 72 73 /* 74 BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. 75 CRTSCTS : output hardware flow control (only used if the cable has 76 all necessary lines. See sect. 7 of Serial-HOWTO) 77 CS8 : 8n1 (8bit,no parity,1 stopbit) 78 CLOCAL : local connection, no modem contol 79 CREAD : enable receiving characters 80 */ 81 newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; 82 83 // CRTSCTS | maybe not needed //DN 090907 84 85 /* 86 IGNPAR : ignore bytes with parity errors 87 ICRNL : map CR to NL (otherwise a CR input on the other computer 88 will not terminate input) 89 otherwise make device raw (no other input processing) 90 */ 91 newtio.c_iflag = IGNPAR ; 92 93 // | ICRNL // i guess this is not needed here. DN 090907 94 95 /* 96 Raw output. 97 */ 98 newtio.c_oflag = 0; 99 100 /* 101 ICANON : enable canonical input 102 disable all echo functionality, and don't send signals to calling program 103 */ 104 newtio.c_lflag = ICANON; 105 106 /* 107 initialize all control characters 108 default values can be found in /usr/include/termios.h, and are given 109 in the comments, but we don't need them here 110 */ 111 newtio.c_cc[VINTR] = 0; /* Ctrl-c */ 112 newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */ 113 newtio.c_cc[VERASE] = 0; /* del */ 114 newtio.c_cc[VKILL] = 0; /* @ */ 115 newtio.c_cc[VEOF] = 4; /* Ctrl-d */ 116 newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ 117 newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ 118 newtio.c_cc[VSWTC] = 0; /* '\0' */ 119 newtio.c_cc[VSTART] = 0; /* Ctrl-q */ 120 newtio.c_cc[VSTOP] = 0; /* Ctrl-s */ 121 newtio.c_cc[VSUSP] = 0; /* Ctrl-z */ 122 newtio.c_cc[VEOL] = 0; /* '\0' */ 123 newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */ 124 newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */ 125 newtio.c_cc[VWERASE] = 0; /* Ctrl-w */ 126 newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */ 127 newtio.c_cc[VEOL2] = 0; /* '\0' */ 128 129 /* 130 now clean the modem line and activate the settings for the port 131 */ 132 tcflush(fd, TCIFLUSH); 133 tcsetattr(fd,TCSANOW,&newtio); 134 135 /* 136 terminal settings done, now handle input 137 In this example, inputting a 'z' at the beginning of a line will 138 exit the program. 139 */ 140 141 FILE *outfile; 142 143 144 char outstring[400]; 145 //char c; 146 char Temp[200], *Token; 147 148 while (STOP==FALSE) { /* loop until we have a terminating condition */ 149 /* read blocks program execution until a line terminating character is 150 input, even if more than 255 chars are input. If the number 151 of characters read is smaller than the number of chars available, 152 subsequent reads will return the remaining chars. res will be set 153 to the actual number of characters actually read */ 154 res = read(fd,buf,255); 155 buf[res]=0; /* set end of string, so we can printf */ 156 157 158 if (strncmp (buf,"[Temp,",6) != 0) continue; 159 160 strncpy(Temp, buf, sizeof(Temp)); 161 Token = strtok(Temp, ", "); 162 for (int i=0; i<NUM_VAL; i++) { 163 Token = strtok(NULL, ", "); 164 if (Token != NULL) Val[i] = atof(Token); 165 Service[i]->updateService(); 166 } 167 168 outfile = openOutfile(myPath); 169 if (outfile == NULL) {return -1;} 170 171 time_t rawtime; 172 struct tm * timeinfo; 173 174 time ( &rawtime ); 175 timeinfo = localtime ( &rawtime ); 176 177 struct timeval timeofday; 178 gettimeofday(&timeofday, NULL); 179 180 181 sprintf(outstring, "DALLAS Sensorvalues %04d %02d %02d %02d %02d %02d %03d %d %s", 182 (*timeinfo).tm_year+1900, 183 (*timeinfo).tm_mon+1, 184 (*timeinfo).tm_mday, 185 (*timeinfo).tm_hour, 186 (*timeinfo).tm_min, 187 (*timeinfo).tm_sec, 188 (int) timeofday.tv_usec/1000, 189 (int)rawtime, 190 buf); 191 192 printf("%s", outstring); 193 194 fprintf(outfile, "%s", outstring); 195 fflush(outfile); 196 fclose(outfile); 197 198 if (poll_stdin_time(atoi(Srv.GetConfig("Period", "10")))==1) { 199 STOP=TRUE; 200 } 201 202 203 if (EvidenceServer::ExitRequest == true) STOP=TRUE; 204 } //end while(STOP==FALSE) 205 206 207 208 /* restore the old port settings */ 209 tcsetattr(fd,TCSANOW,&oldtio); 210 211 /* 212 Delete Evidence services 213 */ 214 for (int i=0; i<NUM_VAL; i++) { 215 delete Service[i]; 216 } 217 218 return 0; 219 } //end main(); 220 221 // OPens or Creates an Outfile named 222 // "CLIM_YYYYMMDD.slow" 223 // this file should contain all enviromentals from noon to noon. 224 // The File is created if it doesn't exist. 225 // all data is appended, nothing will be deleted or overwritten. 226 227 FILE * openOutfile(char *path) 228 { 229 FILE *outfile; 230 231 time_t rawtime; 232 struct tm * timeinfo; 233 234 time ( &rawtime ); 235 timeinfo = gmtime ( &rawtime ); 236 237 // check what day it is today :-) 238 if ((*timeinfo).tm_hour >12){ //then it is already tommorow 239 rawtime += 86400; 240 timeinfo = gmtime ( &rawtime ); 241 } 242 //generate filename 243 char *outfilename; 244 outfilename =(char*) calloc( strlen(path)+25 , sizeof(char)); 245 if (outfilename ==NULL) 246 { 247 perror("could not allocate space for outputfilename"); 248 return NULL; 249 } 250 sprintf(outfilename,"%sCLIM_%04d%02d%02d.slow", 251 path, // from user 252 (*timeinfo).tm_year+1900, 253 (*timeinfo).tm_mon+1, 254 (*timeinfo).tm_mday ); 255 256 // there is no need to check if file is already existing. 257 // if not fopen(name,"a") will create it. 258 // if it exists 259 // it will only be opened for appnendign 260 outfile = fopen(outfilename,"a"); 261 262 if (outfile==NULL){ 263 perror("could not open outputfile"); 264 return NULL;} 265 266 return outfile; 119 usleep(atoi(Srv.GetConfig(SERVER_NAME " Period", "10")) * 1000000); 120 } // while() 121 122 /* restore the old port settings */ 123 tcsetattr(fd,TCSANOW,&oldtio); 267 124 } 268 269 static int poll_stdin_time(int sekunden) {270 struct timeval timeout;271 fd_set read_fds;272 int c;273 int stdin_status;274 275 FD_ZERO(&read_fds);276 FD_SET(STDIN_FILENO, &read_fds);277 timeout.tv_sec = sekunden;278 timeout.tv_usec = 0;279 stdin_status = select(STDIN_FILENO+1, &read_fds, NULL, NULL, &timeout);280 if (stdin_status == 1 ) {281 c=getchar();282 fflush(stdin);283 if (c=='q') {284 return 1;285 }286 } else if (stdin_status ==0) {287 return 0;288 } else {289 perror("select()");290 }291 292 293 294 return -1;295 296 297 }
Note:
See TracChangeset
for help on using the changeset viewer.