forked from Orienfish/ur3-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodbusrtu.c
396 lines (354 loc) · 8.54 KB
/
modbusrtu.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
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
/****************************************************
* This file control the gripper through modbus rtu
* Modified by xfyu on Jan 22
****************************************************/
#include "modbusrtu.h"
#include "main.h"
/****************************************************
* Instruction Set
****************************************************/
unsigned char activate[] = {
0x09, 0x10,
0x03, 0xe8,
0x00, 0x03,
0x06,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x73, 0x30
};
unsigned char read_gripper_status[] = {
0x09, 0x03,
0x07, 0xd0,
0x00, 0x01,
0x85, 0xcf
};
unsigned char activate_success[] = {
0x09, 0x03,
0x02,
0x00, 0x00,
0x59, 0x85
};
unsigned char close_with_certain_speed_certain_force[] = {
0x09, 0x10,
0x03, 0xe8,
0x00, 0x03,
0x06,
0x09, 0x00,
0x00, 0xff,
0xff, 0xff,
0x42, 0x29
};
unsigned char read_until_grip_completed[] = {
0x09, 0x03,
0x07, 0xd0,
0x00, 0x03,
0x04, 0x0e
};
unsigned char grip_is_completed1[] = {
0x09, 0x03,
0x02, 0xb9,
0x00, 0x2a, 0x15
};
unsigned char grip_is_completed2[] = {
0x09, 0x03,
0x02, 0xf9,
0x00, 0x1b, 0xd5
};
unsigned char open_with_certain_speed_certain_force[] = {
0x09, 0x10,
0x03, 0xe8,
0x00, 0x03,
0x06,
0x09, 0x00,
0x00, 0x00,
0xff, 0xff,
0x72, 0x19
};
unsigned char read_until_open_completed[] = {
0x09, 0x03,
0x07, 0xd0,
0x00, 0x03,
0x04, 0x0e
};
unsigned char open_is_completed[] = {
0x09, 0x03,
0x06,
0xf9, 0x00,
0x00, 0x00,
0x03, 0x00,
0x52, 0x2c
};
/****************************************************
* Functions
****************************************************/
/*
* bufcmp - Compare the recv buf with what we
* already have
*
* Input: s1 - addr of the first buf
* s2 - addr of the second buf
* Return Value: 0 - same
* 1 - different
* We don't have to know which buf is smaller, we
* only care whether they are same.
*/
int bufcmp(unsigned char *s1, unsigned char *s2) {
int len1 = sizeof(s1) - 1;
int len2 = sizeof(s2) - 1;
// printf("%d %d\n", len1, len2);
if (len1 != len2)
return 1; /* match fail */
for (int i = 0; i < len1; ++i)
if (s1[i] != s2[i])
return 1;
return 0;
}
/*
* open_modbus - open the serial port
*
* Return Value: >0 - the fd. success
* <=0 - fail
*/
int open_modbus() {
int fd;
fd = open(MODBUS_DEV, O_RDWR);
if (fd < 0) {
perror("open tty error");
return -1;
}
struct termios options;
tcgetattr(fd, &options);
memset(&options, 0, sizeof(options));
// options.c_cflag |= CLOCAL | CREAD;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; /* 8 data bit */
options.c_cflag &= ~PARENB; /* no parity */
options.c_cflag &= ~CSTOPB; /* 1 stop bit */
/* set the baudrate */
if (cfsetispeed(&options, BAUDRATE) < 0) {
perror("baudrate seti error");
return -1;
}
if (cfsetospeed(&options, BAUDRATE) < 0) {
perror("baudrate seto error");
return -1;
}
/* set the wait time */
options.c_cc[VTIME] = 10;
options.c_cc[VMIN] = 4;
/* bind the options to fd */
if (tcsetattr(fd, TCSANOW, &options) < 0) {
perror("attr set error");
return -1;
}
return fd;
}
/*
* gripper_activate - activate the gripper
*
* Return Value: 0 - success
* -1 - fail
*/
int gripper_activate() {
int fd;
int read_cnt;
unsigned char recv_buf[BUF_SIZE];
if ((fd = open_modbus()) < 0)
return -1;
/* activate */
if (write(fd, activate, sizeof(activate)) < 0) {
perror("write error");
return -1;
}
/* #ifdef DEBUG
if ((read_cnt = read(fd, recv_buf, BUF_SIZE)) < 0) {
perror("read error");
return -1;
}
fprintf(stdout, "Activate Receive: ");
for (int i = 0; i < read_cnt; ++i)
fprintf(stdout, "0x%x ", recv_buf[i]);
fprintf(stdout, "\n");
#endif */
while (1) {
if (write(fd, read_gripper_status,
sizeof(read_gripper_status)) < 0) {
perror("write error");
return -1;
}
/* recv gripper status */
if ((read_cnt = read(fd, recv_buf, BUF_SIZE)) < 0) {
perror("read error");
return -1;
}
#ifdef DEBUG
fprintf(stdout, "Activate Receive: ");
for (int i = 0; i < read_cnt; ++i)
fprintf(stdout, "0x%x ", recv_buf[i]);
fprintf(stdout, "\n");
#endif
if (!bufcmp(activate_success, recv_buf))
break; /* complete */
else
continue; /* not complete */
}
close(fd);
return 0;
}
/*
* gripper_close - close the gripper
*
* Input: speed: 0-255
* force: 0-255
* Return Value: 0 - success
* -1 - fail
*/
int gripper_close(unsigned char speed, unsigned char force) {
int fd;
int read_cnt;
unsigned char recv_buf[BUF_SIZE];
/* generate certain instruction */
Generate_Open_Close_Instruct(speed, force);
if ((fd = open_modbus()) < 0)
return -1;
/* grip */
if (write(fd, close_with_certain_speed_certain_force,
sizeof(close_with_certain_speed_certain_force)) < 0) {
perror("write error");
return -1;
}
while (1) {
if (write(fd, read_gripper_status,
sizeof(read_gripper_status)) < 0) {
perror("write error");
return -1;
}
/* recv gripper status */
if ((read_cnt = read(fd, recv_buf, BUF_SIZE)) < 0) {
perror("read error");
return -1;
}
#ifdef DEBUG
fprintf(stdout, "Close Receive: ");
for (int i = 0; i < read_cnt; ++i)
fprintf(stdout, "0x%x ", recv_buf[i]);
fprintf(stdout, "\n");
#endif
if (!bufcmp(grip_is_completed1, recv_buf))
break; /* complete 1 */
else if (!bufcmp(grip_is_completed2, recv_buf))
break; /* complete 2 */
else
continue; /* not complete */
}
close(fd);
return 0;
}
/*
* gripper_open - open the gripper
*
* Input: speed: 0-255
* force: 0-255
* Return Value: 0 - success
* -1 - fail
*/
int gripper_open(unsigned char speed, unsigned char force) {
int fd;
int read_cnt;
unsigned char recv_buf[BUF_SIZE];
/* generate certain instruction */
Generate_Open_Close_Instruct(speed, force);
if ((fd = open_modbus()) < 0)
return -1;
/* open */
if (write(fd, open_with_certain_speed_certain_force,
sizeof(open_with_certain_speed_certain_force)) < 0) {
perror("write error");
return -1;
}
while (1) {
if (write(fd, read_until_open_completed,
sizeof(read_until_open_completed)) < 0) {
perror("write error");
return -1;
}
/* recv gripper status */
if ((read_cnt = read(fd, recv_buf, BUF_SIZE)) < 0) {
perror("read error");
return -1;
}
#ifdef DEBUG
fprintf(stdout, "Open Receive: ");
for (int i = 0; i < read_cnt; ++i)
fprintf(stdout, "0x%x ", recv_buf[i]);
fprintf(stdout, "\n");
#endif
if (!bufcmp(open_is_completed, recv_buf))
break; /* complete */
else
continue; /* not complete */
}
close(fd);
return 0;
}
/*
* ModBusCRC - compute the crc of a certain string
*
* Parameter: ptr - the start of that string
* size - the length of that string
* Return value: the crc result, a short.
*/
unsigned short ModBusCRC(unsigned char * ptr, unsigned char size) {
unsigned short a, b, tmp, CRC16;
CRC16 = 0xffff; /* initiate CRC16 register value */
for (a = 0; a < size; ++a) {
CRC16 = *ptr ^ CRC16;
for (b = 0; b < 8; ++b) {
tmp = CRC16 & 0x0001;
CRC16 >>= 1;
if (tmp) /* check the bit that move out */
CRC16 = CRC16 ^ 0xa001;
}
ptr++; /* move to the next byte */
}
return ((CRC16 & 0xFF) << 8) | ((CRC16 & 0xFF00) >> 8);
}
/*
* Generate_Open_Close_Instruct - generate the specific intruction
* according to the Macro Definitions in main.h
*/
void Generate_Open_Close_Instruct(unsigned char speed, unsigned char force) {
unsigned char length;
unsigned short CRC;
/* generate open instruction */
open_with_certain_speed_certain_force[11] = speed;
open_with_certain_speed_certain_force[12] = force;
length = sizeof(open_with_certain_speed_certain_force) - 2;
/* Calculate the CRC */
CRC = ModBusCRC(open_with_certain_speed_certain_force, length);
/* Add the CRC to the result */
open_with_certain_speed_certain_force[13] = CRC >> 8;
open_with_certain_speed_certain_force[14] = CRC & 0xff;
/* generate close instruction */
close_with_certain_speed_certain_force[11] = speed;
close_with_certain_speed_certain_force[12] = force;
/* Calculate the CRC, length is same */
CRC = ModBusCRC(close_with_certain_speed_certain_force, length);
/* Add the CRC to the result */
close_with_certain_speed_certain_force[13] = CRC >> 8;
close_with_certain_speed_certain_force[14] = CRC & 0xff;
#ifdef DEBUG
length += 2;
char buf[BUF_SIZE];
sprintf(buf, "Open Instruction: ");
for (int i = 0; i < length; ++i)
sprintf(buf, "%s0x%x ", buf, open_with_certain_speed_certain_force[i]);
fprintf(stdout, "%s\r\n", buf);
sprintf(buf, "Close Instruction: ");
for (int i = 0; i < length; ++i)
sprintf(buf, "%s0x%x ", buf, close_with_certain_speed_certain_force[i]);
fprintf(stdout, "%s\r\n", buf);
#endif
return;
}