The VisAO Camera
QTITempProbe.cpp
1 
2 
3 #include "QTITempProbe.h"
4 
5 //int qtitimeout;
6 
7 // double get_curr_time()
8 // {
9 // struct timespec tsp;
10 // clock_gettime(CLOCK_REALTIME, &tsp);
11 //
12 // return ((double)tsp.tv_sec) + ((double)tsp.tv_nsec)/1e9;
13 // }
14 
15 
16 sig_atomic_t timed_out;
17 
18 static void timeout_handler(int sig, siginfo_t *si, void *uc)
19 {
20  timed_out = 1;
21 }
22 
23 
24 QTITempProbe::QTITempProbe()
25 {
26  init();
27 }
28 
29 
30 
31 
32 QTITempProbe::QTITempProbe(std::string dev_path)
33 {
34  init();
35  setDevPath(dev_path);
36 }
37 
38 QTITempProbe::~QTITempProbe()
39 {
40  if(devOpen)
41  {
42  fin.close();
43  fout.close();
44  }
45 }
46 
47 void QTITempProbe::init()
48 {
49  devOpen = 0;
50  errNum = 0;
51 
52  long long freq_nanosecs;
53 
54  sa.sa_flags = SA_SIGINFO;
55  sa.sa_sigaction = timeout_handler;
56  sigemptyset(&sa.sa_mask);
57  //sigaction(RTSIGTIMEOUT, &sa, NULL);
58 
59 
60  sev.sigev_notify = SIGEV_SIGNAL;
61  sev.sigev_signo = RTSIGTIMEOUT;
62  sev.sigev_value.sival_ptr = &timerid;
63 
64 
65  freq_nanosecs = (long long) 2.*1e9;
66  its.it_value.tv_sec = freq_nanosecs/1000000000;
67  its.it_value.tv_nsec = freq_nanosecs % 1000000000;
68  its.it_interval.tv_sec = 0;
69  its.it_interval.tv_nsec = 0;
70 
71  timer_create(CLOCK_REALTIME, &sev, &timerid);
72 
73 }
74 
75 int QTITempProbe::setDevPath(std::string dev_path)
76 {
77  devPath = dev_path;
78 
79  fin.open(devPath.c_str());
80 
81  if(!fin.good())
82  {
83  std::cerr << "Error opening device path " << dev_path << " for reading.\n";
84  devPath = "";
85  devOpen = 0;
86  errNum = FIN_GOOD_ERROR;
87  return -1;
88  }
89 
90  fout.open(devPath.c_str());
91 
92  if(!fout.good())
93  {
94  std::cerr << "Error opening device path " << dev_path << " for writing.\n";
95  devPath = "";
96  devOpen = 0;
97  errNum = FOUT_GOOD_ERROR;
98  return -1;
99  }
100 
101  devOpen = 1;
102 
103  errNum = 0;
104 
105  //std::cerr << "Opened\n";
106  return 0;
107 }
108 
110 {
111  char details[256];
112 
113  //qtitimeout = 0;
114 
115  if(!devOpen)
116  {
117  std::cerr << "Device not open. Cannot get serial number.\n";
118  errNum = DEV_NOT_OPEN_ERROR;
119 
120  return -1;
121  }
122 
123  if(!fout.good())
124  {
125  std::cerr << "Device " << devPath << " output is not good.\n";
126  errNum = FOUT_GOOD_ERROR;
127  return -1;
128  }
129 
130 
131 
132  std::cout << "1.0" << std::endl;
133 
134  //First stop any streaming and clear the buffer.
135 
136  //fout << "x" << std::endl;
137  fout.put('x');
138  fout.flush();
139 
140  //if(qtitimeout) return -1;
141  std::cout << "1.1" << std::endl;
142  usleep(50000);
143  std::cout << "1.2" << std::endl;
144  fin.readsome(details, 256);
145  std::cout << "1.3" << std::endl;
146  while(fin.gcount() > 0 /*&& !qtitimeout*/) fin.readsome(details, 256);
147 
148  //if(qtitimeout) return -1;
149 
150  std::cout << "1.4" << std::endl;
151  /*details[0] = 0;
152  fin.readsome(details, 256);
153 
154  while(details[0] != 0)std::cout << "GT 2.0\n";
155  {
156  std::cout << details << "--->\n";
157  details[0] = 0;
158  fin.readsome(details, 256);
159  }*/
160 
161  //fout << "3" << std::endl;
162  fout.put('3');
163  std::cout << "1.1" << std::endl;
164  fout.flush();
165 
166  std::cout << "1.5" << std::endl;
167 
168  if(!fin.good())
169  {
170  std::cerr << "Device " << devPath << " input is not good.\n";
171  errNum = FIN_GOOD_ERROR;
172  return -1;
173  }
174 
175  details[0] = 0;
176 
177  std::string line;
178  int isser;
179  std::cout << "1.6" << std::endl;
180  //sleep(1);
181  //exit(0);
182  for(int i=0;i<20;i++)
183  {
184  fin.getline(details, 256);
185 
186  //if(qtitimeout) return -1;
187 
188  line = details;
189  int srsn = line.find_first_of("\r\n");
190  if(srsn > -1) line[srsn] = '\0';
191 
192  std::cout << i << "+=" << line << " ";
193 
194  isser = line.find("Ser.", 0);
195  std::cout << isser << "\n";
196  if(isser > -1) break;
197  }
198  ///\todo handle isser == -1 here
199 
200  std::cout << "Found: " << line << "\n";
201  //fin.getline(details, 256);
202  //std::cout << "\n------------\n" << details << "\n---------------\n";
203 
204  //if(qtitimeout) return -1;
205 
206  //Kill the line feed.
207  details[fin.gcount()-1] = 0;
208 
209  serialNumber = details+7;
210 
211  std::cout << "|<|" << serialNumber << "|>|\n";
212 
213  //fin.getline(details, 256);
214  //std::cout << details << "\n";
215  //if(qtitimeout) return -1;
216 
217  //usleep(250000);
218 
219  //Flush last > char and anything else
220  fin.readsome(details, 256);
221  while(fin.gcount() > 0 /*&& !qtitimeout*/) fin.readsome(details, 256);
222 
223  //if(qtitimeout) return -1;
224 
225  errNum = 0;
226 
227  fin.close();
228  fout.close();
229 
230  return 0;
231 
232 // catch(int e)
233 // {
234 // std::cout << "Caught\n";
235 // return -1;
236 // }
237 }
238 
239 
240 double QTITempProbe::getTemperature()
241 {
242  sigaction(RTSIGTIMEOUT, &sa, NULL);
243 
244  timed_out = 0;
245  timer_settime(timerid, 0, &its, 0);
246 
247  char ts[256];
248  double lt0, ldt;
249  extern int TimeToDie;
250 
251  FILE *fo, *fi;
252 
253  //fout.open(devPath.c_str());
254  fo = fopen(devPath.c_str(),"w");
255 
256  if(!devOpen)
257  {
258  std::cerr << "Device not open. Cannot get temperature.\n";
259  errNum = DEV_NOT_OPEN_ERROR;
260 
261  return -1;
262  }
263 
264 
265  ts[0] = 0;
266 
267  //just in case
268  std::cout << "GT 1.0" << std::endl;
269 
270  //fin.sync();
271  //fin.readsome(ts, 256);
272 
273 // fout << "x" << std::endl;
274 // usleep(100000);
275 // fin.readsome(ts, 256);
276 // while(fin.gcount() > 0) fin.readsome(ts, 256);
277 
278  fi = fopen(devPath.c_str(), "r");
279  fsync(fileno(fi));
280 
281  std::cout << "GT 2.0" << std::endl;
282  //Must flush!
283  //fout << "2" << std::endl;
284  //fout.put('2');
285  putc('2',fo);
286  std::cout << "GT 2.1" << std::endl;
287  fflush(fo);
288  //fout.flush();
289 
290  fclose(fo);
291 
292  //if(timed_out) return -1000;
293 
294  //fout.close();
295  //fin.open(devPath.c_str());
296 
297 
298  if(timed_out)
299  {
300  //return -1000;
301  fsync(fileno(fi));
302  timed_out = 0;
303  }
304  std::cout << "GT 3.0" << std::endl;
305 
306  //fin.readsome(ts, 256);
307  //std::cout << "ts[0]= " << (int) ts[0] << "\n";
308  fgets(ts, 256, fi);
309  if(timed_out) return -1000;
310  std::cout << ts << std::endl;
311  std::cout << "GT 4.0" << std::endl;
312 
313  lt0 = get_curr_time();
314  ldt = 0.;
315  while((ts[0] == 0 || strlen(ts) < 6) && ldt < 3.0 && !timed_out)
316  {
317  fsync(fileno(fi));
318  fgets(ts, 256, fi);
319  std::cout << ts << std::endl;
320  //fin.readsome(ts, 256);
321  ldt = lt0 - get_curr_time();
322  //std::cout << "ts[0]= " << (int) ts[0] << "\n";
323  }
324  if(timed_out) return -1000;
325  if(ldt >= 3.)
326  {
327  std::cerr << "get temperature timeout\n";
328  return -100000;
329  }
330 
331  if(ts[0] == '>') ts[0] = ' ';
332  //std::cout << "ts = " << ts << "|" << strlen(ts) << "\n";
333 
334  //fin.close();
335  fclose(fi);
336 
337  std::cout << "===>" << strtod(ts, 0) << std::endl;
338  return strtod(ts, 0);
339 
340 }
341 
342 
343 
344 
345 
346 // int main(int argc, char *argv[])
347 // {
348 // QTITempProbe qti;
349 //
350 // if(argc < 2)
351 // qti.setDevPath("/dev/ttyACM0");
352 // else
353 // qti.setDevPath(argv[1]);
354 //
355 // qti.readSerialNumber();
356 //
357 // double t0 = get_curr_time();
358 // std::cout.precision(20);
359 // std::cout << "#" << qti.getSerialNumber() << "\n";
360 // std::cout << "#" << t0 << "\n";
361 // std::cout.precision(5);
362 // for(int i=0;i<100000;i++)
363 // {
364 //
365 // std::cout << get_curr_time() - t0 << " " << qti.getTemperature() << std::endl;
366 // sleep(2);
367 // }
368 //
369 // return 0;
370 // }
371 
double get_curr_time(void)
Gets the current CLOCK_REALTIME time, returns it in seconds to double precision.
Definition: libvisaoc.c:40
int readSerialNumber()
int TimeToDie
Global set by SIGTERM.