当前位置:Gxlcms > mysql > evc小项目杂记(2)

evc小项目杂记(2)

时间:2021-07-01 10:21:17 帮助过:16人阅读

两个wince小程序,Gprs功分器测试,和开关机拨号测试,都已经顺利完工,现在有空回头学一下之前一知半解的细节问题。 1. 在cedit中打印log -- //在edittext中打印信息voidCSimTestDlg::ShowMessage(CStringstrMsg){ CEdit*pMsgInfo = (CEdit*)GetDlgItem(IDC


两个wince小程序,Gprs功分器测试,和开关机拨号测试,都已经顺利完工,现在有空回头学一下之前一知半解的细节问题。

1. 在cedit中打印log

--

//在edittext中打印信息
voidCSimTestDlg::ShowMessage(CStringstrMsg)
{
      CEdit*pMsgInfo = (CEdit*)GetDlgItem(IDC_EDIT_TIPS);
      pMsgInfo->SetSel(pMsgInfo->GetWindowTextLength(),pMsgInfo->GetWindowTextLength());     //光标移动到当前所显示文本的末尾
      pMsgInfo->ReplaceSel(strMsg);   //在光标末尾插入字符串
          pMsgInfo->ReplaceSel(_T("\r\n"));
}


2.写log文件

--

//自己写的,解决了中文乱码和末尾有小方框的问题,可能还有隐患没发现
//写log记录到\ResidentFlash\开关机测试log.txt
int CSimTestDlg::writelog(CString logStr){
 
      CFilemyFile;
      char*pData = new char[500];
      TCHAR*pStrLog = new TCHAR[500];
 
      CStringstr_time = _T("");
      CStringstr_result = _T("");
      CStringstrFile = _T("");
 
       SYSTEMTIMEsys;     
      GetLocalTime(&sys );
 
     
      str_time.Format(_T("%4d/%02d/%02d%02d:%02d:%02d"),sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond);          
      str_result= str_time + logStr;
     
      intfileLength = str_result.GetLength();                 
      wcscpy(pStrLog,(LPCTSTR)str_result);
      intcpylen =MyWideCharToMultiByte_test(pStrLog,pData,fileLength*2);//获得unicode转Ansi之后的长度,说明http://blog.csdn.net/sky1415/article/details/4137872
 
       strFile= _T("\\ResidentFlash\\开关机测试log.txt");
      myFile.Open(strFile,CFile::modeCreate | CFile::modeWrite |CFile::typeBinary|CFile::modeNoTruncate);//modeNoTruncate表示为续写方式
     
      myFile.SeekToEnd();
      myFile.Write(pData,cpylen);
      myFile.Flush();
      myFile.Close();
 
      delete[]pData;     
      delete[]pStrLog;
      return0;
}
intCGPRS_SIGNAL_TESTDlg::MyWideCharToMultiByte_test(WCHAR*wchars, CHAR* schars,int scharsLen)
{
      memset(schars,0, scharsLen);
      CStringm_snd = wchars;
      intlen = m_snd.GetLength();
      CStringtmpstr(m_snd); //复制要发送的字符串
      intmultibytelen = WideCharToMultiByte(//计算从Unicode转换到Ansi后需要的字节数
              CP_ACP,//根据ANSI codepage转换
              WC_COMPOSITECHECK|WC_DEFAULTCHAR, //转换出错用缺省字符代替
              (LPCWSTR)tmpstr.GetBuffer(len),//要转换的字符串地址
              len,//要转换的个数
              0,//转换后字符串放置的地址
              0,//最多转换字符的个数,为0表示返回转换Unicode后需要多少个字节
              0,//缺省的字符:"\0"
             0//缺省的设置
      );
      WideCharToMultiByte(//转换Unicode到Ansi
              CP_ACP,WC_COMPOSITECHECK |WC_DEFAULTCHAR, (LPCWSTR)tmpstr.GetBuffer(len), len,(char*)schars, //转换到缓冲区中
              scharsLen,//最多个字节
              0,0);
      returnmultibytelen;
}
 

3. 调用系统程序并打开窗口

-- CreateProcess:http://baike.baidu.com/link?url=aLC_mEQNRDbEOf_wfXOxeH1Q2Q-XpIr9mXQDZKnHLXmPq-1niVJhYH05eTtCEnnE

#define   FILE_PLAY_EXEC     _T("\\ResidentFlash\\player\\PLAYER.EXE")
#define   FILE_PLAY_DATA      _T("\\ResidentFlash\\PandaNormal.avi")
      PROCESS_INFORMATIONinfo;
      CreateProcess(FILE_PLAY_EXEC,FILE_PLAY_DATA, NULL, NULL, FALSE,CREATE_NEW_CONSOLE, NULL, NULL, NULL,&info);
 

4. 定时器设置

--

//.cpp注册定时器
BEGIN_MESSAGE_MAP(CGPRS_SIGNAL_TESTDlg,CDialog)
       //{{AFX_MSG_MAP(CGPRS_SIGNAL_TESTDlg)
      ON_BN_CLICKED(IDC_BUTTON_START,OnButtonStart)
      ON_BN_CLICKED(IDC_BUTTON_STOP,OnButtonStop)
      ON_WM_TIMER()//定时器设置
      ON_MESSAGE(WM_GPRSRECV,OnGPRSRecv)//接收消息
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
//.cpp实现OnTimer,对话框定时消失
voidCGPRS_SIGNAL_TESTDlg::OnTimer(UINT_PTRnIDEvent)
{
      //TODO: Add your control notification handler code here
 
      HWNDhWnd = ::FindWindow(NULL, _T("提示"));   //查找标题为B的窗口,返回窗口句柄
 
      if(hWnd)   //如果窗口句柄存在
       {
              ::SendMessage(hWnd,WM_CLOSE,NULL, NULL);//可行
              //DestroyWindow();   //关闭整个窗口,不行
              //keybd_event(13,0,0,0);//模拟回车键按下,有啪的一声
             //keybd_event(VK_RETURN,0,0,0); //模拟回车键按下,有啪的一声
      }
      KillTimer(nIDEvent);   //关闭定时器
      CDialog::OnTimer(nIDEvent);
}
//.h文件中声明
        afx_msg void OnTimer(UINT_PTRnIDEvent);//接收消息

5.设置控件背景色

--

//.h文件中,注册函数
afx_msg HBRUSH OnCtlColor(CDC* pDC,CWnd*pWnd, UINT nCtlColor);
 
//cpp中重载消息响应函数
BEGIN_MESSAGE_MAP(CLytNandTestDlg, CDialog)
//{{AFX_MSG_MAP(CLytNandTestDlg)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
 
//cpp文件中实现方法,根据id设置不同颜色
/************************************************************************/
/*               UI显示                                                 */
/************************************************************************/
HBRUSH CLytNandTestDlg::OnCtlColor(CDC*pDC,CWnd* pWnd, UINT nCtlColor)
{
      HBRUSHhbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
     
      //Are we painting theIDC_MYSTATIC control? We can use
      //CWnd::GetDlgCtrlID() to perform the most efficient test.
      if(pWnd->GetDlgCtrlID() == IDC_SHOW)
      {
            
             RETAILMSG(1,(_T("OnCtlColor\n")));
              //Set the text color to red.
              pDC->SetTextColor(RGB(255,255,255));
            
              m_brush=   ::CreateSolidBrush( RGB(0,0,255));
            
              //Set the background mode fortext to transparent
              //so background will show thru.
              pDC->SetBkMode(TRANSPARENT);
            
              //Return handle to our CBrushobject.
              hbr= m_brush;
      }
     
     
      returnhbr;
}


6.注册表读写

-- 调用示例

//修改注册表,自动开机
reg->WriteRegMultiString(HKEY_LOCAL_MACHINE,_T("init\\"),L"Launch80",_T("\\ResidentFlash\\OurAPP\\开关机测试v1.3.exe"));         
reg->DeleteRegValue(HKEY_LOCAL_MACHINE,_T("init\\"),L"Launch80");

源码

/***************************************************************************
$Id$
Copyright (C) Centerm Information Co.,Ltd.All rights reserved.
 
Model Name:
Reg.cpp
 
Description:
注册表测试
 
Environment:
WinCE5.0、6.0
 
History:
      

/***************************************************************************
$Id$
Copyright (C) Centerm Information Co.,Ltd.All rights reserved.
 
Model Name:
Reg.h
 
Description:
注册表测试
 
Environment:
WinCE5.0、6.0
 
History:
      


7. 将系统时间转化为CString, 查询文件是否存在,得到当前绝对路径

--

//系统时间转化为字符串
CString Systime2CString(SYSTEMTIME m_time){
 
   TCHARszDateTime[100] = {0};
 
  _stprintf(szDateTime, _T("%04d/%02d/%02d%02d:%02d:%02d"),m_time.wYear,m_time.wMonth,
              m_time.wDay,m_time.wHour,m_time.wMinute, m_time.wSecond);
  CString str = szDateTime;
      returnstr;
}
//文件是否存在
BOOL IsFileExist(LPTSTR lpFilePath) 
{  
      WIN32_FIND_DATAm_fd;
      if(FindFirstFile(lpFilePath ,&m_fd) ==INVALID_HANDLE_VALUE )
              returnFALSE;
      returnTRUE;
}
 
 
/*
//获得当前绝对路径
void GetCurPath(WCHAR* strFilePath)
{
    
   int i = 0;
   int sLen = 0;
   sLen = GetModuleFileName(NULL,strFilePath,260);
   for(i=sLen;i!=0;i--)
    {
      if(strFilePath[i]==WCHAR('\\'))
       {
          break;
       }
       strFilePath[i]= WCHAR('\0');
    }
}


8.关机

--

//关机挂起
void CSimTestDlg::halt(){
      inttestCnt  =mprofile->GetPrivateProfileInt(div,ID_testcnt, 0, iniFilePath);
      mprofile->WritePrivateProfileInt(div,ID_testcnt, testCnt+1,iniFilePath);
      Sleep(300);
      SetSystemPowerState(NULL,POWER_STATE_OFF, POWER_FORCE);
}

9.ras拨号

//获取ppp网络状态
BOOL CSimTestDlg::IsConnect()
{
  if(NULL != hRasConn)
   {
      RASCONNSTATUS rasConStatus;
      rasConStatus.dwSize = sizeof(RASCONNSTATUS);
      RasGetConnectStatus(hRasConn,&rasConStatus);
            
      if(RASCS_Connected == rasConStatus.rasconnstate)
      {
          return TRUE;
      }
   }
  return FALSE;
}
 
 
//移除已经拨上去的ras
void CSimTestDlg::RemoveConnect(){
     
      RASCONNentries[25];
      DWORDbufsize=0;
      DWORDnumEntries=0;
      bufsize=25*sizeof(RASCONN);
      entries[0].dwSize=sizeof(RASCONN);
      RASCONNSTATUSstatus;
      status.dwSize=sizeof(RASCONNSTATUS);
      if(RasEnumConnections(entries,&bufsize,&numEntries)==0)
       {
              for(DWORDi=0;ireDialCnt= 0;
      DWORDstate_wait_for_event;     //是否已经拨号失败即“断开连接”
      inttestCnt =mprofile->GetPrivateProfileInt(div, ID_testcnt, 0, iniFilePath);
 
      while(shouldEndTest==FALSE&& pDlg->reDialCnt < 10) {
 
             pDlg->m_test_state=STATE_TESTING_DAIL;//正在拨号的提示
 
              //获得当前系统时间
              dwLast= GetTickCount();
              pDlg->RemoveConnect();          
     
             while(pDlg->IsConnect()&&shouldEndTest==FALSE)
              {
                    if(pDlg->hRasConn!=NULL)
                     {
                           RETAILMSG(1,(_T("退出1\r\n")));
                           RasHangUp(pDlg->hRasConn);         
                     }          
                    Sleep(100);//100MS
                    dwCurrent=::GetTickCount();
                    if(dwCurrent-dwLast>=dwInterval)
                     { 
                           pDlg->SetDlgItemText(IDC_STATUS,_T("5s还没退出重启模块.....等待7S"));//ui
                           pDlg->m_gpio=CreateFile(_T("GPI1:"),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
 
                            if(pDlg->m_gpio!= INVALID_HANDLE_VALUE ) {
 
                                  DeviceIoControl(pDlg->m_gpio,IOCTL_CDMA_OFF,NULL,NULL,NULL,NULL,NULL,NULL);
                                  if(myDelay(2000)==FALSE)
                                   {
                                         DialFailReason= -1;
                                          gotoDialFail;
                                   }
                                  DeviceIoControl(pDlg->m_gpio,IOCTL_CDMA_ON,NULL,NULL,NULL,NULL,NULL,NULL);
                                  if(myDelay(7000)==FALSE)
                                   {
                                         DialFailReason= -1;
                                         gotoDialFail;
                                   }
                                  CloseHandle(pDlg->m_gpio);
                                   pDlg->m_gpio=INVALID_HANDLE_VALUE;
                                  pDlg->SetDlgItemText(IDC_STATUS,_T("重启成功....."));//ui
                            }
                   
                     }
 
              }
 
              if(shouldEndTest==TRUE)
              {
                     gotoDialSuccOrAllFailExit;
              }
 
 
              //开始拨号   
              DWORDret;
              waitcount=0;
              RASDIALPARAMSrasDialParams;
              memset(&rasDialParams, 0,sizeof( RASDIALPARAMS ) );
              rasDialParams.dwSize=sizeof(RASDIALPARAMS);
             _tcscpy(rasDialParams.szEntryName,_T("我的连接"));
             ret=RasDial(NULL,NULL,&rasDialParams,0xFFFFFFFF,pDlg->GetSafeHwnd(),&pDlg->hRasConn);
 
              if(ret!=0)
              {
                    pDlg->SetDlgItemText(IDC_STATUS,_T("本次拨号建立失败"));
                    pDlg->m_test_state=STATE_DAIL_FAIL;
                     DialFailReason= -2;
                     gotoDialFail;
              }   
 
      //等待ppp连接
              while(waitcount<=60&&!shouldEndTest){
          waitcount++;
          if (pDlg->IsConnect())
                     {   
                            //只要连接成功则认为PPP正常      
                            pDlg->m_test_state=STATE_DAIL_SUCC;
                           gotoDialSuccOrAllFailExit;
                     }else{
                            //如果已经监测到ras处在断开状态,直接退出等待
                           state_wait_for_event= WaitForSingleObject(pDlg->hEvent_exitWaitPPP,1000);
                           if(state_wait_for_event==WAIT_OBJECT_0){
                                  DialFailReason= -5;
                                  gotoDialFail;                           
                            }
                     }
 
              }
 
              if(waitcount>=60){
                    pDlg->SetDlgItemText(IDC_STATUS,_T("等待ppp连接超时"));
                     DialFailReason= -3;
              }elseif(shouldEndTest==TRUE){
                     pDlg->SetDlgItemText(IDC_STATUS,_T("用户取消本次测试"));
                     DialFailReason= -4;
              }   
              gotoDialFail;
 
 
      //拨号失败,重试
      DialFail:
             pDlg->m_test_state=STATE_DAIL_FAIL;
              {
                     inttestCnt1  =mprofile->GetPrivateProfileInt(div,ID_dialFailCnt, 0, iniFilePath);
                    mprofile->WritePrivateProfileInt(div,ID_dialFailCnt, testCnt1+1,iniFilePath);
                   
                     switch(DialFailReason){
                     case-1:
                            log_whyfail=_T("软件退出\r\n");
                            break;
                     case-2:
                            log_whyfail=_T("RasDial失败\r\n");
                            break;
                     case-3:
                            log_whyfail=_T("等待ppp连接超时\r\n");
                            break;
                     case-4:
                            log_whyfail=_T("用户取消本次测试\r\n");
                     case-5:
                            log_whyfail= _T("连接已断开\r\n");
                            break;
                     }
                     if(pDlg->reDialCnt==0){
                           log.Format(_T("第[ %d ]次开机,拨号失败原因:")+log_whyfail,(testCnt));
                     }else{
                           log.Format(_T("重拨( %d ),拨号失败原因:")+log_whyfail,pDlg->reDialCnt);
                     }                 
                     pDlg->writelog(log);
                     pDlg->reDialCnt++;
              }
 
 
      }//while
 
//拨号成功,或者重试次数用完
DialSuccOrAllFailExit:
 
             pDlg->m_bt_start.EnableWindow(FALSE);
//           pDlg->m_bt_stop.EnableWindow(TRUE);
             pDlg->m_bt_exit.EnableWindow(FALSE);
            
              //拨号成功退出
              if(pDlg->m_test_state==STATE_DAIL_SUCC){
                     if(pDlg->reDialCnt==0){
                           log.Format(_T("第[ %d ]次开机,拨号成功\r\n"),testCnt);
                     }else{
                           log.Format(_T("第[ %d ]次开机,重拨次数[ %d]\r\n"), testCnt,pDlg->reDialCnt);
                     }
                     pDlg->writelog(log);
                     return0;
              }
              //用完重试次数退出
             elseif(pDlg->reDialCnt>=10){
                     log.Format(_T("第[ %d ]次开机,重拨次数[ %d ]\r\n"),testCnt,pDlg->reDialCnt);
                     pDlg->stopTest();
                     return-1;
              }
              //用户退出
              elseif(shouldEndTest==TRUE){
                     log=_T("用户主动退出测试!");
                     return-2;
              }                 
              //其他情况
              else
                     return-3;
}
 
LRESULTCSimTestDlg::OnRasDialEvent(WPARAMwp, LPARAM lp)
{
      RASCONNSTATErasState=(RASCONNSTATE)wp;
      switch(rasState)
       {
      caseRASCS_OpenPort:
             SetDlgItemText(IDC_STATUS,_T("打开端口......"));
              break;
      caseRASCS_PortOpened:
             SetDlgItemText(IDC_STATUS,_T("端口已打开."));
              break;
      caseRASCS_ConnectDevice:
             SetDlgItemText(IDC_STATUS,_T("连接设备......"));
              break;
      caseRASCS_DeviceConnected:
             SetDlgItemText(IDC_STATUS,_T("设备已连接."));
              break;
      caseRASCS_Authenticate:
              SetDlgItemText(IDC_STATUS,_T("验证用户及密码......"));
              break;
      caseRASCS_Authenticated:
             SetDlgItemText(IDC_STATUS,_T("通过."));
              break;
      caseRASCS_Connected:
             SetDlgItemText(IDC_STATUS,_T("已连接."));
 
              break;
      caseRASCS_Disconnected:
             SetDlgItemText(IDC_STATUS,_T("连接已断开."));
 
              //??
              //SetEvent(hEvent_exitWaitPPP);
              //hRasConn=NULL;
             //m_bt_exit.EnableWindow(TRUE);   
 
              break;
       default:
              returnTRUE;
      }
      returnTRUE;
}

人气教程排行