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

📄 sportdlg.cpp

📁 CSerialPort类使用示例
💻 CPP
字号:
// SPortDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SPort.h"
#include "SPortDlg.h"

#include "SerialPort.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()

/////////////////////////////////////////////////////////////////////////////
// CSPortDlg dialog

CSPortDlg::CSPortDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSPortDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSPortDlg)
	m_strRate = _T("9600");
	m_strParity = _T("Space");
	m_strData = _T("8");
	m_strStop = _T("1");
	m_strCom = _T("COM1");
	m_strSendMsg = _T("CALL05!");
	m_strReceiveMsg = _T("");
	m_strRX = _T("0");
	m_strTX = _T("0");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bSerialPortOpened=false; //初始状态:串口没有打开
}

void CSPortDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSPortDlg)
	DDX_Control(pDX, IDC_CHECK_NEWLINE, m_CtrlNewLine);
	DDX_Control(pDX, IDC_CHECK_CHECK, m_CtrlCheck);
	DDX_Control(pDX, IDC_CHECK_ASCSHOW, m_CtrlASCShow);
	DDX_Control(pDX, IDC_CHECK_ASCSEND, m_CtrlASCSend);
	DDX_Control(pDX, IDC_COMBO_COM, m_CtrlCom);
	DDX_CBString(pDX, IDC_COMBO_RATE, m_strRate);
	DDX_CBString(pDX, IDC_COMBO_PARITY, m_strParity);
	DDX_CBString(pDX, IDC_COMBO_DATA, m_strData);
	DDX_CBString(pDX, IDC_COMBO_STOP, m_strStop);
	DDX_CBString(pDX, IDC_COMBO_COM, m_strCom);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSendMsg);
	DDX_Text(pDX, IDC_EDIT_RECV, m_strReceiveMsg);
	DDX_Text(pDX, IDC_STATIC_RECV, m_strRX);
	DDX_Text(pDX, IDC_STATIC_SEND, m_strTX);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSPortDlg, CDialog)
	//{{AFX_MSG_MAP(CSPortDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_MESSAGE(WM_COMM_RXCHAR, OnComm)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSPortDlg message handlers

BOOL CSPortDlg::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
	SetDlgItemText(IDC_EDIT_MSG,"串口关闭!");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSPortDlg::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 CSPortDlg::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 CSPortDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSPortDlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	m_strReceiveMsg = "";
	m_strRX = "0";
	m_strTX = "0";
	UpdateData(false);
}

void CSPortDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	UINT nPort = m_CtrlCom.GetCurSel() + 1;
	UINT nBaud = atoi(m_strRate);
	char parity[5];
	strcpy(parity,m_strParity);
	UINT nDataBits = atoi(m_strData);
	int nStopBits = atoi(m_strStop);
	if(m_bSerialPortOpened == false)
	{
		//***初始化串口**************************************************
		if(Sp.InitPort(this,nPort, nBaud,parity[0],nDataBits,nStopBits,EV_RXFLAG | EV_RXCHAR,512))
		{
			SetDlgItemText(IDC_EDIT_MSG,"串口打开成功!");
			m_bSerialPortOpened = true;
			SetDlgItemText(IDC_BUTTON_OPEN,"关闭串口");
			//***开始监控************************************************
			Sp.StartMonitoring();
		}
		else
		{
			SetDlgItemText(IDC_EDIT_MSG,"没有发现此串口或被占用!");
			m_bSerialPortOpened = false;
		}
	}
	else
	{
		Sp.ClosePort();
		SetDlgItemText(IDC_EDIT_MSG,"串口关闭成功!");
		m_bSerialPortOpened = false;
		SetDlgItemText(IDC_BUTTON_OPEN,"打开串口");
	}
}

void CSPortDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	//***检查串口是否打开********************************************
	if(!m_bSerialPortOpened)
	{
		MessageBox("串口没有打开!","提示");
		return;
	}
	UpdateData(true); //读入编辑框中的数据
	char SendMsg[512];
	
	if(m_CtrlASCSend.GetCheck())
		SendASC(m_strSendMsg);

	if(m_CtrlCheck.GetCheck())
		m_strSendMsg += GetCheck(m_strSendMsg);

	strcpy(SendMsg,m_strSendMsg);
	Sp.WriteToPort(SendMsg); //发送数据	

	int n = m_strSendMsg.GetLength();
	nTX = atoi(m_strTX);
	nTX +=n;
	m_strTX.Format("%d",nTX); 
	UpdateData(false);
}

LONG CSPortDlg::OnComm(WPARAM ch, LPARAM port)
{
	CString strTemp;
	strTemp.Format("%s",ch);

	if(m_CtrlASCShow.GetCheck())
		strTemp = RecvASC(strTemp);
	m_strReceiveMsg +=strTemp;
	int n = m_strReceiveMsg.GetLength();
	m_strRX.Format("%d",n); 
	if(m_CtrlNewLine.GetCheck())
		 m_strReceiveMsg+="\r\n";
	UpdateData(false);  //将接收到的字符显示在接收编辑框中
	return 0;
}

char CSPortDlg::GetCheck(CString strCmd)
{
	int check = strCmd.GetAt(0);
	int nLength = strCmd.GetLength();
	for(int i=1;i<nLength;i++)
	{
		check = check ^ strCmd.GetAt(i);
	}
	char OR = (char)check;
	return OR;
}

CString CSPortDlg::SendASC(CString strCmd)
{
	int nLength = strCmd.GetLength();
	CString str1;
	CString str2;
	CString SendTemp;
	CString strSend = "";
	for(int i=0; i<nLength; i++ )
	{
		if(strCmd.GetAt(i) !=' ')
		{
			str1 = strCmd.GetAt(i);
			if(i<(nLength-1))
			{
				if(strCmd.GetAt(i+1) != ' ')
				{
					str2 = strCmd.GetAt(i+1);
					SendTemp = str1+str2;
					char *p = (char*)(LPCTSTR)SendTemp;  
					char *str;  
					int SendASC = (int)strtol(p, &str, 16);
					SendTemp = (char)SendASC;
					strSend += SendTemp;
					i++;
				}
			}	
		}
	}
	strCmd = strSend;
	return strCmd; 
}

CString CSPortDlg::RecvASC(CString strRecv)
{
	CString strTemp;
	CString strRecvMsg;
	for(int i=0; i<strRecv.GetLength(); i++)
	{
		int RecvASC = strRecv.GetAt(i);
		strTemp.Format("%02X",RecvASC);
		strRecvMsg  += strTemp + " ";
	}
	strRecv = strRecvMsg;
	return strRecv;
}

⌨️ 快捷键说明

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