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

📄 mac.cpp

📁 VC代码
💻 CPP
字号:
#include <stdio.h>
#include <Windows.h>
#include <time.h>
#include "D:\Program Files\Teradyne\Spectrum\D0_9\TOOLKIT\INCLUDE\Clib.l"

#define  MAC_ADDRESS_LENGTH 12

char SerialNumber[15];
char sComCode[15];
char MAC_Address1[15];
char sJobPath[100];

char* Spears_log="log.txt";
bool Read_log(char *);
bool ExecuteFile(char*);
bool WriteImgFile(char *, char *);
FILE* pFile;
char ServerError[100];
//to check SerialNumber
int nSN_beg;
int nSN_end;
char sSN_str[15];


extern "C" __declspec(dllexport) GetMacAddress(void) 
{
	//Check the spectrum is been pushed on Cancel or about button
	int nCtl;	
	char sTemp[200];

	SPECTRUM_GetFlag(CLIB_CANCEL_ABORT,&nCtl);
	if(nCtl==CLIB_ON) return 0;
	
	//Get Serial Number
	SPECTRUM_GetSystemString(CLIB_SERIALNUMBER,SerialNumber,12);	//get the serial number
	if(strlen(SerialNumber)!=12)
	{
		MessageBox(NULL,"Serial Number's length is wrong","Serial Number",NULL); return 1;
	}
	
	//Get Board's ComCode
	SPECTRUM_GetUserString("ComCode",sComCode,0,sizeof(sComCode));
	//Get JobPath
	SPECTRUM_GetUserString("JOBPATH", sJobPath, 0,sizeof(sJobPath));

	//Run Spears_m
	sprintf(sTemp,"interface_m.bat %s %s >%s", SerialNumber,sComCode, Spears_log);
	MessageBox(NULL,sTemp,NULL,NULL);				
	if(ExecuteFile(sTemp))
	{
//		MessageBox(NULL,"Execute pass",NULL,NULL);
		if(!Read_log(Spears_log))
		{
			sprintf(sTemp,"Couldn't get MAC address from Server");
			MessageBox(NULL, sTemp,"MAC Address Error",NULL);

			sprintf(sTemp,"\\S030\\TR\nFail Error:%s\\TK\\S012\n",ServerError);
			SPECTRUM_PrintToOutputWindow(sTemp);
		
		}
		else
		{			
			WriteImgFile(MAC_Address1,"\\ildp\\mac\\mac.img");

			sprintf(sTemp,"\\S030\\TB\nThe Board's MAC address is %s\nThe Board's S/N is %s\\TK\\S012\n",MAC_Address1,SerialNumber);
			SPECTRUM_PrintToOutputWindow(sTemp);

		}
	}
	return 0;
}

bool ExecuteFile(char * pStr)
{
	STARTUPINFO si;
    memset(&si,0,sizeof(si));
    si.cb=sizeof(si);
	PROCESS_INFORMATION pi;
	SetCurrentDirectory("C:\\interface");
	if(CreateProcess(NULL,pStr,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
	{
		WaitForSingleObject(pi.hProcess,INFINITE);	return true;		
	}
	else
	{
		MessageBox(NULL,"Process Create Failed",NULL,NULL);	return false;
	}
}

bool Read_log(char * file)  
{
	FILE * pFile;
	char log[200];
	char Addr[10];
	char *Beg, *End;

	if((pFile = fopen (file, "r"))!=NULL)
	{
		fgets(log,200,pFile);
		if(log[0]=='0')
		{
			strcpy(Addr,"MAC1=");  //Find the first MAC address
			Beg=strstr(log,Addr);
			Beg+=5;  //remove the string "MACx=", x is for 1,2 or others.
			End=strchr(Beg,',');
			strncpy(MAC_Address1,Beg,End-Beg);
			MessageBox(NULL, MAC_Address1,NULL,NULL);
			return true;
		}
		else 
		{
			strcpy(ServerError,log+3);
			return false;
		}
		fclose(pFile);
	}
	return false;
}


bool WriteImgFile(char *MAC_Addr, char *ImgFile)
{
	FILE *fp;		
	int MAC_Value[MAC_ADDRESS_LENGTH]={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
	char nibble[] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
	char errorString[100];

	MessageBox(NULL, ImgFile,NULL,NULL);
	MessageBox(NULL, sJobPath,NULL,NULL);

	strcat(sJobPath,ImgFile);
	MessageBox(NULL, sJobPath,NULL,NULL);

	if( (fp = fopen(sJobPath, "wb")) == (FILE *) NULL)
	{
		sprintf(errorString,"Couldn't open image file '%s'", sJobPath);
		MessageBox(NULL, errorString, "writeImgFile()",NULL);
		return false;
	}

	for (int i=0;i < MAC_ADDRESS_LENGTH;i++)
	{
		if (MAC_Addr[i] == '0') MAC_Value[i]=0x0;
		if (MAC_Addr[i] == '1') MAC_Value[i]=0x1;
		if (MAC_Addr[i] == '2') MAC_Value[i]=0x2;
		if (MAC_Addr[i] == '3') MAC_Value[i]=0x3;
		if (MAC_Addr[i] == '4') MAC_Value[i]=0x4;
		if (MAC_Addr[i] == '5') MAC_Value[i]=0x5;
		if (MAC_Addr[i] == '6') MAC_Value[i]=0x6;
		if (MAC_Addr[i] == '7') MAC_Value[i]=0x7;
		if (MAC_Addr[i] == '8') MAC_Value[i]=0x8;
		if (MAC_Addr[i] == '9') MAC_Value[i]=0x9;
		if (MAC_Addr[i] == 'a') MAC_Value[i]=0xA;
		if (MAC_Addr[i] == 'A') MAC_Value[i]=0xA;
		if (MAC_Addr[i] == 'b') MAC_Value[i]=0xB;
		if (MAC_Addr[i] == 'B') MAC_Value[i]=0xB;
		if (MAC_Addr[i] == 'c') MAC_Value[i]=0xC;
		if (MAC_Addr[i] == 'C') MAC_Value[i]=0xC;
		if (MAC_Addr[i] == 'd') MAC_Value[i]=0xD;
		if (MAC_Addr[i] == 'D') MAC_Value[i]=0xD;
		if (MAC_Addr[i] == 'e') MAC_Value[i]=0xE;
		if (MAC_Addr[i] == 'E') MAC_Value[i]=0xE;
		if (MAC_Addr[i] == 'f') MAC_Value[i]=0xF;
		if (MAC_Addr[i] == 'F') MAC_Value[i]=0xF;
	}
	nibble[0] = (MAC_Value[0]  << 4) + (MAC_Value[1] );
	nibble[1] = (MAC_Value[2]  << 4) + (MAC_Value[3] );
	nibble[2] = (MAC_Value[4]  << 4) + (MAC_Value[5] );
	nibble[3] = (MAC_Value[6]  << 4) + (MAC_Value[7] );
	nibble[4] = (MAC_Value[8]  << 4) + (MAC_Value[9] );
	nibble[5] = (MAC_Value[10] << 4) + (MAC_Value[11]);

	fwrite(&nibble,sizeof(nibble),1,fp);
	fclose(fp);
	return true;
}

⌨️ 快捷键说明

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