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

📄 carcontrollerdlg.cpp

📁 EVC开发的一串口软件件
💻 CPP
字号:
// CarControllerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CarController.h"
#include "CarControllerDlg.h"

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

static HANDLE hComport;//add

/////////////////////////////////////////////////////////////////////////////
// CCarControllerDlg dialog

CCarControllerDlg::CCarControllerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCarControllerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCarControllerDlg)
	m_edit = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCarControllerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCarControllerDlg)
	DDX_Text(pDX, IDC_EDIT, m_edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCarControllerDlg, CDialog)
	//{{AFX_MSG_MAP(CCarControllerDlg)
	ON_NOTIFY(TCN_SELCHANGE, IDC_Control, OnSelchangeControl)
	ON_BN_CLICKED(IDC_FORWORD, OnForword)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_BN_CLICKED(IDC_TURNLEFT, OnTurnleft)
	ON_BN_CLICKED(IDC_TURNRIGHT, OnTurnright)
	ON_BN_CLICKED(IDC_AUTORUN, OnAutorun)
	ON_BN_CLICKED(IDC_BACKWORD, OnBackword)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCarControllerDlg message handlers

BOOL CCarControllerDlg::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
	DCB dcb;
	COMMTIMEOUTS timeouts;	 

    hComport = CreateFile(L"COM1:", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

	if (hComport == INVALID_HANDLE_VALUE)
	{
		CString err;

		err.Format(L"COM open failed ! error = %d", GetLastError());
        MessageBox(err, L"Error", MB_ICONERROR);
	}
	else
	{
        GetCommState(hComport ,&dcb);
        
		dcb.BaudRate = 9600;
		dcb.ByteSize = 8;
		dcb.StopBits = ONESTOPBIT;
		dcb.fParity = FALSE;
		dcb.Parity = NOPARITY;
		dcb.fOutxCtsFlow = FALSE;
		dcb.fOutxDsrFlow = FALSE;
		dcb.fOutX = FALSE;
		dcb.fInX = FALSE;
		dcb.fDtrControl = DTR_CONTROL_ENABLE;
		dcb.fRtsControl = RTS_CONTROL_ENABLE;
		dcb.fDsrSensitivity = FALSE;
		dcb.fErrorChar = FALSE;
		dcb.fAbortOnError = FALSE;

		SetCommState(hComport, &dcb);

		timeouts.ReadIntervalTimeout = MAXWORD;
		timeouts.ReadTotalTimeoutMultiplier = 0;
		timeouts.ReadTotalTimeoutConstant = 0;
		timeouts.WriteTotalTimeoutMultiplier = 0;
		timeouts.WriteTotalTimeoutConstant = 0;
		SetCommTimeouts(hComport, &timeouts);
	}
    //full screen
	CRect m_FullScreenRect; //全屏区域
	CRect WindowRect; 
	GetWindowRect(&WindowRect); 
	CRect ClientRect;
	RepositionBars(0, 0xffff, AFX_IDW_PANE_FIRST, reposQuery, &ClientRect);
	ClientToScreen(&ClientRect); 
	int nFullWidth=GetSystemMetrics(SM_CXSCREEN);
	int nFullHeight=GetSystemMetrics(SM_CYSCREEN);
	m_FullScreenRect.left = WindowRect.left-ClientRect.left;
	m_FullScreenRect.top=WindowRect.top-ClientRect.top;
	m_FullScreenRect.right=WindowRect.right-ClientRect.right+nFullWidth;
	m_FullScreenRect.bottom=WindowRect.bottom-ClientRect.bottom+nFullHeight;
	this->SetWindowPos(&wndBottom,m_FullScreenRect.left,m_FullScreenRect.top,m_FullScreenRect.Width(),m_FullScreenRect.Height(),SWP_SHOWWINDOW);


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



void CCarControllerDlg::OnSelchangeControl(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CCarControllerDlg::OnForword() 
{
	// TODO: Add your control notification handler code here
	char tx;
	DWORD bytes_written;
	UpdateData(TRUE); 

	tx = '5';

	PurgeComm(hComport, PURGE_TXCLEAR|PURGE_RXCLEAR);
	WriteFile(hComport, &tx, 1, &bytes_written, 0);
    
	CString str;
	str.Format(L"Command: Forward");
	MessageBox(str);
    
	
}

void CCarControllerDlg::OnReset() 
{
	// TODO: Add your control notification handler code here
		char tx;
	DWORD bytes_written;

	UpdateData(TRUE); 

	tx = '8';

	PurgeComm(hComport, PURGE_TXCLEAR|PURGE_RXCLEAR);
	WriteFile(hComport, &tx, 1, &bytes_written, 0);
    
	CString str;
	str.Format(L"Command: Reset");
	MessageBox(str);
	
}

void CCarControllerDlg::OnTurnleft() 
{
	// TODO: Add your control notification handler code here
		char tx_buf[1];
	DWORD bytes_written;

	UpdateData(TRUE); 

	tx_buf[0] = '7';

	PurgeComm(hComport, PURGE_TXCLEAR|PURGE_RXCLEAR);
	WriteFile(hComport, tx_buf, 1, &bytes_written, 0);
    
	CString str;
	str.Format(L"Command: Turn left");
	MessageBox(str);
	
}

void CCarControllerDlg::OnTurnright() 
{
	// TODO: Add your control notification handler code here
		char tx_buf[1];
	DWORD bytes_written;

	UpdateData(TRUE); 

	tx_buf[0] = '9';

	PurgeComm(hComport, PURGE_TXCLEAR|PURGE_RXCLEAR);
	WriteFile(hComport, tx_buf, 1, &bytes_written, 0);
    
	CString str;
	str.Format(L"Command: Turn right");
	MessageBox(str);
	
}

void CCarControllerDlg::OnAutorun() 
{
	// TODO: Add your control notification handler code here
		char tx_buf[1];
	DWORD bytes_written;

	UpdateData(TRUE); 

	tx_buf[0] = 'a';

	PurgeComm(hComport, PURGE_TXCLEAR|PURGE_RXCLEAR);
	WriteFile(hComport, tx_buf, 1, &bytes_written, 0);
    
	CString str;
	str.Format(L"Command: Auto run");
	MessageBox(str);
	
}

void CCarControllerDlg::OnBackword() 
{
	// TODO: Add your control notification handler code here
		char tx_buf[1];
	DWORD bytes_written;

	UpdateData(TRUE); 

	tx_buf[0] = '0';

	PurgeComm(hComport, PURGE_TXCLEAR|PURGE_RXCLEAR);
	WriteFile(hComport, tx_buf, 1, &bytes_written, 0);
    
	CString str;
	str.Format(L"Command: Backward");
	MessageBox(str);
	
}

⌨️ 快捷键说明

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