LAPTOP-SNT8I5JK\Boounion
2025-06-21 4aeb9529b84ec52bfd40f0078c01d648cab7edc3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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();
}