1 |
kevin |
4 |
// ClientDlg.cpp : implementation file
|
2 |
|
|
//
|
3 |
|
|
|
4 |
|
|
#include "stdafx.h"
|
5 |
|
|
#include "Client.h"
|
6 |
|
|
#include "ClientDlg.h"
|
7 |
|
|
#include ".\clientdlg.h"
|
8 |
|
|
|
9 |
|
|
#ifdef _DEBUG
|
10 |
|
|
#define new DEBUG_NEW
|
11 |
|
|
#endif
|
12 |
|
|
|
13 |
kevin |
12 |
|
14 |
kevin |
16 |
CString Ip, Delay;
|
15 |
kevin |
12 |
unsigned long Port;
|
16 |
kevin |
16 |
UINT Status( LPVOID Param );
|
17 |
|
|
void Start();
|
18 |
|
|
void startthread();
|
19 |
|
|
CButton m_led1;
|
20 |
|
|
CButton m_led2;
|
21 |
|
|
CButton m_led3;
|
22 |
kevin |
4 |
|
23 |
|
|
// CAboutDlg dialog used for App About
|
24 |
|
|
|
25 |
|
|
class CAboutDlg : public CDialog
|
26 |
|
|
{
|
27 |
|
|
public:
|
28 |
|
|
CAboutDlg();
|
29 |
|
|
|
30 |
|
|
// Dialog Data
|
31 |
|
|
enum { IDD = IDD_ABOUTBOX };
|
32 |
|
|
|
33 |
|
|
protected:
|
34 |
|
|
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
|
35 |
|
|
|
36 |
|
|
// Implementation
|
37 |
|
|
protected:
|
38 |
|
|
DECLARE_MESSAGE_MAP()
|
39 |
|
|
};
|
40 |
|
|
|
41 |
|
|
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
|
42 |
|
|
{
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
|
46 |
|
|
{
|
47 |
|
|
CDialog::DoDataExchange(pDX);
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
|
51 |
|
|
END_MESSAGE_MAP()
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
// CClientDlg dialog
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
|
58 |
|
|
: CDialog(CClientDlg::IDD, pParent)
|
59 |
|
|
{
|
60 |
|
|
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
void CClientDlg::DoDataExchange(CDataExchange* pDX)
|
64 |
|
|
{
|
65 |
|
|
CDialog::DoDataExchange(pDX);
|
66 |
|
|
DDX_Control(pDX, IDC_LED1, m_led1);
|
67 |
|
|
DDX_Control(pDX, IDC_LED2, m_led2);
|
68 |
|
|
DDX_Control(pDX, IDC_LED3, m_led3);
|
69 |
|
|
DDX_Control(pDX, IDC_KNAP1, m_knap1);
|
70 |
|
|
DDX_Control(pDX, IDC_KNAP2, m_knap2);
|
71 |
|
|
DDX_Control(pDX, IDC_TEMP, m_temp);
|
72 |
|
|
DDX_Control(pDX, IDC_POT, m_pot);
|
73 |
|
|
DDX_Control(pDX, IDC_IPADDRESS1, m_ip);
|
74 |
|
|
DDX_Control(pDX, IDC_PORT, m_port);
|
75 |
kevin |
16 |
DDX_Control(pDX, IDC_DELAY, m_delay);
|
76 |
kevin |
4 |
}
|
77 |
|
|
|
78 |
|
|
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
|
79 |
|
|
ON_WM_SYSCOMMAND()
|
80 |
|
|
ON_WM_PAINT()
|
81 |
|
|
ON_WM_QUERYDRAGICON()
|
82 |
|
|
//}}AFX_MSG_MAP
|
83 |
|
|
ON_BN_CLICKED(IDC_IPPORT, OnBnClickedIpport)
|
84 |
kevin |
9 |
ON_BN_CLICKED(IDC_Connect, OnBnClickedConnect)
|
85 |
kevin |
4 |
END_MESSAGE_MAP()
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
// CClientDlg message handlers
|
89 |
kevin |
9 |
/*Client socket class*/
|
90 |
|
|
class ClientSocket : public CSocket{
|
91 |
|
|
public:
|
92 |
|
|
ClientSocket() { };
|
93 |
|
|
virtual void OnReceive(int nErrorCode);
|
94 |
|
|
virtual void OnSend(int nErrorCode);
|
95 |
|
|
};
|
96 |
kevin |
4 |
BOOL CClientDlg::OnInitDialog()
|
97 |
|
|
{
|
98 |
|
|
CDialog::OnInitDialog();
|
99 |
|
|
|
100 |
|
|
// Add "About..." menu item to system menu.
|
101 |
|
|
|
102 |
|
|
// IDM_ABOUTBOX must be in the system command range.
|
103 |
|
|
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
|
104 |
|
|
ASSERT(IDM_ABOUTBOX < 0xF000);
|
105 |
|
|
|
106 |
|
|
CMenu* pSysMenu = GetSystemMenu(FALSE);
|
107 |
|
|
if (pSysMenu != NULL)
|
108 |
|
|
{
|
109 |
|
|
CString strAboutMenu;
|
110 |
|
|
strAboutMenu.LoadString(IDS_ABOUTBOX);
|
111 |
|
|
if (!strAboutMenu.IsEmpty())
|
112 |
|
|
{
|
113 |
|
|
pSysMenu->AppendMenu(MF_SEPARATOR);
|
114 |
|
|
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
// Set the icon for this dialog. The framework does this automatically
|
119 |
|
|
// when the application's main window is not a dialog
|
120 |
|
|
SetIcon(m_hIcon, TRUE); // Set big icon
|
121 |
|
|
SetIcon(m_hIcon, FALSE); // Set small icon
|
122 |
|
|
|
123 |
|
|
// TODO: Add extra initialization here
|
124 |
kevin |
9 |
|
125 |
|
|
// Get data from regedit
|
126 |
kevin |
4 |
CString strip, strport;
|
127 |
kevin |
12 |
char chaip[16];
|
128 |
kevin |
16 |
char chadelay[16];
|
129 |
kevin |
4 |
CRegKey reg;
|
130 |
|
|
unsigned long size = 16;
|
131 |
kevin |
9 |
LONG res=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
132 |
kevin |
4 |
|
133 |
|
|
if (res == ERROR_SUCCESS)
|
134 |
|
|
{
|
135 |
kevin |
12 |
res=reg.QueryStringValue("IP", chaip, &size);
|
136 |
kevin |
4 |
}
|
137 |
kevin |
12 |
Ip = chaip;
|
138 |
kevin |
4 |
m_ip.SetWindowText(Ip);
|
139 |
kevin |
9 |
LONG res2=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
140 |
kevin |
4 |
|
141 |
|
|
if (res2 == ERROR_SUCCESS)
|
142 |
|
|
{
|
143 |
kevin |
12 |
res2=reg.QueryDWORDValue("Port", Port);
|
144 |
kevin |
4 |
}
|
145 |
kevin |
12 |
strport.Format("%d",Port);
|
146 |
|
|
m_port.SetWindowText(strport);
|
147 |
kevin |
16 |
|
148 |
|
|
LONG res3=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
|
149 |
|
|
|
150 |
|
|
if (res3 == ERROR_SUCCESS)
|
151 |
|
|
{
|
152 |
|
|
res3=reg.QueryStringValue("Delay",chadelay,&size);
|
153 |
|
|
}
|
154 |
|
|
Delay = chadelay;
|
155 |
|
|
m_delay.SetWindowText(Delay);
|
156 |
kevin |
4 |
reg.Close();
|
157 |
|
|
|
158 |
kevin |
9 |
//connect til server
|
159 |
kevin |
16 |
Start();
|
160 |
|
|
|
161 |
kevin |
4 |
|
162 |
|
|
return TRUE; // return TRUE unless you set the focus to a control
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
|
166 |
|
|
{
|
167 |
|
|
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
168 |
|
|
{
|
169 |
|
|
CAboutDlg dlgAbout;
|
170 |
|
|
dlgAbout.DoModal();
|
171 |
|
|
}
|
172 |
|
|
else
|
173 |
|
|
{
|
174 |
|
|
CDialog::OnSysCommand(nID, lParam);
|
175 |
|
|
}
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
// If you add a minimize button to your dialog, you will need the code below
|
179 |
|
|
// to draw the icon. For MFC applications using the document/view model,
|
180 |
|
|
// this is automatically done for you by the framework.
|
181 |
|
|
|
182 |
|
|
void CClientDlg::OnPaint()
|
183 |
|
|
{
|
184 |
|
|
if (IsIconic())
|
185 |
|
|
{
|
186 |
|
|
CPaintDC dc(this); // device context for painting
|
187 |
|
|
|
188 |
|
|
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
189 |
|
|
|
190 |
|
|
// Center icon in client rectangle
|
191 |
|
|
int cxIcon = GetSystemMetrics(SM_CXICON);
|
192 |
|
|
int cyIcon = GetSystemMetrics(SM_CYICON);
|
193 |
|
|
CRect rect;
|
194 |
|
|
GetClientRect(&rect);
|
195 |
|
|
int x = (rect.Width() - cxIcon + 1) / 2;
|
196 |
|
|
int y = (rect.Height() - cyIcon + 1) / 2;
|
197 |
|
|
|
198 |
|
|
// Draw the icon
|
199 |
|
|
dc.DrawIcon(x, y, m_hIcon);
|
200 |
|
|
}
|
201 |
|
|
else
|
202 |
|
|
{
|
203 |
|
|
CDialog::OnPaint();
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
// The system calls this function to obtain the cursor to display while the user drags
|
208 |
|
|
// the minimized window.
|
209 |
|
|
HCURSOR CClientDlg::OnQueryDragIcon()
|
210 |
|
|
{
|
211 |
|
|
return static_cast<HCURSOR>(m_hIcon);
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
|
215 |
kevin |
9 |
ClientSocket sockClient;
|
216 |
|
|
CString strIp, strPort;
|
217 |
|
|
|
218 |
|
|
/*Clientsocket class used for client socket connections*/
|
219 |
|
|
void ClientSocket::OnReceive( int nErrorCode )
|
220 |
|
|
{
|
221 |
|
|
char buffer[4096];
|
222 |
|
|
int size;
|
223 |
kevin |
16 |
CString buff;
|
224 |
kevin |
9 |
|
225 |
|
|
size = Receive(buffer,4095);
|
226 |
|
|
buffer[size] = 0;
|
227 |
kevin |
16 |
MessageBox(0,buffer,0,MB_OK);
|
228 |
|
|
buff = buffer;
|
229 |
|
|
if( buff == "Tændt" )
|
230 |
|
|
{
|
231 |
|
|
char test[10];
|
232 |
|
|
int i;
|
233 |
|
|
i = m_led1.GetCheck();
|
234 |
|
|
// _itot(i,test,10); Fejltjeck
|
235 |
|
|
// MessageBox(0,test,0,MB_OK);
|
236 |
|
|
if(i == 0)
|
237 |
|
|
{
|
238 |
|
|
m_led1.SetCheck(1);
|
239 |
|
|
}
|
240 |
|
|
else
|
241 |
|
|
{
|
242 |
|
|
m_led1.SetCheck(0);
|
243 |
|
|
}
|
244 |
|
|
}
|
245 |
|
|
else
|
246 |
|
|
{
|
247 |
|
|
MessageBox(0,"FEJL i onrecive",0,MB_OK);
|
248 |
|
|
}
|
249 |
kevin |
9 |
Close();
|
250 |
|
|
}
|
251 |
|
|
void ClientSocket::OnSend( int nErrorCode )
|
252 |
|
|
{
|
253 |
|
|
// CString sendnu; skal ikke sende nu
|
254 |
|
|
|
255 |
|
|
// sendnu = Sendtekst; skal ikke sende nu
|
256 |
|
|
|
257 |
|
|
// Send(sendnu,sendnu.GetLength()); skal ikke sende nu
|
258 |
|
|
}
|
259 |
kevin |
4 |
void CClientDlg::OnBnClickedIpport()
|
260 |
|
|
{
|
261 |
kevin |
9 |
//Set new port and ip for the server in regedit
|
262 |
|
|
m_ip.GetWindowText(strIp);
|
263 |
|
|
m_port.GetWindowText(strPort);
|
264 |
kevin |
16 |
m_delay.GetWindowText(Delay);
|
265 |
|
|
int test;
|
266 |
|
|
test = atoi(Delay);
|
267 |
|
|
if(test < 10000 && test > 100)
|
268 |
|
|
{
|
269 |
kevin |
4 |
CRegKey reg;
|
270 |
kevin |
9 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
271 |
kevin |
4 |
reg.SetStringValue("","Project-data");
|
272 |
kevin |
9 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
273 |
|
|
reg.SetStringValue("IP",strIp);
|
274 |
|
|
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
275 |
kevin |
12 |
reg.SetDWORDValue("Port",atoi(strPort));
|
276 |
kevin |
16 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
|
277 |
|
|
reg.SetStringValue("Delay",Delay);
|
278 |
kevin |
4 |
reg.Close();
|
279 |
kevin |
16 |
}
|
280 |
|
|
else
|
281 |
|
|
{
|
282 |
|
|
MessageBox("fejl i delay",0,MB_OK);
|
283 |
|
|
}
|
284 |
kevin |
4 |
}
|
285 |
kevin |
9 |
|
286 |
|
|
void Start()
|
287 |
|
|
{
|
288 |
kevin |
16 |
sockClient.Create();
|
289 |
|
|
if (sockClient.Connect(Ip,Port))
|
290 |
|
|
{
|
291 |
|
|
//sockClient.Send(strPort,strPort.GetLength(),NULL);
|
292 |
|
|
//sockClient.Send(data,data.GetLength(),NULL);
|
293 |
|
|
//sockClient.Close();
|
294 |
|
|
}
|
295 |
|
|
else
|
296 |
|
|
{
|
297 |
|
|
int error = GetLastError();
|
298 |
|
|
CString tmp;
|
299 |
|
|
tmp.Format("%d", error);
|
300 |
|
|
}
|
301 |
kevin |
9 |
}
|
302 |
|
|
void CClientDlg::OnBnClickedConnect()
|
303 |
kevin |
16 |
{
|
304 |
|
|
|
305 |
|
|
}
|
306 |
|
|
UINT Status( LPVOID Param )
|
307 |
kevin |
9 |
{
|
308 |
kevin |
16 |
CString status;
|
309 |
|
|
status = "RB1";
|
310 |
|
|
Sleep(atoi(Delay));
|
311 |
|
|
sockClient.Send(status,status.GetLength(),NULL);
|
312 |
|
|
startthread();
|
313 |
|
|
return TRUE;
|
314 |
kevin |
9 |
}
|
315 |
kevin |
16 |
void startthread()
|
316 |
|
|
{
|
317 |
|
|
AfxBeginThread(Status,0,THREAD_PRIORITY_NORMAL,0,0,NULL);
|
318 |
|
|
} |