bool QPlc::OpenConnection(void) { bool result=true; char ip[16]; Plc_Type vPlcType; vPlcType=(Plc_Type) m_PlcType; strcpy(ip,m_ip.toStdString().c_str()); m_Session=OpenSession(ip); if (m_Session!=NULL) { if (RegisterSession(m_Session)<0) { CloseSession(m_Session); return false; } } else { return false; } m_Connection=ConnectPLCOverCNET(m_Session,vPlcType,(long)m_Session,GetSerial(),m_time_out,m_PlcPath,m_path_size);//(int)m_Session if (m_Connection==NULL) { return false; } qDebug("Connection OK"); return result; }
int Connect(PLC *Plc,char *TagName, char *responseValue) { int result=SUCCESS; int path_size=0; char ip[16]; BYTE path[40]; Eip_Session *Session=NULL; Eip_Connection *Connection=NULL; int dataType; Log(LOG_DEBUG,"[Connect] Building Session for %s\n",Plc->PlcName); path_size=ParsePath(Plc->PlcPath,ip,path); if (path_size>0) { Session=OpenSession(ip); if (Session!=NULL) { if (RegisterSession(Session)<0) { CloseSession(Session); Log(LOG_CRIT,"[Connect] Unable to register session for Plc: %s (%s) \n",Plc->PlcName,cip_err_msg); return ERROR; } } else { Log(LOG_CRIT,"[Connect] Unable to open session for Plc: %s (%s)\n",Plc->PlcName,cip_err_msg); return ERROR; } if (Plc->NetWork) Connection=ConnectPLCOverDHP(Session,Plc->PlcType,(int)Session,GetSerial(),MAX_SAMPLE,Plc->NetWork,path,path_size); else Connection=ConnectPLCOverCNET(Session,Plc->PlcType,(int)Session,GetSerial(),MAX_SAMPLE,path,path_size); if (Connection!=NULL) { Log(LOG_DEBUG,"[Connect] Connection (%p) created for PLC : %s (%s) )\n",Connection,Plc->PlcName,cip_err_msg); } else { Log(LOG_CRIT,"[Connect] Unable to create connection for Plc: %s (%s)\n",Plc->PlcName,cip_err_msg); return ERROR; } } else Log(LOG_ERR,"[Connect] Invalid path : %s\n",Plc->PlcPath); Log(LOG_DEBUG,"[Connect] Connect : %s [%s](%p / %p)\n",TagName,writeValue,Session,Connection); if (ReadTag(Plc, Session, Connection, TagName, &dataType, responseValue)!=SUCCESS) return ERROR; else { if (isWrite) { if (WriteTag(Plc, Session, Connection, TagName,dataType)!=SUCCESS) return ERROR; Log(LOG_DEBUG,"[Connect] %s [%s] %x (%p / %p)\n",TagName,writeValue,dataType,Session,Connection); if (ReadTag(Plc, Session, Connection, TagName, &dataType, responseValue)!=SUCCESS) return ERROR; } } 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); }