Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #include <mysql.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <termios.h>
  9.  
  10. typedef int serialPort;
  11. serialPort SerialLib_open(const char * portPath);
  12. int i;
  13. int t=0;
  14.  
  15. void finish_with_error(MYSQL *con)
  16. {
  17. fprintf(stderr, "%s\n", mysql_error(con));
  18. mysql_close(con);
  19. }
  20.  
  21. int main(int argc, char **argv)
  22. {
  23. char format[128]; // variable stokage date
  24. MYSQL *con = mysql_init(NULL);
  25. char *server = "localhost";
  26. char *user = "root";
  27. char *password = "password";
  28. char *database = "stationmeteo_local";
  29. char ecriture[128]; // char trame acquisition
  30. t=0;
  31.  
  32. if (con == NULL)
  33. {
  34. fprintf(stderr, "%s\n", mysql_error(con));
  35. }
  36.  
  37. if (mysql_real_connect(con, server, user, password, database, 0, NULL, 0)== NULL)
  38. {
  39. finish_with_error(con);
  40. }
  41.  
  42. while(1)
  43. {
  44. serialPort p = SerialLib_open("/dev/ttyAMA0"); // ouverture port AMA0
  45. char buffer[100];
  46. while (t==0)
  47. {
  48. i=read(p,buffer,100); // lecture port et stokage dans un buffer
  49. t=1;
  50. if (i>0)
  51. {
  52. if ((i==21) && (buffer[0]='D') && (buffer[19]='F')) // acquisition de 21 données
  53. {
  54. buffer[20]='\0'; // fin de chaine
  55. printf("trame recu: %s, long %d \n",buffer,i);
  56. char temp[3],humidite[2],dvent[3],vvent[3],p1[3],p2[3],pluie[1]; // séparation de la$
  57. temp[0]=buffer[1];
  58. temp[1]=buffer[2];
  59. temp[2]=buffer[3];
  60. temp[3]='\0';
  61. humidite[0]=buffer[4];
  62. humidite[1]=buffer[5];
  63. humidite[2]='\0';
  64. dvent[0]=buffer[6];
  65. dvent[1]=buffer[7];
  66. dvent[2]=buffer[8];
  67. dvent[3]='\0';
  68. vvent[0]=buffer[9];
  69. vvent[1]=buffer[10];
  70. vvent[2]=buffer[11];
  71. vvent[3]='\0';
  72. p1[0]=buffer[12];
  73. p1[1]=buffer[13];
  74. p1[2]=buffer[14];
  75. p1[3]='\0';
  76. p2[0]=buffer[15];
  77. p2[1]=buffer[16];
  78. p2[2]=buffer[17];
  79. p2[3]='\0';
  80. pluie[0]=buffer[18];
  81. pluie[1]='\0';
  82. time_t temps;
  83. struct tm date;
  84. time(&temps);
  85. date=*localtime(&temps);
  86. strftime(format,128,"'%Y-%m-%d %X'",&date); // création du format datetime
  87. strcpy(ecriture,"INSERT INTO t_acquisition VALUES('',");
  88. strcat(ecriture,format);
  89. strcat(ecriture,",'");
  90. strcat(ecriture,temp);
  91. strcat(ecriture,"','");
  92. strcat(ecriture,humidite);
  93. strcat(ecriture,"','");
  94. strcat(ecriture,dvent);
  95. strcat(ecriture,"','");
  96. strcat(ecriture,vvent);
  97. strcat(ecriture,"','");
  98. strcat(ecriture,p1);
  99. strcat(ecriture,"','");
  100. strcat(ecriture,p2);
  101. strcat(ecriture,"','");
  102. strcat(ecriture,pluie);
  103. strcat(ecriture,"')");
  104. printf("chaine %s\n",ecriture); // affichage requète SQL
  105.  
  106. if (mysql_query(con,ecriture)) // envoie sur la base de données
  107. {
  108. finish_with_error(con);
  109. }
  110.  
  111. }
  112. }
  113. else t=1;
  114. }
  115. close(p); // fermeture du port
  116. t=0;
  117. }
  118. mysql_close(con);
  119. }
  120.  
  121. serialPort SerialLib_open(const char * serialPortPath)
  122. {
  123. int fd;
  124. fd = open(serialPortPath, O_RDONLY | O_NOCTTY );
  125. if (fd == -1)
  126. {
  127. printf("Could not open the serial port : %s - ", serialPortPath);
  128. }
  129. else
  130. {
  131. struct termios options;
  132. tcgetattr(fd, &options);
  133. options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; // reglage port 1200 bauds en lecture
  134. options.c_iflag = 0;
  135. options.c_oflag = 0;
  136. options.c_lflag = ICANON;
  137. tcsetattr(fd, TCSANOW, &options);
  138. }
  139. return (serialPort)fd;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement