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

📄 testportdlg.cpp

📁 一个功能比较强大的串口通讯程序。用类的形式表现
💻 CPP
字号:
// testportDlg.cpp : implementation file
//

#include "stdafx.h"
#include "testport.h"
#include "testportDlg.h"
//#include "cnComm.h"
#include "serialoperate.h"
#include "spc2701.h"
#include "jvc1481.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CSerialWnd m_serial;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//CPortOperate		m_serial;
SPC2701CMD spc2701_1;
SPC2701CMD spc2701;
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()

/////////////////////////////////////////////////////////////////////////////
// CTestportDlg dialog

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

void CTestportDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestportDlg)
	DDX_Text(pDX, IDC_EDIT1, m_sendstr);
	DDX_Text(pDX, IDC_EDIT2, m_recstr);
	//}}AFX_DATA_MAP
}

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

/////////////////////////////////////////////////////////////////////////////
// CTestportDlg message handlers

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

void CTestportDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
//	state = MyComm1.SetState("9600,n,8,2");
//	MyComm1.Open(1, "9600,8,n,1");
//	if (m_serial.Open("COM1",this) != ERROR_SUCCESS)
//	{
//		AfxMessageBox(_T("Unable to open COM-port"),MB_ICONSTOP|MB_OK);
//		GetParent()->PostMessage(WM_CLOSE);
//	}
//	m_serial.Setup(m_serial.EBaudrate::EBaud115200,
//		m_serial.EDataBits::EData8,
//		m_serial.EParity::EParNone,
//		m_serial.EStopBits::EStop1);

}


//MyComm1.Read(buf, 1000);
//MyComm1.Write(buf, 200);

void CTestportDlg::OnOK() 
{
	// TODO: Add extra validation here
	CString temp;
	char szData[20]={0};
	int i;
	int abc[20];
	unsigned long dwRead;
	BYTE bt;
	CString strtemp;
	spc2701.portinit("COM1",m_serial.EBaudrate::EBaud9600,
		m_serial.EDataBits::EData8,
		m_serial.EParity::EParNone,
		m_serial.EStopBits::EStop1);
//	spc2701.cmdFocusMode(1,1,"12");

	UpdateData(TRUE);
	temp = m_sendstr;
//	m_serial.SendString(temp);
//	m_recstr = m_serial.ReadString(9);
/*	do
	{
		m_serial.Read(szData,9,&dwRead);
		i = i+1;
		Sleep(100);
		szData[dwRead] = '\0';
	} while (dwRead != 9);

    for(i = 0;i<9;i++) //将数组转换为Cstring型变量
    {
	    bt = szData[i]; //字符型
		strtemp.Format("%02X ", bt);	//以十六进制显示
		m_recstr += strtemp; //加入接收编辑框对应字符串 
	}
*/	
	//CDialog::OnOK();
}
void CTestportDlg::SendString(CString str)
{
	int length,i,strdata;
	BYTE tempdata[2];
	CString strtemp;

	strtemp = "00";
	length = str.GetLength()/2;
//	i = str.GetLength() div 2;
//	if (i==1)
//	{
//		AfxMessageBox("字符串长度不是偶数");
//		return;
//	}
	for (i=0;i<length;i++)
	{
		strtemp.SetAt(0,str.GetAt(i*2));
		strtemp.SetAt(1,str.GetAt(i*2+1));
		strdata = String2Hex(strtemp);
		tempdata[0] = strdata;
		m_serial.Write(tempdata,1);
		Sleep(10);
	}

}

int CTestportDlg::String2Hex(CString str)
{	//将字符串转换为16进制,俩个字符间插入了一个空格
	int hexdata, lowhexdata;
	int hexdatalen = 0;
	int len = str.GetLength();
//	senddata.SetSize(len/2);

	for (int i = 0; i < len; )
	{
		char lstr, hstr = str[i];
		if (hstr == ' ')	//如果遇到空格则跳过
		{
			i++;
			continue;
		}
		i++;
		if (i >= len)
			break;

		lstr = str[i];
		if ((hexdata = ConvertHexChar(hstr)) == -1)
		{
			AfxMessageBox("输入字符超出16进制范围,无法按16进制正常发送",MB_ICONERROR);		//将高字符转换为16进制字符
			return -1;
		}
		if ((lowhexdata = ConvertHexChar(lstr)) == -1)	//再将低字符转换为16进制字符
		{
			AfxMessageBox("输入字符超出16进制范围,无法按16进制正常发送",MB_ICONERROR);		//将高字符转换为16进制字符
			return -1;
		}

		if ((hexdata ==16) || (lowhexdata == 16))
			break;							//如果这俩个字符中任意一个大于16则跳出
		else
			hexdata = hexdata*16 + lowhexdata;	//否则把俩个字符叠加
		i++;
//		senddata[hexdatalen] = (char)hexdata;	//保存到要发送的缓冲区中
		hexdatalen++;
	}

//	senddata.SetSize(hexdatalen);
//	return hexdatalen;
	return hexdata;
}

char CTestportDlg::ConvertHexChar(char ch)
{	//将一个字符转换为相应的十六进制
	if ((ch >= '0') && (ch <= '9'))
		return ch - 0x30;
	else if ((ch >= 'A') && (ch <= 'F'))
		return ch - 'A' + 10;
	else if ((ch >= 'a') && (ch <= 'f'))
		return ch - 'a' + 10;
	else
		return (-1);
}

⌨️ 快捷键说明

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