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

📄 ppc_testserialdlg.cpp

📁 WinCE环境下串口接收、发送数据的一个程序
💻 CPP
字号:
// TestSerialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestSerial.h"
#include "TestSerialDlg.h"

#include "setup.h"

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

#define WM_NEW_DATA_ARRIVE WM_USER+1
/////////////////////////////////////////////////////////////////////////////
// CTestSerialDlg dialog

CTestSerialDlg::CTestSerialDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestSerialDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestSerialDlg)
		// 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 CTestSerialDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestSerialDlg)
	DDX_Control(pDX, IDC_RECEIVE, m_CtrlReceive);
	DDX_Text(pDX, IDC_RECEIVE, m_strReceive);
	DDX_Text(pDX, IDC_TRANSMIT, m_strTransmit);
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestSerialDlg, CDialog)
	//{{AFX_MSG_MAP(CTestSerialDlg)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_SETUP, OnButtonSetup)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_Receive, OnBUTTONReceive)
	ON_MESSAGE(WM_NEW_DATA_ARRIVE,OnDataArrivedMsg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestSerialDlg message handlers

BOOL CTestSerialDlg::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
		//初始化变量
	BaudRates[0]=1200;
	BaudRates[1]=2400;
	BaudRates[2]=4800;
	BaudRates[3]=9600;
	BaudRates[4]=19200;
	BaudRates[5]=38400;
	BaudRates[6]=57600;
	BaudRates[7]=115200;

	PortIDs[0]="COM1:";
	PortIDs[1]="COM2:";
	PortIDs[2]="COM3:";
	PortIDs[3]="COM4:";
	PortIDs[4]="COM5:";
	PortIDs[5]="COM6:";
	PortIDs[6]="COM7:";
	PortIDs[7]="COM8:";
	PortIDs[8]="COM9:";

	PortNo=1;
	BaudRate=3;
	DataBits=8;
	StopBits=ONESTOPBIT;
	Parity=NOPARITY;

	Open=FALSE;
	Receive=FALSE;
	HexDisplay=FALSE;
	HexSend=FALSE;

	m_strDataReceived=_T("");

	m_pSerial=new CPSerialPort();
	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CTestSerialDlg::OnDataArrive(char *data,int length,DWORD userdata)
{
	CTestSerialDlg *pWnd=(CTestSerialDlg *)userdata;
	::SendMessage(pWnd->m_hWnd,WM_NEW_DATA_ARRIVE,(WPARAM)data,LPARAM(length));
}

void CTestSerialDlg::OnDataArrivedMsg(WPARAM wParam,LPARAM lParam)
{
	//处理收到的数据
	int length=(int)lParam;
	char *data=(char*)wParam;
	CString THex(_T(""));

	int i;

	if(length!=0)
	{
		if(HexDisplay)
		{
			for(i=0;i<length;i++)
			{
				if(data[i]<16)
				{
					THex.Format(_T("0%X "),data[i]);
				}
				else
				{
					THex.Format(_T("%2X "),data[i]);
				}
				m_strDataReceived+=THex;
			}
		}
		else
		{
			m_strDataReceived=CString(data).Left(length);
		}
		m_strReceive+=m_strDataReceived;
		m_strDataReceived=_T("");
		SetDlgItemText(IDC_RECEIVE,m_strReceive);
		m_CtrlReceive.SetSel(m_strReceive.GetLength(),m_strReceive.GetLength(),FALSE);
	}
}

void CTestSerialDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	if(!Open)
	{
		PortID=PortIDs[PortNo-1];
		Open=m_pSerial->OpenPort(PortID,BaudRates[BaudRate],DataBits,StopBits,Parity,OnDataArrive,(DWORD)this);
		if(Open)
		{
			SetDlgItemText(IDC_BUTTON_OPEN,_T("Close"));
		}
	}
	else
	{
		m_pSerial->ClosePort();
		Open=FALSE;
		Receive=FALSE;
		SetDlgItemText(IDC_BUTTON_Receive,_T("Receive"));
		SetDlgItemText(IDC_BUTTON_OPEN,_T("Open"));
	}

}

void CTestSerialDlg::OnButtonSetup() 
{
	// TODO: Add your control notification handler code here
	CSetup* Dialog1;
	Dialog1=new CSetup;

	Dialog1->BaudRate=BaudRate;
	Dialog1->PortNo=PortNo-1;
	Dialog1->DataBits=DataBits-7;
	Dialog1->StopBits=StopBits;
	Dialog1->Parity=Parity;

	if(Dialog1->DoModal()==IDOK)
	{
		BaudRate=Dialog1->BaudRate;
		PortNo=Dialog1->PortNo+1;
		DataBits=Dialog1->DataBits+7;
		StopBits=Dialog1->StopBits;
		Parity=Dialog1->Parity;
	}

	delete Dialog1;
}

void CTestSerialDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	DWORD dwCharToWrite=0;
	DWORD dwBytesWritten=0;
	int i,j;

	GetDlgItemText(IDC_TRANSMIT,m_strTransmit);
	if(!HexSend)
	{
		dwCharToWrite=(DWORD)m_strTransmit.GetLength();
	}
	else
	{
		for(i=0;i<m_strTransmit.GetLength()/2;i++)
		{
			if(((m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')||(m_strTransmit.GetAt(i*2)>='A'&&m_strTransmit.GetAt(i*2)<='F'))&&((m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')||(m_strTransmit.GetAt(i*2+1)>='A'&&m_strTransmit.GetAt(i*2+1)<='F')))
			{
				dwCharToWrite++;
			}
		}
	}
	dwBytesWritten=0;
	
	if(Open&&dwCharToWrite!=0)
	{
		char* buf=new char[dwCharToWrite];
		if(!HexSend)
		{			
			for(i=0;i<(int)dwCharToWrite;i++)
			{
				buf[i]=(char)m_strTransmit.GetAt(i);
			}
		}
		else
		{
			j=0;
			for(i=0;i<m_strTransmit.GetLength()/2;i++)
			{
				if(((m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')||(m_strTransmit.GetAt(i*2)>='A'&&m_strTransmit.GetAt(i*2)<='F'))&&((m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')||(m_strTransmit.GetAt(i*2+1)>='A'&&m_strTransmit.GetAt(i*2+1)<='F')))
				{
					if(m_strTransmit.GetAt(i*2+1)>='0'&&m_strTransmit.GetAt(i*2+1)<='9')
					{
						buf[j]=m_strTransmit.GetAt(i*2+1)-48;
					}
					else
					{
						buf[j]=m_strTransmit.GetAt(i*2+1)-55;
					}
					if(m_strTransmit.GetAt(i*2)>='0'&&m_strTransmit.GetAt(i*2)<='9')
					{
						buf[j]+=(m_strTransmit.GetAt(i*2)-48)*16;
					}
					else
					{
						buf[j]+=(m_strTransmit.GetAt(i*2)-55)*16;
					}
					j++;
				}
			}
		}
		dwBytesWritten=m_pSerial->WritePort(buf,dwCharToWrite);
		if(dwBytesWritten==0)
		{
			AfxMessageBox(_T("无法向端口写入数据!"));
		}
		delete[] buf;		
	}
}

void CTestSerialDlg::OnBUTTONReceive() 
{
	// TODO: Add your control notification handler code here
	if(!Receive)
	{
		Receive=m_pSerial->Activate();
		if(Receive)
		{
			SetDlgItemText(IDC_BUTTON_Receive,_T("Stop"));
		}
	}
	else
	{
		m_pSerial->Deactivate();
		Receive=FALSE;
		SetDlgItemText(IDC_BUTTON_Receive,_T("Receive"));
	}
}

⌨️ 快捷键说明

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