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

📄 ecinterface.c

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

   /*
   ** msg should still have the original command, and options (both in host order) in it.
   ** Context is also preserved, though the order was never changed
   */

   iSize                                 = CB_GetDataSizeComBuf( aCbData ); /* CB_GetDataSizeComBuf(pTrrbl->pComBuf); */
   msg->sEncap.sPkt_tag.sTag.iTag_length = iSize;
   msg->lLength                          = iSize;

   /*
   ** Instead of copying the data to a new buffer, just attach the combuff and send it to the
   ** transmission task
   */
   msg->pData            = aCbData->pData;    /* pTrrbl->pComBuf->pData; /* point the datapointer to the combuffer */
   msg->pCb              = aCbData;           /* pTrrbl->pComBuf; */
   EnCdMessage.Command   = SENDCOMMBUF;                                  /* Send the attached message using a combuf */
   EnCdMessage.PacketBuf = msg;                                          /* Tcpip message to be sent.  It will be deleted by the send task */

   /*
   ** Send to queue the message at the send task, or the management task depending on connection state
   */
   Status = OE_MsgQSend(msg->sOutputQ,
                       (char *) &EnCdMessage.Command,
                       sizeof EnCdMessage,
                       WAIT_FOREVER,
                       MSG_PRI_NORMAL);

   /*
   ** Send failed, we need to delete the packet here lest we loose it
   */
   if (Status == OE_ERROR)
   {
      CB_DeleteComBuf( aCbDestHost );
      CB_DeleteComBuf( msg->pCb );
      PktBufFree(msg);
      return ECI_E_SEND_SOME_DATA_FAILED;
   }

	/* ! */
	return ECI_E_SUCCESS;
}

/*
** start edits: September,26th 2005, H.F.
**
** function to let the user change the run/idle state of the PRODUCER Assembly
*/
EXTFUNC ETHERNETIPEXAMPLECODE_API void EC_UpdateRunIdleStateOfProducerAssembly( BOOL theRun_Idle_Flag, int theAssemblyInstanceNo ){

/*
** update the selected run/idle state
*/
	t_UserDefinedAssemblyInstance *aProducerAssemblyInstance;
	ID_TrrblType             *pIdTrrbl;

	if( aProducerAssemblyInstance = EC_FindUserDefinedAssemblyInstance(theAssemblyInstanceNo))
	{
		/* run? */
		if( theRun_Idle_Flag )
			aProducerAssemblyInstance->itsState |= 0x0001;
		else /* idle */
			aProducerAssemblyInstance->itsState &= 0xFFFE;


		/*
		** start edits: October,14th 2005, H.F.
		**
		** set run in SY_RunIdleArea, if the producer is actually
		** producing data (i.e. is active); otherwise idle
		*/

		if( theRun_Idle_Flag && ( aProducerAssemblyInstance->itsState & 0x0004 /* active */ ) )
			SY_RunIdleArea[aProducerAssemblyInstance->itsAppDataAreaId-AD_APP_DATA_AREA_EC_BASE] = 1 /* run */;
		else
			SY_RunIdleArea[aProducerAssemblyInstance->itsAppDataAreaId-AD_APP_DATA_AREA_EC_BASE] = 0 /* idle */;

		/*
		** Generate an event indicating that the Extended Device Status
		** might have changed.
		*/

		pIdTrrbl = GS_NewTrrbl( ID_TrrblType );
		pIdTrrbl->eRequest    = TREQ_UPDATE_STATUS;
		pIdTrrbl->iStatusMask = ID_STATUS_STATE_MASK;
		pIdTrrbl->iStatus     = ID_STATUS_IDLE;	/* won't be used! */
		GS_PutTrrbl( ID_xQid, pIdTrrbl );

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


		EC_Run_IdleCB(theRun_Idle_Flag,theAssemblyInstanceNo);
	}

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


#ifdef DEBUG

char *dbg_strfill( char *s, char ch, int n ){
/*
** fill string s with ch (n times), append '\0' at the end
*/
	/* ! */
	int i=0;

	/* ! */
	while( i<n ) s[i++] = ch; s[i]='\0';

	/* ! */
	return s;
}

char *dbg_strchcat( char *s, char ch ){
/*
** append ch to string s
*/
char p[2]="*"; *p=ch; strcat(s,p);  return s;
}

static char *str_hs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char *dbg_u2str( unsigned long w, unsigned int base, char *s, unsigned int maxlen, unsigned int fillen ){
/*
** convert w to string s, taking the base into account, if maxlen!=0 in consideration of fillen fill up the result with '0'
*/
unsigned int i, r;

	/* ! */
	strcpy( s, "" );
	if( (base>1) && (base<=(int)strlen( str_hs )) ){
		for( i=0; i<maxlen; i++ ){
			r  = (unsigned int)(w % base);
			dbg_strchcat( s, str_hs[r] );
			w -= r;
			w  = w / base;
			if( !w ) break;
		}
		if( w ) dbg_strfill( s, '*', maxlen );
		strrev( s );
	}

	/* ! */
	if( strlen( s )<fillen ){
		strrev( s );
		while( strlen( s )<fillen )
			strcat( s, "0" );
		strrev( s );
	}

	/* ! */
	return s;
}

char *dbg_catstr( char *theOutStr, char *theInStr ){
	return strcat( theOutStr, theInStr );
}

char *dbg_catint( char *theOutStr, long theValue, unsigned int theBase, int theLen ){
/*
** append the ASCII representation of theValue to String theOutStr
*/
char aStr[32+1];

	/* ! */
	if( theValue<0 ){ if( theBase==10 ) strcat( theOutStr, "-" ); theValue = -theValue; }

	/* ! */
	dbg_u2str( theValue, theBase, aStr, sizeof( aStr )-1, theLen );

	/* ! */
	strcat( theOutStr, aStr );

	/* ! */
	return theOutStr;
}

char *dbg_ExtractFileName( char *theFullPath, char *theFileName ){
char *aPtr;

	/* ! */
	strcpy( theFileName, theFullPath ); strrev( theFileName );

	/* ! */
	if( NULL!=(aPtr=strstr(theFileName,"\\")) ){
		*aPtr = '\0';
		strrev( theFileName );
	}

	/* ! */
	return theFileName;
}

struct{
	unsigned long itsId;
	char          itsName[256+1];
}gThreadInfo[100];
int gThreadInfoCount=0;

int dbg_ThreadInfoIdx( unsigned long theThreadId ){
int aIdx;

	/* ! */
	for( aIdx=0; aIdx<gThreadInfoCount; aIdx++ )
		if( gThreadInfo[aIdx].itsId==theThreadId )
			return aIdx;
	
	/* ! */
	return -1;
}

char *dbg_ThreadName( unsigned long theThreadId ){

	/* ! */
	int aIdx = dbg_ThreadInfoIdx( theThreadId ); if( aIdx<0 ) return "UnknownThread";

	/* ! */
	return gThreadInfo[aIdx].itsName;
}

void dbg_ThreadDeleted( unsigned long theThreadId ){

	/* ! */
	int i, aIdx;
	aIdx = dbg_ThreadInfoIdx( theThreadId ); if( aIdx<0 ) return;

	/* ! */
	{ char aMsg[256+1]; strcpy( aMsg, "ThreadDeleted: " ); dbg_catint(aMsg,theThreadId,16,0); DBLN( aMsg ); }

	/* ! */
	for( i=aIdx; i<gThreadInfoCount-1; i++ )
		gThreadInfo[i] = gThreadInfo[i+1];

	/* ! */
	gThreadInfoCount--;
}

void dbg_ThreadCreated( unsigned long theThreadId, char *theThreadName ){
char aMsg[256+1];

	/* ! */
	strcpy( aMsg, "ThreadCreated: " ); dbg_catint(aMsg,theThreadId,16,0); dbg_catstr(aMsg,": " ); dbg_catstr( aMsg, theThreadName ); DBLN( aMsg );

	/* ! */
	if( gThreadInfoCount>=sizeof(gThreadInfo)/sizeof(gThreadInfo[0]) )
		return;

	/* ! */
	gThreadInfo[gThreadInfoCount].itsId = theThreadId;
	strcpy( gThreadInfo[gThreadInfoCount].itsName, theThreadName );

	/* ! */
	gThreadInfoCount++;
}

char *dbg_FormatTimeStr( unsigned long theTickCount, char *theTimeStr ){
int aMySec, aMiSec, aSec, aMin;
	aMySec = theTickCount %   10; theTickCount /=   10;
	aMiSec = theTickCount % 1000; theTickCount /= 1000;
	aSec   = theTickCount %   60; theTickCount /=   60;
	aMin   = theTickCount %   60; theTickCount /=   60;
	sprintf( theTimeStr, "%02d:%02d.%03d.%1d", aMin, aSec, aMiSec, aMySec );

	/* ! */
	return theTimeStr;
}
	
LARGE_INTEGER gCPF = { 0 };

static CRITICAL_SECTION       EC_QSection;
static int gCSIsInitialized = 0;

int gFirstCall = 1;
FILE *gLogFile = NULL;
void dbg_PrintLine( char *theStr, int theErrorCode, char *theMsg, char *theFile, unsigned int theLine ){
char aMsg[512+1];
unsigned long aTickCount;
char aTimeStr[256+1];
char aFileName[256+1]; 
LARGE_INTEGER aPC; 

if( !gCSIsInitialized ){
	InitializeCriticalSection( &EC_QSection );
	gCSIsInitialized = 1;
}

EnterCriticalSection( &EC_QSection );

	/* ! */
	strcpy( aMsg, "" );

	/* ! */
	if( !gCPF.QuadPart )
		QueryPerformanceFrequency ( &gCPF );

	/* ! */
	QueryPerformanceCounter( &aPC ); aTickCount = aPC.QuadPart / (gCPF.QuadPart / (1000*10));
	dbg_FormatTimeStr( aTickCount, aTimeStr );

	dbg_ExtractFileName( theFile, aFileName );

	dbg_catstr( aMsg, aTimeStr );                               dbg_catstr( aMsg, ": " );
	dbg_catstr( aMsg, dbg_ThreadName( GetCurrentThreadId() ) ); dbg_catstr( aMsg, ", " );

	/* ! */
	if( *theStr      ){ dbg_catstr( aMsg, theStr                              ); dbg_catstr( aMsg, ", " ); }
	if( theErrorCode ){ dbg_catint( aMsg,  theErrorCode, 10, 0                ); dbg_catstr( aMsg, ", " ); }
	if( *theMsg      ){ dbg_catstr( aMsg, theMsg                              ); dbg_catstr( aMsg, ","  ); }
	dbg_catstr( aMsg, " '" ); dbg_catstr( aMsg, aFileName ); dbg_catstr( aMsg, "'" );
	dbg_catstr( aMsg, " "  ); dbg_catint( aMsg, theLine, 10, 0 );

#ifdef _CONSOLE
	printf( "%s\n", aMsg );
#else
	/* ! */
	if( gLogFile==NULL ){
		gLogFile = fopen( "C:\\EtherNetIP.log", gFirstCall ? "w" : "a"); gFirstCall = 0;
	}
	if( gLogFile!=NULL ){
		fprintf( gLogFile, "%s\n", aMsg ); fflush( gLogFile );
		fclose( gLogFile ); gLogFile = NULL;
	}

#endif

LeaveCriticalSection( &EC_QSection );

}

char *dbg_ToHex( char *theOutStr, int theMaxOutLen, unsigned char *theBuf, int thePortion, int theDataLen, int theFrstPos ){
/*
** append the ASCII representation of the first theLen bytes to theOutStr beginnig at theBuf
*/
int i;
char aTmpStr[8];

	/* ! */
	strcpy( theOutStr, "[" ); dbg_catint( theOutStr, theDataLen, 10, 0 ); dbg_catstr( theOutStr, "." ); dbg_catint( theOutStr, theFrstPos, 10, 0 ); dbg_catstr( theOutStr, "]:" );

	/* ! */
	for( i=0; i<thePortion; i++ ){

		/* ! */
		strcpy( aTmpStr, " " );

		/* ! */
		dbg_catint( aTmpStr, theBuf[i], 16, 2 );

		/* ! */
		if( (int)((strlen( theOutStr ) + strlen( aTmpStr ))) >theMaxOutLen )
			break;

		/* ! */
		strcat( theOutStr, aTmpStr );
	}

	/* ! */
	return theOutStr;
}


void dbg_LogHex( char *thePrFx, void *theData, int theDataLen ){
/*
** log of the hex value with thePrFx prefixed
*/
#define PORTION 64
char aHexData[512+1], aMsg[512+1];
int i, aRest;

	/* ! */
	aRest = theDataLen;

	/* ! */
	for( i=0; i<theDataLen; i+=PORTION ){

		/* ! */
		int aPortion = PORTION; if( aRest>0 && aRest<PORTION ) aPortion = aRest;
		dbg_ToHex( aHexData, sizeof( aHexData )-1, &(((unsigned char *)theData)[i]), aPortion, theDataLen, i ); aRest -= aPortion;

		/* ! */
		strcpy( aMsg, thePrFx ); dbg_catstr( aMsg, aHexData ); DBLN( aMsg );
	}
}

char *dbg_GetLastErrorText( char *theStr, int theMaxLen, unsigned long theLastError ){
/*
**
*/
char *lpszTemp;
DWORD dwSize, dwRet;

	/* ! */
	lpszTemp = NULL;
	dwSize = theMaxLen;

	/* ! */
	strcpy( theStr, "" );

	/* ! */
	if( !theLastError )
		theLastError = GetLastError();
	
	/* ! */
    dwRet=FormatMessage(
					FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
                    NULL,
                    (DWORD)theLastError,
                    LANG_NEUTRAL,
                    (char *)&lpszTemp,
                    0,
                    NULL
	);

    /* ! supplied buffer is not long enough */
    if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) )

		/* ! */
        theStr[0] = TEXT('\0');

    else{

		/* ! */
        lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0');  /* remove cr and newline character */

		/* ! */
        sprintf( theStr, TEXT("%s (0x%x)"), lpszTemp, GetLastError() );

    }

	/* ! */
    if( lpszTemp!=NULL )
        LocalFree( (HLOCAL) lpszTemp );

	/* ! */
	return theStr;
}

void dbg_LogLastError( char *thePreFix ){
	char aLastErrorText[512+1], aMsg[1024+1];
	strcpy( aMsg, thePreFix );
	dbg_catstr( aMsg, dbg_GetLastErrorText(aLastErrorText,sizeof(aLastErrorText)-1,WSAGetLastError()) );
	DBLN( aMsg );
}

#endif /* DEBUG */


⌨️ 快捷键说明

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