source: tools/ListenToArduino/ListenToArduino.cc@ 154

Last change on this file since 154 was 153, checked in by daqct3, 15 years ago
Added configuration request for measurement period
File size: 8.2 KB
Line 
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>
12
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 */
29
30 #define FALSE 0
31 #define TRUE 1
32
33 #define myPath "/ct3data/SlowData/" // will be used later on
34 //#define myPath "" // for testing purposes
35
36
37 FILE * openOutfile(char *);
38 static int poll_stdin_time(int);
39
40 volatile int STOP=FALSE;
41
42 int main(int argc, char *argv[])
43 {
44 int fd, res;
45 struct termios oldtio,newtio;
46 char buf[255];
47
48 /*
49 Start Evidence server and create services
50 */
51 EvidenceServer Srv(SERVER_NAME);
52
53 float Val[NUM_VAL];
54 char Name[100];
55 DimService *Service[NUM_VAL];
56
57 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 }
62
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
218return 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
227FILE * 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;
267}
268
269static 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 TracBrowser for help on using the repository browser.