int readplc(PLC *plc,Eip_Session *session,Eip_Connection *connection,char *address,Data_Type type,double *value) { DHP_Header temp={0,0,0,0}; DHP_Header *dhp=&temp; //PCCC_Header *head=NULL; int tns=getpid(); switch (plc->PlcType) { case PLC5: case SLC500: { PLC_Read *data=NULL; dhp->Dest_adress=plc->Node; if (plc->NetWork) // DHP data=ReadPLCData(session,connection,dhp,NULL,0,plc->PlcType,tns++,address,1); else data=ReadPLCData(session,connection,NULL,NULL,0,plc->PlcType,tns++,address,1); if (data!=NULL) { if (!cip_errno) { *value=PCCC_GetValueAsFloat(data,0); Log(LOG_DEBUG,"ReadPlc : %s on %s = %f (%s)\n",address,plc->PlcName,*value,cip_err_msg); } else { Log(LOG_WARNING,"Get PCCC value on tag %s (%s) : (%d / ext %d) %s\n",address,plc->PlcName,cip_errno,cip_err_msg,cip_ext_errno); } free(data); } else { Log(LOG_WARNING,"Error while decoding PCCC value on tag %s (%s) : (%d) %s\n",address,plc->PlcName,cip_errno,cip_err_msg); } }; break; case LGX: { LGX_Read *data=ReadLgxData(session,connection,address,1); if (data!=NULL) { if (!cip_errno) { *value=GetLGXValueAsFloat(data,0); Log(LOG_DEBUG,"ReadPlc : %s on %s = %f (%s)\n",address,plc->PlcName,*value,cip_err_msg); } else { Log(LOG_WARNING,"Get value : (%d) %s\n",cip_errno,cip_err_msg); } free(data); } else { Log(LOG_WARNING,"ReadLgxData error on tag : %s (%d : %s)\n",address,cip_errno,cip_err_msg); } }; break; default:Log(LOG_WARNING,"Plc type unknow for : %s\n",plc->PlcName); break; } return(cip_errno); }
int ReadTag(TAG *tag) { int result=0; DHP_Header dhp={0,0,0,0}; if (tag->Plc->Session==NULL) { if (!BuildSession(tag->Plc)) return(0); } if (tag->Plc->Connection==NULL) { if (!BuildConnection(tag->Plc)) return(0); } Log(LOG_DEBUG,"ReadTag : %s (%p / %p)\n",tag->TagName,tag->Plc->Session,tag->Plc->Connection); switch (tag->Plc->PlcType) { case PLC5: case SLC500: { PLC_Read *data; dhp.Dest_adress=tag->Plc->Node; if (tag->Plc->NetWork) // DHP data=ReadPLCData(tag->Plc->Session,tag->Plc->Connection,&dhp,NULL,0,tag->Plc->PlcType,tns++,tag->TagName,1); else data=ReadPLCData(tag->Plc->Session,tag->Plc->Connection,NULL,NULL,0,tag->Plc->PlcType,tns++,tag->TagName,1); //Log(LOG_DEBUG,"Reading : %s on %s (%s)\n",TAGs[i].TagName,(TAGs[i].Plc)->PlcName,s_err_msg); if (data!=NULL) { tag->Value=PCCC_GetValueAsFloat(data,0); if (!cip_errno) { Log(LOG_DEBUG,"%s on %s = %f (%s)\n",tag->TagName,tag->Plc->PlcName,tag->Value,cip_err_msg); result=1; } else { Log(LOG_WARNING,"Get PCCC value on tag %s: (%d / ext %d) %s\n",tag->TagName,cip_errno,cip_err_msg,cip_ext_errno); result=0; } free(data); } else { Log(LOG_WARNING,"ReadPLCData error on tag %s: (%d) %s\n",tag->TagName,cip_errno,cip_err_msg); result=0; } }; break; case LGX: { LGX_Read *data=ReadLgxData(tag->Plc->Session,tag->Plc->Connection,tag->TagName,1); if (data!=NULL) { tag->Value=GetLGXValueAsFloat(data,0); if (!cip_errno) { Log(LOG_DEBUG,"%s on %s = %f (%s)\n",tag->TagName,tag->Plc->PlcName,tag->Value,cip_err_msg); result=1; } else { Log(LOG_WARNING,"Get value : (%d) %s\n",cip_errno,cip_err_msg); result=0; } free(data); } else { Log(LOG_WARNING,"ReadLgxData error on tag : %s (%s)\n",tag->TagName,cip_err_msg); result=0; } }; break; default:Log(LOG_WARNING,"Plc type unknow for : %s\n",tag->Plc->PlcName); break; } if (result) tag->Time_Value=time(NULL); return(result); }
int main(int argc,char *argv[]) { //cip_debuglevel=LogDebug; // You can uncomment this line to see the data exchange between TuxEip and your Logic controller char *IP="192.168.1.4\0"; BYTE path[]={1,0}; // char *IP="10.140.200.58\0"; // BYTE path[]={1,0}; int res; int count=1; char *var; int tns=getpid(); printf("Starting, Pid = %d\n",getpid()); if (argc<2) { printf("You have to provide a tag name\n"); return(1); } var=argv[1]; /* Openning a session */ printf("entering OpenSession \n"); Eip_Session *session=OpenSession(IP); if (session==NULL) { printf("Error : OpenSession %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); return(1); } printf("OpenSession Ok\n"); printf("entering RegisterSession \n"); res=RegisterSession(session); if (res!=Error) { int i,val_int,val_bool; float val_float; printf("RegisterSession Ok\n"); printf("entering ConnectPLCOverCNET\n"); Eip_Connection *connection=ConnectPLCOverCNET( session, // session whe have open SLC500, // plc type 0x12345678, // Target To Originator connection ID 0x6789, // Connection Serial Number 5000, // Request Packet Interval path, // Path to the ControlLogix sizeof(path) // path size ); if (connection!=NULL) { printf("ConnectPLCOverCNET Ok, \n"); printf("Reading %s\n",var); //LGX_Read *data=ReadPlcData(session,connection,var,1); PLC_Read *data=ReadPLCData(session,connection,NULL,NULL,0,SLC500,tns++,var,count); if (data!=NULL) { if (data->Varcount>0) { printf("ReadPLCData Ok :\n"); printf("\telements :%d\n\tDatatype : %d\n\ttotalsize : %d\n\tElement size : %d\n",data->Varcount,data->type,data->totalsize,data->elementsize,data->mask); for (i=0;i<data->Varcount;i++) { val_bool=PCCC_GetValueAsBoolean(data,i); val_int=PCCC_GetValueAsInteger(data,i); val_float=PCCC_GetValueAsFloat(data,i); printf("Value [%d] = %f (%d)\n",i,val_float,val_int,val_bool); } } else printf("Error ReadPLCData : %s\n",cip_err_msg); free(data); // You have to free the data return by ReadLgxData } else { printf("Error : ReadPLCData %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); } printf("entering Forward_Close\n"); res=Forward_Close(connection); if (res!=Error) printf("Forward_Close %s\n",cip_err_msg); else printf("Error : Forward_Close %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); } else { printf("Error : ConnectPLCOverCNET %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); } UnRegisterSession(session); printf("UnRegister : %s\n",cip_err_msg); }else { printf("Error : RegisterSession %s (%d:%d)\n",cip_err_msg,cip_errno,cip_ext_errno); } printf("entering CloseSession\n"); CloseSession(session); return(0); }
int ReadTag(PLC *Plc, Eip_Session *Session, Eip_Connection *Connection, char *TagName, int *DataType, char *responseValue) { int result=SUCCESS; DHP_Header dhp={0,0,0,0}; int tns=getpid(); float FValue; int IValue; Log(LOG_DEBUG,"[ReadTag] %s [%s](%p / %p)\n",TagName,writeValue,Session,Connection); switch (Plc->PlcType) { case PLC5: case SLC500: { PLC_Read *data; dhp.Dest_adress=Plc->Node; if (Plc->NetWork) // DHP data=ReadPLCData(Session,Connection,&dhp,NULL,0,Plc->PlcType,tns++,TagName,1); else data=ReadPLCData(Session,Connection,NULL,NULL,0,Plc->PlcType,tns++,TagName,1); if (data!=NULL) { Log(LOG_DEBUG,"[ReadTag] %s on %s Datatype:%x (%s)\n",TagName,Plc->PlcName,data->type,cip_err_msg); *DataType = data->type; switch (data->type) { case PLC_BIT: case PLC_INTEGER: // TODO case PLC_TIMER: { IValue=PCCC_GetValueAsBoolean(data,0); if (!cip_errno) { Log(LOG_DEBUG,"[ReadTag] %s on %s = %d (%s)\n",TagName,Plc->PlcName,IValue,cip_err_msg); sprintf(responseValue,"%d",IValue); result=SUCCESS; } else { Log(LOG_WARNING,"[ReadTag] Get PCCC value on tag %s: (%d / ext %d) %s\n",TagName,cip_errno,cip_err_msg,cip_ext_errno); result=ERROR; } }break; case PLC_FLOATING: { FValue=PCCC_GetValueAsFloat(data,0); if (!cip_errno) { Log(LOG_DEBUG,"[ReadTag] %s on %s = %f (%s)\n",TagName,Plc->PlcName,FValue,cip_err_msg); sprintf(responseValue,"%f",FValue); result=SUCCESS; } else { Log(LOG_WARNING,"[ReadTag] Get PCCC value on tag %s: (%d / ext %d) %s\n",TagName,cip_errno,cip_err_msg,cip_ext_errno); result=ERROR; } }break; default:{ Log(LOG_WARNING,"[ReadTag] Datatype type unknown for : %s\n",TagName); result=ERROR; } break; } free(data); } else { Log(LOG_WARNING,"[ReadTag] ReadPLCData error on tag %s: (%d) %s\n",TagName,cip_errno,cip_err_msg); result=ERROR; } }; break; case LGX: { LGX_Read *data=ReadLgxData(Session,Connection,TagName,1); if (data!=NULL) { Log(LOG_DEBUG,"[ReadTag] %s on %s Datatype:%x (%s)\n",TagName,Plc->PlcName,data->type,cip_err_msg); *DataType = data->type; switch(data->type) { case LGX_BOOL: { IValue=GetLGXValueAsInteger(data,0); if (!cip_errno) { if (IValue!=0) IValue=1; Log(LOG_DEBUG,"[ReadTag] %s on %s = %d (%s)\n",TagName,Plc->PlcName,IValue,cip_err_msg); sprintf(responseValue,"%d",IValue); result=SUCCESS; } else { Log(LOG_WARNING,"[ReadTag] Get value : (%d) %s\n",cip_errno,cip_err_msg); result=ERROR; } }break; case LGX_BITARRAY: { // }break; case LGX_SINT: case LGX_INT: case LGX_DINT: { IValue=GetLGXValueAsInteger(data,0); if (!cip_errno) { Log(LOG_DEBUG,"[ReadTag] %s on %s = %d (%s)\n",TagName,Plc->PlcName,IValue,cip_err_msg); sprintf(responseValue,"%d",IValue); result=SUCCESS; } else { Log(LOG_WARNING,"[ReadTag] Get value : (%d) %s\n",cip_errno,cip_err_msg); result=ERROR; } }break; case LGX_REAL: { FValue=GetLGXValueAsFloat(data,0); if (!cip_errno) { Log(LOG_DEBUG,"[ReadTag] %s on %s = %f (%s)\n",TagName,Plc->PlcName,FValue,cip_err_msg); sprintf(responseValue,"%f",FValue); result=SUCCESS; } else { Log(LOG_WARNING,"[ReadTag] Get value : (%d) %s\n",cip_errno,cip_err_msg); result=ERROR; } }break; } free(data); } else { Log(LOG_WARNING,"[ReadTag] ReadLgxData error on tag : %s (%s)\n",TagName,cip_err_msg); result=ERROR; } }; break; default:{ Log(LOG_WARNING,"[ReadTag] Plc type unknown for : %s\n",Plc->PlcName); result=ERROR; } break; } return result; }