Monitoring du Phytotron
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

t.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Experiments with the serial input
  3. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. */
  5. /**********************************************************************
  6. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  7. Version 2, December 2004
  8. Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
  9. Everyone is permitted to copy and distribute verbatim or modified
  10. copies of this license document, and changing it is allowed as long
  11. as the name is changed.
  12. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  13. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  14. 0. You just DO WHAT THE FUCK YOU WANT TO.
  15. **********************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <strings.h>
  19. #include <ctype.h>
  20. #include <time.h>
  21. #include <unistd.h> //Used for UART
  22. #include <fcntl.h> //Used for UART
  23. #include <errno.h>
  24. #include <termios.h> //Used for UART
  25. #include <getopt.h>
  26. #include "serial.h"
  27. int verbosity;
  28. /* ---------------------------------------------------------------- */
  29. int read_temps(char *buff, int k)
  30. {
  31. float datas[4];
  32. int raw[4], foo;
  33. long temps;
  34. foo = parse4_Ivalues(buff, 'T', raw);
  35. //
  36. if (foo >= 0) {
  37. temps = time(NULL);
  38. values2temperature(raw, datas);
  39. printf("%ld %f %f %f %f\n", temps,
  40. datas[0], datas[1], datas[2], datas[3]);
  41. fflush(stdout);
  42. }
  43. else {
  44. fprintf(stderr, "%s: parse -> %d\n", __func__, foo);
  45. }
  46. return 0;
  47. }
  48. /* ---------------------------------------------------------------- */
  49. int read_light(char *buff, int k)
  50. {
  51. int val;
  52. if (1 == sscanf(buff+1, "%d", &val)) {
  53. printf("--------- %04X ----------\n", val);
  54. return 0;
  55. }
  56. return -3;
  57. }
  58. /* ---------------------------------------------------------------- */
  59. int loop(int sfd, int iters)
  60. {
  61. int count, foo;
  62. long temps;
  63. char ligne[200], buff[200];
  64. struct tm *p_tms;
  65. for (count=0; count<iters; count++) {
  66. //
  67. foo = getline_to(sfd, ligne, 100, 0);
  68. //
  69. if (verbosity) {
  70. /* fprintf(stderr, "getline #%d on %d -> %d\n",
  71. count, iters, foo); */
  72. fprintf(stderr, "%s\n", ligne);
  73. }
  74. //
  75. if (verbosity > 1)
  76. {
  77. p_tms = localtime(&temps);
  78. (void)strftime(buff, 19, "%H:%M:%S", p_tms);
  79. fprintf(stderr, "%s %6d / %d\n", buff, count, iters);
  80. }
  81. foo = 0;
  82. switch (ligne[0]) {
  83. case 'L':
  84. foo = read_light(ligne, 0);
  85. break;
  86. case 'T':
  87. foo = read_temps(ligne, 0);
  88. break;
  89. default:
  90. fprintf(stderr, "*** WTF '%s' ?\n", ligne);
  91. foo = -1;
  92. break;
  93. }
  94. if (foo) {
  95. fprintf(stderr, "%s : error %d after switch\n",
  96. __func__, foo);
  97. }
  98. if (verbosity) puts("");
  99. }
  100. return 0;
  101. }
  102. /* ---------------------------------------------------------------- */
  103. void help(int k)
  104. {
  105. puts("options : ");
  106. puts("\t-d\tserial device to read.");
  107. puts("\t-K\tset the K parameter.");
  108. puts("\t-n\tnumber of records to grab.");
  109. puts("\t-t\tselect the function");
  110. puts("\t-v\tincrease verbosity.");
  111. }
  112. /* ---------------------------------------------------------------- */
  113. int main (int argc, char *argv[])
  114. {
  115. int serial_in;
  116. char *device = "/dev/ttyS0";
  117. int nbre, speed, opt, K=0;
  118. int param, retv;
  119. /* set some default values */
  120. verbosity = 0;
  121. nbre = 25;
  122. speed = 9600;
  123. param = 1;
  124. while ((opt = getopt(argc, argv, "d:n:vh")) != -1) {
  125. switch (opt) {
  126. case 'p': param = atoi(optarg); break;
  127. case 'v': verbosity++; break;
  128. case 'K': K = atoi(optarg); break;
  129. case 'n': nbre = atoi(optarg); break;
  130. case 'd': device = optarg; break;
  131. case 'h': help(0); exit(0);
  132. default:
  133. fprintf(stderr, "%s : uh ?", argv[0]);
  134. exit(1);
  135. break;
  136. }
  137. }
  138. if (verbosity) {
  139. fprintf(stderr, "Testing Serial Software - compiled " \
  140. __DATE__ " " __TIME__ "\n");
  141. }
  142. serial_in = prepare_UART(device, speed);
  143. if (serial_in < 0) {
  144. fprintf(stderr, "%s : open device : error %d on %s\n",
  145. argv[0], serial_in, device);
  146. exit(1);
  147. }
  148. fprintf(stderr, "going to listen on %d\n", serial_in);
  149. switch (param) {
  150. case 0:
  151. retv = loop(serial_in, nbre);
  152. break;
  153. case 1:
  154. retv = -1;
  155. break;
  156. default:
  157. retv = -2;
  158. break;
  159. }
  160. fprintf(stderr, "Returned value is %d\n", retv);
  161. return 0;
  162. }
  163. /* ---------------------------------------------------------------- */