// reads register(s) from controller at address to len // abel_comm_object_read(obj,"n7:90",4) // will return a ABELDataReturn // with type 0, len 4 // with num of values at n7:90,n7:91,n7:92,n7:93 ABELDataReturn* abel_comm_object_read(ABELCommObject* obj,char* addr,int len) { ABELDataReturn* out; out = abel_data_return_new(); int type; type = abel_comm_object_get_device_type(obj); int x,sts,extsts; unsigned int temp1, temp2; struct _data data; out->len=len; obj->comm1.tns = obj->comm1.tns + 4; data=protread3(obj->comm1,addr,len,type,FALSE); if (data.len == -1) { sts = data.data[0]; extsts = data.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 (obj->comm1.handle); } switch (data.name.section) { case 0: if (data.name.floatdata == TRUE) { out->type=1; for (x=0;x<data.len;x=x+2) { temp1 = (data.data[x]); temp2 = (data.data[x+1]); out->flt[x]=itof(temp1,temp2); } } if (data.name.floatdata == FALSE) { out->type=0; for (x=0;x<(data.len);x++) out->num[x]=(int)data.data[x]; } break; case 1: case 2: case 3: case 4: case 5: case 6: for (x=0;x<data.len;x++) printf ("%02X ",(byte)data.data[x]); printf ("\n"); } return out; }
int main (int argc, char *argv[]) { struct _comm comm; struct _data data; struct plc5stat status; int count,x,sts,extsts,type; unsigned int temp1, temp2; count=0; if (argc == 3) count = 1; if (argc == 4) count = atoi(argv[3]); if ((argc < 3) || (argc > 4)) { printf ("\nThis program will read a register from an Ethernet connected Allen Bradley\n"); printf ("PLC-5. It should also work with a Pyramid Integrator.\n\n"); printf ("Correct Usage:\nab <plc ip addr> <plc register> {<quantity>}\n"); printf ("ab 192.168.10.5 n7:0 - will read one integer from N7:0 on plc 192.168.10.5\n"); printf ("ab 192.168.10.5 n7:30 4 - will read four integers from N7:30 on plc 192.168.10.5\n"); printf ("\n\n"); exit (-1); } comm=abel_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; if (status.type == 0xee) type = SLC; comm.tns = comm.tns + 4; data=protread3(comm,argv[2],count,type,FALSE); if (data.len == -1) { sts = data.data[0]; extsts = data.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 (-1); } switch (data.name.section) { case 0: if (data.name.floatdata == TRUE) { for (x=0;x<data.len;x=x+2) { temp1 = (data.data[x]); temp2 = (data.data[x+1]); printf ("%f\n",itof(temp1,temp2)); } } if (data.name.floatdata == FALSE) { for (x=0;x<(data.len);x++) printf ("%d\n",(short)data.data[x]); } break; case 1: case 2: case 3: case 4: case 5: case 6: for (x=0;x<data.len;x++) printf ("%02X ",(byte)data.data[x]); printf ("\n"); } close (comm.handle); exit (0); }