⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ecinterface.c

📁 基于EthernetIP协议的应用程序,可以读取AB公司Controllogix系列Ethernetip协议PLC数据. 此软件代码可用于工业控制.
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <stdlib.h>
#include <stdio.h>

/* ! */
#include "gs.h"
#include "ad.h"
#include "cd.h"
#include "cd_util.h"
#include "bf.h"
#include "ad_util.h"
#include "en.h"
#include "sy.h"		/* October,14th 2005, H.F. */
#include "id.h"		/* October,14th 2005, H.F. */

/* ! */
#include "windows.h"

/* ! */
#include "ECUserDefined.h"
#include "ECInterface.h"

/* ! */
#include "ab_env.h"

/* ! identity defaults */
UINT16 EC_ProductCode        =14; /* PRODUCT_CODE; */
UINT16 EC_ProductType        =11; /* PRODUCT_TYPE; */
UINT32 EC_SerialNumber       = 0;
UINT16 EC_VendorId           = 1; /*         VENDOR; */
UINT8  EC_MajorRev           = 1; /* MAJOR_REVISION; */
UINT8  EC_MinorRev           = 1; /* MINOR_REVISION; */
UINT8  EC_ProductName[256+1] = {'\0' };
UINT16 EC_Status             = 0;

/* ! */
#define MAX_USER_DEFINED_CLASSES 10

/* ! global error variable	*/
enum e_ECIError gECIError=ECI_E_SUCCESS;

char *EC_ErrorText( enum e_ECIError theECIError ){
/**/
/**/
/**/

#ifdef DEBUG

	/* ! */
	switch( theECIError ){
		case ECI_E_SUCCESS                          : return "SUCCESS"                       ;
		case ECI_E_CREATEUSERDEFINED_CALL_MISSING   : return "CREATEUSERDEFINED_CALL_MISSING";
		case ECI_E_OUT_OF_MEMORY                    : return "OUT_OF_MEMORY"                 ;
		case ECI_E_CLASS_NOT_FOUND                  : return "CLASS_NOT_FOUND"               ;
		case ECI_E_INSTANCE_NOT_FOUND               : return "INSTANCE_NOT_FOUND"            ;
		case ECI_E_ATTRIBUTE_NOT_FOUND              : return "ATTRIBUTE_NOT_FOUND"           ;
		case ECI_E_USERDEFINED_IS_NULL              : return "USERDEFINED_IS_NULL"           ;
		case ECI_E_CLASS_IS_NULL                    : return "CLASS_IS_NULL"                 ;
		case ECI_E_CLASSCODE_IS_EMPTY               : return "CLASSCODE_IS_EMPTY"            ;
		case ECI_E_ATTRIBUTE_IS_NULL                : return "ATTRIBUTE_IS_NULL"             ;
		case ECI_E_ATTRIBUTECODE_IS_EMPTY           : return "ATTRIBUTECODE_IS_EMPTY"        ;
		case ECI_E_DATASIZE_IS_0                    : return "DATASIZE_IS_0"                 ;
		case ECI_E_DATAPTR_IS_NULL                  : return "DATAPTR_IS_NULL"               ;
		case ECI_E_INSTANCE_IS_NULL                 : return "INSTANCE_IS_NULL"              ;
		case ECI_E_INSTANCECODE_IS_EMPTY            : return "INSTANCECODE_IS_EMPTY"         ;
		case ECI_E_INVALID_TICKPERIOD               : return "INVALID_TICKPERIOD"            ;
		case ECI_E_USERDEFINEDASSEMBLY_IS_EMPTY     : return "USERDEFINEDASSEMBLY_IS_EMPTY"  ;
		case ECI_E_MAX_USER_DEFINED_CLASSES         : return "ECI_E_MAX_USER_DEFINED_CLASSES";
		default                                     : return "unknown gECIError"             ;
	}
#else
	return "ErrorText only available in DEBUG mode";
#endif
}

/* ! */
#define RET(error,ret){ gECIError=error; return ret; }

/* ! */
#define SUB_MALLOC( ptr, tp, size )\
	( ptr ) = NULL;\
	if( size ){\
		( ptr ) = ( tp )malloc( ( size ) );\
		if( ( ptr )==NULL )\
			RET( ECI_E_OUT_OF_MEMORY, -1 );\
		memset( ( ptr ), 0, ( size ) );\
	}

t_UserDefinedAssembly *gUserDefinedAssembly=NULL;

t_UserDefinedAssemblyInstance *EC_FindUserDefinedAssemblyInstance( unsigned int theInstanceNo ){
/*
** returns a pointer to the instance record with the specified InstanceNo
*/
unsigned int i;

	/* ! */
	for( i=0; i<gUserDefinedAssembly->itsInstancesCount; i++ )
		if( gUserDefinedAssembly->itsInstances[i].itsInstanceNo==theInstanceNo )
			return &gUserDefinedAssembly->itsInstances[i];

	/* ! */
	return NULL;
}

int EC_FindUserDefinedAssemblyInstanceNoByAppDataAreaId( int theAppDataAreaId ){
/*
**
*/
	/* ! */
	switch( theAppDataAreaId ){

		/* ! */
		case AD_APP_DATA_AREA_EC_BASE:
		case AD_APP_DATA_AREA_EC_1   :
		case AD_APP_DATA_AREA_EC_2   :
		case AD_APP_DATA_AREA_EC_3   :
		case AD_APP_DATA_AREA_EC_4   :
		case AD_APP_DATA_AREA_EC_5   :
		case AD_APP_DATA_AREA_EC_6   :
		case AD_APP_DATA_AREA_EC_7   :
		case AD_APP_DATA_AREA_EC_8   :
		case AD_APP_DATA_AREA_EC_9   :
		case AD_APP_DATA_AREA_EC_10  :
		case AD_APP_DATA_AREA_EC_TOP : return gUserDefinedAssembly==NULL ? -1 : gUserDefinedAssembly->itsInstances[theAppDataAreaId-AD_APP_DATA_AREA_EC_BASE].itsInstanceNo;

		/* ! */
		default                        : return -1;
	}
}

int EC_CreateUserDefinedAssembly( unsigned int theInstancesCount ){
/*
** allocate enough room for the user defined assembly objects
*/
int aMaxAreaCount, aId;
unsigned int i;

	/* ! detemine the max User defined Area count */
	aMaxAreaCount = AD_APP_DATA_AREA_EC_TOP - AD_APP_DATA_AREA_EC_BASE + 1;


	/* ! */
	ECASSERT( gUserDefinedAssembly==NULL,                     0, "gUserDefinedAssembly!=NULL"      );
	ECASSERT( theInstancesCount>0,                            0, "theClassesCount<=0"              );
	ECASSERT( theInstancesCount<=(unsigned int)aMaxAreaCount, 0, "theInstancesCount>aMaxAreaCount" );


	/* ! create UserDefinedAssembly */
	SUB_MALLOC( gUserDefinedAssembly, t_UserDefinedAssembly *, sizeof( t_UserDefinedAssembly ) );

	/* ! create InstancesInfo */
	SUB_MALLOC( gUserDefinedAssembly->itsInstances, t_UserDefinedAssemblyInstance *, theInstancesCount*sizeof( t_UserDefinedAssemblyInstance ) );

	/* ! */
	gUserDefinedAssembly->itsInstancesCount = theInstancesCount;

	/* ! preset of all userdefined assembly objects */
	for( i=0; i<theInstancesCount; i++ ){

		/* ! */
		aId = AD_APP_DATA_AREA_EC_BASE + i;

		/* ! */
		gUserDefinedAssembly->itsInstances[i].itsAppDataAreaId = aId;

		/* ! */
		ad_s.asDataArea[ aId ].pabBaseAddress = NULL;
		ad_s.asDataArea[ aId ].iSize          =    0;


		/*
		** start edits: October,17th 2005, H.F.
		*/

		memset(SY_DefaultConfigArea[i].Data,0,sizeof(SY_DefaultConfigArea[i].Data));
		SY_DefaultConfigArea[i].iSize = 0;

		/*
		** end edits: October,17th 2005, H.F.
		*/
	}


	/*
	** start edits: October,14th 2005, H.F.
	*/

	/* set NullArea now and forever to 0 */
	memset(SY_NullArea,0,sizeof(SY_NullArea));

	/* preset of the run/idle area */
	memset(SY_RunIdleArea,0,sizeof(SY_RunIdleArea));

	/* preset of the config area */
	memset(SY_ConfigArea,0,sizeof(SY_ConfigArea));

	/*
	** end edits: October,14th 2005, H.F.
	*/


	/* ! */
	RET(ECI_E_SUCCESS,0);
}

int EC_CreateUserDefinedAssemblyInstance( unsigned int theInstanceNo, e_UserDefinedAssemblyInstance theType, unsigned char **theDataPtr, unsigned int theDataByteSize ){
/*
** create an user defined assembly object
*/
unsigned int i;
t_UserDefinedAssemblyInstance *aSY;

	/* ! */
	ECASSERT( gUserDefinedAssembly!=NULL, 0, "gUserDefinedAssembly==NULL" );

	/* ! find free index postion */
	for( i=0; i<gUserDefinedAssembly->itsInstancesCount; i++ )
		if( !gUserDefinedAssembly->itsInstances[i].itsInstanceNo )
			break;

	/* ! */
	ECASSERT( i<gUserDefinedAssembly->itsInstancesCount, 0, "i>=gUserDefinedAssembly->itsInstancesCount" );

	/* ! */
	aSY = &gUserDefinedAssembly->itsInstances[i];

	/* ! */
	aSY->itsInstanceNo        = theInstanceNo;
	aSY->itsSYType            = theType;
	aSY->itsUserDataByteSize  = theDataByteSize;

	/*
	** start edits: September,9th 2005, H.F.
	*/

	aSY->itsExclusiveOwnedStatus = 0; /* not exclusive owned */

	/*
	** end edits: September,9th 2005, H.F.
	*/

	/*
	** start edits: September,19th 2005, H.F.
	*/

	aSY->itsState = 0x0000;			/* idle, non-existent and not configured */
	aSY->itsIOConnectionCount = 0;	/* no I/O connection is established with this instance */

	/*
	** end edits: September,19th 2005, H.F.
	*/


	/* ! offset */
	switch( aSY->itsSYType ){
		case CONSUMER  : aSY->itsMaxDataByteSize   = theDataByteSize + 4; break; /* ! plus 4 bytes for the 32-bit header */
		case PRODUCER  : aSY->itsMaxDataByteSize   = theDataByteSize + 4; break; /* ! plus 4 bytes for the 32-bit header */
		case CONFIG    : aSY->itsMaxDataByteSize   = theDataByteSize + 4; break; /* ! Config is treated like output data */
		case STATUSIN  : aSY->itsMaxDataByteSize   = theDataByteSize + 4; break;
		case STATUSOUT : aSY->itsMaxDataByteSize   = theDataByteSize + 4; break;
	}

	*theDataPtr = (unsigned char *)malloc( aSY->itsMaxDataByteSize ); ECASSERT(*theDataPtr!=NULL,0,"*theDataPtr==NULL");
	memset( *theDataPtr, 0, aSY->itsMaxDataByteSize );


	/*
	** start edits: October,17th 2005, H.F.
	**
	** store "out-of-box" configuration
	*/

	if( aSY->itsSYType == CONFIG )
	{
		/* get config size */
		SY_DefaultConfigArea[aSY->itsAppDataAreaId-AD_APP_DATA_AREA_EC_BASE].iSize = aSY->itsMaxDataByteSize;

		/* check size */
		if( aSY->itsMaxDataByteSize >
				sizeof(SY_DefaultConfigArea[aSY->itsAppDataAreaId-AD_APP_DATA_AREA_EC_BASE].Data) )
			GS_LogEvent( GS_BAD_SIZE, 0, 0, FATAL );

		/* store config */
		memcpy(SY_DefaultConfigArea[aSY->itsAppDataAreaId-AD_APP_DATA_AREA_EC_BASE].Data,
			   *theDataPtr,aSY->itsMaxDataByteSize);
	}

	/*
	** end edits: October,17th 2005, H.F.
	*/


	/* ! */
	ad_s.asDataArea[ aSY->itsAppDataAreaId ].pabBaseAddress = *theDataPtr;
	ad_s.asDataArea[ aSY->itsAppDataAreaId ].iSize          =  aSY->itsMaxDataByteSize;

	/* ! offset */
	switch( aSY->itsSYType ){
		case CONSUMER : (*theDataPtr) += 4; break;
		case PRODUCER : /*(*theDataPtr) += 4*/; break;
		case CONFIG   : (*theDataPtr) += 4; break;
		case STATUSIN : /*(*theDataPtr) += 4;*/ break;
		case STATUSOUT: (*theDataPtr) += 4; break;
	}

	/* ! */
	RET( ECI_E_SUCCESS, 0 );
}

/* ! */
t_UserDefined *gUserDefined = NULL;


int EC_CreateUserDefined( unsigned int theClassesCount ){
/*
** allocate enough room for the user defined classes
*/
	/* ! */
	ECASSERT( gUserDefined==NULL, 0, "gUserDefined!=NULL" );
	ECASSERT( theClassesCount>0,  0, "theClassesCount<=0" );

	/* ! */
	if( theClassesCount>MAX_USER_DEFINED_CLASSES )
		RET(ECI_E_MAX_USER_DEFINED_CLASSES,-1);

	/* ! create UserDefined */
	SUB_MALLOC( gUserDefined, t_UserDefined *, sizeof( t_UserDefined ) );

	/* ! create Classes */
	SUB_MALLOC( gUserDefined->itsClasses, t_Class *, theClassesCount*sizeof( t_Class ) );
	gUserDefined->itsClassesCount = theClassesCount;

	/* ! */
	RET(ECI_E_SUCCESS,0);
}

t_Class *FindClass( unsigned int theClassCode ){
/*
** return pointer to the Class identified by theClassCode
*/
unsigned int c;

	/* ! */
	if( gUserDefined==NULL )
		RET(ECI_E_CREATEUSERDEFINED_CALL_MISSING,NULL);

	/* ! */
	for( c=0; c<gUserDefined->itsClassesCount; c++ )

		if( gUserDefined->itsClasses[c].itsClassCode==theClassCode )
			RET(ECI_E_SUCCESS,&gUserDefined->itsClasses[c] );

	/* ! */
	RET(ECI_E_CLASS_NOT_FOUND,NULL);
}

t_Instance *FindInstance( t_Class *theClass, unsigned int theInstanceCode ){
/*
** return pointer to the Instance of theClass identified by theInstanceCode
*/
unsigned int i;

	/* ! */
	for( i=0; i<theClass->itsInstancesCount; i++ )
		if( theClass->itsInstances[i].itsInstanceCode==theInstanceCode )
			RET( ECI_E_SUCCESS, &theClass->itsInstances[i] );

	/* ! */
	RET(ECI_E_INSTANCE_NOT_FOUND,NULL);
}

t_Attribute *FindClassAttribute( t_Class *theClass, unsigned char theAttributeCode ){
/*
** return pointer to the Attribute of theClass idetified by theAttributeCode
*/
unsigned int i;

	/* ! */
	for( i=0; i<theClass->itsAttributesCount; i++ )
		if( theClass->itsAttributes[i].itsAttributeCode==theAttributeCode )
			RET( ECI_E_SUCCESS, &theClass->itsAttributes[i] );

	/* ! */
	RET(ECI_E_ATTRIBUTE_NOT_FOUND,NULL);
}

t_Attribute *FindInstanceAttribute( t_Instance *theInstance, unsigned char theAttributeCode ){
/*
** return pointer to the Attibute of the Instance identified by theAttributeCode
*/
unsigned int i;

	/* ! */
	for( i=0; i<theInstance->itsAttributesCount; i++ )
		if( theInstance->itsAttributes[i].itsAttributeCode==theAttributeCode )
			RET( ECI_E_SUCCESS, &theInstance->itsAttributes[i] );

	/* ! */
	RET(ECI_E_ATTRIBUTE_NOT_FOUND,NULL);
}


int EC_CreateClass( unsigned int theClassCode, unsigned int theAttributesCount, unsigned int theInstancesCount ){
/*
** create new class
*/
t_Class *aClass;

	/* ! a classcode of 0 return the next free class */
	if( (aClass=FindClass( 0 ))==NULL )
		RET(gECIError,-1);

	/* ! */
	ECASSERT( !aClass->itsAttributesCount, 0, "aClass->itsAttributesCount==0" ); ECASSERT( aClass->itsAttributes==NULL, 0, "aClass->itsAttributes!=NULL" );
	ECASSERT( !aClass->itsInstancesCount,  0, "aClass->itsInstancesCount==0"  ); ECASSERT( aClass->itsInstances ==NULL, 0, "aClass->itsInstances !=NULL" );

	/* ! set ClassCode */
	aClass->itsClassCode = theClassCode;

	/* ! create Attibutes */
	SUB_MALLOC( aClass->itsAttributes, t_Attribute *, theAttributesCount*sizeof( t_Attribute ) );
	aClass->itsAttributesCount = theAttributesCount;

	/* ! create Instances */
	SUB_MALLOC( aClass->itsInstances, t_Instance *, theInstancesCount*sizeof( t_Instance ) );
	aClass->itsInstancesCount = theInstancesCount;

	/* ! */
	RET(ECI_E_SUCCESS,0);
}


int EC_CreateInstance( unsigned int theClassCode, unsigned int theInstanceCode, unsigned int theAttributesCount ){
/*
** create new instance for class idetified by theClassCode
*/
t_Class    *aClass;
t_Instance *aInstance;

	/* ! look for the class */
	if( (aClass=FindClass( theClassCode ))==NULL )
		RET(gECIError,-1);

	/* ! an InstanceCode of 0 returns the next free instance */
	if( (aInstance=FindInstance( aClass, 0 ))==NULL )
		RET(gECIError,-1);

	/* ! */
	ECASSERT( !aInstance->itsAttributesCount, 0, "aInstance->itsAttributesCount!=0" ); ECASSERT( aInstance->itsAttributes==NULL, 0, "aInstance->itsAttributes==NULL" );

	/* ! set InstanceCode */
	aInstance->itsInstanceCode=theInstanceCode;

	/* ! create Attributes */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -