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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 loop(int sfd, int iters)
  19. {
  20. int count, foo;
  21. long temps;
  22. char ligne[200];
  23. float datas[4];
  24. struct tm *p_tms;
  25. for (count=0; count<iters; count++) {
  26. foo = getline_to(sfd, ligne, 100, 0);
  27. //
  28. if (verbosity) {
  29. /* fprintf(stderr, "getline #%d on %d -> %d\n",
  30. count, iters, foo); */
  31. fprintf(stderr, "%s\n", ligne);
  32. }
  33. foo = parse4values(ligne, 'T', datas);
  34. //
  35. if (foo >= 0) {
  36. temps = time(NULL);
  37. values2temperature(datas);
  38. printf("%ld %f %f %f %f\n", temps,
  39. datas[0], datas[1], datas[2], datas[3]);
  40. fflush(stdout);
  41. }
  42. else {
  43. fprintf(stderr, "%s: parse -> %d\n", __func__, foo);
  44. }
  45. //
  46. if (verbosity > 1)
  47. {
  48. p_tms = localtime(&temps);
  49. (void)strftime(ligne, 19, "%H:%M:%S", p_tms);
  50. fprintf(stderr, "\t%s\n", ligne);
  51. }
  52. }
  53. return 0;
  54. }
  55. /* ---------------------------------------------------------------- */
  56. void help(int k)
  57. {
  58. puts("options : ");
  59. puts("\t-d\tserial device to read.");
  60. puts("\t-n\tnumber of records to grab.");
  61. puts("\t-v\tincrease verbosity.");
  62. }
  63. /* ---------------------------------------------------------------- */
  64. int main (int argc, char *argv[])
  65. {
  66. int serial_in;
  67. char *device = "/dev/ttyACM0";
  68. int nbre, speed, opt;
  69. /* set some default values */
  70. verbosity = 0;
  71. nbre = 25;
  72. speed = 9600;
  73. while ((opt = getopt(argc, argv, "d:n:vh")) != -1) {
  74. switch (opt) {
  75. case 'v': verbosity++; break;
  76. case 'n': nbre = atoi(optarg); break;
  77. case 'd': device = optarg; break;
  78. case 'h': help(0); exit(0);
  79. default:
  80. fprintf(stderr, "%s : uh ?", argv[0]);
  81. exit(1);
  82. break;
  83. }
  84. }
  85. if (verbosity) {
  86. fprintf(stderr, "Testing Serial Software - compiled " \
  87. __DATE__ " " __TIME__ "\n");
  88. }
  89. serial_in = prepare_UART(device, speed);
  90. if (serial_in < 0) {
  91. fprintf(stderr, "%s : open device : error %d on %s\n",
  92. argv[0], serial_in, device);
  93. exit(1);
  94. }
  95. fprintf(stderr, "going to listen on %d\n", serial_in);
  96. (void)loop(serial_in, nbre);
  97. return 0;
  98. }
  99. /* ---------------------------------------------------------------- */