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

📄 rs232testdlg.cpp

📁 Windows下的串口程序示例
💻 CPP
字号:
// RS232TestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RS232Test.h"
#include "RS232TestDlg.h"

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

// 设置双重消息循环,以使进程从长时操作中退出
int CheckMessageQueque(HWND hWnd)
	{
	MSG msg;
	while(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
		{
		if(msg.message!=WM_QUIT)
			{
			GetMessage(&msg,NULL,0,0);
			if(!IsDialogMessage(hWnd,&msg))
				DispatchMessage(&msg);
			}
			else return 1;
		}
	return 0;
	}

/////////////////////////////////////////////////////////////////////////////
// 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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRS232TestDlg dialog

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

void CRS232TestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRS232TestDlg)
	DDX_Text(pDX, IDC_EDIT1, m_Result);
	DDX_Text(pDX, IDC_STATUS, m_Status);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRS232TestDlg, CDialog)
	//{{AFX_MSG_MAP(CRS232TestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BREAK, OnBreak)
	ON_BN_CLICKED(IDC_WRITE, OnWrite)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRS232TestDlg message handlers

BOOL CRS232TestDlg::OnInitDialog()
{
	char Ret1=Init(2);

	char Report[80];
	wsprintf(Report,"0x%x",Ret1);
	MessageBox(Report,"Init");

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

char CRS232TestDlg::Init(char PortNum)
{
	int Port=PortNum-1;
	char RetValue;
	__asm
	{
		mov ah,0
		mov al,0c3H
		mov edx,Port
		int 14H
		mov RetValue,ah
	}
		return RetValue;
}
char CRS232TestDlg::Read(char PortNum,char *Buffer)
{
	int Port=PortNum-1;
	char RetValue;
	char Temp;
	__asm
		{
		mov ah,2
		mov edx,Port
		int 14h
		mov Temp,al
		mov RetValue,ah
		}
	*Buffer=Temp;
	return RetValue;
}
char CRS232TestDlg::Write(char PortNum,char *Buffer)
{
	int Port=PortNum-1;
	char RetValue;
	char Temp=*Buffer;
	__asm
	{
		mov ah,1
		mov al,Temp
		mov edx,Port
		int 14h
		mov RetValue,ah
	}
	return RetValue;
}
char CRS232TestDlg::ReadStatus(char PortNum)
{
	int Port=PortNum-1;
	char RetValue;
	__asm
	{
		mov ah,3
		mov edx,Port
		int 14h
		mov RetValue,ah
	}
	return RetValue;
}
int CRS232TestDlg::ReadTest(char PortNum)
{
	unsigned char Temp;
	char Buffer;
	BreakFlag=0;
	while(1)
	{
		while(1)
		{
			CheckMessageQueque(m_hWnd);
			if(BreakFlag)
				return -1;
			Temp=ReadStatus(PortNum);
			if(Temp&01)
				break;
		}
		Temp=Read(PortNum,&Buffer);
		if(!(Temp&0x80))
			MessageBox("dfkj","");
		CheckMessageQueque(m_hWnd);
		if(BreakFlag)
			return -1;
		m_Result=Buffer;
		m_Status=Temp;
		UpdateData(0);
	}
	return 0;
}
int CRS232TestDlg::WriteTest(char PortNum)
{
	char Buffer='0';
	unsigned char Temp;
	BreakFlag=0;
	while(1)
	{
		CheckMessageQueque(m_hWnd);
		if(BreakFlag)
			return -1;
		while(1)
		{
			CheckMessageQueque(m_hWnd);
			if(BreakFlag)
				return -1;
			Temp=ReadStatus(PortNum);
			if(Temp&0x20)
				break;
		}
	Temp=ReadStatus(PortNum);
	Temp=Write(PortNum,&Buffer);
	if(!(Temp&0x80))
		MessageBox("dfkj","dfk");
	m_Status=Temp;
	UpdateData(0);
	}
}
void CRS232TestDlg::OnOK() 
{
int Ret2=ReadTest(2);
}



void CRS232TestDlg::OnBreak() 
{
	BreakFlag=1;	
}

void CRS232TestDlg::OnWrite() 
{
	WriteTest(2);
}

⌨️ 快捷键说明

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