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