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

📄 seraildlg.cpp

📁 这是一个VC++写的串口通信程序
💻 CPP
字号:
// SerailDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VecDraw.h"

#include "ConfigDlg.h"

#include "VecDrawDoc.h"

#include "SerailDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSerailDlg dialog


CSerailDlg::CSerailDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSerailDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSerailDlg)
	m_Screen = _T("");
	count=0;
	m_HexDsp = FALSE;
	//}}AFX_DATA_INIT
}


void CSerailDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerailDlg)
	DDX_Text(pDX, IDC_EDIT2, m_Screen);
	DDX_Check(pDX, IDC_CHECK1, m_HexDsp);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSerailDlg, CDialog)
	//{{AFX_MSG_MAP(CSerailDlg)
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
	ON_BN_CLICKED(IDC_SendToCom, OnSendToCom)
	ON_BN_CLICKED(IDC_bTest, OnbTest)
	ON_BN_CLICKED(IDC_bRecive, OnbRecive)
	ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CSerailDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	
	m_ListBox.SubclassDlgItem(IDC_LIST1, this);
//	m_ListBox2.SubclassDlgItem(IDC_LIST2, this);

	m_Edit.SubclassDlgItem(IDC_EDIT1, this);

	// init the ports
		
		if (m_Ports[0].InitPort(this,1,9600,'N'))
			m_Ports[0].StartMonitoring();
		else
		{
			// port not found
			m_Edit.SetWindowText("NOT FOUND");
			m_Edit.EnableWindow(FALSE);
//			m_ListBox.EnableWindow(FALSE);
		}			
	return TRUE;  // return TRUE  unless you set the focus to a control
}

/////////////////////////////////////////////////////////////////////////////
// CSerailDlg message handlers

void CSerailDlg::OnSendToCom() 
{
	// TODO: Add your control notification handler code here
		// TODO: Add your control notification handler code here
	char buf[100];
	memset(&buf, 0, sizeof(buf));
	GetDlgItemText(IDC_EDIT1, buf, sizeof(buf));
	if (strcmp("NOT FOUND", buf) == 0)
		return;

	m_Ports[0].WriteToPort(buf);	
	
}

void CSerailDlg::OnConfigCom() 
{
	// TODO: Add your control notification handler code here
//	CConfigDlg* dlg = new CConfigDlg;
	CConfigDlg* dlg = new CConfigDlg(this, m_Ports[0].GetDCB());

	DWORD dwCommEvents = m_Ports[0].GetCommEvents();
	dlg->m_strSendBuffer.Format("%d", m_Ports[0].GetWriteBufferSize());
	dlg->m_CommBreakDetected = (dwCommEvents & EV_BREAK) > 0 ? TRUE : FALSE;
	dlg->m_CommCTSDetected   = (dwCommEvents & EV_CTS) > 0 ? TRUE : FALSE;
	dlg->m_CommDSRDetected   = (dwCommEvents & EV_DSR) > 0 ? TRUE : FALSE;
	dlg->m_CommERRDetected   = (dwCommEvents & EV_ERR) > 0 ? TRUE : FALSE;
	dlg->m_CommRingDetected  = (dwCommEvents & EV_RING) > 0 ? TRUE : FALSE;
	dlg->m_CommRLSDDetected  = (dwCommEvents & EV_RLSD) > 0 ? TRUE : FALSE;
	dlg->m_CommRXchar        = (dwCommEvents & EV_RXCHAR) > 0 ? TRUE : FALSE;
	dlg->m_CommRXcharFlag    = (dwCommEvents & EV_RXFLAG) > 0 ? TRUE : FALSE;
	dlg->m_CommTXEmpty       = (dwCommEvents & EV_TXEMPTY) > 0 ? TRUE : FALSE;

	if (dlg->DoModal() == IDOK)
	{
		DWORD dwCommEvents = 0;
		if (dlg->m_CommBreakDetected)
			dwCommEvents |= EV_BREAK;
		if (dlg->m_CommCTSDetected)
			dwCommEvents |= EV_CTS;
		if (dlg->m_CommDSRDetected)
			dwCommEvents |= EV_DSR;
		if (dlg->m_CommERRDetected)
			dwCommEvents |= EV_ERR;
		if (dlg->m_CommRingDetected)
			dwCommEvents |= EV_RING;
		if (dlg->m_CommRLSDDetected)
			dwCommEvents |= EV_RLSD;
		if (dlg->m_CommRXchar)
			dwCommEvents |= EV_RXCHAR;
		if (dlg->m_CommRXcharFlag)
			dwCommEvents |= EV_RXFLAG;
		if (dlg->m_CommTXEmpty)
			dwCommEvents |= EV_TXEMPTY;
		
		m_Ports[0].InitPort(this, 
			1, 
			atoi(dlg->m_BaudRate),
			dlg->m_Parity[0],
			atoi(dlg->m_ByteSize),
			atoi(dlg->m_StopBits),
			dwCommEvents,
			atoi(dlg->m_strSendBuffer));
				m_Ports[0].StartMonitoring();
	}
	
	delete dlg;
	
}

LONG CSerailDlg::OnCommunication(WPARAM ch, LPARAM port)
{

	CString string,strtemp;
	unsigned char tempchar;
	
	GetDlgItem(IDC_EDIT2)->GetWindowText(string);

	tempchar=(unsigned char)ch;
	m_HexDsp=((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck()&1;
	if(m_HexDsp)
	{
		strtemp.Format(_T("%2lx"),tempchar);
		m_strReceived+=strtemp;
		m_strReceived+=" ";
	}
	else
	{
		strtemp=(char) ch;
		m_strReceived+=strtemp;
		m_strReceived+=" ";
	}
	RecvBuf[count++]=ch;
	string+=strtemp;

	SetDlgItemTextA(IDC_EDIT2,string);
	return 0;
}


void CSerailDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	m_Ports[0].StopMonitoring();			
	CDialog::OnCancel();
}

void CSerailDlg::OnOK() 
{
	// TODO: Add extra validation here
	m_Ports[0].StopMonitoring();
	CDialog::OnOK();
}

void CSerailDlg::OnbTest() 
{
	// TODO: Add your control notification handler code here
	CFile cf;
	unsigned char buf[4096];
//	memset(&buf, 0, sizeof(buf));

	unsigned char chr[2];
	unsigned char ch;

	CString string,strtemp;
	memset(&buf, 0, sizeof(buf));
	strtemp.Empty(); string.Empty();
	if(cf.Open(_T("test.dat"),CFile::modeRead|CFile::typeBinary))
	{
		int fsize=cf.GetLength();
		m_ReadLen.Format(_T("ReadSize=%d  "),fsize);
		cf.Read(buf,sizeof(char)*fsize);
		for(int i=0;i<fsize;i++)
		{
			ch=buf[i];
			chr[0]=buf[i];
			chr[1]=0;
			if(ch>15)
				strtemp.Format(_T("%2lx"),ch);
			else
			{
				strtemp.Format(_T("0%x"),ch);
			}
			string+=strtemp;
			string+=' ';
			strtemp.Empty();
			if(((i+1)%16)==0)
			{
				m_ListBox.AddString(string);
				string.Empty();
			}
//			m_Ports[0].WriteToPort(chr,1);	
		}
		m_ListBox.AddString(string);
//		SetDlgItemTextA(IDC_EDIT2,string);
		m_Ports[0].WriteToPort(buf,fsize);	
	}
}

void CSerailDlg::OnbRecive() 
{
	// TODO: Add your control notification handler code here
	BYTE temp;
	CFile arFile;
	arFile.Open("MyRecv.Dat",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
	for(int i=0;i<count;i++)
	{
		temp=RecvBuf[i];
		arFile.Write(&temp,1);
	}
	
	arFile.Close();
}

void CSerailDlg::OnCheck1() 
{
	// TODO: Add your control notification handler code here
	SetDlgItemTextA(IDC_EDIT2,"");
}

⌨️ 快捷键说明

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