-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_protocol.c
More file actions
489 lines (438 loc) · 12.9 KB
/
binary_protocol.c
File metadata and controls
489 lines (438 loc) · 12.9 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//*****************************************************************************
//
// BINARY PROTOCOL
//
//*****************************************************************************
#include "uip.h"
#include "binary_protocol.h"
#include "utils/ustdlib.h"
#include "string.h"
#include "drivers/rit128x96x4.h"
#include "../../shared/Services.h"
struct fausto_state *fs;
int initialized;
int GetInputLength(struct binary_message *msg)
{
return availableServices[msg->Service]->input_size;
}
int GetOutputLength(struct binary_message *msg)
{
return availableServices[msg->Service]->output_size;
}
unsigned char GetService(struct binary_message *msg)
{
return (msg->Service);
}
void process_message(struct binary_message *msg) {
fs->state = GetService(msg);
switch(fs->state){
case 1:
break;
default:
break;
}
fs->msg.Service = 255;
fs->count = 0;
}
//*****************************************************************************
//
// Macro for easy access to buffer data
//
//*****************************************************************************
#define BUF_APPDATA ((u8_t *)uip_appdata)
//*****************************************************************************
//
// UDP Connection.
//
//*****************************************************************************
static struct uip_udp_conn *udp_conn = 0;
//*****************************************************************************
//
// FESTO PLC commands (Can be found in Festo Software's (FST) Help)
//
//*****************************************************************************
static const char command_reset[] = "ME1.3=1";
static const char command_start[] = "ME1.0=1";
static const char command_stop[] = "ME1.1=0";
static const char extend_minislide[] = "MA0.0=1";
static const char retract_minislide[] = "MA0.0=0";
static const char open_gripper[] = "MA0.1=0";
static const char close_gripper[] = "MA0.1=1";
static const char linear_motion_0[] = "MA0.5=0";
static const char linear_motion_1[] = "MA0.5=1";
static const char rotary_motion_0[] = "MA0.6=0";
static const char rotary_motion_1[] = "MA0.6=1";
static const char outputword_station[] = "DEW0";
static char set_motion_bit[] = "MA0.X=X";
unsigned char b[2];
//*****************************************************************************
//
// Display a uIP type IP Address.
//
//*****************************************************************************
void
show_ip(void *ipaddr, unsigned long ulCol, unsigned long ulRow)
{
char pucBuf[16];
unsigned char *pucTemp = (unsigned char *)ipaddr;
usprintf(pucBuf, "%d.%d.%d.%d", pucTemp[0], pucTemp[1], pucTemp[2],
pucTemp[3]);
RIT128x96x4StringDraw(pucBuf, ulCol, ulRow, 15);
}
//*****************************************************************************
//
// Initialize the program.
//
// Starts to listen for incoming connection requests on TCP port 12345.
//
//*****************************************************************************
void
fausto_init(void)
{
uip_ipaddr_t addr;
if(udp_conn == 0) {
RIT128x96x4StringDraw("Setting MPS IP...", 12, 32, 15);
uip_ipaddr(&addr, 172, 26, 5, 1);
udp_conn = uip_udp_new(&addr, HTONS(992));
if(udp_conn != 0) {
RIT128x96x4StringDraw("OK", 115, 32, 15);
show_ip(addr, 18, 40);
//uip_udp_bind(udp_conn, HTONS(2222));
//uip_listen(HTONS(2222));
} else {
RIT128x96x4StringDraw("FAIL", 115, 32, 15);
}
}
//
// Listen to port 12345.
//
uip_listen(HTONS(12345));
initialized = 0; // ironic, huh?
}
//*****************************************************************************
//
// FESTO COMMAND Application Callback Function
//
//*****************************************************************************
void serviceMPSReset()
{
RIT128x96x4StringDraw("MPS being reset", 12, 48, 15);
memcpy(uip_appdata, command_reset, sizeof(command_reset));
uip_udp_send(sizeof(command_reset));
}
void serviceMPSStart()
{
RIT128x96x4StringDraw("Starting", 12, 48, 15);
memcpy(uip_appdata, command_start, sizeof(command_start));
uip_udp_send(sizeof(command_start));
}
void serviceMPSStop()
{
RIT128x96x4StringDraw("Stopping", 12, 48, 15);
memcpy(uip_appdata, command_stop, sizeof(command_stop));
uip_udp_send(sizeof(command_stop));
}
void serviceMPSExtendMiniSlide()
{
RIT128x96x4StringDraw("Extend Mini Slide", 12, 48, 15);
memcpy(uip_appdata, extend_minislide, sizeof(extend_minislide));
uip_udp_send(sizeof(extend_minislide));
}
void serviceMPSRetractMiniSlide()
{
RIT128x96x4StringDraw("Retract Mini Slide", 12, 48, 15);
memcpy(uip_appdata, retract_minislide, sizeof(retract_minislide));
uip_udp_send(sizeof(retract_minislide));
}
void serviceMPSOpenGripper()
{
RIT128x96x4StringDraw("Open Gripper", 12, 48, 15);
memcpy(uip_appdata, open_gripper, sizeof(open_gripper));
uip_udp_send(sizeof(open_gripper));
}
void serviceMPSCloseGripper()
{
RIT128x96x4StringDraw("Close Gripper", 12, 48, 15);
memcpy(uip_appdata, close_gripper, sizeof(close_gripper));
uip_udp_send(sizeof(close_gripper));
}
void serviceMPSStatusWord()
{
RIT128x96x4StringDraw("Req MPS Data", 12, 56, 15);
RIT128x96x4StringDraw(" ", 80, 56, 15);
memcpy(uip_appdata, outputword_station, sizeof(outputword_station));
uip_udp_send(sizeof(outputword_station));
}
//*****************************************************************************
//
// Here, Bits 2, 3 and 4 correspond to the I/O Configuration of the PLC.
// Open FST-->Online-->Online Display. Click on Output. Point the mouse to the Row OW0, Bit 2, 3, and 4.
// You will see that Bit 2, 3, and 4 corresponds to Bit 0, Bit 1 and Bit 2.
// That mapping is what has been done here exactly.
// The Bits can be set by sending a 1.
// So, here in the first 3 cases we force the value to 0/1 based on what number the user enters on the C++ appln output terminal.
// Incase, the User enters height 5 and angle 2, the bits are set accordingly.
// To the left of _BIT2 (in FST), you will find Start_LA and Start_RA
// Once the height and angle are entered, we set the Linear and Rotary positions by setting 0/1
//
//*****************************************************************************
void serviceMPSSetPositionLMB2()
{
set_motion_bit[4] = '2';
set_motion_bit[6] = (fs->msg.Data[0] & 0x01) ? '1' : '0';
memcpy(uip_appdata, set_motion_bit, sizeof(set_motion_bit));
uip_udp_send(sizeof(set_motion_bit));
}
void serviceMPSSetPositionLMB3()
{
set_motion_bit[4] = '3';
set_motion_bit[6] = (fs->msg.Data[0] & 0x02) ? '1' : '0';
memcpy(uip_appdata, set_motion_bit, sizeof(set_motion_bit));
uip_udp_send(sizeof(set_motion_bit));
}
void serviceMPSSetPositionLMB4()
{
set_motion_bit[4] = '4';
set_motion_bit[6] = (fs->msg.Data[0] & 0x04) ? '1' : '0';
memcpy(uip_appdata, set_motion_bit, sizeof(set_motion_bit));
uip_udp_send(sizeof(set_motion_bit));
}
void serviceMPSSetPositionLMB0()
{
memcpy(uip_appdata, linear_motion_0, sizeof(linear_motion_0));
uip_udp_send(sizeof(linear_motion_0));
}
void serviceMPSSetPositionLMB1()
{
memcpy(uip_appdata, linear_motion_1, sizeof(linear_motion_1));
uip_udp_send(sizeof(linear_motion_1));
}
void serviceMPSSetPositionRMB2()
{
set_motion_bit[4] = '2';
set_motion_bit[6] = (fs->msg.Data[1] & 0x01) ? '1' : '0';
memcpy(uip_appdata, set_motion_bit, sizeof(set_motion_bit));
uip_udp_send(sizeof(set_motion_bit));
}
void serviceMPSSetPositionRMB3()
{
set_motion_bit[4] = '3';
set_motion_bit[6] = (fs->msg.Data[1] & 0x02) ? '1' : '0';
memcpy(uip_appdata, set_motion_bit, sizeof(set_motion_bit));
uip_udp_send(sizeof(set_motion_bit));
}
void serviceMPSSetPositionRMB4()
{
set_motion_bit[4] = '4';
set_motion_bit[6] = (fs->msg.Data[1] & 0x04) ? '1' : '0';
memcpy(uip_appdata, set_motion_bit, sizeof(set_motion_bit));
uip_udp_send(sizeof(set_motion_bit));
}
void serviceMPSSetPositionRMB0()
{
memcpy(uip_appdata, rotary_motion_1, sizeof(rotary_motion_1));
uip_udp_send(sizeof(rotary_motion_1));
}
void serviceMPSSetPositionRMB1()
{
memcpy(uip_appdata, rotary_motion_0, sizeof(rotary_motion_0));
uip_udp_send(sizeof(rotary_motion_0));
fs->state = 255;
fs->packets_transmitted = 0;
}
void
festo_appcall(void)
{
int i,j;
char *buffer;
char dest[64];
if(!initialized) return;
if(uip_connected()) {
RIT128x96x4StringDraw("Connect", 70, 48, 15);
return;
} else if(uip_poll()) {
/*localService_t localService = localServices[fs->state];
uint8_t serviceId = localService.serviceId;
void* serviceFunction = localService.serviceFunction;
uint8_t* params = 0;
// Calculate the size in bits of all input and output parameters
uint32_t sizeInput = ...;
uint32_t sizeOutput = ...;
params = (uint8_t*)malloc((sizeInput + sizeOutput) >> 3);
// Copy parameters from message to params data structure
memcpy(params, &msg.Data, sizeInput);
serviceFunction(&msg.Data); */
switch (fs->state) {
case 0:
serviceMPSReset();
fs->state = 255; // Fix fs->state to some constant value, so that the state is idle, so that it doesnt do anything.
return;
case 1:
serviceMPSStart();
fs->state = 255;
return;
case 2:
serviceMPSStop();
fs->state = 255;
return;
case 3:
serviceMPSExtendMiniSlide();
fs->state = 255;
return;
case 4:
serviceMPSRetractMiniSlide();
fs->state = 255;
return;
case 5:
serviceMPSOpenGripper();
fs->state = 255;
return;
case 6:
serviceMPSCloseGripper();
fs->state = 255;
return;
case 7:
/* DATA[0] = desired heigth [0-7]
* DATA[1] = desired angle [0-7]
* motion byte n4 = position bit [2, 3, 4] == [0, 1, 2]
* motion byte n6 = bit value [0, 1]
*/
switch(fs->packets_transmitted++) {
case 0:
serviceMPSSetPositionLMB2();
break;
case 1:
serviceMPSSetPositionLMB3();
break;
case 2:
serviceMPSSetPositionLMB4();
break;
case 3:
serviceMPSSetPositionLMB0();
break;
case 4:
serviceMPSSetPositionLMB1();
break;
case 5:
serviceMPSSetPositionRMB2();
break;
case 6:
serviceMPSSetPositionRMB3();
break;
case 7:
serviceMPSSetPositionRMB4();
break;
case 8:
serviceMPSSetPositionRMB0();
break;
case 9:
serviceMPSSetPositionRMB1();
break;
default:
break;
}
return;
case 8:
serviceMPSStatusWord();
return;
default:
return;
}
} else if(uip_newdata()) {
RIT128x96x4StringDraw("Got data", 12, 64, 15);
if (uip_udp_conn->rport == HTONS(992)) {
buffer = (char*)uip_appdata;
for(i=0;i<256;i++){
if(buffer[i] == '=') {
i++;
for(j=0; buffer[i] >= '0' && buffer[i] <= '9' && j<4; j++, i++){
dest[j] = buffer[i];
dest[j+1] = 0;
}
break;
}
}
RIT128x96x4StringDraw(dest, 70, 64, 15);
switch (fs->state) {
case 8:
fs->msg.Data[0] = 0;
for(i=0;dest[i]!=0 && i < 4;i++){
fs->msg.Data[0] *= 10;
fs->msg.Data[0] += dest[i] - '0';
}
fs->packets_transmitted = 1;
break;
default:
break;
}
}
return;
}
}
//*****************************************************************************
//
// HTTP Application Callback Function
// Tried out may steps, just for printf debugging. The LCD screen on the ARM is an advantage, you can see which step you go wrong.
//
//*****************************************************************************
void
binary_protocol_appcall(void)
{
unsigned char *buffer;
int i;
fs = (struct fausto_state *)&(uip_conn->appstate);
buffer = (unsigned char *)&fs->msg;
switch(uip_conn->lport)
{
case HTONS(12345):
if(uip_connected()){
initialized = 1;
RIT128x96x4StringDraw("Connected ", 12, 72, 15);
fs->count = 0;
fs->state = 255;
} else if(uip_poll()) {
RIT128x96x4StringDraw("Polled ", 80, 72, 15);
switch (fs->state) {
case 8:
if(fs->packets_transmitted == 1) {
fs->msg.Service = 8;
uip_send(buffer, 2);
fs->state = 255;
fs->packets_transmitted = 0;
}
break;
default:
// Ok, whatever...
return;
}
} else if(uip_newdata()) {
RIT128x96x4StringDraw("Got_data! ", 12, 96, 15);
b[0] = 0x30 + uip_len;
RIT128x96x4StringDraw((const char *)b, 70, 96, 15);
for(i=0; i < uip_len; i++) buffer[fs->count + i] = BUF_APPDATA[i];
fs->count += uip_len;
if(fs->count >= 1) {
if(GetInputLength(&fs->msg) <= (fs->count - 1)) {
process_message(&fs->msg);
} else {
RIT128x96x4StringDraw("Not enough data", 12, 56, 15);
b[0] = 0x30 + (2-fs->count);
RIT128x96x4StringDraw((const char *)b, 110, 56, 15);
b[0] = GetInputLength(&fs->msg);
RIT128x96x4StringDraw((const char *)b, 110, 64, 15);
}
}
} else if(uip_acked()) {
RIT128x96x4StringDraw("ACKED ", 12, 80, 15);
}
break;
default:
{
//
// Should never happen.
//
uip_abort();
break;
}
}
}