root / mainbox / tool.c @ cce97007
History | View | Annotate | Download (3.74 KB)
1 | 7bdb98c5 | Tom Mullins | #include "tool.h" |
---|---|---|---|
2 | 15fa09f7 | Tom Mullins | #include "query.h" |
3 | 7bdb98c5 | Tom Mullins | #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 | 14688426 | Tom Mullins | struct timeval timeout;
|
12 | |||
13 | 7bdb98c5 | Tom Mullins | 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 | 14688426 | Tom Mullins | modbus_get_response_timeout(ctx, &timeout); |
26 | printf("Response timeout is %lus %luus\n", timeout.tv_sec, timeout.tv_usec);
|
||
27 | |||
28 | 7bdb98c5 | Tom Mullins | 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 | 3811693d | Tom Mullins | static void tool_read_user(struct tool_t *tool) { |
49 | 7bdb98c5 | Tom Mullins | unsigned short serno[2]; |
50 | 14688426 | Tom Mullins | if (modbus_read_input_registers(ctx, MB_INP_SERNOL, 2, serno) == -1) { |
51 | 7bdb98c5 | Tom Mullins | 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 | 15fa09f7 | Tom Mullins | printf("Granting access to %08x on %s (%d)\n", tool->user, tool->name,
|
60 | tool->address); |
||
61 | 7bdb98c5 | Tom Mullins | tool_write_coil(MB_COIL_EN, 1);
|
62 | dc472500 | Tom Mullins | tool->state = TS_ON; |
63 | 7bdb98c5 | Tom Mullins | } |
64 | |||
65 | static void tool_deny_access(struct tool_t *tool) { |
||
66 | 15fa09f7 | Tom Mullins | printf("Denying access to %08x on %s (%d)\n", tool->user, tool->name,
|
67 | tool->address); |
||
68 | 7bdb98c5 | Tom Mullins | tool_write_coil(MB_COIL_EN, 0);
|
69 | dc472500 | Tom Mullins | tool->state = TS_OFF; |
70 | 7bdb98c5 | Tom Mullins | } |
71 | |||
72 | void tool_request_disable(struct tool_t *tool) { |
||
73 | 15fa09f7 | Tom Mullins | printf("Requesting disable on %s (%d)\n", tool->name, tool->address);
|
74 | 7bdb98c5 | Tom Mullins | tool_write_coil(MB_COIL_REQ_DIS, 1);
|
75 | dc472500 | Tom Mullins | tool->state = TS_REQ_DIS; |
76 | 7bdb98c5 | Tom Mullins | } |
77 | |||
78 | void tool_poll(struct tool_t *tool) { |
||
79 | unsigned char status[N_COILS]; |
||
80 | |||
81 | 14688426 | Tom Mullins | if (modbus_set_slave(ctx, tool->address) == -1) { |
82 | 7bdb98c5 | Tom Mullins | fprintf(stderr, "modbus_set_slave: %s\n", modbus_strerror(errno));
|
83 | } |
||
84 | |||
85 | 15fa09f7 | Tom Mullins | /* If we can't read from the tool, we only want this error message to print
|
86 | * once, thus tool->connected */
|
||
87 | 7bdb98c5 | Tom Mullins | if (modbus_read_bits(ctx, 0, N_COILS, status) == -1) { |
88 | 15fa09f7 | Tom Mullins | 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 | 7bdb98c5 | Tom Mullins | return;
|
94 | 15fa09f7 | Tom Mullins | } else if (!tool->connected) { |
95 | fprintf(stderr, "Reconnected to %s (%d)\n", tool->name, tool->address);
|
||
96 | tool->connected = 1;
|
||
97 | 7bdb98c5 | Tom Mullins | } |
98 | |||
99 | 15fa09f7 | Tom Mullins | /*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 | 1b054655 | Tom Mullins | /*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 | dc472500 | Tom Mullins | |
109 | 7bdb98c5 | Tom Mullins | 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 | 3811693d | Tom Mullins | tool_read_user(tool); |
122 | 15fa09f7 | Tom Mullins | if (query_user_permission(tool->address, tool->user)) {
|
123 | 7bdb98c5 | Tom Mullins | tool_grant_access(tool); |
124 | 1b054655 | Tom Mullins | } else {
|
125 | 7bdb98c5 | Tom Mullins | tool_deny_access(tool); |
126 | 1b054655 | Tom Mullins | } |
127 | 7bdb98c5 | Tom Mullins | } |
128 | break;
|
||
129 | |||
130 | case TS_ON:
|
||
131 | if (!status[MB_COIL_EN]) {
|
||
132 | 15fa09f7 | Tom Mullins | printf("Tool %s (%d) is off\n", tool->name, tool->address);
|
133 | 7bdb98c5 | Tom Mullins | tool->state = TS_OFF; |
134 | } |
||
135 | break;
|
||
136 | |||
137 | case TS_REQ_DIS:
|
||
138 | if (!status[MB_COIL_EN]) {
|
||
139 | 15fa09f7 | Tom Mullins | printf("Tool %s (%d) is off after requested disable\n", tool->name,
|
140 | tool->address); |
||
141 | 7bdb98c5 | Tom Mullins | tool->state = TS_OFF; |
142 | } |
||
143 | break;
|
||
144 | |||
145 | } |
||
146 | |||
147 | } |