-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbee.c
191 lines (150 loc) · 4.44 KB
/
zbee.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#define __WINDOWS_COM__
#include "com/serial.h"
#include "zbee.h"
/*
// Simulation
#define TIMEOUT 1
#define SLEEP_UNIT 1
*/
// With hardware
#define TIMEOUT 100
#define SLEEP_UNIT 150
void
finalize_serial(HANDLE fd)
{
//Set_Configure_Port(fd,OldConf); // Restore previous configuration.
Close_Port(fd); // Close.
}
int
force_exit(HANDLE fd, char* string)
{
printf("FORCE EXIT: %s\n", string);
finalize_serial(fd);
system("PAUSE");
exit(-1);
}
HANDLE
initialize_serial(char* puerto)
{
HANDLE fd;
fd = Open_Port(puerto); // Open the serial port.
//OldConf = Get_Configure_Port(fd); // Save the previous configuration.
Configure_Port(fd,B9600,"8N1"); // Use the current configuration.
return fd;
}
int
read_all(HANDLE fd, char* data)
{
int a;
if((a = Kbhit_Port(fd))!=0){ // Is there something to read from serial port?
Read_Port(fd,data,a); // Then read it and show it.
data[a] = 0;
//printf("Data received: '%s'\n",data);
}
return a;
}
void
send_data_to(HANDLE fd, address dest, char* data, int size)
{
char buff[16];
printf("Sending data to %c%c%c%c: '%s'...\n", dest[0], dest[1], dest[2], dest[3], data);
sprintf(buff, "ATDL%c%c%c%c", dest[0], dest[1], dest[2], dest[3]);
write_AT_command(fd, (char*)buff);
Write_Port(fd,data,size); // Write the serial port.
}
void
send_string_to(HANDLE fd, address dest, char* data)
{
send_data_to(fd, dest, data, strlen(data));
}
int
check_OK_retrieved(HANDLE fd)
{
char buff[1024];
int size;
int res;
//printf("Waiting for OK response...");
res = Read_Port_Blocking(fd, buff);
if (res > 1)
{
if (strncmp("OK", buff, 2)==0)
return TRUE;
}
return FALSE;
}
int
Read_Port_Blocking(HANDLE fd, char* buff)
{
int size_to_read = 0;
int time_out=0;
do
{
time_out++;
Sleep(SLEEP_UNIT);
size_to_read = Kbhit_Port(fd); /* How many bytes are available to read? */
}
while(size_to_read == 0 && time_out < TIMEOUT);
Read_Port(fd, buff, size_to_read);
//printf("Read port blocking time_out %d size_to_read %d\n", time_out, size_to_read);
return size_to_read;
}
int
write_AT_command(HANDLE fd, char* command)
{
/* Clean the buffer. */
int size;
int res;
char buff[1024];
Sleep(SLEEP_UNIT*10); /* No too many commands in a short period of time! */
res = Clean_Buffer(fd);
if (res != TRUE)
force_exit(fd, "Clean buffer");
printf("AT Command: %s\n", command);
Write_Port(fd,"+++",3); /* Enter to command mode. */
if (check_OK_retrieved(fd))
{
Write_Port(fd,command,strlen(command)); /* Send command. */
Write_Port(fd,"\r",1); /* Enter command. */
if (check_OK_retrieved(fd))
{
Write_Port(fd,"ATCN",4); /* Exit command mode. */
//Sleep(500);
Write_Port(fd,"\r" , 1);
if (check_OK_retrieved(fd))
{
printf("Command sent OK.\n");
return TRUE;
}
}
}
printf("ERROR: Cannot send %s command.\n", command);
return FALSE;
}
void
change_local_address(HANDLE fd, address source)
{
char command_my_address[10];
sprintf(command_my_address, "ATMY%c%c%c%c",
source[0],
source[1],
source[2],
source[3]);
write_AT_command(fd, command_my_address); /* MY ADDRESS */
}
void
initialize_zigbee_module(HANDLE* fdr, char* serial_port, address source)
{
int res;
char buff[1024];
HANDLE fd;
/* Initialize serial port. */
fd = initialize_serial(serial_port);
printf("Initializing ZigBee module...\n");
change_local_address(fd, source);
write_AT_command(fd, "ATDH0000"); /* DEST. ADDRESS */
write_AT_command(fd, "ATDLFFFF"); /* DEST. ADDRESS */
write_AT_command(fd, "ATCH0x0C"); /* CHANNEL ID */
write_AT_command(fd, "ATID0000"); /* PAN ID */
write_AT_command(fd, "ATMM2"); /* ACK */
*fdr = fd;
}