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

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