📄 comportdlg.cpp
字号:
switch(m_nStopBits)
{
case ONESTOPBIT:
comSetting.m_strStopBits = "1"; //ONESTOPBIT
break;
case ONE5STOPBITS:
comSetting.m_strStopBits = "1.5"; //ONE5STOPBITS
break;
case TWOSTOPBITS:
comSetting.m_strStopBits = "2"; //TWOSTOPBITS
break;
default:;
}
comSetting.m_nFlowCtrl = m_nFlowCtrl;
if(comSetting.DoModal()==IDOK)
{
m_strPort = comSetting.m_strPort;
m_nBaud = atoi(comSetting.m_strBaudRate);
m_nDataBits = atoi(comSetting.m_strDataBits);
if(comSetting.m_nParity == "NONE")
m_nParity = NOPARITY;
else if(comSetting.m_nParity == "EVEN")
m_nParity = EVENPARITY;
else
m_nParity = ODDPARITY;
str.Format("A stopbits = %s",comSetting.m_strStopBits);
if(comSetting.m_strStopBits == "1")
{
m_nStopBits = ONESTOPBIT;
str.Format("1 m_nStopBits = %d; ONESTOPBIT= %d",m_nStopBits, ONESTOPBIT);
//MessageBox(str);
}
else if(comSetting.m_strStopBits == "1.5")
{
m_nStopBits = ONE5STOPBITS;
MessageBox("1.5");
}
else
{
m_nStopBits = TWOSTOPBITS;
MessageBox("2");
}
str.Format("B stopbits = %s; m_nStopBits = %d",comSetting.m_strStopBits, m_nStopBits);
//MessageBox(str);
m_nFlowCtrl = comSetting.m_nFlowCtrl;
str.Format("1 m_strPort=%s, m_nBaud = %d, m_nDataBits= %d,\
m_nParity = %d, m_nStopBits = %d, m_nFlowCtrl = %d",
m_strPort, m_nBaud, m_nDataBits, m_nParity, m_nStopBits, m_nFlowCtrl);
MessageBox(str);
}
}
void timefunction()
{
//得到的是格林威治时间
CString str;
WORD currentyear;
WORD currentmonth;
WORD currentday;
WORD currenthour;
WORD currentminute;
WORD currentsecond;
WORD currentmilsecond;
WORD currentdayofweek;
SYSTEMTIME currenttime;
GetSystemTime(¤ttime);
currentyear = currenttime.wYear;
currentmonth = currenttime.wMonth;
currentday = currenttime.wDay;
currenthour = currenttime.wHour;
currentminute = currenttime.wMinute;
currentsecond = currenttime.wSecond;
currentmilsecond = currenttime.wMilliseconds;
currentdayofweek = currenttime.wDayOfWeek;
// str.Format("Today is %d-%d-%d",currentyear, currentmonth, currentday);
str.Format("Today is: %d-%d-%d\nNow is: %d :%d :%d ::%d\nWeek: %d", currentyear,currentmonth,
currentday, currenthour, currentminute, currentsecond, currentmilsecond, currentdayofweek);
AfxMessageBox(str);
}
void filepathfunction()
{
CString str1;
CFile cfile(_T("D:\\MyProject\\project\\ComPort\\1.log"), CFile::modeRead);
str1.Format("GetFilePath= %s; Name= %s; length=%d; Title= %s; Position=%s; \
",cfile.GetFilePath(),cfile.GetFileName(),\
cfile.GetLength(),cfile.GetFileTitle(), cfile.GetPosition());
AfxMessageBox(str1);
}
void getsystemvaliable()
{
//获得系统变量.
#define BUFSIZE 1024
CString str1;
LPTSTR lpszSystemInfo;
DWORD cchBuff = BUFSIZE; //DWORD = unsigned long
// char tchBuffer[BUFSIZE];
TCHAR tchBuffer[BUFSIZE]; // TCHAR = char
lpszSystemInfo = tchBuffer;
//Disaply computer name.
//GetComputerName(LPTSTR, LPDWORD) LPTSTR = char*
if(GetComputerName(lpszSystemInfo, &cchBuff))
{
str1.Format("Computer name: %s",lpszSystemInfo);
}
cchBuff = BUFSIZE;
//Get UserName
if(GetUserName(lpszSystemInfo, &cchBuff))
str1.Format("UserName= %s", lpszSystemInfo);
//Get System directory d:\windows\system32
if(GetSystemDirectory(lpszSystemInfo, MAX_PATH+1))
str1.Format("System directory: %s", lpszSystemInfo);
//Get windows directory
if(GetWindowsDirectory(lpszSystemInfo, MAX_PATH + 1))
str1.Format("windows directory: %s",lpszSystemInfo);
//Expand the PATH environment variable
DWORD dwResult;
dwResult = ExpandEnvironmentStrings("PATH=%PATH%",lpszSystemInfo, BUFSIZE);
if(dwResult <= BUFSIZE)
str1.Format("Environment variable= %s", lpszSystemInfo);
//Expand the OS environment variable
dwResult = ExpandEnvironmentStrings(
"OS=%OS%",
lpszSystemInfo,
BUFSIZE);
if(dwResult <= BUFSIZE)
str1.Format("os environment : %s",lpszSystemInfo);
dwResult = ExpandEnvironmentStrings(
"TEMP= %TEMP%",
lpszSystemInfo, BUFSIZE);
if(dwResult<= BUFSIZE)
str1.Format("TEMP path: %s",lpszSystemInfo);
}
void getcurrdirfunction()
{
CString str1;
DWORD dir;
DWORD buflen;
TCHAR tchBuffer1[BUFSIZE]; // TCHAR = char
buflen = BUFSIZE;
LPTSTR lpbuf;
lpbuf = tchBuffer1;
dir = GetCurrentDirectory(buflen,lpbuf);
str1.Format("Current directory: %s",lpbuf);
AfxMessageBox(str1);
}
void definetype()
{
typedef long aa, bb;
aa vaa = 300;
bb vbb = 600;
}
void cfilefunction()
{
CString filename;
CString mode;
CString writecstring;
filename = "testlog\\1.log";
mode = "a+";
writecstring = "ABCDEFG 1234567";
FILE *lpfile = fopen(filename,mode);
fwrite(writecstring,1,strlen(writecstring), lpfile);
fclose(lpfile);
}
void createdir()
{
if (CreateDirectory("testlog\\TXT\\",NULL))
AfxMessageBox("Create directory oK!");
else
AfxMessageBox("Create failed!");
}
void ctimefunction()
{
//获得当前系统时间
DWORD year;
DWORD month;
DWORD day;
DWORD hour;
DWORD minute;
DWORD second;
CString str;
CTime currtime = CTime::GetCurrentTime();
year = currtime.GetYear();
month = currtime.GetMonth();
day = currtime.GetDay();
hour = currtime.GetHour();
minute = currtime.GetMinute();
second = currtime.GetSecond();
str.Format("Now is %d-%d-%d %d:%d:%d",year,month,day,hour,minute,second);
AfxMessageBox(str);
//自己设置一个时间进行获得
CTime t( 1999, 3, 19, 22, 15, 0 ); // 10:15PM March 19, 1999
struct tm* osTime; // A pointer to a structure containing time elements.
osTime = t.GetLocalTm( NULL );
hour = osTime->tm_hour;
str.Format("hour: %d",hour);
AfxMessageBox(str);
}
void dwordtostr()
{
DWORD dwaa = 200;
CString strbb;
CString strhex;
strhex.Format("%d",dwaa);
AfxMessageBox(strhex);
}
void createdir1()
{
CString currdir;
DWORD dir;
DWORD buflen;
TCHAR tchBuffer1[BUFSIZE]; // TCHAR = char
LPTSTR lpbuf;
lpbuf = tchBuffer1;
buflen = BUFSIZE;
dir = GetCurrentDirectory(buflen,lpbuf);
currdir.Format("%s",lpbuf);
//获得当前的时间用于创建目录
DWORD year,month,day;
CTime currtime = CTime::GetCurrentTime();
year = currtime.GetYear();
month = currtime.GetMonth();
day = currtime.GetDay();
//创建目录
CString currpath;
currpath.Format("%s\\testlog\\%d-%d-%d\\",currdir,year,month,day);
AfxMessageBox(currpath);
CreateDirectory(currpath,NULL);
}
void stringanalysis()
{
CString str1;
CString str2;
str1 = "abcdefghijklmnopqrstuvwxyz";
if(str1.Find("pqrst",0)!=-1 )
AfxMessageBox("Find the string");
// re = str1.Find("pqrst",0);
// str2.Format("re = %d",re);
return;
}
void stringempty()
{
CString aa;
aa = "ksdaf";
aa ="";
if(aa.IsEmpty())
AfxMessageBox("empty");
}
void CComPortDlg::OnbtnShowLog()
{
// timefunction(); //学习时间的获得
// filepathfunction(); //分离文件路径及文件名
// getcurrdirfunction(); //获得当前目录
// definetype(); //类型定义别名
// cfilefunction(); //打开一个文件
// createdir(); //创建目录
// ctimefunction(); //获得本地时间
// dwordtostr();
// createdir1(); //创建时间创建目录
// stringanalysis();
// stringempty();
// m_bcalltestfindstring = TRUE; //call testfindstring function.
// m_bcallexpectstring = TRUE; //Call expectstring function
m_testthread->ResumeThread();
starttest = TRUE;
// MessageBox(str1);
//log.ShowLength();
}
BOOL CComPortDlg::recordlog(char buf[], int length)
{
int tempstrlen;
CString tempstr;
for(int i=0; i<length; i++)
{
switch(buf[i])
{
case '\n':
tempstr +="\n";
break;
case '\b':
break;
case '\a':
break;
default:
//MessageBox(tempstr);
tempstr += buf[i];
}
}
tempstrlen = strlen(tempstr);
CString tempstr1;
log.Record(tempstr);
return TRUE;
}
BOOL CComPortDlg::recordlog(CString str, BOOL lastrecord)
{
CString strtemp1;
m_strrecordlog_temp += str;
if(strlen(m_strrecordlog_temp)>2048)
{
log.Record(m_strrecordlog_temp);
m_strrecordlog_temp = "";
}
/*else
{
strtemp1.Format("m_strrecordlog_temp length= %d",strlen(m_strrecordlog_temp));
//MessageBox(strtemp1);
}*/
if (lastrecord == 1)
{
log.Record(m_strrecordlog_temp);
m_strrecordlog_temp = "";
}
return TRUE;
}
void CComPortDlg::Onbtnlastlog()
{
CString str = "";
recordlog(str, 1);
}
void CComPortDlg::Onbtnsendcommand()
{
char * commd;
commd = "dir\n";
m_bisonchar = FALSE;
SendCom(commd,strlen(commd));
}
BOOL CComPortDlg::getcomportbuffer(CString str)
{
m_strbuffer += str;
return TRUE;
}
void CComPortDlg::Onbtngetbuffer()
{
MessageBox(m_strbuffer);
}
UINT testread(LPVOID pParam)
{
CComPortDlg* pDlgtest = (CComPortDlg*)pParam;
while(TRUE)
{
pDlgtest->m_strtestbuffer= pDlgtest->m_strbuffer;
Sleep(500);
//Run test function.
if (pDlgtest->m_bcalltestfindstring)
{
pDlgtest->testfindstring("dir\n","2849792",4);
}
//Run exception function.
if(pDlgtest->m_bcallexpectstring)
{
pDlgtest->expectstring("2849792",4);
}
}
return 1;
}
BOOL CComPortDlg::testreadthread()
{
m_testreadthread = AfxBeginThread(
testread,
this,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED,
NULL);
if(m_testreadthread == NULL)
{
return FALSE;
}
else
{
m_testreadthread->ResumeThread();
}
return TRUE;
}
BOOL CComPortDlg::testfindstring(char* input, CString output, int itime)
{
if(!m_bsendonetime)
{
m_strtestbuffer = "";
m_strbuffer = "";
m_bisonchar = FALSE;
starttime = CTime::GetCurrentTime();
endtime = starttime;
waittime = itime;
m_testfindstring = output;
SendCom(input,strlen(input));
m_bsendonetime = TRUE;
}
if((endtime > (starttime+waittime)))
{
MessageBox("No find string!");
m_bcalltestfindstring = FALSE;
m_bsendonetime = FALSE;
}
else
{
if(m_strtestbuffer.Find(m_testfindstring,0) != -1)
{
MessageBox("find string!");
m_bcalltestfindstring = FALSE;
m_bsendonetime = FALSE;
}
}
endtime = CTime::GetCurrentTime();
return TRUE;
}
BOOL CComPortDlg::expectstring(CString expectstring, int itime)
{
if(!m_bexpectonetime)
{
m_bexpectonetime = TRUE;
starttime = CTime::GetCurrentTime();
endtime = starttime;
}
if(endtime>(starttime+itime))
{
m_bcallexpectstring = FALSE;
m_bexpectonetime = FALSE;
MessageBox("No expect string fail!");
}
else
{
if(m_strtestbuffer.Find(expectstring,0) != -1)
{
m_bcallexpectstring = FALSE;
m_bexpectonetime = FALSE;
MessageBox("Expect string Pass");
}
}
endtime = CTime::GetCurrentTime();
return TRUE;
}
UINT testfun(LPVOID pParam)
{
CComPortDlg* pDlgtest = (CComPortDlg*)pParam;
LPDWORD exitcode ;
DWORD dcode = 0;
// exitcode = &dcode;
if(pDlgtest->starttest)
{
pDlgtest->m_strtestbuffer;
// if(pDlgtest->testfindstr("dir\n","niagarahub",4))
// {
// if(pDlgtest->testfindstr("config t\n","niagarahub(config)#",4))
// pDlgtest->testfindstr("int f0/1\n","niagarahub(config-if)#",4);
// }
pDlgtest->testfindstr("dir\n","niagarahub",4);
pDlgtest->testfindstr("config t\n","niagarahub(config)#",4);
pDlgtest->testfindstr("int f0/1\n","niagarahub(config-if)#",4);
pDlgtest->testfindstr("exit\n","niagarahub(config)#",4);
pDlgtest->testfindstr("exit\n","niagarahub",4);
}
pDlgtest->thiscomclose();
// GetExitCodeThread(pDlgtest->pcomopen, exitcode);
// CString strcode;
// DWORD newcode;
// strcode.Format("exitcode = %d", newcode);
// AfxMessageBox(strcode);
return 1;
}
BOOL CComPortDlg::testthread()
{
m_testthread = AfxBeginThread(
testfun,
this,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED,
NULL);
if(m_testthread == NULL)
{
return FALSE;
}
/* else
{
m_testthread->ResumeThread();
}
*/
return TRUE;
}
BOOL CComPortDlg::testfindstr(char* input, CString output, int itime)
{
BOOL findstatus;
m_bisonchar = FALSE;
starttime = CTime::GetCurrentTime();
endtime = starttime;
waittime = itime;
m_testfindstring = output;
SendCom(input,strlen(input));
while(TRUE)
{
Sleep(500);
if((endtime > (starttime+waittime)))
{
// MessageBox("No find string! testfindstr");
findstatus = FALSE;
break;
}
else
{
if(m_strtestbuffer.Find(m_testfindstring,0) != -1)
{
// MessageBox("find string! testfindstr");
findstatus = TRUE;
break;
}
}
endtime = CTime::GetCurrentTime();
}
if(findstatus)
return TRUE;
else
return FALSE;
}
void CComPortDlg::thiscomclose()
{
if(!m_bConnected)
{
MessageBox("Com Port was closed!");
return ;
}
m_bConnected = FALSE;
CComPortDlg::ComClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -