Blame view

code_arduino/com_Xbee/progC_initialisation_Xbee/xbeeATCmd.c 2.34 KB
a4d85595   aknockae   Ajout de serialAr...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  /** fichier xbeeATCmd.c **/
  
  /*****************************************************************/
  /** Commandes pour configurer les modules XBee.                 **/
  /*****************************************************************/
  
  /** Fichiers d'inclusion **/
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
  
  /** Constantes **/
  
  #define TAILLE_TAMPON	128
  
  /** Fonctions **/
  
  void xbeeReponse(int ds)
  {
  register int i;
  char d[TAILLE_TAMPON];
  sync();
  for(i=0;i<TAILLE_TAMPON;i++){
    if(read(ds,d+i,1)!=1){ perror("xbeeReponse.read"); exit(-1); }
    if(d[i]==0x0d) break;
    }
  int size=i;
  for(i=0;i<size;i++)
    if(d[i]!=0x0d) fprintf(stdout,"%c",d[i]);
  if(size>0) fprintf(stdout," (");
  for(i=0;i<size;i++){
    fprintf(stdout,"%.2x",d[i]);
    if(i<size-1) fprintf(stdout," ");
    }
  if(size>0) fprintf(stdout,")\n");
  }
  
  void xbeeDefaut(int ds)
  {
  #ifdef DEBUG
  printf("{xbeeDefaut}\n");
  #endif
  char* d="ATRE\r";
  write(ds,d,strlen(d));
  xbeeReponse(ds);
  }
  
  void xbeeSauver(int ds)
  {
  #ifdef DEBUG
  printf("{xbeeSauver}\n");
  #endif
  char* d="ATWR\r";
  write(ds,d,strlen(d));
  xbeeReponse(ds);
  }
  
  void xbeeSortir(int ds)
  {
  #ifdef DEBUG
  printf("{xbeeSortir}\n");
  #endif
  char *cmd="ATCN\r";
  write(ds,cmd,strlen(cmd));
  xbeeReponse(ds);
  }
  
  void xbeeConfigureVitesse(int ds,unsigned char vitesse)
  {
  #ifdef DEBUG
  printf("{xbeeConfigureVitesse %d}\n",vitesse);
  #endif
  if(vitesse<0 || vitesse>7) return;
  char cmd[TAILLE_TAMPON];
  sprintf(cmd,"ATBD %x\r",vitesse);
  write(ds,cmd,strlen(cmd));
  xbeeReponse(ds);
  }
  
  void xbeeRecupereVitesse(int ds)
  {
  #ifdef DEBUG
  printf("{xbeeRecupereVitesse}\n");
  #endif
  char *cmd="ATBD\r";
  write(ds,cmd,strlen(cmd));
  xbeeReponse(ds);
  }
  
  /* Parametre canal entre 0x0B et 0x1A */
  void xbeeConfigureCanal(int ds,char canal)
  {
  #ifdef DEBUG
  printf("{xbeeConfigureCanal %02x} : debut\n",canal);
  #endif
  char cmd[TAILLE_TAMPON];
  sprintf(cmd,"ATCH %x\r",canal);
  sync();
  write(ds,cmd,strlen(cmd));
  xbeeReponse(ds);
  #ifdef DEBUG
  printf("{xbeeConfigureCanal} : fin\n");
  #endif
  }
  
  void xbeeRecupereCanal(int ds)
  {
  #ifdef DEBUG
  printf("{xbeeRecupereCanal} : debut\n");
  #endif
  char *cmd="ATCH\r";
  sync();
  write(ds,cmd,strlen(cmd));
  xbeeReponse(ds);
  #ifdef DEBUG
  printf("{xbeeRecupereCanal} : fin\n");
  #endif
  }
  
  void xbeeModeCommande(int ds)
  {
  #ifdef DEBUG
  printf("{xbeeModeCommande}\n");
  #endif
  char *cmd="+++";
  sleep(1);
  sync();
  write(ds,cmd,strlen(cmd));
  xbeeReponse(ds);
  }