📄 communication1dlg.cpp
字号:
// COMMunication1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "COMMunication1.h"
#include "COMMunication1Dlg.h"
#include "SetMode.h"
#include "mscomm.h"
#include <math.h>
#include "stdio.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()
/////////////////////////////////////////////////////////////////////////////
// CCOMMunication1Dlg dialog
CCOMMunication1Dlg::CCOMMunication1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CCOMMunication1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCOMMunication1Dlg)
m_Send = _T("");
m_Receive = _T("");
myBaudRate="19200";
myCom=1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON3);
}
void CCOMMunication1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCOMMunication1Dlg)
DDX_Control(pDX, IDC_CHECK3, m_hexsend);
DDX_Control(pDX, IDC_HEX, m_hex);
DDX_Text(pDX, IDC_EDIT2, m_Send);
DDX_Text(pDX, IDC_EDIT1, m_Receive);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCOMMunication1Dlg, CDialog)
//{{AFX_MSG_MAP(CCOMMunication1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(IDM_SET, OnSet)
ON_BN_CLICKED(IDC_OPEN_BTN, OnOpenBtn)
ON_BN_CLICKED(IDC_CLEAR_BTN, OnClearBtn)
ON_WM_CREATE()
ON_BN_CLICKED(IDC_SEND_BTN, OnSendBtn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCOMMunication1Dlg message handlers
BOOL CCOMMunication1Dlg::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 CCOMMunication1Dlg::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 CCOMMunication1Dlg::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 CCOMMunication1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCOMMunication1Dlg::OnSet()
{ CSetMode dlg;
// dlg.DoModal();
// TODO: Add your command handler code here
if(dlg.DoModal()==IDOK)
{
myBaudRate=dlg.m_BaudRate;
if(dlg.m_Com=="COM1")
myCom=1;
else if(dlg.m_Com=="COM2")
myCom=2;
else if(dlg.m_Com=="COM3")
myCom=3;
else if(dlg.m_Com=="COM4")
myCom=4;
}
}
void CCOMMunication1Dlg::OnOpenBtn()
{ CString str,strPara;
UpdateData();
GetDlgItem(IDC_OPEN_BTN)->GetWindowText(str);
// strPara.Format("%d",myCom);
UpdateData(FALSE);
if(str=="打开串口")
{
GetDlgItem(IDC_OPEN_BTN)->SetWindowText("关闭串口");
myComm.SetCommPort(myCom);
strPara=myBaudRate;
strPara+=",";
strPara+=myParity;
strPara+=",8,1";
myComm.SetSettings(strPara);
myComm.SetInBufferSize(1024);
myComm.SetInBufferCount(0);
myComm.SetInputMode(1);
myComm.SetInputLen(0);
myComm.SetRThreshold(1);
myComm.SetPortOpen(1);
MessageBox("串口打开成功");
}
else
{
GetDlgItem(IDC_OPEN_BTN)->SetWindowText("打开串口");
myComm.SetPortOpen(0);
MessageBox("串口关闭成功");
}
}
void CCOMMunication1Dlg::OnClearBtn()
{ UpdateData();
// TODO: Add your control notification handler code here
//m_Receive="";
m_Receive = _T("");
UpdateData(FALSE);
}
BEGIN_EVENTSINK_MAP(CCOMMunication1Dlg, CDialog)
//{{AFX_EVENTSINK_MAP(CCOMMunication1Dlg)
ON_EVENT(CCOMMunication1Dlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CCOMMunication1Dlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT input1;
long len1,k;
COleSafeArray safearray1;
BYTE rxdata[2048];
CString strtemp;
//myComm.GetInput();
switch(myComm.GetCommEvent())
{
case 2:
input1=myComm.GetInput();
safearray1=input1;
len1=safearray1.GetOneDimSize();
for(k=0;k<len1;k++)
safearray1.GetElement(&k,rxdata+k);
for(k=0;k<len1;k++) //将数组转换为Cstring型变量
{
BYTE bt=*(char*)(rxdata+k);
// UpdateData(TRUE);
if(m_hex.GetCheck())
strtemp.Format("%02X",bt);
else
strtemp.Format("%c",bt);
m_Receive=m_Receive+strtemp;
}
}
UpdateData(FALSE);
}
int CCOMMunication1Dlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
DWORD style=WS_VISIBLE|WS_CHILD;
if(!myComm.Create(NULL,style,CRect(0,0,0,0),this,IDC_MSCOMM1))
{
AfxMessageBox("创建MScomm控件失败");
}
return 0;
}
int CCOMMunication1Dlg::hex(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);
}
void CCOMMunication1Dlg::OnSendBtn()
{
// CString strSend;
// TODO: Add your control notification handler code here
// CByteArray hexdata;
// int len=String2Hex(m_Send ,hexdata);
// myComm.SetOutput(COleVariant(hexdata));
UpdateData();
if(m_hexsend.GetCheck())
{
CByteArray data;
int len=String2Hex(m_Send,data);
myComm.SetOutput(COleVariant(data));
}
else
myComm.SetOutput(COleVariant(m_Send));//发送数据
// myComm.GetInput();
}
int CCOMMunication1Dlg::String2Hex(CString str, CByteArray &senddata)
{
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];
hexdata=hex(hstr);
lowhexdata=hex(lstr);
if((hexdata==16)||(lowhexdata==16))
break;
else
hexdata=hexdata*16+lowhexdata;
i++;
senddata[hexdatalen]=(char)hexdata;
hexdatalen++;
}
senddata.SetSize(hexdatalen);
return hexdatalen;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -