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

📄 bike_speeddlg.cpp

📁 vc开发的自行车里程速度表!串口检测脉冲信号
💻 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 + -