📄 eg03dlg.cpp
字号:
// Eg03Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Eg03.h"
#include "Eg03Dlg.h"
#include "Pcomm.h" //这里添加串口通信的头文件
#define BaudRate B57600 //波特率
#define DataBits BIT_8 //数据位
#define Parity P_NONE //效验位
#define StopBits STOP_1 //停止位
#define WM_PCOMM WM_USER+500 //自定义消息
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////串口中断回调函数//////////////////////////////////
VOID CALLBACK CntIrq(int port)
{
if(::AfxGetMainWnd())
{
if(::AfxGetMainWnd()->m_hWnd)
{
::PostMessage(::AfxGetMainWnd()->m_hWnd,WM_PCOMM,0,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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEg03Dlg dialog
CEg03Dlg::CEg03Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CEg03Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEg03Dlg)
// 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 CEg03Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEg03Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEg03Dlg, CDialog)
//{{AFX_MSG_MAP(CEg03Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_PORTOPEN, OnBtnPortopen)
ON_BN_CLICKED(IDC_BTN_SEND, OnBtnSend)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_PCOMM,OnPcomm) //这里是消息影射
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEg03Dlg message handlers
BOOL CEg03Dlg::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
((CComboBox *)GetDlgItem(IDC_COMPORT))->InsertString(0,"请选择");
((CComboBox *)GetDlgItem(IDC_COMPORT))->InsertString(1,"1");
((CComboBox *)GetDlgItem(IDC_COMPORT))->InsertString(2,"2");
((CComboBox *)GetDlgItem(IDC_COMPORT))->InsertString(3,"3");
((CComboBox *)GetDlgItem(IDC_COMPORT))->InsertString(4,"4");
((CComboBox *)GetDlgItem(IDC_COMPORT))->InsertString(5,"5");
((CComboBox *)GetDlgItem(IDC_COMPORT))->SetCurSel(1);
return TRUE; // return TRUE unless you set the focus to a control
}
void CEg03Dlg::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 CEg03Dlg::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 CEg03Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEg03Dlg::OnBtnPortopen()
{
// TODO: Add your control notification handler code here
Port=GetDlgItemInt(IDC_COMPORT);
CString a;
GetDlgItemText(IDC_BTN_PORTOPEN,a);
if(!strcmp(a,"打开"))
{
if(SIO_OK!=sio_open(Port))
{
MessageBox("串口打开错误");
}
else
{
sio_ioctl(Port,BaudRate,DataBits | StopBits | Parity);
sio_cnt_irq(Port,CntIrq,1);
SetDlgItemText(IDC_BTN_PORTOPEN,"关闭");
}
}
else
{
sio_close(Port);
SetDlgItemText(IDC_BTN_PORTOPEN,"打开");
}
}
/////////////////////////////////////////////////////////////
//函数:OnPcomm()
//功能:从串口读取数据
/////////////////////////////////////////////////////////////
void CEg03Dlg::OnPcomm()
{
char buf[200];
int end=sio_read(Port,buf,100);
if(end)
{
CString a,b="";
GetDlgItemText(IDC_EDIT_RECMSG,b);
buf[end]=0;
for(int i=0;i<end;i++)
{
a.Format("%X ",(unsigned char)buf[i]);
b+=a;
}
SetDlgItemText(IDC_EDIT_RECMSG,b);
}
}
void CEg03Dlg::OnBtnSend()
{
// TODO: Add your control notification handler code here
CString a;
unsigned char b=0;
GetDlgItemText(IDC_EDIT_SEDMSG,a); //取得编辑框内所有文本
a.MakeUpper();//全部转换为大写
for(unsigned char i=0;i<a.GetLength();i+=3)
{
if(a.GetAt(i)>='A' && a.GetAt(i)<='Z') b=(a.GetAt(i)-55)*16; //判断填入的是字母还是数字,并把字符转换成十六进制数
else if(a.GetAt(i)>='0' && a.GetAt(i)<='9') b=(a.GetAt(i)-0x30)*16;
if(a.GetAt(i+1)>='A' && a.GetAt(i+1)<='Z') b+=(a.GetAt(i+1)-55);
else if(a.GetAt(i+1)>='0' && a.GetAt(i+1)<='9') b+=(a.GetAt(i+1)-0x30);
sio_putch(Port,b); //发送
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -