Revision 9b2b6d91
Moved rfid functions into their own file
main.c | ||
---|---|---|
3 | 3 |
#include "mb.h" |
4 | 4 |
#include "mbport.h" |
5 | 5 |
#include "tooltron_mb.h" |
6 |
#include "serial.h" |
|
7 |
|
|
8 |
#define RFID_OK 1 |
|
9 |
|
|
10 |
static char rfid_read_cmd[] = {'!', 'R', 'W', 1, 32}; |
|
11 |
|
|
12 |
#define RFID_SERNO_SIZE 4 |
|
13 |
static char rfid_serno[RFID_SERNO_SIZE]; |
|
14 |
|
|
15 |
static void rfid_zero_serno() { |
|
16 |
int i; |
|
17 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
18 |
rfid_serno[i] = 0; |
|
19 |
} |
|
20 |
} |
|
21 |
|
|
22 |
static void rfid_read_serno() { |
|
23 |
int i; |
|
24 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
25 |
rfid_serno[i] = serial_read_blocking(); |
|
26 |
} |
|
27 |
} |
|
28 |
|
|
29 |
static char rfid_check_serno() { |
|
30 |
int i; |
|
31 |
for (i = 0; i < RFID_SERNO_SIZE; i++) { |
|
32 |
if (rfid_serno[i] != serial_read_blocking()) { |
|
33 |
return 0; |
|
34 |
} |
|
35 |
} |
|
36 |
return 1; |
|
37 |
} |
|
38 |
|
|
39 |
/* |
|
40 |
* When reading, it must read the same value RFID_MIN_OK times. If it |
|
41 |
* encounters RFID_MAX_ERRS read errors first, it will output all 0's. |
|
42 |
*/ |
|
43 |
#define RFID_MAX_ERRS 10 |
|
44 |
#define RFID_MIN_OK 5 |
|
45 |
|
|
46 |
static void rfid_read_safe() { |
|
47 |
char n_err = 0, n_ok = 0; |
|
48 |
while (1) { |
|
49 |
serial_write(rfid_read_cmd, sizeof(rfid_read_cmd)); |
|
50 |
if (serial_read_blocking() == RFID_OK) { |
|
51 |
if (n_ok == 0) { |
|
52 |
rfid_read_serno(); |
|
53 |
} else if (!rfid_check_serno()) { |
|
54 |
rfid_zero_serno(); |
|
55 |
return; |
|
56 |
} |
|
57 |
n_ok++; |
|
58 |
if (n_ok >= RFID_MIN_OK) { |
|
59 |
return; |
|
60 |
} |
|
61 |
} else { |
|
62 |
n_err++; |
|
63 |
if (n_err >= RFID_MAX_ERRS) { |
|
64 |
rfid_zero_serno(); |
|
65 |
return; |
|
66 |
} |
|
67 |
} |
|
68 |
_delay_ms(50); |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
static void rfid_read() { |
|
73 |
while (1) { |
|
74 |
serial_write(rfid_read_cmd, sizeof(rfid_read_cmd)); |
|
75 |
if (serial_read_blocking() == RFID_OK) { |
|
76 |
rfid_read_serno(); |
|
77 |
return; |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
6 |
#include "rfid.h" |
|
81 | 7 |
|
82 | 8 |
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT num, |
83 | 9 |
eMBRegisterMode mode) { |
... | ... | |
98 | 24 |
int i; |
99 | 25 |
switch (addr) { |
100 | 26 |
case 0: |
101 |
// TODO #define addresses as TYPE_NAME, eg INPUT_SERNO1 |
|
102 | 27 |
// TODO test whether the following works as expected (8 or 16 bit buf?) |
103 | 28 |
for (i = 0; i < 2*num; i++) { |
104 | 29 |
reg_buf[i] = i; |
... | ... | |
121 | 46 |
} |
122 | 47 |
|
123 | 48 |
int main() { |
124 |
// init rfid's serial |
|
125 |
serial_init(); |
|
49 |
rfid_init(); |
|
126 | 50 |
|
127 |
// init modbus and register callbacks |
|
128 | 51 |
eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE); |
129 | 52 |
eMBEnable(); |
130 | 53 |
|
Also available in: Unified diff