计算机COM口数据测试

这篇具有很好参考价值的文章主要介绍了计算机COM口数据测试。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


计算机COM口数据测试
一、基本使用流程

程序需要以管理员身份运行,COM口回路测试需短接2,3pin,测试时候使用控制台,配置测试相关路径,并在测试完成后

1.测试配置路径D:\bigdata\INI\FWCOM.ini

2.测试完成后需要在路径D:\bigdata\LOG\生成测试FWCOM.log文件

程序运行结果示意图

计算机COM口数据测试

运行完成后的日志文件示意图

计算机COM口数据测试

 二、软件设计
源码分为三个部分,common.cpp基本基本信息配置,function.cpp完成功能实现,最后通过main.cpp运行整个程序

common.cpp

 

配置函数代码
#undef UNICODE
#include "common.h"
#include "Config.h"
void TextColor(int color)
{
	HANDLE stdH = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(stdH, color);
}
void PASS()
{
	printf("The test is: ");

	TextColor(GREEN);
	printf("passed");
	printf("\n");
	printf("\n");
	TextColor(WHITE);

}
void FAIL()
{
	printf("The test is: ");

	TextColor(RED);
	printf("failed");
	printf("\n");
	printf("\n");
	TextColor(WHITE);

}
DWORD SubSystemID(MBTYPE mb[])
{
	DWORD decSubSystemID = 0;
	DWORD aimSubSystemID = 0;
	//	BYTE bus = pciSelect[0], dev = pciSelect[1], func = pciSelect[2], offset = 0x2c;
	BYTE bus = 0;
	BYTE dev = 0;
	BYTE func = 0;
	BYTE offset = 0;
	DWORD address = 0;
	for (int i = 0; i < sizeof(mb); i++)
	{
		//		printf("i = %d\n", i);

		if (mb[i].subSystemId != 0)
		{
			bus = mb[i].bus;
			dev = mb[i].dev;
			func = mb[i].fun;
			offset = mb[i].subSystemIdOffset;
			aimSubSystemID = mb[i].subSystemId;
			address = MAKE_CONFIG_ADDRESS(bus, dev, func, offset);
			FoxLib_SetPortVal(PCI_CONFIG_ADDRESS, address, 4);
			FoxLib_GetPortVal(PCI_CONFIG_DATA, &decSubSystemID, 4);
#ifdef DEBUG
			printf("aimSubSystemID = \"%x\", decSubSystemID = \"%x\"\n", aimSubSystemID, decSubSystemID);
#endif
			if (aimSubSystemID == decSubSystemID)
			{
				return 0;
			}

		}

	}
	printf("Not support MB!\n");
	return 1;
}
void GetGlobalData(char* name)
{
	int i = 0;
	int index = 0;
	int toolSelect = 0;
	if (strstr(name, "hwm") != NULL)
	{
		index = 1;
		for (i = 0; i < cTINumberMax; i++)
		{
			if (cFanName[i][0] != '\0')
			{
				iFanIndex[index] = i;
				index++;
				iAllFanNum++;
			}
		}
		index = 1;
		for (i = 0; i < cTINumberMax; i++)
		{
			if (cTempName[i][0] != '\0')
			{
				iTempIndex[index] = i;

				index++;
				iAllTempNum++;
			}
		}
#ifdef DEBUG
		printf("iAllFanNum = %d, iAllTempNum = %d\n", iAllFanNum, iAllTempNum);
#endif

	}
	else if (strstr(name, "idchk") != NULL)
	{
		index = 1;
		for (i = 0; i < cTINumberMax; i++)
		{
			if (cIdChkName[i][0] != '\0')
			{
				iIdChkIndex[index] = i;
				index++;
				iAllIdChkNum++;
			}
		}
#ifdef DEBUG
		printf("iAllIdChkNum = %d\n", iAllIdChkNum);
#endif

	}
	else if (strstr(name, "pcie") != NULL)
	{
		index = 1;
		for (i = 0; i < cTINumberMax; i++)
		{
			if (cPcieName[i][0] != '\0')
			{
				iPcieIndex[index] = i;
				index++;
				iAllPcieNum++;
			}
		}
#ifdef DEBUG
		printf("iAllPcieNum = %d\n", iAllPcieNum);
#endif

	}
	else if (strstr(name, "sata") != NULL)
	{
		index = 1;
		for (i = 0; i < cTINumberMax; i++)
		{
			if (cSataSpdName[i][0] != '\0')
			{
				iSataSpdIndex[index] = i;
				index++;
				iAllSataSpdNum++;
			}
		}
#ifdef DEBUG
		printf("iAllSataSpdNum = %d\n", iAllSataSpdNum);
#endif

	}
	else if (strstr(name, "fntpnl") != NULL)
	{
		index = 1;
		for (i = 0; i < cTINumberMax; i++)
		{
			if (cFntpnlName[i][0] != '\0')
			{
				iFntpnlIndex[index] = i;
				index++;
				iAllFntpnlNum++;
			}
		}
#ifdef DEBUG
		printf("iAllFntpnlNum = %d\n", iAllFntpnlNum);
#endif

	}
	else if (strstr(name, "jumper") != NULL)
	{
		index = 1;
		for (i = 0; i < cTINumberMax; i++)
		{
			if (cJumperName[i][0] != '\0')
			{
				iJumperIndex[index] = i;
				index++;
				iAllJumperNum++;
			}
		}
#ifdef DEBUG
		printf("iAllJumperNum = %d\n", iAllJumperNum);
#endif

	}

}

int GetDataFromConfig(char* fileName, char* dataConfigAppName, char configItemKey[CONFIGNUM][CONFIGDATALENGTH], char dataArray[CONFIGNUM][CONFIGDATALENGTH])
{
	int ret = 0;
	LPTSTR lpReturnedString = NULL;
	LPCTSTR lpFileName = NULL;

	lpReturnedString = (char*)malloc(MAX_PATH);
	memset(lpReturnedString, 0, MAX_PATH);

	int itemNum = 0;
	int itemOffset = 0;
	while (1)
	{

		GetPrivateProfileString(dataConfigAppName, configItemKey[itemNum], NULL, lpReturnedString, MAX_PATH, fileName);
		if (*lpReturnedString == '\0')
		{
#ifdef DEBUG
			printf("Input data error\n");
			printf("-- dataConfigAppName = %s, itemKey = %s, lpReturnedString = %s, fileName = %s\n", dataConfigAppName, configItemKey[itemNum], lpReturnedString, fileName);
#endif		
			ret = 1;
			break;
		}
		else
		{
			itemOffset = 0;
			while (lpReturnedString)
			{
				if (*lpReturnedString == '\0')
				{
					break;
				}
				dataArray[itemNum][itemOffset] = *lpReturnedString;
				itemOffset++;
				lpReturnedString++;

			}
			lpReturnedString -= itemOffset;
#ifdef DEBUG
			printf("dataConfigAppName = %s, itemKey = %s, lpReturnedString = %s, fileName = %s\n", dataConfigAppName, configItemKey[itemNum], lpReturnedString, fileName);
#endif

			itemNum++;
		}
		memset(lpReturnedString, 0, strlen((char*)lpReturnedString));

	}

	return ret;
}
int LogName(char* logName, char* configName)
{
	int lengthConName = strlen(configName);
	int i = 0;
	for (i = 0; i < (lengthConName - 4); i++)
	{
		*logName = *configName;
		logName++;
		configName++;
	}

	logName -= i;

	sprintf(logName, "%s.TXT", logName);
#ifdef DEBUG
	printf("*logName = \"%s\"\n", logName);
#endif
	FILE* fd = NULL;
	fd = fopen(lpLogFileName, "a+");
	if (fd == NULL)
	{
		printf("Create the log file %s failed\n", lpLogFileName);
		return 1;
	}
	return 0;
}

functonc.cpp

功能函数代码
#include "function.h"
#include "..\Common\ExConfig.h"

#define BASSADDRONE 0x3F8
HANDLE hCom;
DCB dcb = { sizeof(DCB) };
char com[5];
int Baudrate[3] = { 0 };
DWORD send_data = 'E';
DWORD resv_data = 'A';

/*
函数功能:设置串口(com)基本配置信息
参数:	baudrate	波特率
		cts	(clear to send)清除发送
		dtr	(data set ready)数据终端准备好
		rts	(request to send)请求数据发送
		dsr	(data set ready)数据准备好
		errorcode  错误代码
		支持硬控件流的UART
*/
int SerialSetting(int baudrate, int cts, int dtr, int rts, char errorCode[])
{
	dcb.fDtrControl = DTR_CONTROL_DISABLE;
	dcb.fRtsControl = RTS_CONTROL_DISABLE;

	if (hCom != INVALID_HANDLE_VALUE)
	{
		CloseHandle(hCom);
	}

	hCom = CreateFile(com, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

	if (hCom == INVALID_HANDLE_VALUE) {
		strcpy_s(itemLog[iItemLogNum].TM_STATUS, "FAIL");
		strcpy_s(itemLog[iItemLogNum].TM_FAILINFO, "DEVICE NO FOUND");
		strcpy_s(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
		TextColor(RED);
		printf("%s NO FOUND\n\n", com);
		TextColor(WHITE);
		iItemLogNum++;
		return 1;
	}
	system("pause");
	GetCommState(hCom, &dcb);
	dcb.BaudRate = baudrate;
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;
	dcb.fOutxCtsFlow = cts;
	dcb.fDtrControl = dtr;
	dcb.fRtsControl = rts;

	int fSuccess = SetCommState(hCom, &dcb);
	if (!fSuccess)	return 1;
	int maskstatus = SetCommMask(hCom, EV_RXCHAR);
	if (maskstatus == 0)	return 1;
	int setupstatus = SetupComm(hCom, 4096, 4096);
	if (setupstatus == 0)	return 1;
	int purgestatus = PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
	if (purgestatus == 0)	return 1;

	COMMTIMEOUTS CommTimeouts;
	CommTimeouts.ReadIntervalTimeout = MAXDWORD;
	CommTimeouts.ReadTotalTimeoutConstant = 1000;
	CommTimeouts.ReadTotalTimeoutMultiplier = 0;
	CommTimeouts.WriteTotalTimeoutConstant = 1000;
	CommTimeouts.WriteTotalTimeoutMultiplier = 0;
	BOOL bTimeOutResult = SetCommTimeouts(hCom, &CommTimeouts);
	if (bTimeOutResult == 0)	return 1;
	return 0;
}

//串口初始化
int InitCOMOne()
{
	int Status = 0;	//状态(成功或失败)
	DWORD sataStatus = 0x0;	//读写地址数据

	Status = FoxLib_SetPortVal(BASSADDRONE + 1, 0, 1);
	if (Status != 0x0)
		return Status;

	Status = FoxLib_SetPortVal(BASSADDRONE + 3, 0x80, 1);
	if (Status != 0x0)
		return Status;

	FoxLib_GetPortVal(BASSADDRONE + 0, &sataStatus, 1);

	Status = FoxLib_SetPortVal(BASSADDRONE + 0, 0x0C, 1);
	if (Status != 0x0)
		return Status;

	Status = FoxLib_SetPortVal(BASSADDRONE + 1, 0x00, 1);
	if (Status != 0x0)
		return Status;

	Status = FoxLib_SetPortVal(BASSADDRONE + 3, 0x03, 1);
	if (Status != 0x0)
		return Status;

	return 0x0;
}

//串口发送数据
int Send_COMOne(DWORD my_date)
{
	int Status = 0;	//状态(成功或失败)
	DWORD sataStatus = 0x0;	//读写地址数据
	UINT32 count = 10000;

	FoxLib_GetPortVal(BASSADDRONE + 5, &sataStatus, 1);

	while (!(sataStatus & 0x20) && (count > 0))
	{
		count--;
		FoxLib_GetPortVal(BASSADDRONE + 5, &sataStatus, 1);
	}

	if (count > 0)
	{
		printf("Count:%d\n", count);
		Status = FoxLib_SetPortVal(BASSADDRONE + 0, my_date, 1);
		if (Status != 0x0)
		{
			return Status;
		}
		printf("Send_COM data %c ", my_date);  //发送数据打印
		TextColor(GREEN);
		printf("Success!\n");
		TextColor(WHITE);
		return 0x0;
	}
	else
	{
		printf("Send_COM data %c ", my_date);
		TextColor(RED);
		printf("Failed!\n\n");
		TextColor(WHITE);
		return 0x01;
	}

}

//串口接收数据
int Recv_COMOne(DWORD* my_date)
{

	DWORD sataStatus = 0;	//读写地址数据
	UINT8 cn = 0;

	while (cn < 100)
	{
		FoxLib_GetPortVal(BASSADDRONE + 5, &sataStatus, 1);
		if (sataStatus & 0x01)
			break;
		else
		{
			Sleep(100);
			cn++;
		}
	}
	FoxLib_GetPortVal(BASSADDRONE + 5, &sataStatus, 1);
	if (sataStatus & 0x01)
	{
		FoxLib_GetPortVal(BASSADDRONE + 0, my_date, 1);
		printf("Recv_COM data  %c ", *my_date);
		TextColor(GREEN);
		printf("Success!\n");
		TextColor(WHITE);
		return 0x0;
	}
	else
	{
		printf("Received data ");
		TextColor(RED);
		printf("Failed!\n\n");
		TextColor(WHITE);
		return 0x01; 
	}
}

int IDCheckMain(char* fileName, char dataConfigAppName[cTINumberMax][cTINameMax], BYTE Num)
{
	int ret = 0;
	WORD venID = 0, devID = 0, revID = 0;
	char dataConfigArray[CONFIGNUM][CONFIGDATALENGTH];
	memset(dataConfigArray, 0, CONFIGNUM * CONFIGDATALENGTH);
	char aimTestLocation[30] = { '\0' };
	char aimTestErrorcode[30] = { '\0' };
	strcpy(headLog.T_DEVICE, "IdCheck");

	int iNum = 0;

#ifdef DEBUG
	printf("Num = %d, iIdChkCurrentNum = %d, iAllIdChkNum + 1 = %d\n", Num, iIdChkCurrentNum, iAllIdChkNum + 1);
#endif
	if (iIdChkCurrentNum == (iAllIdChkNum + 1))
	{
		strcpy(headLog.T_CAPTION, "All");

		for (iNum = 1; iNum < iIdChkCurrentNum; iNum++)
		{

			GetDataFromConfig(fileName, dataConfigAppName[iIdChkIndex[iNum]], configItemKeyIDchk, dataConfigArray);
			venID = AtoX(dataConfigArray[0]);
			memset(dataConfigArray[0], 0, strlen((char*)dataConfigArray[0]));

			devID = AtoX(dataConfigArray[1]);
			memset(dataConfigArray[1], 0, strlen((char*)dataConfigArray[1]));

			revID = AtoX(dataConfigArray[2]);
			memset(dataConfigArray[2], 0, strlen((char*)dataConfigArray[2]));

			strcpy(aimTestLocation, dataConfigArray[3]);
			memset(dataConfigArray[3], 0, strlen((char*)dataConfigArray[3]));

			strcpy(aimTestErrorcode, dataConfigArray[4]);
			memset(dataConfigArray[4], 0, strlen((char*)dataConfigArray[4]));


#ifdef DEBUG
			printf("**iFanNum = %d, venID = %X, devID = %X, revID = %X\n", iNum, venID, devID, revID);
			printf("**aimTestLocation = %s\n", aimTestLocation);
			printf("**aimTestErrorcode = %s\n", aimTestErrorcode);
#endif
			ret += IDCheck(cIdChkName[iIdChkIndex[iNum]], venID, devID, revID, aimTestLocation, aimTestErrorcode);

			memset(aimTestLocation, 0, strlen(aimTestLocation));
			memset(aimTestErrorcode, 0, strlen(aimTestErrorcode));

		}
	}
	else
	{
		GetDataFromConfig(fileName, dataConfigAppName[iIdChkIndex[Num]], configItemKeyIDchk, dataConfigArray);
		venID = AtoX(dataConfigArray[0]);
		memset(dataConfigArray[0], 0, strlen((char*)dataConfigArray[0]));

		devID = AtoX(dataConfigArray[1]);
		memset(dataConfigArray[1], 0, strlen((char*)dataConfigArray[1]));

		revID = AtoX(dataConfigArray[2]);
		memset(dataConfigArray[2], 0, strlen((char*)dataConfigArray[2]));

		strcpy(aimTestLocation, dataConfigArray[3]);
		memset(dataConfigArray[3], 0, strlen((char*)dataConfigArray[3]));

		strcpy(aimTestErrorcode, dataConfigArray[4]);
		memset(dataConfigArray[4], 0, strlen((char*)dataConfigArray[4]));

#ifdef DEBUG
		printf("*venID = %X, devID = %X, revID = %X\n", venID, devID, revID);
		printf("**aimTestLocation = %s\n", aimTestLocation);
		printf("**aimTestErrorcode = %s\n", aimTestErrorcode);
#endif
		strcpy(headLog.T_CAPTION, cIdChkName[Num]);
		strcpy(headLog.T_LOCATION, aimTestLocation);

		ret = IDCheck(cIdChkName[Num], venID, devID, revID, aimTestLocation, aimTestErrorcode);
		memset(aimTestLocation, 0, strlen(aimTestLocation));
		memset(aimTestErrorcode, 0, strlen(aimTestErrorcode));

	}

	tEndTime = getEndTime(endTime);
	time(&tEndTime);
	strcpy(headLog.T_ENDTIME, endTime);
	memset(durTime, 0, LENGTHA);
	sprintf(durTime, "%d", int(difftime(tEndTime, tStartTime)));
	//getDURTime(tEndTime, tStartTime, durTime);
	strcpy(headLog.T_DURATION, durTime);

	if (ret == 0)
	{
		strcpy(headLog.T_STATUS, "PASS");
	}
	else
	{
		strcpy(headLog.T_STATUS, "FAIL");
	}

	HeadLogRecord(headLog);
	for (int i = 0; i < iItemLogNum; i++)
	{
		ItemLogRecord(itemLog[i]);
	}
	WriteLogRecord(lpLogFileName);
	return ret;
}

DWORD getBusDevFun(DWORD code)
{
	int iRet = 0;
	BYTE offset = 0;
	DWORD dwAddr = 0, dwData = 0, ClassCode = 0;
	for (BYTE i = 0; i < 255; i++)
	{
		BYTE bus = i;
		for (BYTE j = 0; j < 32; j++)
		{
			BYTE dev = j;
			for (BYTE k = 0; k < 8; k++)
			{
				BYTE func = k;
				//check the invalid vendor ID
				offset = 0x00;
				dwAddr = MAKE_CONFIG_ADDRESS(bus, dev, func, offset);
				FoxLib_SetPortVal(PCI_CONFIG_ADDRESS, dwAddr, 4);
				FoxLib_GetPortVal(PCI_CONFIG_DATA, &dwData, 4);
				//	dwData = 0xffffffff;
				DWORD IDCode = dwData;

				if (code == IDCode)
				{
					return (bus << 16) + (dev << 8) + func;
				}
			}
		}
	}
	return 0;
}

//保留idcheck内容,但是这里不使用
int IDCheck(char para[], WORD inVenID, WORD inDevID, WORD inRevID, char subLocation[], char errorCode[])
{
	int result = 1;
	WORD venID = 0, devID = 0, revID = 0;
	WORD devIdTemp1 = 0, devIdTemp2 = 0, devIdTemp3 = 0;
	WORD devIdTemp = 0;
	WORD devIDH = 0, devIDL = 0;
	int bus = 0, dev = 0, func = 0;
	DWORD dwAddr = 0, dwData = 0, classCode = 0;
	BYTE indexPort = 0x2e, dataPort = 0x2f;
	//	BYTE byteData;
	BYTE offset = 0;
	BOOL flag = TRUE;
	BOOL bLanOK = TRUE;

	WORD ecAddr = 0, ecAddrH = 0, ecAddrL = 0;
	WORD ecData = 0;
	//for creat log .txt

	if (strcmpi(para, "sio") == 0)
	{
		//ITE test should add next 4 lines to enter PNP mode
		FoxLib_SetPortVal(indexPort, 0x87, 1);
		FoxLib_SetPortVal(indexPort, 0x01, 1);
		FoxLib_SetPortVal(indexPort, 0x55, 1);
		FoxLib_SetPortVal(indexPort, 0x55, 1);

		//get the vendor (NUVOTON)
		FoxLib_SetPortVal(0x2e, 0x20, 1);
		FoxLib_GetPortVal(0x2f, (PDWORD)&devIdTemp1, 1);
		FoxLib_SetPortVal(0x2e, 0x21, 1);
		FoxLib_GetPortVal(0x2f, (PDWORD)&devIdTemp2, 1);
		FoxLib_SetPortVal(0x2e, 0x22, 1);
		FoxLib_GetPortVal(0x2f, (PDWORD)&devIdTemp3, 1);
		venID = 0;
		devID = (devIdTemp1 << 8) + devIdTemp2;
		revID = devIdTemp3;

		strcpy(itemLog[iItemLogNum].TM_SUBLOCATION, "DeviceID");
		strcpy(itemLog[iItemLogNum].TM_TESTITEM, para);
		sprintf(itemLog[iItemLogNum].TM_EXPVALUE1, "%04X", inDevID);
		sprintf(itemLog[iItemLogNum].TM_ACTVALUE1, "%04X", devID);

		if (inDevID != devID)
		{
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "expValue not equal actValue_");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "fail");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
		}
		else {
			strcpy(itemLog[iItemLogNum].TM_STATUS, "PASS");
		}

		iItemLogNum++;

		strcpy(itemLog[iItemLogNum].TM_SUBLOCATION, "RevisionID");
		strcpy(itemLog[iItemLogNum].TM_TESTITEM, para);
		sprintf(itemLog[iItemLogNum].TM_EXPVALUE1, "%04X", inRevID);
		sprintf(itemLog[iItemLogNum].TM_ACTVALUE1, "%04X", revID);
		if (inRevID != revID)
		{
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "expValue not equal actValue_");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "fail");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
		}
		else {
			strcpy(itemLog[iItemLogNum].TM_STATUS, "PASS");
		}
		iItemLogNum++;

	}
	else
	{
		if (strcmpi(para, "pch") == 0)
		{
#ifdef DEBUG
			printf("\n*********\n");
#endif
			// bus 00 dev 1f func 00
			/*int BusDevFun = getBusDevFun(0X06848086);
			bus = (BusDevFun >> 16)&0xff;
			dev = (BusDevFun >> 8)&0xff;
			func = BusDevFun & 0Xff;*/

			bus = 0x00;
			dev = 0x1F;
			func = 0x00;

		}
		else if (strcmpi(para, "audio") == 0)
		{
			// bus 00 dev 1f func 03
			/*int BusDevFun = getBusDevFun(0X06C88086);
			bus = (BusDevFun >> 16) & 0xff;
			dev = (BusDevFun >> 8) & 0xff;
			func = BusDevFun & 0Xff;*/

			bus = 0x00;
			dev = 0x1F;
			func = 0x03;
		}
		else if (strcmpi(para, "lan") == 0)
		{
			int BusDevFun = getBusDevFun(0X816810EC);
			bus = (BusDevFun >> 16) & 0xff;
			dev = (BusDevFun >> 8) & 0xff;
			func = BusDevFun & 0Xff;
		}

		if (bLanOK == TRUE)
		{
			dwAddr = MAKE_CONFIG_ADDRESS(bus, dev, func, 0x00);
			FoxLib_SetPortVal(PCI_CONFIG_ADDRESS, dwAddr, 4);
			FoxLib_GetPortVal(PCI_CONFIG_DATA, &dwData, 4);
			venID = (WORD)dwData;
			devID = (WORD)(dwData >> 16);

			dwAddr = MAKE_CONFIG_ADDRESS(bus, dev, func, 0x08);
			FoxLib_SetPortVal(PCI_CONFIG_ADDRESS, dwAddr, 4);
			FoxLib_GetPortVal(PCI_CONFIG_DATA, &dwData, 4);
			revID = (BYTE)dwData;
		}

		strcpy(itemLog[iItemLogNum].TM_SUBLOCATION, "VendorID");
		strcpy(itemLog[iItemLogNum].TM_TESTITEM, para);
		sprintf(itemLog[iItemLogNum].TM_EXPVALUE1, "%04X", inVenID);
		sprintf(itemLog[iItemLogNum].TM_ACTVALUE1, "%04X", venID);
		if (inVenID != venID)
		{
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "expValue not equal actValue_");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "fail");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
		}
		else {
			strcpy(itemLog[iItemLogNum].TM_STATUS, "PASS");
		}
		iItemLogNum++;

		strcpy(itemLog[iItemLogNum].TM_SUBLOCATION, "DeviceID");
		strcpy(itemLog[iItemLogNum].TM_TESTITEM, para);
		sprintf(itemLog[iItemLogNum].TM_EXPVALUE1, "%04X", inDevID);
		sprintf(itemLog[iItemLogNum].TM_ACTVALUE1, "%04X", devID);
		if (inDevID != devID)
		{
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "expValue not equal actValue_");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "fail");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
		}
		else {
			strcpy(itemLog[iItemLogNum].TM_STATUS, "PASS");
		}
		iItemLogNum++;

		strcpy(itemLog[iItemLogNum].TM_SUBLOCATION, "RevisionID");
		strcpy(itemLog[iItemLogNum].TM_TESTITEM, para);
		sprintf(itemLog[iItemLogNum].TM_EXPVALUE1, "%04X", inRevID);
		sprintf(itemLog[iItemLogNum].TM_ACTVALUE1, "%04X", revID);
		if (inRevID != revID)
		{
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "expValue not equal actValue_");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "fail");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
		}
		else {
			strcpy(itemLog[iItemLogNum].TM_STATUS, "PASS");
		}
		iItemLogNum++;

	}

	if (venID == inVenID && devID == inDevID && revID == inRevID)
	{
		printf("aimvenid: %04X\t aimdevid: %04X\t aimrevid: %04X\n", inVenID, inDevID, inRevID);
		printf("actvenid: %04X\t actdevid: %04X\t actrevid: %04X\n", venID, devID, revID);
		printf("%s ID test is: ", para);
		TextColor(GREEN);
		printf("passed!\n");
		TextColor(WHITE);
		result = 0;
	}
	else
	{
		printf("aimvenid: %04X\t aimdevid: %04X\t aimrevid: %04X\n", inVenID, inDevID, inRevID);
		printf("actvenid: %04X\t actdevid: %04X\t actrevid: %04X\n", venID, devID, revID);
		printf("%s ID test is: ", para);
		TextColor(RED);
		printf("failed!\n");
		TextColor(WHITE);
		result = 1;
	}

	return result;
}

int FwComMain(char* fileName, char dataConfigAppName[cTINumberMax][cTINameMax], BYTE Num)
{
	int nRetCode = 0;
	int iNum = 0;

	char dataConfigArray[CONFIGNUM][CONFIGDATALENGTH];
	memset(dataConfigArray, 0, CONFIGNUM * CONFIGDATALENGTH);
	char aimTestLocation[30] = { '\0' };
	char aimTestErrorcode[30] = { '\0' };
	strcpy(headLog.T_DEVICE, "Serial Port");


#ifdef DEBUG
	printf("Num = %d, iFCOMCurrentNum = %d, iAllFCOMNum + 1 = %d\n", Num, iFCOMCurrentNum, iAllFCOMNum + 1);
#endif
	if (iFCOMCurrentNum == (iAllFCOMNum + 1))
	{

		for (iNum = 1; iNum < iFCOMCurrentNum; iNum++)
		{

			GetDataFromConfig(fileName, cFCOMName[iFCOMIndex[iNum]], configItemKeyFCOM, dataConfigArray);

			strcpy(aimTestLocation, dataConfigArray[1]);
			memset(dataConfigArray[1], 0, strlen((char*)dataConfigArray[1]));

			strcpy(aimTestErrorcode, dataConfigArray[3]);
			memset(dataConfigArray[3], 0, strlen((char*)dataConfigArray[3]));

			Baudrate[0] = atoi(dataConfigArray[4]);
			memset(dataConfigArray[4], 0, strlen((char*)dataConfigArray[4]));

			Baudrate[1] = atoi(dataConfigArray[5]);
			memset(dataConfigArray[5], 0, strlen((char*)dataConfigArray[5]));

			Baudrate[2] = atoi(dataConfigArray[6]);
			memset(dataConfigArray[6], 0, strlen((char*)dataConfigArray[6]));

#ifdef DEBUG
			printf("**iFanNum = %d, Baudrate[0] = %d, Baudrate[1] = %d, Baudrate[2] = %d\n", iNum, Baudrate[0], Baudrate[1], Baudrate[2]);
			printf("**aimTestLocation = %s\n", aimTestLocation);
			printf("**aimTestErrorcode = %s\n", aimTestErrorcode);
#endif

			strcpy(headLog.T_CAPTION, aimTestLocation);
			strcpy(headLog.T_LOCATION, aimTestLocation);

			nRetCode += FwCom(cFCOMName[iFCOMIndex[iNum]], aimTestLocation, aimTestErrorcode, Baudrate);

			memset(aimTestLocation, 0, strlen(aimTestLocation));
			memset(aimTestErrorcode, 0, strlen(aimTestErrorcode));

		}
	}
	else
	{

		GetDataFromConfig(fileName, cFCOMName[Num], configItemKeyFCOM, dataConfigArray);

		strcpy(aimTestLocation, dataConfigArray[1]);
		memset(dataConfigArray[1], 0, strlen((char*)dataConfigArray[1]));

		strcpy(aimTestErrorcode, dataConfigArray[3]);
		memset(dataConfigArray[3], 0, strlen((char*)dataConfigArray[3]));

		Baudrate[0] = atoi(dataConfigArray[4]);
		memset(dataConfigArray[4], 0, strlen((char*)dataConfigArray[4]));

		Baudrate[1] = atoi(dataConfigArray[5]);
		memset(dataConfigArray[5], 0, strlen((char*)dataConfigArray[5]));

		Baudrate[2] = atoi(dataConfigArray[6]);
		memset(dataConfigArray[6], 0, strlen((char*)dataConfigArray[6]));

#ifdef DEBUG
		printf("*Baudrate[0] = %d, Baudrate[1] = %d, Baudrate[2] = %d\n", Baudrate[0], Baudrate[1], Baudrate[2]);
		printf("**aimTestLocation = %s\n", aimTestLocation);
		printf("**aimTestErrorcode = %s\n", aimTestErrorcode);
#endif

		strcpy(headLog.T_CAPTION, aimTestLocation);
		strcpy(headLog.T_LOCATION, aimTestLocation);

		nRetCode = FwCom(cFCOMName[Num], aimTestLocation, aimTestErrorcode, Baudrate);

		memset(aimTestLocation, 0, strlen(aimTestLocation));
		memset(aimTestErrorcode, 0, strlen(aimTestErrorcode));

	}

	tEndTime = getEndTime(endTime);
	time(&tEndTime);
	strcpy(headLog.T_ENDTIME, endTime);
	memset(durTime, 0, LENGTHA);
	sprintf(durTime, "%d", int(difftime(tEndTime, tStartTime)));
	strcpy(headLog.T_DURATION, durTime);

	if (nRetCode == 0)
	{
		strcpy(headLog.T_STATUS, "PASS");
	}
	else
	{
		strcpy(headLog.T_STATUS, "FAIL");
	}

	HeadLogRecord(headLog);
	for (int i = 0; i < iItemLogNum; i++)
	{
		ItemLogRecord(itemLog[i]);
	}
	WriteLogRecord(lpLogFileName);

	return nRetCode;
}

int FwCom(char para[], char subLocation[], char errorCode[], int baudrate[])
{
	int result = 1;
	//for creat log .txt
	strcpy(com, para);
	strcpy(itemLog[iItemLogNum].TM_TESTITEM, para);
	strcpy(itemLog[iItemLogNum].TM_SUBLOCATION, subLocation);

	for (int i = 0; i < 3; i++)
	{
		if (SerialSetting(Baudrate[i], FALSE, DTR_CONTROL_HANDSHAKE, RTS_CONTROL_TOGGLE, errorCode)) return 1;

		printf("%s initing.......\n", para);
		result = InitCOMOne();
		if (result != 0x0)
		{
			printf("COM initing ");
			TextColor(RED);
			printf("Failed!\n\n");
			TextColor(WHITE);
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "COM initing Fail!!!");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			iItemLogNum++;
			return result;
		}

		result = Send_COMOne(send_data);
		if (result != 0x0)
		{
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "Send_COM Fail!!!");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			iItemLogNum++;
			return result;
		}

		Sleep(0x100);

		result = Recv_COMOne(&resv_data); //接收Recv_COMOne()函数的返回值

		//接收失败,提示
		if (result != 0x0)
		{
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "Recv_COM Fail!!!");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			iItemLogNum++;
			return result;
		}

		if (resv_data == send_data)
		{
			printf("COM Send&Receive Test ");
			TextColor(GREEN);
			printf("Passed!\n\n");
			TextColor(WHITE);
			strcpy(itemLog[iItemLogNum].TM_STATUS, "PASS");
			iItemLogNum++;
			return result;
		}
		else
		{
			printf("COM One Send&Receive Test Fail ");
			TextColor(RED);
			printf("Failed!\n\n");
			TextColor(WHITE);
			strcpy(itemLog[iItemLogNum].TM_STATUS, "FAIL");
			strcat(itemLog[iItemLogNum].TM_FAILINFO, "COM Send&Receive Test Fail!!!");
			strcpy(itemLog[iItemLogNum].TM_ERRORCODE, errorCode);
			iItemLogNum++;
			return 0x01;
		}
	}
	return result;
}

main.cpp

主函数带代码
 #include "function.h"
#include "..\Common\ExConfig.h"

#ifndef _DEBUG
#define new DEBUG_NEW
#endif

void PromptInformation(char* name)
{
	int i = 0;
	printf("==========================================================================\n");
	printf("*********Diag COM Tool for %s %s*********\n", projectName, FCOM);
	printf("Usage:\n");
	printf("\t%s [number| all] [ConfigFile]\n", name);
	printf("Note:\n");
	printf("\tnumber :\n");
	for (i = 1; i <= iAllFCOMNum; i++)
	{
		if (i == 1)
		{
			printf("\t\t");
		}

		printf("%d:\"%s\" ", i, cFCOMName[iFCOMIndex[i]]);
		if (i == iAllFCOMNum)
		{
			printf("\n");
			printf("\t\tAll: For all the number test \n");

		}
	}
	printf("==========================================================================\n");

}

int main(int argc, char* argv[])
{
	int result1 = 2;
	int iArgvNum = 0;
	BYTE Num = 0;

	char* lpFileName = (char*)malloc(MAX_PATH);
	memset(lpFileName, 0, MAX_PATH);

	strlwr(argv[0]);
	GetGlobalData(argv[0]);

	memset((char*)lpLogFileName, 0, MAX_PATH);

	strcpy(headLog.T_NAME, argv[0]);
	memset(startTime, 0, LENGTHA);
	tStartTime = getNowTime(startTime);
	time(&tStartTime);
	strcpy(headLog.T_STARTTIME, startTime);

	if (argc == 3)
	{
		strlwr(argv[2]);
		if (strstr(argv[2], ".ini") == NULL)
		{
			PromptInformation(argv[0]);
			return 1;
		}

		iArgvNum = 1;
		if (strcmpi(argv[iArgvNum], "all") == 0)
		{
#ifdef DEBUG
			printf("**argv[iArgvNum] = %s\n", argv[iArgvNum]);
#endif
			iFCOMCurrentNum = iAllFCOMNum + 1;
		}
		else
		{
#ifdef DEBUG
			printf("*argv[iArgvNum] = %s\n", argv[iArgvNum]);
#endif
			iFCOMCurrentNum = atoi(argv[iArgvNum]);
			Num = iFCOMIndex[iFCOMCurrentNum];
			if (iFCOMCurrentNum < 1 || iFCOMCurrentNum > iAllFCOMNum)
			{
				printf("Parameter number is out of range.\n");
				PromptInformation(argv[0]);
				return 1;
			}
		}

		iArgvNum = 2;
		if (LogName(lpLogFileName, argv[iArgvNum]))
		{
			return 1;
		}

		if ((argv[iArgvNum][0] == '.') || (argv[iArgvNum][1] == ':'))
		{
			sprintf((char*)lpFileName, "%s", argv[iArgvNum]);

		}
		else
		{
			sprintf((char*)lpFileName, ".\\%s", argv[iArgvNum]);

		}

		result1 = FwComMain(lpFileName, cFCOMName, Num);

	}
	else
	{
		PromptInformation(argv[0]);
		return 1;
	}

	return result1;
}

 文章来源地址https://www.toymoban.com/news/detail-534518.html

到了这里,关于计算机COM口数据测试的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 计算机基本硬件的内部结构

    世界上第一台计算机 ENIAC 是使用手动接线来控制计算,十分麻烦。 冯·诺依曼提出“存储程序”的概念,是指将指令以二进制代码的形式事先输入计算机的主存储器(内存),然后按照其在存储器中的首地址执行程序的第一条指令,以后就按该程序的规定顺序执行其他指令,

    2024年02月14日
    浏览(40)
  • 计算机视觉基本概念大起底!

    计算机视觉是一门研究如何使机器“看”的科学,更进一步的说,就是指用摄影机和电脑代替人眼对目标进行识别、跟踪和测量等机器视觉,并进一步做图形处理,使电脑处理成为更适合人眼观察或传送给仪器检测的图像或其他形式的信息。 和计算机视觉相关的领域从顶向下

    2024年02月07日
    浏览(66)
  • 计算机系统概论基本知识

    目录 chapter0 chapter1  binary system chapter2 chapter3 chapter4 chapter5 Pseudo-Code (or Program Design Language) way to use fuction  Algorithm Efficiency Proof of correctness chapter6 chapter7 chapter8 Basic Data Structures  data structures Related Concepts Storing Arrays A representation of an algorithm is called a program. 算法的表示称为程序。

    2024年02月06日
    浏览(46)
  • [ARM汇编]计算机原理与数制基础—1.1.1计算机的基本原理

    计算机是一种能够根据指令集自动、高速处理数据的现代化设备。它的基本原理可以总结为:输入、存储、处理和输出数据。接下来,我们将详细介绍这些基本原理。 输入 计算机通过输入设备(如键盘、鼠标等)接收外部数据。用户可以通过这些设备输入指令或数据,计算

    2024年02月08日
    浏览(43)
  • 计算机基本知识扫盲(持续更)

    Q:内存一般指什么? A:内存通常指的是计算机中用于暂时存储数据的硬件设备,也被称为主存或随机存储器(RAM)。内存是计算机中最常用的存储设备之一,因为它可以快速读取和写入数据,以便计算机能够更快地执行任务。 内存通常被用于存储当前正在运行的程序和数据

    2024年02月03日
    浏览(46)
  • 【计算机视觉】相机基本知识(还在更新)

    面阵相机则主要采用的 连续的、面状扫描光线 来实现产品的检测; 线阵相机即利用 单束扫描光 来进行物体扫描的工作的。 (1)面阵CCD工业相机: 优点 :应用面较广,如面积、形状、尺寸、位置,甚至温度等的测量。面阵CCD的优点是可以获取测量图像直观,二维图像信息

    2024年02月12日
    浏览(63)
  • 计算机二级Python基本操作题-序号45

    示例格式如下: 苹果 芒果 草莓 芒果 苹果 草莓 芒果 香蕉 芒果 草莓 统计各类型的数量,从数量多到少的顺序输出类型及对应数量,以英文冒号分隔,每个类型一行。输出结果保存在考生文件夹下,命名为“PY202.txt”。输出参考格式如下: 芒果:4 草莓:3 苹果:2 香蕉:1 示例格

    2024年02月14日
    浏览(49)
  • 计算机二级Python基本排序题-序号45(补充)

    A1-[‘12’,‘05’,‘07’,‘04’] A4-[‘23’,‘03’,‘11’] A3 -[‘12’,‘01’] A2-[‘07’] 输入: Bob 输出: Bob 234567891 1926 输入: bob 输出: 对不起,您输入的用户信息不存在。 pdict[name]返回的是键对应的值,即列表[‘xxx’] pdict[name][0]返回的是列表中的第一个元素,此时返回的是字符

    2024年01月21日
    浏览(56)
  • 【计算机二级考试C语言】C基本语法

    我们已经看过 C 程序的基本结构,这将有助于我们理解 C 语言的其他基本的构建块。 C 程序由各种令牌组成,令牌可以是、标识符、常量、字符串值,或者是一个符号。例如,下面的 C 语句包括五个令牌: 这五个令牌分别是: 在 C 程序中,分号是语句结束符。也就是

    2024年01月18日
    浏览(51)
  • 【计算机图形学基础教程】MFC基本绘图函数2

    CGdiObject类:GDI绘图工具的基类 CBitmap类:封装了GDI画刷,可以选作设备上下文的当前画刷,用于填充图形的内部 CFont类:封装了GDI字体,可以选作设备上下文的当前字体 CPalette类:封装了GDI调色板,提供应用程序和显示器之间的颜色接口 CPen类:封装了GDI画笔,可以选作设备

    2024年02月03日
    浏览(61)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包