// AddPLCInfo.cpp: 实现文件
|
//
|
|
#include "stdafx.h"
|
#include "BoounionPLC.h"
|
#include "afxdialogex.h"
|
#include "AddPLCInfo.h"
|
|
|
// CAddPLCInfo 对话框
|
|
IMPLEMENT_DYNAMIC(CAddPLCInfo, CDialogEx)
|
|
CAddPLCInfo::CAddPLCInfo(CWnd* pParent /*=nullptr*/)
|
: CDialogEx(IDD_DIALOG_ADD_PLC_INFO, pParent)
|
{
|
|
}
|
|
CAddPLCInfo::~CAddPLCInfo()
|
{
|
}
|
|
CString CAddPLCInfo::GetPLCName() { return m_strPLCName; }
|
|
CString CAddPLCInfo::GetIP() { return m_strIP; }
|
|
CString CAddPLCInfo::GetPort() { return m_strPort; }
|
|
bool CAddPLCInfo::IsValidPort(const CString& strPort)
|
{
|
// 判断是否为数字
|
for (int i = 0; i < strPort.GetLength(); ++i) {
|
if (!_istdigit(strPort[i])) {
|
return false; // 非数字
|
}
|
}
|
|
// 转换为整数并判断范围
|
int nPort = _ttoi(strPort);
|
return nPort >= 1 && nPort <= 65535;
|
}
|
|
bool CAddPLCInfo::IsValidIPAddress(const CString& strIP1, const CString& strIP2, const CString& strIP3, const CString& strIP4)
|
{
|
// 校验每一段是否是数字并在范围内
|
return IsValidIPSegment(strIP1) &&
|
IsValidIPSegment(strIP2) &&
|
IsValidIPSegment(strIP3) &&
|
IsValidIPSegment(strIP4);
|
}
|
|
bool CAddPLCInfo::IsValidIPSegment(const CString& strSegment)
|
{
|
// 判断是否为数字
|
for (int i = 0; i < strSegment.GetLength(); ++i) {
|
if (!_istdigit(strSegment[i])) {
|
return false; // 非数字
|
}
|
}
|
|
// 转换为整数并判断范围
|
int nSegment = _ttoi(strSegment);
|
return nSegment >= 0 && nSegment <= 255;
|
}
|
|
void CAddPLCInfo::DoDataExchange(CDataExchange* pDX)
|
{
|
CDialogEx::DoDataExchange(pDX);
|
DDX_Control(pDX, IDC_EDIT_PLC_NAME, m_editPLCName);
|
DDX_Control(pDX, IDC_EDIT_PORT, m_editPort);
|
}
|
|
|
BEGIN_MESSAGE_MAP(CAddPLCInfo, CDialogEx)
|
ON_BN_CLICKED(IDOK, &CAddPLCInfo::OnBnClickedOk)
|
END_MESSAGE_MAP()
|
|
|
// CAddPLCInfo 消息处理程序
|
|
|
void CAddPLCInfo::OnBnClickedOk()
|
{
|
// TODO: 在此添加控件通知处理程序代码
|
CString strIPAddr1;
|
CString strIPAddr2;
|
CString strIPAddr3;
|
CString strIPAddr4;
|
|
m_editPLCName.GetWindowText(m_strPLCName);
|
if (m_strPLCName.IsEmpty()) {
|
AfxMessageBox(_T("PLC 名称不能为空!"));
|
return;
|
}
|
|
// 校验端口号
|
m_editPort.GetWindowText(m_strPort);
|
if (!IsValidPort(m_strPort)) {
|
AfxMessageBox(_T("端口号不合法!请输入 1 到 65535 之间的数字。"));
|
return;
|
}
|
|
// 校验IP地址
|
GetDlgItem(IDC_EDIT_IP_ADDR1)->GetWindowText(strIPAddr1);
|
GetDlgItem(IDC_EDIT_IP_ADDR2)->GetWindowText(strIPAddr2);
|
GetDlgItem(IDC_EDIT_IP_ADDR3)->GetWindowText(strIPAddr3);
|
GetDlgItem(IDC_EDIT_IP_ADDR4)->GetWindowText(strIPAddr4);
|
|
if (strIPAddr1.IsEmpty() || strIPAddr2.IsEmpty() || strIPAddr3.IsEmpty() || strIPAddr4.IsEmpty()) {
|
AfxMessageBox(_T("IP 地址不合法!每段不能为空。"));
|
return;
|
}
|
|
if (!IsValidIPAddress(strIPAddr1, strIPAddr2, strIPAddr3, strIPAddr4)) {
|
AfxMessageBox(_T("IP 地址不合法!每段必须是 0 到 255 的数字。"));
|
return;
|
}
|
|
// 拼接IP地址
|
m_strIP.Format(_T("%s.%s.%s.%s"), strIPAddr1, strIPAddr2, strIPAddr3, strIPAddr4);
|
|
CDialogEx::OnOK();
|
}
|