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 logo_connect_block[] =
83 {3,0,0,22,0x11,0xE0,0x00,0x00,0x00,0x01,0x00, 0xC0,1,9, 0xC1,2,2,0, 0xC2,2,2,0};
85 static const unsigned char other_connect_block[] =
86 {3,0,0,22,0x11,0xE0,0x00,0x00,0x00,0x01,0x00,0xC1,2,1 ,0 ,0xC2,2,0 ,1 ,0xC0,1,9};
107 unsigned char connect_block[22];
109 unsigned char connect_block2[] =
110 {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};
111 unsigned char buf[512];
115 memcpy(connect_block,
cb,
sizeof(
cb));
119 if (
plc_type ==
S7_200) memcpy(connect_block,s7_200_connect_block,
sizeof(connect_block));
120 else if(
plc_type ==
S7_300) memcpy(connect_block,s7_300_connect_block,
sizeof(connect_block));
121 else if(
plc_type ==
S7_400) memcpy(connect_block,s7_400_connect_block,
sizeof(connect_block));
122 else if(
plc_type ==
S7_1200) memcpy(connect_block,s7_1200_connect_block,
sizeof(connect_block));
123 else if(
plc_type ==
LOGO) memcpy(connect_block,logo_connect_block,
sizeof(connect_block));
124 else memcpy(connect_block,other_connect_block,
sizeof(connect_block));
131 if(
function != -1) connect_block[17] =
function;
152 for(i2=0; i2<3; i2++)
196 case ORG_DB: ret = 0x84;
break;
197 case ORG_M: ret = 0x83;
break;
198 case ORG_E: ret = 0x81;
break;
199 case ORG_A: ret = 0x82;
break;
201 case ORG_Z: ret = 28;
204 case ORG_T: ret = 29;
208 default:
return 0x83;
break;
212 int rlSiemensTCP::write(
int org,
int dbnr,
int start_adr,
int len,
const unsigned char *buf,
int function)
214 int i,ibuf,ret,len_byte,length;
219 if(len_byte > (
int)
sizeof(
pdu))
return -1;
227 length =
sizeof(
ih) +
sizeof(
wh) + len_byte;
228 unsigned char total_buf[
sizeof(
ih) +
sizeof(
wh) +
sizeof(
pdu)];
242 wh.
dbnr = (
unsigned char) dbnr;
244 wh.
start_adr[1] = (
unsigned char) start_adr & 0x0ff;;
245 wh.
len[0] = (
unsigned char) len / 256;
246 wh.
len[1] = (
unsigned char) len & 0x0ff;;
249 memcpy(total_buf, &
ih,
sizeof(
ih));
250 memcpy(total_buf+
sizeof(
ih), &
wh,
sizeof(
wh));
251 memcpy(total_buf+
sizeof(
ih)+
sizeof(
wh), buf, len_byte);
254 if(ret < 0)
return ret;
268 if(ret <= 0)
return ret;
271 if(ret <= 0)
return ret;
301 if(ret < 0)
return ret;
314 pdu[i++] = len_byte / 256;
315 pdu[i++] = len_byte & 0x0ff;
316 pdu[i++] = dbnr / 256;
317 pdu[i++] = dbnr & 0x0ff;
319 pdu[i++] = ((start_adr*8)/0x010000) & 0x0ff;
320 pdu[i++] = ((start_adr*8)/0x0100) & 0x0ff;
321 pdu[i++] = (start_adr*8) & 0x0ff;
326 for(ibuf=0; ibuf<len_byte; ibuf++)
328 pdu[i++] = buf[ibuf];
329 if(i > (
int)
sizeof(
pdu))
return -1;
333 if(ret < 0)
return ret;
335 if(ret < 0)
return ret;
336 if(
pdu[15] != 0x05)
return -1;
337 if(
pdu[16] != 0x01)
return -1;
343 fprintf( stderr,
" > Error: Trying to access a DB that does not exist\n");
344 fprintf( stderr,
" Please, check that DB is set. (error code: 10 (0x0a))\n");
347 else if(
pdu[17] == 0x05){
348 fprintf(stderr,
" > Error: Trying to access an address that does not exist.\n");
349 fprintf(stderr,
" Please, check the address range. (error code: 5 (0x05))\n");
352 else if(
pdu[17] == 0x07){
353 fprintf(stderr,
" > Error: the write data size doesn't fit item size\n");
354 fprintf(stderr,
" Please, check the data size. (error code: 7 (0x07))\n");
358 fprintf(stderr,
" > Error: unknown error (código %x!=0xff)\n",
pdu[17]);
370 pdu[i++] = 14 + 12 * (len - 1);
372 pdu[i++] = 6 * len - 1;
381 pdu[i++] = len / 256;
383 pdu[i++] = dbnr / 256;
384 pdu[i++] = dbnr & 0x0ff;
386 pdu[i++] = ((start_adr / 8)/0x010000) & 0x0ff;
387 pdu[i++] = (start_adr / 0x0100) & 0x0ff;
388 pdu[i++] = (start_adr + j) & 0x0ff;
396 pdu[i++] = (buf[j]>0) ? 0x01 : 0x00;
397 if(j < len - 1 )
pdu[i++] = 0x00;
398 if(i > (
int)
sizeof(
pdu))
return -1;
407 pdu[i++] = 5 + len - 1;
414 pdu[i++] = len / 256;
415 pdu[i++] = len & 0x0ff;
416 pdu[i++] = dbnr / 256;
417 pdu[i++] = dbnr & 0x0ff;
419 pdu[i++] = start_adr /0x10000 & 0x0ff;
420 pdu[i++] = ((start_adr*8)/0x0100) & 0x0ff;
421 pdu[i++] = (start_adr*8) & 0x0ff;
424 pdu[i++] = (len * 8) / 256;
425 pdu[i++] = (len * 8) & 0xff;
426 for(
int ibuf=0; ibuf<len; ibuf++)
428 pdu[i++] = buf[ibuf];
429 if(i > (
int)
sizeof(
pdu))
return -1;
436 int i,ret,len_byte,length;
448 length =
sizeof(
ih) +
sizeof(
fh);
462 fh.
dbnr = (
unsigned char) dbnr;
464 fh.
start_adr[1] = (
unsigned char) start_adr & 0x0ff;;
465 fh.
len[0] = (
unsigned char) len / 256;
466 fh.
len[1] = (
unsigned char) len & 0x0ff;;
469 unsigned char total_buf[
sizeof(
ih)+
sizeof(
fh)];
470 memcpy(total_buf, &
ih,
sizeof(
ih));
471 memcpy(total_buf+
sizeof(
ih), &
fh,
sizeof(
fh));
474 if(ret < 0)
return ret;
485 if(ret <= 0)
return ret;
488 if(ret <= 0)
return ret;
492 if(ret <= 0)
return ret;
496 rlDebugPrintf(
"fetch:starting org=%d dbnr=%d start_adr=%d len=%d\n", org, dbnr, start_adr, len);
517 pdu[i++] = len_byte / 256;
518 pdu[i++] = len_byte & 0x0ff;
519 pdu[i++] = dbnr / 256;
520 pdu[i++] = dbnr & 0x0ff;
522 pdu[i] = ((start_adr*8)/0x010000) & 0x0ff;
525 pdu[i++] = ((start_adr*8)/0x0100) & 0x0ff;
526 pdu[i++] = (start_adr*8) & 0x0ff;
530 rlDebugPrintf(
"fetch:write_iso error ret==%d -> return -1\n", ret);
536 rlDebugPrintf(
"fetch:read_iso error ret==%d -> return -1\n", ret);
550 if(ret < i+len_byte)
return -1;
551 for(
int ibuf = 0; ibuf < len_byte; ibuf++)
553 buf[ibuf] =
pdu[i++];
568 rlDebugPrintf(
"read_iso:failure to read iso header ret=%d -> disconnecting\n", ret);
574 rlDebugPrintf(
"read_iso:header vesion mismatch version==%d -> disconnecting\n", ret);
581 rlDebugPrintf(
"read_iso:len==%d from iso header is negative -> disconnecting\n", len);
585 if(len > (
int)
sizeof(
pdu))
587 rlDebugPrintf(
"read_iso:len==%d from iso header is larger than max PDU size -> disconnecting\n", len);
594 rlDebugPrintf(
"read_iso:read buf got timeout -> disconnecting\n");
600 ::printf(
"read_iso() len=%d\n", len);
601 for(i=0; i<len; i++) ::
printf(
"%02x,",buf[i]);
611 if(len > (
int)
sizeof(
pdu))
return -1;
617 unsigned char total_buf[
sizeof(
ih)+
sizeof(
pdu)];
622 memcpy(total_buf, &
ih,
sizeof(
ih));
623 memcpy(total_buf +
sizeof(
ih), buf,
sizeof(
ih)+len);
627 rlDebugPrintf(
"write_iso:failure to write buf -> disconnecting\n");
653 ::printf(
"write_iso() len=%d\n", len);
654 for(i=0; i<len; i++) ::
printf(
"%02x,",buf[i]);
int rlDebugPrintf(const char *format,...)
unsigned char error_block
unsigned char len_org_block
int write_iso(unsigned char *buf, int len)
int write(const void *buf, int len)
unsigned char ident_org_block
unsigned char error_block
unsigned char start_adr[2]
unsigned char op_code_len
unsigned char len_org_block
int read_iso(unsigned char *buf)
int setConnectBlock(const unsigned char *connect_block)
int getConnectBlock(unsigned char *connect_block)
int read(void *buf, int len, int timeout=0)
int getDefaultConnectBlock(unsigned char *connect_block)
int write_bit(int &i, int org, int dbnr, int start_adr, int len, const unsigned char *buf)
int fetch(int org, int dbnr, int start_adr, int length, unsigned char *buf)
unsigned char ident_op_code
unsigned char ident_op_code
int write_byte(int &i, int org, int dbnr, int start_adr, int length, const unsigned char *buf)
unsigned char start_adr[2]
int write(int org, int dbnr, int start_adr, int length, const unsigned char *buf, int function=WriteByte)
unsigned char op_code_len
unsigned char length_high
unsigned char ident_org_block
rlSiemensTCP(const char *adr, int _plc_type=rlSiemensTCP::ANY_SIEMENS_COMPATIBLE_PLC, int _fetch_write=1, int function=-1, int rack_slot=-1)
int printf(const char *format,...)