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 3.5KB

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