?? bike_speeddlg.cpp
字號:
// bike_speedDlg.cpp : implementation file
//
#include "stdafx.h"
#include "bike_speed.h"
#include "bike_speedDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CString str_test;
bool key_Pulse=true;//脈沖開關
bool key_Start=true;//脈沖開關
bool key_ChangeBike=true;//脈沖開關
int Num_Start=1;
int Num_ChangeBike=1;
int Npulse=0;//脈沖數
double Speed=0;//速度
int Npulse_preSecond=0;//每一秒前檢測到的脈沖數
int Npulse_Second=0;//每一秒檢測到的脈沖數
double pulse2speed=1;//每個脈沖對應的車輪速度
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CBike_speedDlg dialog
CBike_speedDlg::CBike_speedDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBike_speedDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBike_speedDlg)
// 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 CBike_speedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBike_speedDlg)
DDX_Control(pDX, IDC_MSCOMM1, m_mscomm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBike_speedDlg, CDialog)
//{{AFX_MSG_MAP(CBike_speedDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPENPORT, OnOpenport)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBike_speedDlg message handlers
BOOL CBike_speedDlg::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 CBike_speedDlg::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 CBike_speedDlg::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 CBike_speedDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
////////////////////////////////////////////////////////////////////shiying
void CBike_speedDlg::OnOpenport()
{
m_mscomm.SetCommPort(4); //串口4
m_mscomm.SetPortOpen(true);
SetTimer(1,1,NULL);//起用一毫秒的定時器
SetTimer(2,1000,NULL);//起用一秒的定時器
}
void CBike_speedDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch (nIDEvent)
{
case 1: ///處理ID為1的定時器
//檢測脈沖 CD為1腳 紅線
if((m_mscomm.GetCDHolding()==1)&(key_Pulse==true))
{
CString str_Npulse;
Npulse++;
str_Npulse.Format("%d",Npulse);//轉換數據格式:char to CString
UpdateData(FALSE);//更新編輯框內容
//顯示脈沖數
SetDlgItemText(IDC_NPULSE,str_Npulse);
key_Pulse=false;
}
else if ((m_mscomm.GetCDHolding()==0)&(key_Pulse==false))
{
key_Pulse=true;
}
//檢測啟停開關 DSR為6腳 白線
if((m_mscomm.GetDSRHolding()==1)&(key_Start==true))
{
key_Start=false;
Num_Start=Num_Start+1;
}
else if ((m_mscomm.GetDSRHolding()==0)&(key_Start==false))
{
key_Start=true;
}
if(Num_Start/2-(double(Num_Start)/2)==0)
{
//顯示
SetDlgItemText(IDC_START,"已啟動");
}else
{
//顯示
SetDlgItemText(IDC_START,"已停止");
}
//檢測選車開關 CTS為8腳 黃線
if((m_mscomm.GetCTSHolding()==1)&(key_ChangeBike==true))
{
key_ChangeBike=false;
Num_ChangeBike=Num_ChangeBike+1;
}
else if ((m_mscomm.GetCTSHolding()==0)&(key_ChangeBike==false))
{
key_ChangeBike=true;
}
if(Num_ChangeBike/2-(double(Num_ChangeBike)/2)==0)
{
//顯示
SetDlgItemText(IDC_CHANGEBIKE,"第二輛車");
}else
{
//顯示
SetDlgItemText(IDC_CHANGEBIKE,"第一輛車");
}
//黑線備用,5腳金屬線為GND
break;
case 2: ///處理ID為2的定時器
CString str_Speed;
//計算速度
Npulse_Second=Npulse-Npulse_preSecond;//計算每一秒檢測到的脈沖數
Speed=Npulse_Second*pulse2speed;//每一秒脈沖數換算速度
str_Speed.Format("%0.3f",Speed);//轉換數據格式:char to CString
UpdateData(FALSE);//更新編輯框內容
//顯示速度
SetDlgItemText(IDC_SPEEDDATA,str_Speed);
Npulse_preSecond=Npulse;//記錄當前脈沖數
break;
}
CDialog::OnTimer(nIDEvent);
}
////////////////////////////////////////////////////////////////////shiying
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -