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

📄 commdlg.cpp

📁 wince下面一个串口程序,用于读取并分GPS数据,主要可以提取经纬度和时间
💻 CPP
字号:
// commDlg.cpp : implementation file
//

#include "stdafx.h"
#include "comm.h"
#include "commDlg.h"
#include <Sipapi.h>

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

/////////////////////////////////////////////////////////////////////////////
// CCommDlg dialog

CCommDlg::CCommDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCommDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCommDlg)
		// 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 CCommDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCommDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCommDlg, CDialog)
	//{{AFX_MSG_MAP(CCommDlg)
	ON_CBN_SELCHANGE(IDC_PORT, OnSelchangePort)
	ON_CBN_SELCHANGE(IDC_BAND, OnSelchangeBand)
	ON_CBN_SELCHANGE(IDC_DATABITS, OnSelchangeDatabits)
	ON_CBN_SELCHANGE(IDC_STOPBITS, OnSelchangeStopbits)
	ON_CBN_SELCHANGE(IDC_PARITY, OnSelchangeParity)
	ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopen)
	ON_BN_CLICKED(IDC_CHKRCVHEX, OnChkrcvhex)
	ON_BN_CLICKED(IDC_CHKSNDHEX, OnChksndhex)
	ON_BN_CLICKED(IDC_AUTOSEND, OnAutosend)
	ON_EN_CHANGE(IDC_EDTFREQUENCY, OnChangeEdtfrequency)
	ON_EN_KILLFOCUS(IDC_EDTFREQUENCY, OnKillfocusEdtfrequency)
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
	ON_BN_CLICKED(IDC_CLSRCV, OnClsrcv)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_CLSSEND, OnClssend)
	ON_BN_CLICKED(IDC_MANSEND, OnMansend)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_WM_TIMER()
	ON_EN_SETFOCUS(IDC_EDTSEND, OnSetfocusEdtsend)
	ON_EN_KILLFOCUS(IDC_EDTSEND, OnKillfocusEdtsend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCommDlg message handlers

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

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
		// Init default Value in Main Window
	// Also Init struct "DlgAttr",which is used save the changed Value in Main Window 
	//port
	CComboBox* pComboBox=(CComboBox *)GetDlgItem(IDC_PORT);
	pComboBox->SetCurSel(0);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.PORT);
	//band
	pComboBox=(CComboBox *)GetDlgItem(IDC_BAND);
	pComboBox->SetCurSel(3);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.BAND);
	// Databits
	pComboBox = (CComboBox*)GetDlgItem(IDC_DATABITS);
	pComboBox->SetCurSel(2);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.DATABITS);
	// Stopbits
	pComboBox = (CComboBox*)GetDlgItem(IDC_STOPBITS);
	pComboBox->SetCurSel(0);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.STOPBITS);
	// Parity
	pComboBox = (CComboBox*)GetDlgItem(IDC_PARITY);
	pComboBox->SetCurSel(1);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.PARITY);
	//Show Hex
	CButton * pBtn=(CButton *)GetDlgItem(IDC_CHKRCVHEX);
	pBtn->SetCheck(0);
	DlgAttr.RCVHEX=pBtn->GetCheck();
	//send Hex
	pBtn=(CButton *)GetDlgItem(IDC_CHKSNDHEX);
	pBtn->SetCheck(0);
	DlgAttr.SENDHEX=pBtn->GetCheck();
	// Auto Send
	pBtn = (CButton*)GetDlgItem(IDC_AUTOSEND);	
	pBtn->SetCheck (0);
	DlgAttr.AUTOSEND = pBtn->GetCheck ();
	//auto sent frequrent
	CEdit *pEdt=(CEdit*)GetDlgItem(IDC_EDTFREQUENCY);
	pEdt->SetWindowText(_T("1000"));
	pEdt->GetWindowText(DlgAttr.FREQUENCY);
	//send bytes count
	pEdt=(CEdit*)GetDlgItem(IDC_SNDBYTES);
	pEdt->SetWindowText(_T("0"));
	//rec bytes count
	pEdt=(CEdit*)GetDlgItem(IDC_RCVBYTES);
	pEdt->SetWindowText(_T("0"));
	//statusbar
	CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATUSBAR);
	m_statusbar_str = "welcome to comm sample";
	pStatic->SetWindowText (m_statusbar_str);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

double CCommDlg::CStringToInt(CString str)
{
	char *m_str;
	int nSize = 2*str.GetLength(); 
	char *pAnsiString = new char[nSize+1]; 
	wcstombs(pAnsiString, str, nSize+1); 
	double x = strtod(pAnsiString,&m_str);
	delete [] pAnsiString;
	return x;
}



void CCommDlg::OnSelchangePort() 
{
	// TODO: Add your control notification handler code here
	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_PORT);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.PORT);
	
}

void CCommDlg::OnSelchangeBand() 
{
	// TODO: Add your control notification handler code here
	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_BAND);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.BAND);
	
}


void CCommDlg::OnSelchangeDatabits() 
{
	// TODO: Add your control notification handler code here
	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_DATABITS);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.DATABITS);
	
}

void CCommDlg::OnSelchangeStopbits() 
{
	// TODO: Add your control notification handler code here
	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_STOPBITS);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.STOPBITS);
	
}

void CCommDlg::OnSelchangeParity() 
{
	// TODO: Add your control notification handler code here
	CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_PARITY);
	pComboBox->GetLBText(pComboBox->GetCurSel(),DlgAttr.PARITY);
	
}

void CCommDlg::OnBtnopen() 
{
	// TODO: Add your control notification handler code here
	// Get Current Button Status
	CString btnstr;
	CButton* pBtnOpen = (CButton*)GetDlgItem(IDC_BTNOPEN);
	pBtnOpen->GetWindowText(btnstr);
	//m_statusbar_str = DlgAttr.PORT+" OPEND! BAND:" + DlgAttr.BAND + ",DATABITS:" + DlgAttr.DATABITS + ",STOPBITS:" + DlgAttr.STOPBITS + ",PARITY:" + DlgAttr.PARITY + ".";
	// Set Button Status
	if (btnstr=="Close Port")
	{
		// Close port Handle
		m_com1.ClosePort ();
		m_com1.StopReadThread ();

		// Set Status Bar Text.
		pBtnOpen->SetWindowText(_T("Open Port"));
		m_statusbar_str = DlgAttr.PORT+" CLOSED!";

	}
	else
	{
		
		// Open Port
		m_com1.InitializePort(this->m_hWnd,DlgAttr.PORT ,(UINT)CStringToInt(DlgAttr.BAND)
			,DlgAttr.PARITY ,(UINT)CStringToInt(DlgAttr.DATABITS)
			,(UINT)CStringToInt(DlgAttr.STOPBITS));
		m_com1.StartReadThread ();

		// Set Status Bar Text.
		pBtnOpen->SetWindowText (_T("Close Port"));
		m_statusbar_str = DlgAttr.PORT+" OPEND! BAND:" + DlgAttr.BAND + ",DATABITS:" 
			+ DlgAttr.DATABITS + ",STOPBITS:" + DlgAttr.STOPBITS + ",PARITY:" 
			+ DlgAttr.PARITY + ".";
	}

	// Get Current Status Bar Handle and Set to window
	CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATUSBAR);
	pStatic->SetWindowText (m_statusbar_str);
	
}

void CCommDlg::OnChkrcvhex() 
{
	// TODO: Add your control notification handler code here
	CButton* pBtnCHKRCVHEX = (CButton*)GetDlgItem(IDC_CHKRCVHEX);
	DlgAttr.RCVHEX = pBtnCHKRCVHEX->GetCheck ();
	
}

void CCommDlg::OnChksndhex() 
{
	// TODO: Add your control notification handler code here
	CButton* pBtnCHKRCVHEX = (CButton*)GetDlgItem(IDC_CHKSNDHEX);
	DlgAttr.SENDHEX = pBtnCHKRCVHEX->GetCheck ();
}

void CCommDlg::OnAutosend() 
{
	// TODO: Add your control notification handler code here
		CButton* pBtnAUTOSEND = (CButton*)GetDlgItem(IDC_AUTOSEND);
	if(pBtnAUTOSEND->GetCheck())
		SetTimer(1,(UINT)CStringToInt(DlgAttr.FREQUENCY),NULL);
	else
		KillTimer(1);
	
}

void CCommDlg::OnChangeEdtfrequency() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	CButton* pBtn = (CButton*)GetDlgItem(IDC_AUTOSEND);
	pBtn->SetCheck (0);
	KillTimer(1);
	
}

void CCommDlg::OnKillfocusEdtfrequency() 
{
	// TODO: Add your control notification handler code here
	CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTFREQUENCY);
	pEdt->GetWindowText (DlgAttr.FREQUENCY);

	
}

void CCommDlg::OnClsrcv() 
{
	// TODO: Add your control notification handler code here
	CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTRCV);
	pEdt->SetWindowText (_T(""));
	
}

void CCommDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	FILE *stream;
	int writecount;
	CString edtrcv;
	SYSTEMTIME st;
	// get current receive data.
	CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTRCV);
	pEdt->GetWindowText (edtrcv);
	GetLocalTime(&st);
	CString str_time;
	str_time.Format (_T("%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n"),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
	edtrcv = str_time + edtrcv;
	if((stream = fopen("receive.log","w+t"))!=NULL)
	{
		writecount = fwrite(UnicodeToAnsi(edtrcv),sizeof(char),strlen(UnicodeToAnsi(edtrcv)),stream);
		fclose(stream);
	}
	else
	{
		AfxMessageBox(L"Open File FAIL!");
	}
	free(stream);
}

void CCommDlg::OnClssend() 
{
	// TODO: Add your control notification handler code here
	CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTSEND);
	pEdt->SetWindowText (_T(""));
	
}

void CCommDlg::OnCommunication(WPARAM ch, LPARAM portnr)
{
	CString tempstr;
	CString temp;
	char hex_buf[4];				// hex convert buffer 
	unsigned char ch_buf;			// ansi convert buffer
	int i;							// bytes count in convert buffer
   // if(portnr==1)
	//{
	// get current display string for appending.
	CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTRCV);
	pEdt->GetWindowText (tempstr);

	// Ansi data
	ch_buf = (unsigned char)ch;

	// Hex data
	i = sprintf(hex_buf,"%.2X",ch_buf)/2;    //take i into account

// -------------display send bytes count.-----------------
	CString rcvcount;

	// get current counts in the form.
	CEdit* prcvEdt = (CEdit*)GetDlgItem(IDC_RCVBYTES);
	prcvEdt->GetWindowText (rcvcount);

// =-----------check if receive hex combox --------------=
	if(DlgAttr.RCVHEX == 0)	// send ANSI
	{
		tempstr=tempstr+CString(ch_buf);
		temp = UnicodeToAnsi(tempstr);
		pEdt->SetWindowText (tempstr);
	}
	else					// send HEX
	{
		tempstr=tempstr+hex_buf + " ";
		pEdt->SetWindowText (tempstr);
	}
	// send byte counts to dialog
	rcvcount.Format (_T("%d"),(int)CStringToInt(rcvcount)+i);
	prcvEdt->SetWindowText(rcvcount);
//	}
//	else
//	{
//  	AfxMessageBox(_T("Portnr=2"));
//	}

}


void CCommDlg::OnMansend() 
{
	// TODO: Add your control notification handler code here
	CString tempstr;
	CString sndstr;

	// Get string in IDC_EDTSEND.
	CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTSEND);
	pEdt->GetWindowText(tempstr);
	// display bytes count.
	pEdt = (CEdit*)GetDlgItem(IDC_SNDBYTES);
	pEdt->GetWindowText(sndstr);

	// convert unicode to ansi first.
	char *buf = UnicodeToAnsi(tempstr);        //***
	char *Sendbuf = new char[strlen(buf)];
	memset(Sendbuf,0,strlen(buf)/2);

	if(DlgAttr.SENDHEX == 0)				// Send ANSI data.
	{
		m_com1.WriteChar (buf);
		sndstr.Format (_T("%d"),(int)CStringToInt(sndstr)+strlen(buf));
	}
	else
	{
		if(IsHex(buf))						// Send HEX data.
		{
			ToHex(buf,Sendbuf);
		//	m_com1.WriteChar (Sendbuf,strlen(buf)/2);
			m_com1.WriteChar(Sendbuf);
			sndstr.Format (_T("%d"),(int)CStringToInt(sndstr)+strlen(buf)/2);
		
		}
	}
	pEdt->SetWindowText(sndstr);

	delete [] Sendbuf;
	free(buf);
	
}

void CCommDlg::OnReset() 
{
	// TODO: Add your control notification handler code here
		CEdit* pEdt = (CEdit*)GetDlgItem(IDC_SNDBYTES);
	pEdt->SetWindowText (_T("0"));
	pEdt = (CEdit*)GetDlgItem(IDC_RCVBYTES);
	pEdt->SetWindowText (_T("0"));
	
}

CCommDlg::~CCommDlg()
{
	m_statusbar_str = "";
	m_com1.ClosePort ();
	m_com1.StopReadThread ();
}

char * CCommDlg::UnicodeToAnsi(LPCTSTR lpString)
{
	// Calculate unicode string length.
	UINT len = wcslen(lpString)*2;
	char *buf = (char *)malloc(len);
	UINT i = wcstombs(buf,lpString,len);
	return buf;
}



void CCommDlg::GetANSI(LPCTSTR lpString,char *charstr)  
{
	int strlen = wcslen(lpString)*2;
	charstr = new char[strlen+1]; 
	wcstombs(charstr, lpString, strlen+1); 
}


void CCommDlg::ToHex(char *pSource, char *pDest)
{	
	int len = strlen(pSource);
	
	char hexchar;
	for(int i=0;i<len;i+=2)
	{
		// 十位数
		if( pSource[i] >= 0x30 && pSource[i]<= 0x39)
			hexchar = (pSource[i]- 0x30 ) * 16;
		else if(pSource[i] >= 0x41 && pSource[i] <= 0x46)
			hexchar = (pSource[i] - 0x37) * 16;
		else if(pSource[i] >= 0x61 && pSource[i] <= 0x66)
			hexchar = (pSource[i] - 0x57) * 16;

		// 个位数加十位数
		if( pSource[i+1] >= 0x30 && pSource[i+1]<= 0x39)
			hexchar = hexchar + (pSource[i+1]- 0x30 );
		else if(pSource[i+1] >= 0x41 && pSource[i+1] <= 0x46)
			hexchar = hexchar + (pSource[i+1] - 0x37);
		else if(pSource[i+1] >= 0x61 && pSource[i+1] <= 0x66)
			hexchar = hexchar + (pSource[i+1] - 0x57);

		pDest[i/2] = hexchar;
	}
}

BOOL CCommDlg::IsHex(char *pString)
{
	// must be ansi code here
	char HEX[] = "0123456789ABCDEFabcdef";		// support HEX Code
	int len = strlen(pString);
	char *result;

	for (int i=0;i<len;i++)
	{
		result = strchr(HEX,pString[i]);
		if(result==NULL)
		{
			AfxMessageBox(L"unsupport char in send buffer!");
			return FALSE;
		}
	}
	return TRUE;
}


void CCommDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	//OnMansend();
	//CDialog::OnTimer(nIDEvent);
	m_com1.GetGPSInfo();
	CEdit* pEdt = (CEdit*)GetDlgItem(IDC_EDTRCV);
	CString temp;
	pEdt->GetWindowText (temp);
	char buffer1[20];
	_itoa(m_com1.m_CurrentTime.GetYear(), buffer1, 10 );
	temp=temp+CString(buffer1)+".";
	_itoa(m_com1.m_CurrentTime.GetMonth(), buffer1, 10 );
	temp=temp+CString(buffer1)+".";
	_itoa(m_com1.m_CurrentTime.GetDay(), buffer1, 10 );
	temp=temp+CString(buffer1)+" ";
	_itoa(m_com1.m_CurrentTime.GetHour(), buffer1, 10 );
	temp=temp+CString(buffer1)+":";
	_itoa(m_com1.m_CurrentTime.GetMinute(), buffer1, 10 );
	temp=temp+CString(buffer1)+":";
	_itoa(m_com1.m_CurrentTime.GetSecond(), buffer1, 10 );
	temp=temp+CString(buffer1)+"   ";
	
	temp=temp+m_com1.m_latitude+"  "+m_com1.m_longitude+"       ";
	pEdt->SetWindowText(temp);

}

void CCommDlg::OnSetfocusEdtsend() 
{
	SipShowIM(SIPF_ON);	
	
}

void CCommDlg::OnKillfocusEdtsend() 
{
	SipShowIM(SIPF_OFF);	
	
}

⌨️ 快捷键说明

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