📄 testcommdlg.cpp
字号:
// TestCommDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestComm.h"
#include "TestCommDlg.h"
#include <math.h>
#include <stdlib.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}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)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestCommDlg dialog
CTestCommDlg::CTestCommDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestCommDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestCommDlg)
m_Recetive = _T("");
m_weidu = _T("");
m_jingdu = _T("");
m_djingdu1 = 0.0;
m_dweidu = 0.0;
m_jddegree = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestCommDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestCommDlg)
DDX_Control(pDX, IDC_MSCOMM1, m_Comm);
DDX_Text(pDX, IDC_EDIT_RECV, m_Recetive);
DDX_Text(pDX, IDC_WEIDU, m_weidu);
DDX_Text(pDX, IDC_JINGDU, m_jingdu);
DDX_Text(pDX, IDC_EDIT1_DOUBLE, m_djingdu1);
DDX_Text(pDX, IDC_EDIT2_WEIDU, m_dweidu);
DDX_Text(pDX, IDC_EDIT1_jd1, m_jddegree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestCommDlg, CDialog)
//{{AFX_MSG_MAP(CTestCommDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_END, OnButtonEnd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestCommDlg message handlers
BOOL CTestCommDlg::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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestCommDlg::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 CTestCommDlg::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 CTestCommDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestCommDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
InitComm();
// }
}
void CTestCommDlg::OnButtonEnd()
{
// TODO: Add your control notification handler code here
//if(!m_Comm.GetPortOpen())
m_Comm.SetPortOpen(FALSE);
}
void CTestCommDlg::InitComm()
{
//设置串口
m_Comm.SetCommPort(4);
//设置数据的读取格式为二进制格式
m_Comm.SetInputMode(1);
//设置传输参数
m_Comm.SetSettings("4800,N,8,1");
//缓冲区被有一个字符就可以接收
m_Comm.SetRThreshold(300);
// 指定缓冲区的大小
m_Comm.SetInBufferSize(1024);
// 指定缓冲区的大小
m_Comm.SetOutBufferSize(1024);
//清空缓冲区
m_Comm.SetInBufferCount(0);
//打开串口
if(!m_Comm.GetPortOpen())
m_Comm.SetPortOpen(TRUE);
}
BEGIN_EVENTSINK_MAP(CTestCommDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CTestCommDlg)
ON_EVENT(CTestCommDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CTestCommDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
int nEvent;
int chlength;
int position;
VARIANT m_input;
char *str,*str1,*porginal1,*porginal2,*porginal3,*porginal4,*porginal5,*porginal6;
int k,i;
CString str2;
//CString jingdu,weidu;
// CString receive,jingdu ,weidu;
//int length,length1;
nEvent=m_Comm.GetCommEvent();
switch(nEvent)
{
//只是对接:
case 2:
//接收缓冲区的字符数目
k=m_Comm.GetInBufferCount();
// m_Comm.SetOutputLen (3);
if(k>0)
{
m_input=m_Comm.GetInput();
m_Comm.SetInBufferCount(0);
str=(char*)(unsigned char*)m_input.parray->pvData;
}
i=0;
str1=str;
porginal2=str1;
while(i<k)
{
i++;
str1++;
}
*str1='\0';
str2=(const char*)str;
m_Recetive+=(LPCTSTR)str2;
// m_jingdu=m_Recetive.Mid(15,9);
// m_weidu=m_Recetive.Mid(28,10);
chlength=m_Recetive.GetLength ();
for(i=0; i<chlength;i++)
{
porginal1=porginal2;
porginal3=porginal1;
porginal3++;
porginal4=porginal3;
porginal3++;
porginal5=porginal3;
porginal3++;
porginal6=porginal3;
porginal3++;
porginal2++;
if(*porginal1=='G'&&*porginal4=='P'&&*porginal5=='G'&&*porginal6=='G'&&*porginal3=='A')
{
position=m_Recetive.Find ( *porginal3);
m_jingdu=m_Recetive.Mid(position+13,9);
m_weidu=m_Recetive.Mid(position+25,10);
//m_dweidu= atof((LPSTR)(LPCTSTR)m_jingdu.GetBuffer(m_jingdu.GetLength()));
//CString temp2;
//temp2.Format("%if",m_dweidu);
m_dweidu=atof(m_weidu); //可以了就用这种转换方式了
m_djingdu1=atof(m_jingdu);
//m_jddegree=m_jingdu.Mid(0,2);
//MessageBox(temp2);
break;
}
}
default:
break;
}
UpdateData(FALSE);
//m_Comm.SetPortOpen(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -