root / main.c @ 20e5429c
History | View | Annotate | Download (2.77 KB)
1 |
#include <stdint.h> |
---|---|
2 |
#include <util/delay.h> |
3 |
#include "mb.h" |
4 |
#include "mbport.h" |
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 |
} |
81 |
|
82 |
eMBErrorCode eMBRegCoilsCB(UCHAR *reg_buf, USHORT addr, USHORT num, |
83 |
eMBRegisterMode mode) { |
84 |
if (mode == MB_REG_WRITE) {
|
85 |
return MB_ENOREG;
|
86 |
} else if (mode == MB_REG_READ) { |
87 |
return MB_ENOREG;
|
88 |
} else {
|
89 |
return MB_EIO;
|
90 |
} |
91 |
} |
92 |
|
93 |
eMBErrorCode eMBRegDiscreteCB(UCHAR *reg_buf, USHORT addr, USHORT num) { |
94 |
return MB_ENOREG;
|
95 |
} |
96 |
|
97 |
eMBErrorCode eMBRegInputCB(UCHAR *reg_buf, USHORT addr, USHORT num) { |
98 |
int i;
|
99 |
switch (addr) {
|
100 |
case 0: |
101 |
// TODO #define addresses as TYPE_NAME, eg INPUT_SERNO1
|
102 |
// TODO test whether the following works as expected (8 or 16 bit buf?)
|
103 |
for (i = 0; i < 2*num; i++) { |
104 |
reg_buf[i] = i; |
105 |
} |
106 |
return MB_ENOERR;
|
107 |
default:
|
108 |
return MB_ENOREG;
|
109 |
} |
110 |
} |
111 |
|
112 |
eMBErrorCode eMBRegHoldingCB(UCHAR *reg_buf, USHORT addr, USHORT num, |
113 |
eMBRegisterMode mode) { |
114 |
if (mode == MB_REG_WRITE) {
|
115 |
return MB_ENOREG;
|
116 |
} else if (mode == MB_REG_READ) { |
117 |
return MB_ENOREG;
|
118 |
} else {
|
119 |
return MB_EIO;
|
120 |
} |
121 |
} |
122 |
|
123 |
int main() {
|
124 |
// init rfid's serial
|
125 |
serial_init(); |
126 |
|
127 |
// init modbus and register callbacks
|
128 |
eMBInit(MB_RTU, SLAVE_ADDR, 0, MB_BAUD, MB_PAR_NONE);
|
129 |
eMBEnable(); |
130 |
|
131 |
while (1) { |
132 |
rfid_read(); |
133 |
eMBPoll(); |
134 |
_delay_ms(200);
|
135 |
} |
136 |
|
137 |
return 0; |
138 |
} |