40 unsigned char connect_block[22];
42 memcpy(
cb,connect_block,
sizeof(
cb));
52 static const unsigned char default_connect_block[] =
53 {3,0,0,22,0x11,0xE0,0x00,0x00,0x00,0x01,0x00,0xC1,2,1 ,0 ,0xC2,2,0 ,1 ,0xC0,1,9};
54 memcpy(connect_block,default_connect_block,
sizeof(default_connect_block));
60 memcpy(
cb,connect_block,
sizeof(
cb));
67 memcpy(connect_block,
cb,
sizeof(
cb));
74 static const unsigned char s7_200_connect_block[] =
75 {3,0,0,22,0x11,0xE0,0x00,0x00,0x00,0x01,0x00,0xC1,2,
'M',
'W',0xC2,2,
'M',
'W',0xC0,1,9};
76 static const unsigned char s7_300_connect_block[] =
77 {3,0,0,22,0x11,0xE0,0x00,0x00,0x00,0x01,0x00,0xC1,2,1 ,0 ,0xC2,2,1 ,2 ,0xC0,1,9};
78 static const unsigned char s7_400_connect_block[] =
79 {3,0,0,22,0x11,0xE0,0x00,0x00,0x00,0x01,0x00,0xC1,2,1 ,0 ,0xC2,2,1 ,3 ,0xC0,1,9};
80 static const unsigned char s7_1200_connect_block[] =
81 {3,0,0,22,0x11,0xE0,0x00,0x00,0x00,0x01,0x00,0xC1,2,1 ,0 ,0xC2,2,1 ,0 ,0xC0,1,9};
82 static const unsigned char other_connect_block[] =
83 {3,0,0,22,0x11,0xE0,0x00,0x00,0x00,0x01,0x00,0xC1,2,1 ,0 ,0xC2,2,0 ,1 ,0xC0,1,9};
84 unsigned char connect_block[22];
86 unsigned char connect_block2[] =
87 {0x03,0x00,0x00,0x19,0x02,0xF0,0x80,0x32,0x01,0x00,0x00,0xCC,0xC1,0x00,0x08,0x00,0x00,0xF0,0x00,0x00,0x01,0x00,0x01,0x03,0xC0};
88 unsigned char buf[512];
92 memcpy(connect_block,
cb,
sizeof(
cb));
96 if (
plc_type ==
S7_200) memcpy(connect_block,s7_200_connect_block,
sizeof(connect_block));
97 else if(
plc_type ==
S7_300) memcpy(connect_block,s7_300_connect_block,
sizeof(connect_block));
98 else if(
plc_type ==
S7_400) memcpy(connect_block,s7_400_connect_block,
sizeof(connect_block));
99 else if(
plc_type ==
S7_1200) memcpy(connect_block,s7_1200_connect_block,
sizeof(connect_block));
100 else memcpy(connect_block,other_connect_block,
sizeof(connect_block));
107 if(
function != -1) connect_block[17] =
function;
128 for(i2=0; i2<3; i2++)
162 case ORG_DB: ret = 0x84;
break;
163 case ORG_M: ret = 0x83;
break;
164 case ORG_E: ret = 0x81;
break;
165 case ORG_A: ret = 0x82;
break;
167 case ORG_Z: ret = 0x84;
break;
168 case ORG_T: ret = 29;
break;
169 default:
return 0x83;
break;
174 int rlSiemensTCP::write(
int org,
int dbnr,
int start_adr,
int len,
const unsigned char *buf,
int function)
176 int i,ibuf,ret,len_byte,length;
181 if(len_byte > (
int)
sizeof(
pdu))
return -1;
189 length =
sizeof(
ih) +
sizeof(
wh) + len_byte;
190 unsigned char total_buf[
sizeof(
ih) +
sizeof(
wh) +
sizeof(
pdu)];
204 wh.
dbnr = (
unsigned char) dbnr;
206 wh.
start_adr[1] = (
unsigned char) start_adr & 0x0ff;;
207 wh.
len[0] = (
unsigned char) len / 256;
208 wh.
len[1] = (
unsigned char) len & 0x0ff;;
211 memcpy(total_buf, &
ih,
sizeof(
ih));
212 memcpy(total_buf+
sizeof(
ih), &
wh,
sizeof(
wh));
213 memcpy(total_buf+
sizeof(
ih)+
sizeof(
wh), buf, len_byte);
216 if(ret < 0)
return ret;
230 if(ret <= 0)
return ret;
233 if(ret <= 0)
return ret;
263 if(ret < 0)
return ret;
276 pdu[i++] = len_byte / 256;
277 pdu[i++] = len_byte & 0x0ff;
278 pdu[i++] = dbnr / 256;
279 pdu[i++] = dbnr & 0x0ff;
281 pdu[i++] = ((start_adr*8)/0x010000) & 0x0ff;
282 pdu[i++] = ((start_adr*8)/0x0100) & 0x0ff;
283 pdu[i++] = (start_adr*8) & 0x0ff;
288 for(ibuf=0; ibuf<len_byte; ibuf++)
290 pdu[i++] = buf[ibuf];
291 if(i > (
int)
sizeof(
pdu))
return -1;
295 if(ret < 0)
return ret;
297 if(ret < 0)
return ret;
298 if(
pdu[15] != 0x05)
return -1;
299 if(
pdu[16] != 0x01)
return -1;
305 fprintf( stderr,
" > Error: Trying to access a DB that does not exist\n");
306 fprintf( stderr,
" Please, check that DB is set. (error code: 10 (0x0a))\n");
309 else if(
pdu[17] == 0x05){
310 fprintf(stderr,
" > Error: Trying to access an address that does not exist.\n");
311 fprintf(stderr,
" Please, check the address range. (error code: 5 (0x05))\n");
314 else if(
pdu[17] == 0x07){
315 fprintf(stderr,
" > Error: the write data size doesn't fit item size\n");
316 fprintf(stderr,
" Please, check the data size. (error code: 7 (0x07))\n");
320 fprintf(stderr,
" > Error: unknown error (código %x!=0xff)\n",
pdu[17]);
332 pdu[i++] = 14 + 12 * (len - 1);
334 pdu[i++] = 6 * len - 1;
343 pdu[i++] = len / 256;
345 pdu[i++] = dbnr / 256;
346 pdu[i++] = dbnr & 0x0ff;
348 pdu[i++] = ((start_adr / 8)/0x010000) & 0x0ff;
349 pdu[i++] = (start_adr / 0x0100) & 0x0ff;
350 pdu[i++] = (start_adr + j) & 0x0ff;
358 pdu[i++] = (buf[j]>0) ? 0x01 : 0x00;
359 if(j < len - 1 )
pdu[i++] = 0x00;
360 if(i > (
int)
sizeof(
pdu))
return -1;
369 pdu[i++] = 5 + len - 1;
376 pdu[i++] = len / 256;
377 pdu[i++] = len & 0x0ff;
378 pdu[i++] = dbnr / 256;
379 pdu[i++] = dbnr & 0x0ff;
381 pdu[i++] = start_adr /0x10000 & 0x0ff;
382 pdu[i++] = ((start_adr*8)/0x0100) & 0x0ff;
383 pdu[i++] = (start_adr*8) & 0x0ff;
386 pdu[i++] = (len * 8) / 256;
387 pdu[i++] = (len * 8) & 0xff;
388 for(
int ibuf=0; ibuf<len; ibuf++)
390 pdu[i++] = buf[ibuf];
391 if(i > (
int)
sizeof(
pdu))
return -1;
398 int i,ret,len_byte,length;
410 length =
sizeof(
ih) +
sizeof(
fh);
424 fh.
dbnr = (
unsigned char) dbnr;
426 fh.
start_adr[1] = (
unsigned char) start_adr & 0x0ff;;
427 fh.
len[0] = (
unsigned char) len / 256;
428 fh.
len[1] = (
unsigned char) len & 0x0ff;;
431 unsigned char total_buf[
sizeof(
ih)+
sizeof(
fh)];
432 memcpy(total_buf, &
ih,
sizeof(
ih));
433 memcpy(total_buf+
sizeof(
ih), &
fh,
sizeof(
fh));
436 if(ret < 0)
return ret;
447 if(ret <= 0)
return ret;
450 if(ret <= 0)
return ret;
454 if(ret <= 0)
return ret;
458 rlDebugPrintf(
"fetch:starting org=%d dbnr=%d start_adr=%d len=%d\n", org, dbnr, start_adr, len);
479 pdu[i++] = len_byte / 256;
480 pdu[i++] = len_byte & 0x0ff;
481 pdu[i++] = dbnr / 256;
482 pdu[i++] = dbnr & 0x0ff;
484 pdu[i] = ((start_adr*8)/0x010000) & 0x0ff;
487 pdu[i++] = ((start_adr*8)/0x0100) & 0x0ff;
488 pdu[i++] = (start_adr*8) & 0x0ff;
492 rlDebugPrintf(
"fetch:write_iso error ret==%d -> return -1\n", ret);
498 rlDebugPrintf(
"fetch:read_iso error ret==%d -> return -1\n", ret);
512 if(ret < i+len_byte)
return -1;
513 for(
int ibuf = 0; ibuf < len_byte; ibuf++)
515 buf[ibuf] =
pdu[i++];
530 rlDebugPrintf(
"read_iso:failure to read iso header ret=%d -> disconnecting\n", ret);
536 rlDebugPrintf(
"read_iso:header vesion mismatch version==%d -> disconnecting\n", ret);
543 rlDebugPrintf(
"read_iso:len==%d from iso header is negative -> disconnecting\n", len);
547 if(len > (
int)
sizeof(
pdu))
549 rlDebugPrintf(
"read_iso:len==%d from iso header is larger than max PDU size -> disconnecting\n", len);
556 rlDebugPrintf(
"read_iso:read buf got timeout -> disconnecting\n");
562 ::printf(
"read_iso() len=%d\n", len);
563 for(i=0; i<len; i++) ::
printf(
"%02x,",buf[i]);
573 if(len > (
int)
sizeof(
pdu))
return -1;
579 unsigned char total_buf[
sizeof(
ih)+
sizeof(
pdu)];
584 memcpy(total_buf, &
ih,
sizeof(
ih));
585 memcpy(total_buf +
sizeof(
ih), buf,
sizeof(
ih)+len);
589 rlDebugPrintf(
"write_iso:failure to write buf -> disconnecting\n");
615 ::printf(
"write_iso() len=%d\n", len);
616 for(i=0; i<len; i++) ::
printf(
"%02x,",buf[i]);