📄 modbusplcdlg.cpp
字号:
// ModbusPLCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ModbusPLC.h"
#include "ModbusPLCDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CModbus *g_pModbus=NULL;
/////////////////////////////////////////////////////////////////////////////
// CModbusPLCDlg dialog
CModbusPLCDlg::CModbusPLCDlg(CWnd* pParent /*=NULL*/)
: CDialog(CModbusPLCDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CModbusPLCDlg)
// 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);
if(g_pModbus==NULL)
g_pModbus = new CModbus();
}
void CModbusPLCDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CModbusPLCDlg)
DDX_Control(pDX, IDC_TAB, m_Sheet);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CModbusPLCDlg, CDialog)
//{{AFX_MSG_MAP(CModbusPLCDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_OPEN_PORT, OnBtnOpenPort)
ON_BN_CLICKED(IDC_BTN_EXIT, OnBtnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CModbusPLCDlg message handlers
BOOL CModbusPLCDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
m_Sheet.AddPage("Modbus数据操作",&m_NormalOptionPage,IDD_DLG_NORMAL_OPTION);
m_Sheet.AddPage("Thinget数据读取",&m_XinjieDataReadPage,IDD_DLG_XINJIE_DATA_READ);
m_Sheet.Show();
CComboBox *pComboBox;
pComboBox = (CComboBox *)GetDlgItem(IDC_COMBO_PORT);
pComboBox->SetWindowText("COM1");
pComboBox = (CComboBox *)GetDlgItem(IDC_COMBO_BAUDRATE);
pComboBox->SetWindowText("19200");
pComboBox = (CComboBox *)GetDlgItem(IDC_COMBO_BYTESIZE);
pComboBox->SetWindowText("8");
pComboBox = (CComboBox *)GetDlgItem(IDC_COMBO_STOPBIT);
pComboBox->SelectString(-1,"1");
pComboBox = (CComboBox *)GetDlgItem(IDC_COMBO_PARITY);
pComboBox->SelectString(-1,"even");
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CModbusPLCDlg::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 CModbusPLCDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CModbusPLCDlg::OnBtnExit()
{
EndDialog(TRUE);
}
BOOL CModbusPLCDlg::DestroyWindow()
{
if(g_pModbus!=NULL)
{
delete g_pModbus;
g_pModbus = NULL;
}
return CDialog::DestroyWindow();
}
void CModbusPLCDlg::OnBtnOpenPort()
{
tagPortParam PortParam;
if(g_pModbus!=NULL)
{
if(!g_pModbus->IsConnected())
{
GetDlgItemText(IDC_COMBO_PORT,PortParam.szPortName,sizeof(PortParam.szPortName));
PortParam.BaudRate = GetDlgItemInt(IDC_COMBO_BAUDRATE);
PortParam.ByteSize = GetDlgItemInt(IDC_COMBO_BYTESIZE);
CComboBox *pComboBox;
pComboBox = (CComboBox *)GetDlgItem(IDC_COMBO_STOPBIT);
PortParam.StopBits = pComboBox->GetCurSel();
pComboBox = (CComboBox *)GetDlgItem(IDC_COMBO_PARITY);
PortParam.Parity = pComboBox->GetCurSel();
PortParam.Flow = 1;
g_pModbus->ConnectionPort(&PortParam);
}
else
{
g_pModbus->DisconnectPort();
}
if(g_pModbus->IsConnected())
SetDlgItemText(IDC_BTN_OPEN_PORT,"关闭端口");
else
SetDlgItemText(IDC_BTN_OPEN_PORT,"打开端口");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -