-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodbus_handler_vcstap.c
278 lines (226 loc) · 7.11 KB
/
modbus_handler_vcstap.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#define MAX_DATA_REGISTER 100
#define MIN_CONFIG_REGISTER 1000
#define MAX_CONFIG_REGISTER 1011
int16 map_modbus(int16 addr) {
int16 val;
int8 i;
/* VCS registers */
if ( addr >= 3 && addr <= 83 ) {
addr -= 3;
i=addr % 2;
if ( 1 == i )
addr -= 1;
addr = addr >> 1;
timer.vcs_read_lock=1;
return 0;
}
/* XRW2G data */
switch ( addr ) {
/* data */
case 0: return 0; /* VCS control register ... always reads as 0 */
case 1: return timer.vcs_query_age;
case 2: return timer.xrw2g_age;
/* configuration */
case 1000: return config.serial_prefix;
case 1001: return config.serial_number;
case 1002: return 'V';
case 1003: return 'C';
case 1004: return 'S';
case 1005: return 0;
case 1006: return config.modbus_address;
case 1007: return config.sensor_source;
case 1008: return config.pair_serial_prefix;
case 1009: return config.pair_serial_number;
case 1010: return config.world_to_xbee;
case 1011: return config.world_to_xport;
/* we should have range checked, and never gotten here */
default: return 65535;
}
}
int8 modbus_valid_read_registers(int16 start, int16 end) {
if ( start <= MAX_DATA_REGISTER && end <= MAX_DATA_REGISTER+1 )
return 1;
if ( start >= MIN_CONFIG_REGISTER && end <= MAX_CONFIG_REGISTER+1 )
return 1;
return 0;
}
int8 modbus_valid_write_registers(int16 start, int16 end) {
/* factory unlock */
if ( 19999==start && 20000==end)
return 1;
/* write eeprom */
if ( start >= 1998 && end <= 2000+1 )
return 1;
if ( start >= MIN_CONFIG_REGISTER && end <= MAX_CONFIG_REGISTER+1 )
return 1;
return 0;
}
void modbus_read_register_response(int8 address, int16 start_address, int16 register_count ) {
int16 i;
int16 l;
modbus_serial_send_start(address, FUNC_READ_HOLDING_REGISTERS);
modbus_serial_putc(register_count*2);
for( i=0 ; i<register_count ; i++ ) {
l=map_modbus(start_address+i);
modbus_serial_putc(make8(l,1));
modbus_serial_putc(make8(l,0));
}
modbus_serial_send_stop();
}
/*
try to write the specified register
if successful, return 0, otherwise return a modbus exception
*/
exception modbus_write_register(int16 address, int16 value) {
/* if we have been unlocked, then we can modify serial number */
if ( timer.factory_unlocked ) {
if ( 1000 == address ) {
config.serial_prefix=value;
return 0;
} else if ( 1001 == address ) {
config.serial_number=value;
return 0;
}
}
/* publicly writeable addresses */
switch ( address ) {
case 1006:
/* Modbus address {0 to 127} */
if ( value > 127 ) return ILLEGAL_DATA_VALUE;
config.modbus_address=value;
break;
case 1007:
if ( value > 1 ) return ILLEGAL_DATA_VALUE;
config.sensor_source=value;
break;
case 1008:
if ( value > 255 ) return ILLEGAL_DATA_VALUE;
config.pair_serial_prefix=value;
break;
case 1009:
config.pair_serial_number=value;
break;
case 1010:
if ( value > 1 ) return ILLEGAL_DATA_VALUE;
config.world_to_xbee=value;
break;
case 1011:
if ( value > 1 ) return ILLEGAL_DATA_VALUE;
config.world_to_xport=value;
break;
case 1997:
/* write default config to EEPROM */
if ( 1 != value ) return ILLEGAL_DATA_VALUE;
reset_cpu();
case 1998:
/* write default config to EEPROM */
if ( 1 != value ) return ILLEGAL_DATA_VALUE;
write_default_param_file();
break;
case 1999:
/* write config to EEPROM */
if ( 1 != value ) return ILLEGAL_DATA_VALUE;
write_param_file();
break;
case 19999:
/* unlock factory programming registers when we get 1802 in passcode register */
if ( 1802 != value ) {
timer.factory_unlocked=0;
return ILLEGAL_DATA_VALUE;
}
timer.factory_unlocked=1;
/* green LED for 2 seconds */
timer.led_on_green=200;
timer.led_on_red=0;
break;
default:
return ILLEGAL_DATA_ADDRESS;
}
/* must not have triggered an exception */
return 0;
}
void modbus_process(void) {
int16 start_addr;
int16 num_registers;
exception result;
int8 i;
/* check for message */
if ( modbus_kbhit() ) {
/* check if it is addressed to us */
if ( modbus_rx.address==config.modbus_address ) {
/* green LED for 200 milliseconds */
timer.led_on_green=20;
timer.led_on_red=0;
switch(modbus_rx.func) {
case FUNC_READ_HOLDING_REGISTERS: /* 3 */
case FUNC_READ_INPUT_REGISTERS: /* 4 */
start_addr=make16(modbus_rx.data[0],modbus_rx.data[1]);
num_registers=make16(modbus_rx.data[2],modbus_rx.data[3]);
/* make sure our address is within range */
if ( ! modbus_valid_read_registers(start_addr,start_addr+num_registers) ) {
modbus_exception_rsp(config.modbus_address,modbus_rx.func,ILLEGAL_DATA_ADDRESS);
timer.modbus_last_error=ILLEGAL_DATA_ADDRESS;
/* red LED for 1 second */
timer.led_on_red=100;
timer.led_on_green=0;
} else {
modbus_read_register_response(config.modbus_address,start_addr,num_registers);
}
break;
case FUNC_WRITE_SINGLE_REGISTER: /* 6 */
start_addr=make16(modbus_rx.data[0],modbus_rx.data[1]);
/* try the write */
result=modbus_write_register(start_addr,make16(modbus_rx.data[2],modbus_rx.data[3]));
if ( result ) {
/* exception */
modbus_exception_rsp(config.modbus_address,modbus_rx.func,result);
timer.modbus_last_error=result;
/* red LED for 1 second */
timer.led_on_red=100;
timer.led_on_green=0;
} else {
/* no exception, send ack */
modbus_write_single_register_rsp(config.modbus_address,
start_addr,
make16(modbus_rx.data[2],modbus_rx.data[3])
);
}
break;
case FUNC_WRITE_MULTIPLE_REGISTERS: /* 16 */
start_addr=make16(modbus_rx.data[0],modbus_rx.data[1]);
num_registers=make16(modbus_rx.data[2],modbus_rx.data[3]);
/* attempt to write each register. Stop if exception */
for ( i=0 ; i<num_registers ; i++ ) {
result=modbus_write_register(start_addr+i,make16(modbus_rx.data[5+i*2],modbus_rx.data[6+i*2]));
if ( result ) {
/* exception */
modbus_exception_rsp(config.modbus_address,modbus_rx.func,result);
timer.modbus_last_error=result;
/* red LED for 1 second */
timer.led_on_red=100;
timer.led_on_green=0;
break;
}
}
/* we could have gotten here with an exception already send, so only send if no exception */
if ( 0 == result ) {
/* no exception, send ack */
modbus_write_multiple_registers_rsp(config.modbus_address,start_addr,num_registers);
}
break;
default:
/* we don't support most operations, so return ILLEGAL_FUNCTION exception */
modbus_exception_rsp(config.modbus_address,modbus_rx.func,ILLEGAL_FUNCTION);
timer.modbus_last_error=ILLEGAL_FUNCTION;
/* red led for 1 second */
timer.led_on_red=100;
timer.led_on_green=0;
}
} else {
/* MODBUS packet for somebody else */
/* yellow LED 200 milliseconds */
timer.led_on_green=20;
timer.led_on_red=20;
}
}
}