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

📄 comdebugdlg.cpp

📁 串口通信程序,适用于手机的软件更新
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ComDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ComDebug.h"
#include "ComDebugDlg.h"
#include <math.h>
//#include "hasp.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

UCHAR yayaf[100] ={(char)0x3c,(char)0x69,(char)0x3c,(char)0x70,(char)0x00,(char)0x09,(char)0x11 //[0]-[6]
			,(char)0xE9,(char)0xE1							      //[7]-[8]
			,(char)0x1C,(char)0x64,(char)0x6F,(char)0x77,(char)0x6E,(char)0x6C,(char)0x6F //[9]-[15]
			,(char)0x61,(char)0x64,(char)0x20,(char)0x69,(char)0x64,(char)0x65,(char)0x6E //[16]-[22]
			,(char)0x74,(char)0x69,(char)0x66,(char)0x69,(char)0x63,(char)0x61,(char)0x74 //[23]-[29]
			,(char)0x69,(char)0x6F,(char)0x6E,(char)0x20,(char)0x61,(char)0x72,(char)0x65,(char)0x61}; //[30]-[37]
	////////////////////350 355 639 9A9C 855 859 UART1///////////////////////////

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CColorButton	m_btnOk;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDOK, m_btnOk);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComDebugDlg dialog

CComDebugDlg::CComDebugDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CComDebugDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CComDebugDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CComDebugDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CComDebugDlg)
	DDX_Control(pDX, IDC_CHECK2, m_che);
	DDX_Control(pDX, IDC_EDIT_Stase, m_Stase);
	DDX_Control(pDX, IDC_EDIT_IMEI, m_Imei);
	DDX_Control(pDX, IDC_COMBO_Type, m_Type);
	DDX_Control(pDX, IDC_BTN_SETCOM, m_btnSetCom);
	DDX_Control(pDX, IDC_BTN_SEND, m_btnSend);
	DDX_Control(pDX, IDC_BTN_SAVE, m_btnSave);
	DDX_Control(pDX, IDC_BTN_BROWSE, m_btnBrowse);
	DDX_Control(pDX, IDC_EDIT_SENDEDDATA, m_edtSendedData);
	DDX_Control(pDX, IDC_EDIT_RECEIVEDDATA, m_edtReceivedData);
	DDX_Control(pDX, IDC_EDIT_SENDDATA, m_edtSendData);
	DDX_Control(pDX, IDC_COMBO_STOPBITS, m_comboStopBits);
	DDX_Control(pDX, IDC_COMBO_PARITY, m_comboParity);
	DDX_Control(pDX, IDC_COMBO_BYTESIZE, m_comboByteSize);
	DDX_Control(pDX, IDC_COMBO_BAUDRATE, m_comboBaudRate);
	DDX_Control(pDX, IDC_COMBO_COMPORT, m_comboComPort);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CComDebugDlg, CDialog)
	//{{AFX_MSG_MAP(CComDebugDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
	ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
	ON_BN_CLICKED(IDC_BTN_SETCOM, OnBtnSetcom)
	ON_BN_CLICKED(IDC_CHK_16, OnChk16)
	ON_BN_CLICKED(IDC_BTN_SAVE, OnBtnSave)
	ON_BN_CLICKED(IDC_CHK_UNINTERRUPTED, OnChkUninterrupted)
	ON_WM_CLOSE()
	ON_EN_CHANGE(IDC_EDIT_SENDINTERVAL, OnChangeEditSendinterval)	
	//}}AFX_MSG_MAP
	ON_MESSAGE(DM_GETDEFID, OnGetDefID)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComDebugDlg message handlers

BOOL CComDebugDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	InitComboBox();
	InitPara();
	// TODO: Add extra initialization here
	///////////////////
	/////////////////////////////////////////HASP


//  m_Type.SetCurSel(0);
	
//	m_Type.AddString(" PHILIPS 350");
	m_Type.AddString(" PHILIPS 355");	
//	m_Type.AddString(" PHILIPS 530");
//	m_Type.AddString(" PHILIPS 535");
//	m_Type.AddString(" PHILIPS 639");
//	m_Type.AddString(" PHILIPS 855");
//	m_Type.AddString(" PHILIPS 760");
//	m_Type.AddString(" PHILIPS 859");

//	m_Type.AddString(" PHILIPS 655");
//	m_Type.AddString(" PHILIPS 162");
//	m_Type.AddString(" PHILIPS 9@98");
//	m_Type.AddString(" PHILIPS 9@9I");
//	m_Type.AddString(" PHILIPS 362");
//	m_Type.AddString(" PHILIPS 9@9S");
	m_Type.SetCurSel(0);
	m_Imei.SetLimitText(15);
//  MessageBox("请先正确选择型号!","PHILIPS",MB_OK);	
	return TRUE;  // return TRUE unless you set the focus to a control
}

void CComDebugDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CComDebugDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CComDebugDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CComDebugDlg::OnDestroy() 
{
	KillTimer(1);

	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
}

void CComDebugDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	int nLineIndex=0;
	int i,nLen;
	char ch;
//	unsigned char ch = 0;
	CString szText(""),szInfo("");
	CString szCh(""),sz16Text("");
	CString szTemp("");

	switch(nIDEvent)
	{
	case 1:
		if(!m_SerialPort.IsRecvBuffEmpty())
		{
			KillTimer(1);

			szText = "";

			while(!m_SerialPort.IsRecvBuffEmpty())
			{
				m_SerialPort.ReadCom(ch);

				if(ch == 0x00)
				{
					ch = '`';
				}

				if(szText.IsEmpty())
				{
					szText.Format(_T("%c"), ch);
				}
				else
				{
					szText.Insert(szText.GetLength(), ch);
				}
			}

			szTemp.Format(_T("%s"), m_szReceivedText);
			szInfo.Format(_T("%s"), szText);
			m_szReceivedText.Format(_T("%s%s"), szTemp, szInfo);

			if(IsDlgButtonChecked(IDC_CHK_16))
			{
//				m_edtReceivedData.GetWindowText(szText);
				nLen = m_szReceivedText.GetLength();
				
				for(i=0; i<nLen; i++)
				{
					szCh.Format(_T("0x%x,"), m_szReceivedText[i]);
					sz16Text += szCh;
				}
				
				m_edtReceivedData.SetWindowText(sz16Text);
			}
			else
			{
				m_edtReceivedData.SetWindowText(m_szReceivedText);
			}

			if(m_szReceivedText.GetLength() > (1024 * 10))
			{
				m_szReceivedText = "";
			}

			SetTimer(1, 10, NULL);
		}
		/*
		if(!m_SerialPort.IsRecvBuffEmpty())
		{
			m_SerialPort.ReadCom(&ch);
			szText.Format("%c ", ch);
			nLineIndex = m_edtReceivedData.GetWindowTextLength();
			m_edtReceivedData.SetSel(nLineIndex, nLineIndex + szText.GetLength());
			m_edtReceivedData.ReplaceSel(szText);
			XTRACE("%c,", ch);
		}
		*/

		break;
	default:
		break;
	}
	
	CDialog::OnTimer(nIDEvent);
}

LRESULT CComDebugDlg::OnGetDefID(WPARAM wParam, LPARAM lParam) 
{
	CString szClassName("");
	CWnd *pWnd = GetFocus();

	if(NULL != pWnd)
	{
		::GetClassName(pWnd->GetSafeHwnd(), szClassName.GetBuffer(80), 80);
		
		if(szClassName == "Edit") 
		{
			UINT nID = pWnd->GetDlgCtrlID();

			if(nID == IDC_EDIT_SENDDATA)
			{
				int nKeyState = GetKeyState(13);

				if(nKeyState <= -127) 
				{
					OnBtnSend();
				}
			}
		}

		szClassName.ReleaseBuffer();
	}
		
	return MAKELONG(0, DC_HASDEFID); 
} 

BOOL CComDebugDlg::GetCharFromHexString(CString szHex, char &ch, WORD &wValue, int &nType)
{
	CString szHexValue[] = {"0", "1", "2", "3",
							  "4", "5", "6", "7",
							  "8", "9", "A", "B",
							  "C", "D", "E", "F"
							};
	CString szTemp(""),szChar("");
	int i,nCount,nIndex;
	int nValue=0;
	BOOL bFind=FALSE;

	szHex.MakeUpper();
	szHex.TrimLeft();

	if(szHex.Left(2) != "0X")
	{
		return FALSE;
	}

	szTemp = szHex.Right(szHex.GetLength() - 2);
	nCount = szTemp.GetLength();

	for(i=0; i<nCount; i++)
	{
		szChar = szTemp.Mid(i, 1);
		bFind = FALSE;

		for(nIndex=0; nIndex<sizeof(szHexValue)/sizeof(CString); nIndex++)
		{
			if(szChar == szHexValue[nIndex])
			{
				bFind = TRUE;
				break;
			}
		}

		if(bFind)
		{
			nValue += nIndex * (int)pow(16, nCount - 1 - i);
		}
	}

	if(nValue > 255)
	{
		nType = 1;
		wValue = (WORD)nValue;
	}
	else
	{
		nType = 0;
		ch = (char)nValue;
	}

	return TRUE;
}

void CComDebugDlg::InitComboBox()
{
	int nIndex=0;
	CString szCom(""),szCreateCom("");
	HANDLE hCom=NULL;

	//初始化串口ComboBox
	for(int i=1; i<=256; i++)
	{
		szCreateCom.Format(_T("\\\\.\\COM%d"), i);
		szCom.Format(_T("COM%d"), i);
		hCom = CreateFile(szCreateCom, GENERIC_READ | GENERIC_WRITE, 0, NULL,	OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL); // 重叠方式

		if(hCom != INVALID_HANDLE_VALUE)
		{
			m_comboComPort.AddString(szCom);
			CloseHandle(hCom);
			hCom = NULL;
		}
	}

	if(m_comboComPort.GetCount() > 0)
	{
		m_comboComPort.SetCurSel(0);
	}

	//设置波特率
	nIndex = m_comboBaudRate.FindStringExact(-1, "115200");

	if(nIndex != CB_ERR)
	{
		m_comboBaudRate.SetCurSel(nIndex);
	}

	//设置数据位
	nIndex = m_comboByteSize.FindStringExact(-1, "8");

	if(nIndex != CB_ERR)
	{
		m_comboByteSize.SetCurSel(nIndex);
	}

	//设置停止位
	nIndex = m_comboStopBits.FindStringExact(-1, "1");

	if(nIndex != CB_ERR)
	{
		m_comboStopBits.SetCurSel(nIndex);
	}

	//设置校验方式
	nIndex = m_comboParity.FindStringExact(-1, "无检验");

	if(nIndex != CB_ERR)
	{
		m_comboParity.SetCurSel(nIndex);
	}
}

void CComDebugDlg::OnBtnBrowse() 
{
	// TODO: Add your control notification handler code here
   char szFilters[] = "发送内容 (*.txt)|*.txt||";
   CString szPathName("");

   CFileDialog fileDlg (TRUE, "txt", "Cmd.txt",
      OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
   
   if(fileDlg.DoModal() ==IDOK )
   {
      szPathName = fileDlg.GetPathName();
	  LoadCmdFile(szPathName);
   }
}

void CComDebugDlg::OnBtnSend() 
{
		m_SerialPort.m_File00.Close();
		m_SerialPort.CloseCom();
		UCHAR stase[30]="串口以关闭." ;
		m_Stase.SetWindowText((char*)stase);
	// TODO: Add your control notification handler code here
/*	int nLineIndex = 0;
	DWORD dwStart;
	CString szCmd(""),szText(""),szTemp("");
	MSG message;

	if(!m_bInitComOk)
	{
		return;
	}

	m_edtSendData.GetWindowText(szCmd);

	if(szCmd.IsEmpty())
	{
		return;
	}
	else if((szCmd == "cls") || (szCmd == "CLS"))
	{
		m_edtSendedData.SetSel(0, -1);
		m_edtSendedData.Clear();
		
		m_edtReceivedData.SetSel(0, -1);
		m_edtReceivedData.Clear();
		m_szReceivedText = "";
	}
	else if((szCmd == "clss") || (szCmd == "CLSS"))
	{
		m_edtSendedData.SetSel(0, -1);
		m_edtSendedData.Clear();
	}
	else if((szCmd == "clsr") || (szCmd == "CLSR"))
	{
		m_edtReceivedData.SetSel(0, -1);
		m_edtReceivedData.Clear();
		m_szReceivedText = "";
	}
	else
	{
		m_bUninterrupted = (BOOL)IsDlgButtonChecked(IDC_CHK_UNINTERRUPTED);
		szText = "";

		do
		{
			szText = szCmd + " ";
			nLineIndex = m_edtSendedData.GetWindowTextLength();
			m_edtSendedData.SetSel(nLineIndex, nLineIndex + szText.GetLength());
			m_edtSendedData.ReplaceSel(szText);
			
			if(szText.GetLength() > 1024)
			{
				szText = "";
				m_edtSendedData.SetWindowText("");
			}

			//循环语句中转让控制权
			if(::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
			{
				::TranslateMessage(&message);
				::DispatchMessage(&message);
			}

			SendData(szCmd);

			((CEdit *)GetDlgItem(IDC_EDIT_SENDINTERVAL))->GetWindowText(szTemp);
			m_dwSendInterval = ::atoi(szTemp);
			if(m_dwSendInterval < 0)
			{
				m_dwSendInterval = 0;
			}
			else if(m_dwSendInterval > 60000)
			{
				m_dwSendInterval = 60000;
			}

⌨️ 快捷键说明

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