void KillConnections(void) { Log(LOG_NOTICE,"There is %d Sessions and %d Connections\n",SESSIONs.Count,CONNECTIONs.Count); ELEMENT *elt; while ((elt=GetFirst(&CONNECTIONs))!=NULL) { Eip_Connection *connexion=elt->Data; RemoveChListe_Ex(&CONNECTIONs,elt); free(elt); if (connexion==NULL) continue; if (Forward_Close(connexion)>=0) Log(LOG_NOTICE,"Connection (%p) Killed\n",connexion); else Log(LOG_WARNING,"Unable to kill Connection (%p)\n",connexion); } memset(&CONNECTIONs,0,sizeof(CONNECTIONs)); while ((elt=GetFirst(&SESSIONs))!=NULL) { Eip_Session *session=elt->Data; RemoveChListe_Ex(&SESSIONs,elt); free(elt); if (UnRegisterSession(session)>=0) Log(LOG_NOTICE,"Session (%p) Killed\n",session); else Log(LOG_WARNING,"Unable to kill session (%p)\n",session); CloseSession(session); } memset(&SESSIONs,0,sizeof(SESSIONs)); }
int KillConnection(Eip_Connection *connection) { int res=0; if (connection==NULL) return(0); RemoveChListe(&CONNECTIONs,connection); if ((res=Forward_Close(connection))>=0) Log(LOG_DEBUG,"Connection (%p) Killed\n",connection); else Log(LOG_WARNING,"Unable to kill Connection (%p)\n",connection); Log(LOG_DEBUG,"There is %d Connection\n",CONNECTIONs.Count); return(res); }
int readwritedata(PLC *plc,LISTE *var) { int res,i,path_size; char ip[16],path[40]; Eip_Session *session=NULL; Eip_Connection *connection=NULL; path_size=ParsePath(plc->PlcPath,ip,path); if (path_size<=0) return(1); session=OpenSession(ip); if (session!=NULL) { session->Sender_ContextL=getpid(); if (RegisterSession(session)>=0) { if (plc->NetWork) connection=_ConnectPLCOverDHP(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, GetSerial(), //ConnSerialNumber _OriginatorVendorID,_OriginatorSerialNumber,_TimeOutMultiplier, UPDATERATE*500, _Transport, plc->NetWork, path, path_size); else connection=_ConnectPLCOverCNET(session, plc->PlcType, _Priority,_TimeOut_Ticks, (int)session, //TO_ConnID, GetSerial(), //ConnSerialNumber _OriginatorVendorID,_OriginatorSerialNumber,_TimeOutMultiplier, UPDATERATE*500, _Transport, path, path_size); /******* Connection is Ok **********/ if (connection!=NULL) { Log(LOG_DEBUG,"Connection (%p) created for PLC : %s\n",connection,cip_err_msg); /*********** Reading / Writing Value from Lgx ******************/ for(i=0;i<AB_VARCOUNT;i++) { double val_float; int val_int; TAG *atag=var->Data[i]; Log(LOG_INFO,"entering ReadData %s\n",atag->Value_Address); res=readplc(plc,session,connection,atag->Value_Address,AB_REAL,&val_float); if (res) Log(LOG_WARNING,"ReadData (%d): %s\n",res,cip_err_msg); Log(LOG_INFO,"ReadData (%d): %s\n",res,cip_err_msg); atag->Value=val_float; val_int=1; Log(LOG_INFO,"entering WriteData %s = %f\n",atag->Reset_Address,val_int); res=writeplc(plc,session,connection,atag->Reset_Address,AB_BIT,&val_int); if (res) Log(LOG_WARNING,"WriteData (%d): %s\n",res,cip_err_msg); Log(LOG_INFO,"WriteData (%d): %s\n",res,cip_err_msg); } /***************************************************************/ if ((res=Forward_Close(connection))>=0) Log(LOG_DEBUG,"Connection (%p) Killed\n",connection); else Log(LOG_WARNING,"Unable to kill Connection (%p)\n",connection); } else //connection=NULL { Log(LOG_CRIT,"Unable to create connection : %s\n",cip_err_msg); } if ((res=UnRegisterSession(session))>=0) Log(LOG_DEBUG,"Session (%p) Killed\n",session); else Log(LOG_WARNING,"Unable to kill session (%p)\n",session); //Log(LOG_WARNING,"closing session (%p)\n",session); CloseSession(session); return(0); }else // Prob RegisterSession { Log(LOG_CRIT,"Unable to register session : %s \n",cip_err_msg); CloseSession(session); return(1); } } else { Log(LOG_CRIT,"Unable to open session for : %s\n",cip_err_msg); return(1); } }
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); }