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.3KB

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