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

📄 sdddlg.cpp

📁 一个用于测试串口的小程序,短小精悍,希望大家喜欢 !
💻 CPP
字号:
// sddDlg.cpp : implementation file
//

#include "stdafx.h"
#include "sdd.h"
#include "sddDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CSddDlg dialog

CSddDlg::CSddDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSddDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSddDlg)
		// 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 CSddDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSddDlg)
	DDX_Control(pDX, IDC_MSCOMM1, m_ctrlComm);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSddDlg, CDialog)
	//{{AFX_MSG_MAP(CSddDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSddDlg message handlers

BOOL CSddDlg::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
	SetTimer(2,750,NULL);

	// TODO: Add extra initialization here

	///////////////////////////////////////////
	m_ctrlComm.SetCommPort(1); //选择COM1
	m_ctrlComm.SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes
	m_ctrlComm.SetOutBufferSize(512); //设置输入缓冲区的大小,Bytes

 
		if (!m_ctrlComm.GetPortOpen())
		{
	    try
		{
			//端口设置这里需要修改
			//short int nComPort = CZnskxtApp::ComStrToInt(gConfig.Port);
		    m_ctrlComm.SetCommPort(1);//nComPort);
			m_ctrlComm.SetPortOpen(true);
			//CString strSetting = gConfig.Speed + ',' + gConfig.CheckBit.GetAt(0) + ',' + gConfig.DataBit + ',' + gConfig.StopBit;
			CString strSetting = "9600,n,8,1";
			m_ctrlComm.SetInputMode(1); //设置输入方式为二进制方式
			m_ctrlComm.SetSettings(strSetting);			
	        m_ctrlComm.SetRThreshold(1); //为1表示有一个字符引发一个事件    
	        m_ctrlComm.SetInputLen(0);
			m_ctrlComm.GetInput();//先预读缓冲区以清除残留数据
		}
		catch(CException ee)
		{
			AfxMessageBox("串口COM2不能打开,请检查串口的配置!");
		}

		}
///////////////////////////////////////////////



 
		
//////////////////////////////////////////////////////////////////////////		
	

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

int CSddDlg::ReadPort()
{
	   int iTemp; 
	   iTemp = 0;
	   CFile MyFile;
	   MyFile.Open("C:\\EntryPort.dat",CFile::modeRead,NULL);

	   DWORD m_nFileLen = MyFile.GetLength();
	   char portnum[10];
	   MyFile.Read(portnum,10);
	   portnum[m_nFileLen] = '\0';
	   //////////////////////////////////////////////
	   //这段代码测试用
	   CString tmpstr;
	   tmpstr.Format("%s",portnum);
	   //AfxMessageBox(tmpstr);
	   ///////////////////////////////////////////////
	   iTemp = atoi(portnum);
	   MyFile.Close();

	return iTemp;
}

int CSddDlg::WritePortSignalNULL()
{
	   int iTemp; 
	   iTemp = 0;
	   CFile MyFile;
	   MyFile.Open("C:\\PortSignal.dat",CFile::modeWrite,NULL);

	   CString CStrTemp;
	   CStrTemp = "0";
	   MyFile.Write(CStrTemp,CStrTemp.GetLength());
	   MyFile.Close();

	return iTemp;
}

void CSddDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default

		CByteArray mByteAry,mByteAry2;
	    CByteArray mIn;
		int g_nEntryPortClosed = 0;
		int g_nEntryPort = 0;
		int g_nEntrySignal = -1;
		int TempSize = 14;
		VARIANT variant_inp;
		COleSafeArray safearray_inp;
		LONG len,k;
		BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.

		int jj = 0;//测试用

		mIn.RemoveAll();
		mByteAry.RemoveAll();
		mByteAry2.RemoveAll();

//打开串口
	if (!m_ctrlComm.GetPortOpen())
	{
	    try
		{
			//端口设置这里需要修改
			//short int nComPort = CZnskxtApp::ComStrToInt(gConfig.Port);
		        m_ctrlComm.SetCommPort(1);//nComPort);
			//CString strSetting = gConfig.Speed + ',' + gConfig.CheckBit.GetAt(0) + ',' + gConfig.DataBit + ',' + gConfig.StopBit;
			CString strSetting = "9600,n,8,1";
			m_ctrlComm.SetSettings(strSetting);
			m_ctrlComm.SetPortOpen(true);
		}
		catch(CException ee)
		{
			AfxMessageBox("串口COM2不能打开,请检查串口的配置!");
		}
		
	}

	//#AABB(data)  写数据
	//

	///////////////////////////////////////////////////////////////////////////
		int r1,r2,r3;
		r1 = r2 = r3;

	//读串口信息
	//int count = m_ctrlComm.GetInBufferCount();

	BYTE result1,result2,result3;
	result1 = result2 = result3 = 10;

	int je1;
	je1 = 'A';
	je1 = 'T';
	//设置要发送的数据
	mByteAry.SetSize(3);
	mByteAry.SetAt(0,0x41);
	mByteAry.SetAt(1,0x54);
	mByteAry.SetAt(2,0x0D);

	m_ctrlComm.SetOutput(COleVariant(mByteAry));
	//23 30 31 30 30 30 31 0D//#010001

	Sleep(100);//延迟5秒//电保持时间大约为16ms左右,取其一半

	   COleVariant olearray = m_ctrlComm.GetInput();

       int hha = m_ctrlComm.GetInBufferCount(); //接收到的字符数目
       CString strRcv;
       strRcv = (const char *)(unsigned char*) olearray.parray->pvData;

	BYTE bTemp;
	for(k = 0;k < (TempSize - 4); )
	{
	bTemp = strRcv.GetAt(k);
	if(bTemp == 0x3E)
		k = k + 2;
	else
		break;
	}
	//itoa()

	bTemp = strRcv.GetAt(k+4);//判断第TempSize-4个的数据即可,但得到是数字字符



	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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