int get_array static uint8_t buf ARRAY_SIZE uint8_t ibuf ARRAY_SIZE st

 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
int get_array()
{
static uint8_t buf[ARRAY_SIZE];
uint8_t ibuf[ARRAY_SIZE] = {0};
static int bytes_read = 0, got_hdr = 0;
int i, j = 0, n = 0, num_bytes;
if((num_bytes = read (fdrs232, &buf[bytes_read], ARRAY_SIZE - bytes_read)) <= 0) {
return errno;
}
for(i = bytes_read; i < bytes_read+num_bytes; i++) {
if (!(buf[i] & 0x80)) {
++n;
}
if(buf[i] == ARRAY_HEADER) {
if(got_hdr) {
++cnt_array_err;
bytes_read = 0;
}
else
got_hdr = 1;
j = i;
n = 0;
}
}
num_bytes -= j;
bytes_read += num_bytes;
if(bytes_read == ARRAY_SIZE) {
bytes_read = 0;
got_hdr = 0;
if(!n) {
cnt_array_ok++;
memcpy(array,buf,ARRAY_SIZE);
return 1;
} else {
cnt_array_err++;
return -1;
}
}
return 0;
}