int main (int argc, char *argv[]) { struct namedata name; int x; printf ("About to convert name %s\n",argv[1]); name = nameconv5(argv[1],SLC,TRUE); if (name.len == 0) name = nameconv5(argv[1],PLC5250,TRUE); for (x=0;x<name.len;x++) printf ("%02X ",name.data[x]); printf ("\n"); exit (0); }
extern struct _data typed_write (struct _comm comm1, char *addr, int count, struct _data data, int plctype, int debug) { struct ethernet_header header,rcvd; struct pccc_word_read df1_1; struct _data retdata; struct namedata name; struct custom_pccc custom; int x,place,d,location,hlen; bzero (&header,sizeof(header)); bzero (&rcvd, sizeof(rcvd)); bzero (&retdata,sizeof(retdata)); header.mode = 1; header.submode = PCCC_CMD; header.conn = htonl(comm1.commid); custom.request_id = htonl(0xe87573); custom.name_id = 0; memcpy (&header.custom, &custom, _CUSTOM_LEN); df1_1.src=0; df1_1.control=5; df1_1.dst=0; df1_1.lsap=0; df1_1.cmd=0x0f; df1_1.sts=0; if (plctype == PLC5250) df1_1.tns = comm1.tns; if (plctype != PLC5250) df1_1.tns = htons(comm1.tns); df1_1.fnc=0x67; df1_1.offset = 0; name = nameconv5(addr,plctype,debug); for (x=0;x<name.len;x++) df1_1.data[x] = name.data[x]; place = name.len; type = name.type; if (type > 7) type = 9; len = name.typelen; if (len > 7) len = 9; df1_1.data[place++] = (type * 16) + len; if (type == 9) df1_1.data[place++] = name.type; if (len == 9) df1_1.data[place++] = name.typelen; if (count == 0) count = 1; location=0; for (x=0;x<count;x++) { if (name.floatdata == TRUE) { d=data.data[location+1]; df1_1.data[place++] = (d & 255); df1_1.data[place++] = (d / 256); d=data.data[location]; df1_1.data[place++] = (d & 255); df1_1.data[place++] = (d / 256); location = location + 2; } if (name.floatdata == FALSE) { d=data.data[x]; df1_1.data[place++] = (d & 255); if (name.typelen > 1) df1_1.data[place++] = (d / 256); } } df1_1.trans = count; memcpy (&header.df1_stuff, &df1_1, PCCC_WORD_LEN + place); header.pccc_length = htons(PCCC_WORD_LEN + place); hlen = _ENET_HEADER_LEN + PCCC_WORD_LEN + place; rcvd = senddf1 (comm1, header, hlen, debug); if (rcvd.df1_stuff[4] != (df1_1.cmd + 0x40)) { retdata.len=255; retdata.data[0]=BADCMDRET; retdata.data[1]=0; return retdata; } if (rcvd.df1_stuff[5] != 0) { retdata.len=255; retdata.data[0]=rcvd.df1_stuff[5]; //Error Response - STS retdata.data[1]=rcvd.df1_stuff[8]; //Error Response - EXT STS return retdata; } return retdata; }
extern struct _typed_data typed_read (struct _comm comm1, char *addr, int count, int plctype, int debug) { struct ethernet_header header,rcvd; struct pccc_word_read df1_1; struct namedata name; struct _typed_data data; struct custom_pccc custom; int place,start,hlen; bzero (&header,sizeof(header)); bzero (&rcvd, sizeof(rcvd)); bzero (&data, sizeof(data)); header.mode = 1; header.submode = 7; header.conn = htonl(comm1.commid); custom.request_id = 0xe87573; custom.name_id = 0; memcpy (&header.custom, &custom, _CUSTOM_LEN); df1_1.src=0; df1_1.control=5; df1_1.dst=0; df1_1.lsap=0; df1_1.cmd=0x0f; df1_1.sts=0; df1_1.tns=htons(comm1.tns); df1_1.fnc=0x68; df1_1.offset = 0; name = nameconv5(addr,plctype,debug); memcpy (&data.name,&name,sizeof(name));; for (x=0;x<name.len;x++) df1_1.data[x] = name.data[x]; place = name.len; if (count == 0) count = 1; df1_1.trans= (count); df1_1.data[place++] = ((byte)count & 255); df1_1.data[place++] = ((byte) count / 256); memcpy (&header.df1_stuff, &df1_1, PCCC_WORD_LEN+place); hlen = _ENET_HEADER_LEN + PCCC_WORD_LEN + place; header.pccc_length = htons(PCCC_WORD_LEN + place); rcvd = senddf1 (comm1, header, hlen, debug); if (rcvd.df1_stuff[4] != (df1_1.cmd + 0x40)) { data.typelen=-1; data.data[0]=BADCMDRET; data.data[1]=0; return data; } if (rcvd.df1_stuff[5] != 0) { data.typelen=-1; data.data[0]=rcvd.df1_stuff[5]; //Error Response - STS data.data[1]=rcvd.df1_stuff[8]; //Error Response - EXT STS return data; } start=9; data.type = (rcvd.df1_stuff[8] / 16); data.typelen = (rcvd.df1_stuff[8] & 15); if (data.type > 7) { data.type = rcvd.df1_stuff[start++]; } if (data.typelen > 7) { data.typelen = rcvd.df1_stuff[start++]; } if (data.type == 9) { data.type = (rcvd.df1_stuff[start] / 16); data.typelen = (rcvd.df1_stuff[start++] & 15); if (data.type == 9) data.type = rcvd.df1_stuff[start++]; if (data.typelen == 9) data.typelen = rcvd.df1_stuff[start++]; } if (name.floatdata == TRUE) count = count * 2; data.len = count; switch (name.section) { case 0: case 2: for (x=0;x<data.len;x++) { switch (data.typelen) { case 1: data.data[x] = rcvd.df1_stuff[start++]; break; case 2: data.data[x] = (rcvd.df1_stuff[start+1]*256) + rcvd.df1_stuff[start]; start = start + data.typelen; break; case 4: data.data[x++] = (rcvd.df1_stuff[start+3]*256) + rcvd.df1_stuff[start+2]; data.data[x] = (rcvd.df1_stuff[start+1]*256) + rcvd.df1_stuff[start+0]; start = start + data.typelen; break; } } break; case 1: case 3: case 4: case 5: case 6: data.len = data.typelen; data.len++; for (x=0;x<data.len;x++) { data.data[x] = rcvd.df1_stuff[x+start]; } } return data; }
int df1_typed_read (_comm_header *comm, _df1_comm *df1_comm, char *reg, _df1_resp *resp, byte node, byte channel, int plctype, unsigned short count, int debug) { _df1 *df1; _pccc_namedata *name; _pccc_word_read *cmd; int x,place; dprint (DEBUG_TRACE,"Entering df1_typed_read\n"); if (df1_comm == NULL) { CELLERROR(1,"df1_comm struct not defined"); return (-1); } if (strlen(reg) < 1) { CELLERROR(2,"Invalid register to read."); return (-2); } if (node > 63) { CELLERROR(3,"Node must be less than 64"); return (-3); } if ((channel < 1) || (channel > 2)) { CELLERROR(4,"Channel must be 1 or 2"); return (-4); } df1 = malloc(sizeof(_df1_word_read)); if (df1 == NULL) { CELLERROR(5,"Could not allocate DF1 struct"); return (-5); } name = malloc(sizeof(_pccc_namedata)); if (name == NULL) { CELLERROR(6,"Could not allocate name struct"); return (-6); } cmd = malloc(sizeof(_pccc_word_read)); if (cmd == NULL) { CELLERROR(8,"Could not allocate CMD struct"); return (-8); } if (resp == NULL) { CELLERROR(9,"Response struct not allocated"); return (-9); } memset (cmd, 0, sizeof(_pccc_word_read)); memset (df1,0,sizeof(_df1_word_read)); memset (name, 0, sizeof(_pccc_namedata)); memset (resp, 0, sizeof(_df1_resp)); place = 0; df1->df1_port = channel; cmd->cmd = 0x0f; cmd->fnc = 0x68; cmd->tns = comm->df1_tns; cmd->dst = node; cmd->offset = 0; nameconv5(reg, name, plctype, debug); memcpy (cmd->data, name->data, name->len); place = name->len; if (count == 0) count=1; count = count * 2; if (name->floatdata == TRUE) count = count * 2; cmd->data[place++] = (byte)count; if (plctype == PLC5250) cmd->trans = htons(count/2); if (plctype != PLC5250) cmd->trans = (count); df1->df1_datalen = 16 + place; memcpy (df1->cmd, cmd, 16+place); x = send_df1 (comm, df1_comm, df1, resp, debug); if (x < 0) printf ("Send_DF1 failed - status = %d.\n",x); free(df1); free(name); free(cmd); return x; }
extern struct _data protwrite2 (struct _comm comm1, char *addr, int count, struct _data outdata, int plctype, int debug) { struct ethernet_header header,rcvd; struct pccc_data df1_1; struct _data retdata; struct namedata name; struct custom_pccc custom; int x,place,d,location,hlen; bzero (&header,sizeof(header)); bzero (&rcvd, sizeof(rcvd)); bzero (&retdata,sizeof(retdata)); if (plctype != SLC) return retdata; header.mode = 1; header.submode = PCCC_CMD; header.conn = htonl(comm1.commid); custom.request_id = 0xe87573; df1_1.src=0; df1_1.control=5; df1_1.dst=0; df1_1.lsap=0; df1_1.cmd=0x0f; df1_1.sts=0; df1_1.tns = htons(comm1.tns); df1_1.fnc=0xa9; name = nameconv5(addr,plctype,debug); retdata.name = name; place=0; if (count==0) count=1; df1_1.data[place++] = name.typelen * count; if (name.file > 254) { df1_1.data[place++] = 0xff; df1_1.data[place++] = (name.file & 255); df1_1.data[place++] = (name.file / 256); } if (name.file < 255) df1_1.data[place++] = name.file; df1_1.data[place++] = name.type; if (name.element > 254) { df1_1.data[place++] = 0xff; df1_1.data[place++] = (name.element & 255); df1_1.data[place++] = (name.element / 256); } if (name.element < 255) df1_1.data[place++] = name.element; location=0; for (x=0;x<count;x++) { if (name.floatdata == TRUE) { d=outdata.data[location+1]; df1_1.data[place++] = (d & 255); df1_1.data[place++] = (d / 256); d=outdata.data[location]; df1_1.data[place++] = (d & 255); df1_1.data[place++] = (d / 256); location = location + 2; } if (name.floatdata == FALSE) { d=outdata.data[x]; df1_1.data[place++] = (d & 255); if (name.typelen > 1) df1_1.data[place++] = (d / 256); } } memcpy (&header.df1_stuff, &df1_1, PCCC_DATA_LEN+place); header.pccc_length = htons(PCCC_DATA_LEN+place); hlen = _ENET_HEADER_LEN + PCCC_DATA_LEN + place; rcvd = senddf1 (comm1, header, hlen, debug); if (rcvd.df1_stuff[4] != (df1_1.cmd + 0x40)) { retdata.len=-1; retdata.data[0]=BADCMDRET; retdata.data[1]=0; return retdata; } if (rcvd.df1_stuff[5] != 0) { retdata.len=-1; retdata.data[0]=rcvd.df1_stuff[5]; //Error Response - STS retdata.data[1]=rcvd.df1_stuff[8]; //Error Response - EXT STS return retdata; } return retdata; }
extern struct _data protread4 (struct _comm comm1, char *addr, int count, int bitmask, int plctype, int debug) { struct ethernet_header header,rcvd; struct pccc_data df1_1; struct namedata name; struct _data data; struct custom_pccc custom; int place,index,hlen; bzero (&header,sizeof(header)); bzero (&rcvd, sizeof(rcvd)); bzero (&custom, sizeof(custom)); bzero (&data, sizeof(data)); if (plctype != SLC) return data; header.mode = 1; header.submode = PCCC_CMD; header.conn = htonl(comm1.commid); custom.request_id = htonl(0x7377e8); memcpy (&header.custom, &custom, _CUSTOM_LEN); df1_1.src=0; df1_1.control=5; df1_1.dst=0; df1_1.lsap=0; df1_1.cmd=0x0f; df1_1.sts=0; df1_1.tns=htons(comm1.tns); df1_1.fnc=0xa3; name = nameconv5(addr,plctype,debug); data.name = name; place=0; df1_1.data[place++] = name.typelen * count; if (name.file > 254) { df1_1.data[place++] = 0xff; df1_1.data[place++] = (name.file & 255); df1_1.data[place++] = (name.file / 256); } if (name.file < 255) df1_1.data[place++] = name.file; df1_1.data[place++] = name.type; if (name.element > 254) { df1_1.data[place++] = 0xff; df1_1.data[place++] = (name.element & 255); df1_1.data[place++] = (name.element / 256); } if (name.element < 255) df1_1.data[place++] = name.element; df1_1.data[place++] = name.subelement; df1_1.data[place++] = (bitmask & 255); df1_1.data[place++] = (bitmask / 256); memcpy (&header.df1_stuff, &df1_1, PCCC_DATA_LEN + place); header.pccc_length = htons(PCCC_DATA_LEN + place); hlen = _ENET_HEADER_LEN + PCCC_DATA_LEN + place; rcvd = senddf1 (comm1, header, hlen, debug); if (rcvd.df1_stuff[4] != (df1_1.cmd + 0x40)) { data.len=-1; data.data[0]=BADCMDRET; data.data[1]=0; return data; } if (rcvd.df1_stuff[5] != 0) { data.len=-1; data.data[0]=rcvd.df1_stuff[5]; //Error Response - STS data.data[1]=rcvd.df1_stuff[8]; //Error Response - EXT STS return data; } data.len = count; if (name.floatdata == TRUE) count = count * 2; for (x=0;x<count;x++) { index = 8+(x*name.typelen); switch (name.typelen) { case 1: data.data[x] = rcvd.df1_stuff[index]; break; case 2: data.data[x] = (rcvd.df1_stuff[index+1]*256) + rcvd.df1_stuff[index]; break; case 4: data.data[x++] = (rcvd.df1_stuff[index+3]*256) + rcvd.df1_stuff[index+2]; data.data[x] = (rcvd.df1_stuff[index+1]*256) + rcvd.df1_stuff[index]; break; } } return data; }
extern struct _data word_write (struct _comm comm1, char *addr, int count, struct _data outdata, int plctype, int debug) { struct ethernet_header header,rcvd; struct pccc_word_read df1_1; struct _data retdata; struct namedata name; struct custom_pccc custom; int x,place,d,hlen; bzero (&header,sizeof(header)); bzero (&rcvd, sizeof(rcvd)); bzero (&retdata,sizeof(retdata)); header.mode = 1; header.submode = PCCC_CMD; header.conn = htonl(comm1.commid); custom.request_id = 0xe87573; df1_1.src=0; df1_1.control=5; df1_1.dst=0; df1_1.lsap=0; df1_1.cmd=0x0f; df1_1.sts=0; if (plctype == PLC5250) df1_1.tns = comm1.tns; if (plctype != PLC5250) df1_1.tns = htons(comm1.tns); df1_1.fnc=0; df1_1.offset = 0; name = nameconv5(addr,plctype,debug); for (x=0;x<name.len;x++) df1_1.data[x] = name.data[x]; place = name.len; if (count == 0) count = 1; count = count * 2; for (x=0;x<(count/2);x++) { d=outdata.data[x]; df1_1.data[place] = (d & 255); place++; df1_1.data[place] = (d / 256); place++; } if (plctype == PLC5250) df1_1.trans = (count/2); if (plctype != PLC5250) df1_1.trans = htons(count/2); // place++; memcpy (&header.df1_stuff, &df1_1, PCCC_WORD_LEN+place); header.pccc_length = htons(PCCC_WORD_LEN+place); hlen = _ENET_HEADER_LEN + PCCC_WORD_LEN + place; rcvd = senddf1 (comm1, header, hlen, debug); if (rcvd.df1_stuff[4] != (df1_1.cmd + 0x40)) { retdata.len=-1; retdata.data[0]=BADCMDRET; retdata.data[1]=0; return retdata; } if (rcvd.df1_stuff[5] != 0) { retdata.len=-1; retdata.data[0]=rcvd.df1_stuff[5]; //Error Response - STS retdata.data[1]=rcvd.df1_stuff[8]; //Error Response - EXT STS return retdata; } return retdata; }
int main (int argc, char *argv[]) { struct _comm comm; struct _data data,ret; struct plc5stat status; struct namedata name; struct floatstuff fdata; int count,x,sts,extsts,type; byte buf[32]; count=0; if ((argc < 3) || (argc > 4)) { printf ("\nThis program will write registers to an Ethernet connected Allen Bradley\n"); printf ("PLC-5. It should also work with a Pyramid Integrator.\n\n"); printf ("Correct Usage:\nabtypedwrite <plc ip addr> <plc register> {<quantity>}\n"); printf ("abtypedwrite 192.168.10.5 n7:0 - will write one integer from N7:0 on 192.168.10.5\n"); printf ("abtypedwrite plc n7:30 4 - will write four integers from N7:30 on plc\n"); printf ("\n\n"); exit (-1); } if (argc == 4) count = atoi(argv[3]); if (argc == 3) count = 1; comm=attach(argv[1],FALSE); if (comm.error != 0) { printf ("Could not connect. Check your address and try again.\n"); exit (-1); } status = getstatus(comm,FALSE); type = PLC5; if (status.type == 0xde) type = PLC5250; comm.tns = comm.tns + 4; name = nameconv5(argv[2],type,FALSE); if (name.floatdata != TRUE) { for (x=0;x<count;x++) { printf ("Entry #%d of %d:",x+1,count); fgets (buf,10,stdin); printf ("\n"); data.data[x]=atoi(buf); } } if (name.floatdata == TRUE) { count = count*2; for (x=0;x<count;x=x+2) { printf ("Entry #%d of %d:",(x/2)+1,count/2); bzero (&buf,sizeof(buf)); fgets (buf,10,stdin); printf ("\n"); fdata = ftoi (atof(buf)); data.data[x] = fdata.data1; data.data[x+1] = fdata.data2; } } ret = typed_write (comm, argv[2], count, data, type, FALSE); if (ret.len == 255) { sts = ret.data[0]; extsts = ret.data[1]; printf ("An error occured. The PLC STS byte is %d, the EXT STS byte is %d\n",sts,extsts); if (sts != 0xf0) printf ("Primary Error code is %s\n",errors[(sts/16)]); if (extsts != 0) printf ("Extended error code is %s\n",ext_errors[extsts]); } close (comm.handle); exit (0); }
int df1_create_memory (_comm_header *comm, _df1_comm *df1_comm, char *reg, _df1_resp *resp, byte node, byte channel, int plctype, int debug) { _df1 *df1; _pccc_namedata *name; _pccc_word_read *cmd; int x,place; dprint (DEBUG_TRACE,"Entering df1_read_section_size\n"); if (df1_comm == NULL) { CELLERROR(1,"df1_comm struct not defined"); return (-1); } if (strlen(reg) < 1) { CELLERROR(2,"Invalid register to read."); return (-2); } if (node > 63) { CELLERROR(3,"Node must be less than 64"); return (-3); } if ((channel < 1) || (channel > 2)) { CELLERROR(4,"Channel must be 1 or 2"); return (-4); } df1 = malloc(sizeof(_df1)); if (df1 == NULL) { CELLERROR(5,"Could not allocate DF1 struct"); return (-5); } name = malloc(sizeof(_pccc_namedata)); if (name == NULL) { CELLERROR(6,"Could not allocate name struct"); return (-6); } cmd = malloc(sizeof(_pccc_normal)); if (cmd == NULL) { CELLERROR(8,"Could not allocate CMD struct"); return (-8); } if (resp == NULL) { CELLERROR(9,"Response struct not allocated"); return (-9); } memset (cmd, 0, sizeof(_pccc_normal)); memset (df1,0,sizeof(_df1)); memset (name, 0, sizeof(_pccc_namedata)); memset (resp, 0, sizeof(_df1_resp)); place = 0; df1->df1_port = channel; cmd->cmd = 0x0f; cmd->fnc = 0x27; cmd->tns = comm->df1_tns; cmd->dst = node; nameconv5(reg, name, plctype, debug); memcpy (cmd->data, name->data, name->len); place = name->len; if (name->type != 8) cmd->data[place++] = (name->type * 16); if (name->type == 8) { cmd->data[place++] = 0x90; cmd->data[place++] = name->type; } df1->df1_datalen = 16 + place; memcpy (df1->cmd, cmd, 16+place); x = send_df1 (comm, df1_comm, df1, resp, debug); if (x < 0) printf ("Send_DF1 failed - status = %d.\n",x); free(df1); free(name); free(cmd); return x; }