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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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, num_bytes, bytes2read = ARRAY_SIZE-bytes_read;
if((num_bytes = read(fdrs232,ibuf,bytes2read)) <= 0)
{
// printf("Failed to read data from RS-232, err %d (%s).\n",errno, strerror(errno));
return errno;
}
for(i=0; i<num_bytes; i++)
if(ibuf[i] == ARRAY_HEADER)
{
if(got_hdr)
{
cnt_array_err++;
bytes_read = 0;
memset(buf,0,sizeof(buf));
}
else
got_hdr = 1;
j = i;
}
num_bytes -= j;
cnt = j;
/* memcpy(&buf[bytes_read],&ibuf[j],num_bytes); */
bytes_read += num_bytes;
if(bytes_read == ARRAY_SIZE)
{
for(i=cnt, j=0; i<ARRAY_SIZE; i++)
if(!(buf[i] & 0x80))
j++;
bytes_read = 0;
got_hdr = 0;
if(!j)
{
cnt_array_ok++;
memcpy(array,buf,ARRAY_SIZE);
memset(buf,0,sizeof(buf));
return 1;
}
else
{
cnt_array_err++;
memset(buf,0,sizeof(buf));
return -1;
}
}
return 0;
}