root / mainbox / tool.c @ cce97007
History | View | Annotate | Download (3.74 KB)
1 |
#include "tool.h" |
---|---|
2 |
#include "query.h" |
3 |
#include "tooltron_mb.h" |
4 |
#include <modbus.h> |
5 |
#include <stdio.h> |
6 |
#include <errno.h> |
7 |
|
8 |
modbus_t *ctx; |
9 |
|
10 |
int tool_init_mb(const char *device) { |
11 |
struct timeval timeout;
|
12 |
|
13 |
ctx = modbus_new_rtu(device, MB_BAUD, 'N', 8, 1); |
14 |
if (ctx == NULL) { |
15 |
fprintf(stderr, "modbus_new_rtu: %s\n", modbus_strerror(errno));
|
16 |
return 1; |
17 |
} |
18 |
|
19 |
if (modbus_connect(ctx) == -1) { |
20 |
fprintf(stderr, "modbus_connect: %s\n", modbus_strerror(errno));
|
21 |
modbus_free(ctx); |
22 |
return 1; |
23 |
} |
24 |
|
25 |
modbus_get_response_timeout(ctx, &timeout); |
26 |
printf("Response timeout is %lus %luus\n", timeout.tv_sec, timeout.tv_usec);
|
27 |
|
28 |
return 0; |
29 |
} |
30 |
|
31 |
void tool_close_mb() {
|
32 |
modbus_close(ctx); |
33 |
modbus_free(ctx); |
34 |
} |
35 |
|
36 |
static void tool_write_coil(int addr, int bit) { |
37 |
if (modbus_write_bit(ctx, addr, bit) == -1) { |
38 |
fprintf(stderr, "modbus_write_bit: %s\n", modbus_strerror(errno));
|
39 |
} |
40 |
} |
41 |
|
42 |
static void tool_init(struct tool_t *tool) { |
43 |
// TODO write current limit values
|
44 |
tool_write_coil(MB_COIL_INIT, 1);
|
45 |
tool->state = TS_OFF; |
46 |
} |
47 |
|
48 |
static void tool_read_user(struct tool_t *tool) { |
49 |
unsigned short serno[2]; |
50 |
if (modbus_read_input_registers(ctx, MB_INP_SERNOL, 2, serno) == -1) { |
51 |
fprintf(stderr, "modbus_read_registers: %s\n", modbus_strerror(errno));
|
52 |
tool->user = 0;
|
53 |
} else {
|
54 |
tool->user = MODBUS_GET_INT32_FROM_INT16(serno, 0);
|
55 |
} |
56 |
} |
57 |
|
58 |
static void tool_grant_access(struct tool_t *tool) { |
59 |
printf("Granting access to %08x on %s (%d)\n", tool->user, tool->name,
|
60 |
tool->address); |
61 |
tool_write_coil(MB_COIL_EN, 1);
|
62 |
tool->state = TS_ON; |
63 |
} |
64 |
|
65 |
static void tool_deny_access(struct tool_t *tool) { |
66 |
printf("Denying access to %08x on %s (%d)\n", tool->user, tool->name,
|
67 |
tool->address); |
68 |
tool_write_coil(MB_COIL_EN, 0);
|
69 |
tool->state = TS_OFF; |
70 |
} |
71 |
|
72 |
void tool_request_disable(struct tool_t *tool) { |
73 |
printf("Requesting disable on %s (%d)\n", tool->name, tool->address);
|
74 |
tool_write_coil(MB_COIL_REQ_DIS, 1);
|
75 |
tool->state = TS_REQ_DIS; |
76 |
} |
77 |
|
78 |
void tool_poll(struct tool_t *tool) { |
79 |
unsigned char status[N_COILS]; |
80 |
|
81 |
if (modbus_set_slave(ctx, tool->address) == -1) { |
82 |
fprintf(stderr, "modbus_set_slave: %s\n", modbus_strerror(errno));
|
83 |
} |
84 |
|
85 |
/* If we can't read from the tool, we only want this error message to print
|
86 |
* once, thus tool->connected */
|
87 |
if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) { |
88 |
if (tool->connected) {
|
89 |
fprintf(stderr, "Cannot connect to %s (%d): %s\n", tool->name,
|
90 |
tool->address, modbus_strerror(errno)); |
91 |
tool->connected = 0;
|
92 |
} |
93 |
return;
|
94 |
} else if (!tool->connected) { |
95 |
fprintf(stderr, "Reconnected to %s (%d)\n", tool->name, tool->address);
|
96 |
tool->connected = 1;
|
97 |
} |
98 |
|
99 |
/*uint16_t current;
|
100 |
if (modbus_read_input_registers(ctx, MB_INP_CURRENT, 1, ¤t) == -1) {
|
101 |
fprintf(stderr, "modbus_read_registers: %s\n", modbus_strerror(errno));
|
102 |
} else {
|
103 |
printf("Current: %d\n", current);
|
104 |
}*/
|
105 |
|
106 |
/*printf("new:%d en:%d req_dis:%d init:%d\n", status[MB_COIL_NEW],
|
107 |
status[MB_COIL_EN], status[MB_COIL_REQ_DIS], status[MB_COIL_INIT]);*/
|
108 |
|
109 |
if (!status[MB_COIL_INIT]) {
|
110 |
tool->state = TS_INIT; |
111 |
} |
112 |
|
113 |
switch (tool->state) {
|
114 |
|
115 |
case TS_INIT:
|
116 |
tool_init(tool); |
117 |
break;
|
118 |
|
119 |
case TS_OFF:
|
120 |
if (status[MB_COIL_NEW]) {
|
121 |
tool_read_user(tool); |
122 |
if (query_user_permission(tool->address, tool->user)) {
|
123 |
tool_grant_access(tool); |
124 |
} else {
|
125 |
tool_deny_access(tool); |
126 |
} |
127 |
} |
128 |
break;
|
129 |
|
130 |
case TS_ON:
|
131 |
if (!status[MB_COIL_EN]) {
|
132 |
printf("Tool %s (%d) is off\n", tool->name, tool->address);
|
133 |
tool->state = TS_OFF; |
134 |
} |
135 |
break;
|
136 |
|
137 |
case TS_REQ_DIS:
|
138 |
if (!status[MB_COIL_EN]) {
|
139 |
printf("Tool %s (%d) is off after requested disable\n", tool->name,
|
140 |
tool->address); |
141 |
tool->state = TS_OFF; |
142 |
} |
143 |
break;
|
144 |
|
145 |
} |
146 |
|
147 |
} |