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

📄 rwcomdlg.cpp

📁 读写串口.rar
💻 CPP
字号:
// RWComDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RWCom.h"
#include "RWComDlg.h"
#include "ComAccess.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()

/////////////////////////////////////////////////////////////////////////////
// CRWComDlg dialog

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

void CRWComDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRWComDlg)
	DDX_Control(pDX, IDC_EDIT1, m_EditControl);
	DDX_Text(pDX, IDC_EDIT1, m_EditStr);
	DDX_Text(pDX, IDC_EDIT2, m_EditStr2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRWComDlg, CDialog)
	//{{AFX_MSG_MAP(CRWComDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRWComDlg message handlers

BOOL CRWComDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

//// write
void CRWComDlg::OnButton1() 
{
    CString  sChnNo;
    int  ilen;
	static unsigned char Buf[64];
	char SendBuf[64];

    memset(Buf,'\0',64);
	memset(SendBuf,'\0',64);

    UpdateData(true);   
       
    ilen = m_EditStr.GetLength();

	for ( int i=0; i < ilen; i++ )
	{
	   Buf[i] = m_EditStr.GetAt(i);
	}



	if (ilen > 0)
	{
		CComAccess ComAccess;
	
		if (ComAccess.Open("com1",9600,0,1,8))
		{
			
 	    ComAccess.WriteData(Buf, ilen);
   	    ComAccess.Close();
		}
		else
		{
 		MessageBox("不能打开 COM 口", "提示");
		}
	}

	
}


//// read
void CRWComDlg::OnButton2() 
{
	CComAccess ComAccess;

    int  ilen;
	static unsigned char Buf[64];
	char SendBuf[64];

    memset(Buf,'\0',64);
	memset(SendBuf,'\0',64);

    ilen = m_EditStr.GetLength();

	if (ComAccess.Open("com1",9600,0,1,8))
	{
 	    ComAccess.ReadData(Buf, ilen);
  	    ComAccess.Close();
	}
	else
	{
 		MessageBox("不能打开 COM 口", "提示");
	}
    m_EditStr2 = ""; 
	for (int i=0; i < ilen; i++)
	{
       m_EditStr2 = m_EditStr2 + (CString)Buf[i];
	}

	UpdateData(false);  
    AfxMessageBox(m_EditStr2);
    /*

	ComAccess  *com;
	char       buffer[256+1];

	int        read_len,
	           read_result, 
	           write_result;

	// check nums arguments
	if ( argc < 4 )
	{
		printf("Usage:\n"
		       "comaccess comX stringToSend lenStringToReceive\n"
			   "Press any key!\n");
		_getch();
		return;
	}

	// construct the communication device object
	if ( ! (com = new ComAccess()) )
	{
		printf("Error: Not enough memory to create ComAccess object!\n");
		return;
	}

	// open and init the communication device with the passed com number
	if ( ! com->Open(argv[1]) )
	{
		printf("Error: Can't open communication device!\n"
		       "%s", com->GetErrorMessage());
		delete com;
		return;
	}

	// write the passed string to the communication device
	write_result = com->WriteData(argv[2], strlen(argv[2]));

	// -1 ? then we got an error and print it
	if ( write_result < 0 )
		printf(com->GetErrorMessage());

	// read incoming data if exist

	buffer[0] = '\0';

	// make sure not to overload buffer
	read_len = ( (read_len=abs(atoi(argv[3]))) > 256 ) ? 256 : read_len; 
	read_result = com->ReadData(buffer, read_len);

	// -1 ? then we got an error and print it
	if ( (read_result < 0)  )
		printf(com->GetErrorMessage());
		
	// set end of received data
	buffer[read_len] = '\0';

	// display results
	printf("%d bytes written\n"
	       "%d bytes read\n"    
	       "%s", 
		   write_result, read_result, buffer);

	com->Close();
	delete com;

    */

}

⌨️ 快捷键说明

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