计算机COM口数据测试
计算机COM口数据测试
一、基本使用流程
程序需要以管理员身份运行,COM口回路测试需短接2,3pin,测试时候使用控制台,配置测试相关路径,并在测试完成后
1.测试配置路径D:\bigdata\INI\FWCOM.ini
2.测试完成后需要在路径D:\bigdata\LOG\生成测试FWCOM.log文件
程序运行结果示意图
运行完成后的日志文件示意图
二、软件设计
源码分为三个部分,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;
}
热门相关:洪荒二郎传 回眸医笑,冷王的神秘嫡妃 重生野性时代 锦乡里 新春的高潮