source: tools/ListenToArduino/main.c@ 104

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