1 | // DlgRemoteParameters.cpp : implementation file
|
---|
2 | //
|
---|
3 |
|
---|
4 | #include "stdafx.h"
|
---|
5 | #include "Modbucfg.h"
|
---|
6 | #include "DlgRemoteParameters.h"
|
---|
7 |
|
---|
8 | #ifdef _DEBUG
|
---|
9 | #define new DEBUG_NEW
|
---|
10 | #undef THIS_FILE
|
---|
11 | static char THIS_FILE[] = __FILE__;
|
---|
12 | #endif
|
---|
13 |
|
---|
14 | /////////////////////////////////////////////////////////////////////////////
|
---|
15 | // CDlgRemoteParameters dialog
|
---|
16 |
|
---|
17 |
|
---|
18 | CDlgRemoteParameters::CDlgRemoteParameters(CWnd* pParent /*=NULL*/)
|
---|
19 | : CDialog(CDlgRemoteParameters::IDD, pParent)
|
---|
20 | {
|
---|
21 | m_bModeNew=FALSE;
|
---|
22 | m_pConnection=NULL;
|
---|
23 |
|
---|
24 | //{{AFX_DATA_INIT(CDlgRemoteParameters)
|
---|
25 | //}}AFX_DATA_INIT
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | void CDlgRemoteParameters::DoDataExchange(CDataExchange* pDX)
|
---|
30 | {
|
---|
31 | CDialog::DoDataExchange(pDX);
|
---|
32 | //{{AFX_DATA_MAP(CDlgRemoteParameters)
|
---|
33 | DDX_Control(pDX, IDC_EDIT_TIME_OUT, m_edtTimeOut);
|
---|
34 | DDX_Control(pDX, IDC_EDIT_PORT, m_edtServerPort);
|
---|
35 | DDX_Control(pDX, IDC_EDIT_IP_ADDR, m_edtServerAddress);
|
---|
36 | //}}AFX_DATA_MAP
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | BEGIN_MESSAGE_MAP(CDlgRemoteParameters, CDialog)
|
---|
41 | //{{AFX_MSG_MAP(CDlgRemoteParameters)
|
---|
42 | ON_BN_CLICKED(IDOKey, OnOKey)
|
---|
43 | //}}AFX_MSG_MAP
|
---|
44 | END_MESSAGE_MAP()
|
---|
45 |
|
---|
46 |
|
---|
47 | void CDlgRemoteParameters::Connection(CConnection* pConnection)
|
---|
48 | {
|
---|
49 | m_pConnection = pConnection;
|
---|
50 | }
|
---|
51 |
|
---|
52 | CConnection* CDlgRemoteParameters::Connection(){
|
---|
53 |
|
---|
54 | return m_pConnection;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | BOOL CDlgRemoteParameters::ShowParameters()
|
---|
60 | {
|
---|
61 |
|
---|
62 | ASSERT(m_pConnection);
|
---|
63 |
|
---|
64 | CRemoteModbus* pRemoteModbus = m_pConnection->RemotePtr();
|
---|
65 |
|
---|
66 | CString str;
|
---|
67 |
|
---|
68 | m_edtServerAddress.SetWindowText(pRemoteModbus->Host());
|
---|
69 |
|
---|
70 | str.Format("%d",pRemoteModbus->Port());
|
---|
71 |
|
---|
72 | m_edtServerPort.SetWindowText(str);
|
---|
73 |
|
---|
74 | str.Format("%d",pRemoteModbus->Timeout());
|
---|
75 |
|
---|
76 | m_edtTimeOut.SetWindowText(str);
|
---|
77 |
|
---|
78 | if (!m_bModeNew) {
|
---|
79 | m_edtServerPort.EnableWindow(FALSE);
|
---|
80 | m_edtServerAddress.EnableWindow(FALSE);
|
---|
81 | }
|
---|
82 |
|
---|
83 | return TRUE;
|
---|
84 | }
|
---|
85 |
|
---|
86 | BOOL CDlgRemoteParameters::SaveParameters()
|
---|
87 | {
|
---|
88 |
|
---|
89 | DWORD dwTimeout;
|
---|
90 |
|
---|
91 | CString str;
|
---|
92 | CString sConnectionName;
|
---|
93 | CString sMachine;
|
---|
94 |
|
---|
95 | CRemoteModbus* pRemoteModbus=NULL;
|
---|
96 |
|
---|
97 | UpdateData(TRUE);
|
---|
98 |
|
---|
99 |
|
---|
100 | if (m_bModeNew) {
|
---|
101 |
|
---|
102 | m_edtServerPort.GetWindowText(str);
|
---|
103 | str.TrimLeft();
|
---|
104 | str.TrimRight();
|
---|
105 |
|
---|
106 | m_edtServerAddress.GetWindowText(sMachine);
|
---|
107 |
|
---|
108 | sMachine.TrimLeft();
|
---|
109 | sMachine.TrimRight();
|
---|
110 |
|
---|
111 | sConnectionName = sMachine + _T(":") + str;
|
---|
112 |
|
---|
113 | if (gConnections.Lookup(sConnectionName,m_pConnection)) {
|
---|
114 | AfxMessageBox("Connection Alredy Exist");
|
---|
115 | Connection(NULL);
|
---|
116 | return FALSE;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | m_pConnection= gConnections.Add(sConnectionName, CConnection::REMOTE);
|
---|
121 |
|
---|
122 | if (NULL==m_pConnection) {
|
---|
123 | AfxMessageBox("Error Adding Connection");
|
---|
124 | return FALSE;
|
---|
125 | }
|
---|
126 |
|
---|
127 | pRemoteModbus=m_pConnection->RemotePtr();
|
---|
128 |
|
---|
129 |
|
---|
130 | }
|
---|
131 | else {
|
---|
132 |
|
---|
133 | //edit connection
|
---|
134 | ASSERT(m_pConnection);
|
---|
135 | pRemoteModbus= m_pConnection->RemotePtr();
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | //timeout
|
---|
141 | m_edtTimeOut.GetWindowText(str);
|
---|
142 | dwTimeout=(DWORD)atol(str);
|
---|
143 | if (0==dwTimeout) {
|
---|
144 | AfxMessageBox("Invalid timeout");
|
---|
145 | goto ParamError;
|
---|
146 | }
|
---|
147 |
|
---|
148 | pRemoteModbus->Timeout(dwTimeout);
|
---|
149 |
|
---|
150 |
|
---|
151 | //host
|
---|
152 | m_edtServerAddress.GetWindowText(str);
|
---|
153 | pRemoteModbus->Host(str);
|
---|
154 |
|
---|
155 | //port
|
---|
156 | m_edtServerPort.GetWindowText(str);
|
---|
157 | pRemoteModbus->Port((WORD)atol(str));
|
---|
158 |
|
---|
159 |
|
---|
160 | pRemoteModbus->ReconnectOnEveryMessage(TRUE);
|
---|
161 |
|
---|
162 | if (!pRemoteModbus->openConnection()) {
|
---|
163 | AfxMessageBox("Connection Error");
|
---|
164 | goto ParamError;
|
---|
165 | }
|
---|
166 |
|
---|
167 | return (TRUE);
|
---|
168 |
|
---|
169 | ParamError:
|
---|
170 | if (m_bModeNew) {
|
---|
171 | gConnections.Remove(sConnectionName); //Remove New Connection
|
---|
172 | Connection(NULL);
|
---|
173 | }
|
---|
174 | return (FALSE);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /////////////////////////////////////////////////////////////////////////////
|
---|
178 | // CDlgRemoteParameters message handlers
|
---|
179 |
|
---|
180 |
|
---|
181 |
|
---|
182 | void CDlgRemoteParameters::OnCancel()
|
---|
183 | {
|
---|
184 | // TODO: Add extra cleanup here
|
---|
185 |
|
---|
186 | CDialog::OnCancel();
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 |
|
---|
191 | BOOL CDlgRemoteParameters::OnInitDialog()
|
---|
192 | {
|
---|
193 | CDialog::OnInitDialog();
|
---|
194 |
|
---|
195 | // TODO: Add extra initialization here
|
---|
196 |
|
---|
197 | if (!m_bModeNew) {
|
---|
198 | ShowParameters();
|
---|
199 | }
|
---|
200 |
|
---|
201 | return TRUE; // return TRUE unless you set the focus to a control
|
---|
202 | // EXCEPTION: OCX Property Pages should return FALSE
|
---|
203 | }
|
---|
204 |
|
---|
205 | void CDlgRemoteParameters::OnOKey()
|
---|
206 | {
|
---|
207 | if (SaveParameters()) {
|
---|
208 | CDialog::OnOK();
|
---|
209 | }
|
---|
210 |
|
---|
211 | }
|
---|