/[H7]/trunk/Client/ClientDlg.cpp
ViewVC logotype

Annotation of /trunk/Client/ClientDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 37 - (hide annotations) (download)
Thu Feb 1 09:23:20 2007 UTC (17 years, 4 months ago) by kevin
File size: 9236 byte(s)
Fjernet update tråden i mine 3 led knapper, og pillet lidt ved gui
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 30 //Globale variabler osv. :-S
14 kevin 37 CString Ip, Delay;
15 kevin 12 unsigned long Port;
16 kevin 16 UINT Status( LPVOID Param );
17 kevin 30 UINT Update( LPVOID Param );
18     void Buffcheck(CString);
19 kevin 16 void Start();
20     CButton m_led1;
21     CButton m_led2;
22     CButton m_led3;
23 kevin 30 CButton m_knap1;
24     CButton m_knap2;
25     CEdit m_temp;
26     CEdit m_pot;
27 kevin 4
28     // CAboutDlg dialog used for App About
29    
30 kevin 32 bool continueThread;
31    
32 kevin 4 class CAboutDlg : public CDialog
33     {
34     public:
35     CAboutDlg();
36    
37     // Dialog Data
38     enum { IDD = IDD_ABOUTBOX };
39    
40     protected:
41     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
42    
43     // Implementation
44     protected:
45     DECLARE_MESSAGE_MAP()
46     };
47    
48     CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
49     {
50     }
51    
52     void CAboutDlg::DoDataExchange(CDataExchange* pDX)
53     {
54     CDialog::DoDataExchange(pDX);
55     }
56    
57     BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
58     END_MESSAGE_MAP()
59    
60    
61     // CClientDlg dialog
62    
63    
64     CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
65     : CDialog(CClientDlg::IDD, pParent)
66     {
67     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
68     }
69    
70     void CClientDlg::DoDataExchange(CDataExchange* pDX)
71     {
72     CDialog::DoDataExchange(pDX);
73     DDX_Control(pDX, IDC_LED1, m_led1);
74     DDX_Control(pDX, IDC_LED2, m_led2);
75     DDX_Control(pDX, IDC_LED3, m_led3);
76     DDX_Control(pDX, IDC_KNAP1, m_knap1);
77     DDX_Control(pDX, IDC_KNAP2, m_knap2);
78     DDX_Control(pDX, IDC_TEMP, m_temp);
79     DDX_Control(pDX, IDC_POT, m_pot);
80     DDX_Control(pDX, IDC_IPADDRESS1, m_ip);
81     DDX_Control(pDX, IDC_PORT, m_port);
82 kevin 16 DDX_Control(pDX, IDC_DELAY, m_delay);
83 kevin 4 }
84    
85     BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
86     ON_WM_SYSCOMMAND()
87     ON_WM_PAINT()
88     ON_WM_QUERYDRAGICON()
89     //}}AFX_MSG_MAP
90     ON_BN_CLICKED(IDC_IPPORT, OnBnClickedIpport)
91 kevin 30 ON_BN_CLICKED(IDC_LED1, OnBnClickedLed1)
92     ON_BN_CLICKED(IDC_LED2, OnBnClickedLed2)
93     ON_BN_CLICKED(IDC_LED3, OnBnClickedLed3)
94 kevin 32 ON_BN_CLICKED(IDOK, OnBnClickedOk)
95     ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
96 kevin 4 END_MESSAGE_MAP()
97    
98    
99     // CClientDlg message handlers
100 kevin 9 /*Client socket class*/
101     class ClientSocket : public CSocket{
102     public:
103     ClientSocket() { };
104     virtual void OnReceive(int nErrorCode);
105     };
106 kevin 4 BOOL CClientDlg::OnInitDialog()
107     {
108     CDialog::OnInitDialog();
109    
110     // Add "About..." menu item to system menu.
111    
112     // IDM_ABOUTBOX must be in the system command range.
113     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
114     ASSERT(IDM_ABOUTBOX < 0xF000);
115    
116     CMenu* pSysMenu = GetSystemMenu(FALSE);
117     if (pSysMenu != NULL)
118     {
119     CString strAboutMenu;
120     strAboutMenu.LoadString(IDS_ABOUTBOX);
121     if (!strAboutMenu.IsEmpty())
122     {
123     pSysMenu->AppendMenu(MF_SEPARATOR);
124     pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
125     }
126     }
127    
128     // Set the icon for this dialog. The framework does this automatically
129     // when the application's main window is not a dialog
130     SetIcon(m_hIcon, TRUE); // Set big icon
131     SetIcon(m_hIcon, FALSE); // Set small icon
132    
133     // TODO: Add extra initialization here
134 kevin 9
135     // Get data from regedit
136 kevin 4 CString strip, strport;
137 kevin 12 char chaip[16];
138 kevin 16 char chadelay[16];
139 kevin 4 CRegKey reg;
140     unsigned long size = 16;
141 kevin 9 LONG res=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
142 kevin 4
143     if (res == ERROR_SUCCESS)
144     {
145 kevin 12 res=reg.QueryStringValue("IP", chaip, &size);
146 kevin 4 }
147 kevin 32
148    
149 kevin 12 Ip = chaip;
150 kevin 4 m_ip.SetWindowText(Ip);
151 kevin 9 LONG res2=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
152 kevin 4
153     if (res2 == ERROR_SUCCESS)
154     {
155 kevin 12 res2=reg.QueryDWORDValue("Port", Port);
156 kevin 4 }
157 kevin 12 strport.Format("%d",Port);
158     m_port.SetWindowText(strport);
159 kevin 16
160     LONG res3=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ);
161    
162     if (res3 == ERROR_SUCCESS)
163     {
164     res3=reg.QueryStringValue("Delay",chadelay,&size);
165     }
166     Delay = chadelay;
167     m_delay.SetWindowText(Delay);
168 kevin 4 reg.Close();
169    
170 kevin 9 //connect til server
171 kevin 16 Start();
172 kevin 32 continueThread = true;
173     AfxBeginThread(Status,0,THREAD_PRIORITY_NORMAL,0,0,NULL);
174 kevin 16
175 kevin 4
176     return TRUE; // return TRUE unless you set the focus to a control
177     }
178    
179     void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
180     {
181     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
182     {
183     CAboutDlg dlgAbout;
184     dlgAbout.DoModal();
185     }
186     else
187     {
188     CDialog::OnSysCommand(nID, lParam);
189     }
190     }
191    
192     // If you add a minimize button to your dialog, you will need the code below
193     // to draw the icon. For MFC applications using the document/view model,
194     // this is automatically done for you by the framework.
195    
196     void CClientDlg::OnPaint()
197     {
198     if (IsIconic())
199     {
200     CPaintDC dc(this); // device context for painting
201    
202     SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
203    
204     // Center icon in client rectangle
205     int cxIcon = GetSystemMetrics(SM_CXICON);
206     int cyIcon = GetSystemMetrics(SM_CYICON);
207     CRect rect;
208     GetClientRect(&rect);
209     int x = (rect.Width() - cxIcon + 1) / 2;
210     int y = (rect.Height() - cyIcon + 1) / 2;
211    
212     // Draw the icon
213     dc.DrawIcon(x, y, m_hIcon);
214     }
215     else
216     {
217     CDialog::OnPaint();
218     }
219     }
220    
221     // The system calls this function to obtain the cursor to display while the user drags
222     // the minimized window.
223     HCURSOR CClientDlg::OnQueryDragIcon()
224     {
225     return static_cast<HCURSOR>(m_hIcon);
226     }
227    
228    
229 kevin 9 ClientSocket sockClient;
230 kevin 30 CString strIp, strPort, buff;
231 kevin 9
232     /*Clientsocket class used for client socket connections*/
233     void ClientSocket::OnReceive( int nErrorCode )
234     {
235     char buffer[4096];
236     int size;
237 kevin 30
238 kevin 9 size = Receive(buffer,4095);
239     buffer[size] = 0;
240 kevin 16 buff = buffer;
241 kevin 32
242     OutputDebugString(buff + "\n");
243    
244     while (1) //homebrewed tokenizer
245     {
246     int pos = buff.Find('|',0);
247     if (pos != -1)
248     {
249     CString token = buff.Left(pos);
250     buff = buff.Right( buff.GetLength() - pos -1);
251     Buffcheck(token);
252     }
253     else
254     break;
255     }
256 kevin 9 }
257 kevin 4 void CClientDlg::OnBnClickedIpport()
258     {
259 kevin 9 //Set new port and ip for the server in regedit
260     m_ip.GetWindowText(strIp);
261     m_port.GetWindowText(strPort);
262 kevin 16 m_delay.GetWindowText(Delay);
263     int test;
264     test = atoi(Delay);
265 kevin 37 if(test < 10000 && test > 500)
266 kevin 16 {
267 kevin 4 CRegKey reg;
268 kevin 9 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
269 kevin 4 reg.SetStringValue("","Project-data");
270 kevin 9 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
271     reg.SetStringValue("IP",strIp);
272     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
273 kevin 12 reg.SetDWORDValue("Port",atoi(strPort));
274 kevin 16 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client");
275     reg.SetStringValue("Delay",Delay);
276 kevin 4 reg.Close();
277 kevin 16 }
278     else
279     {
280     MessageBox("fejl i delay",0,MB_OK);
281     }
282 kevin 4 }
283 kevin 9
284     void Start()
285     {
286 kevin 16 sockClient.Create();
287     if (sockClient.Connect(Ip,Port))
288     {
289 kevin 30
290 kevin 16 }
291     else
292     {
293     int error = GetLastError();
294     CString tmp;
295     tmp.Format("%d", error);
296     }
297 kevin 9 }
298 kevin 16 UINT Status( LPVOID Param )
299 kevin 9 {
300 kevin 32 while(continueThread)
301     {
302     CString status;
303     int statusint = 100;
304     status.Format("%d",statusint);
305     sockClient.Send(status,status.GetLength(),NULL);
306     Sleep(atoi(Delay));
307     }
308 kevin 16 return TRUE;
309 kevin 9 }
310 kevin 32 void Buffcheck(CString data)
311 kevin 30 {
312 kevin 32 int intbuff=0, temp, pot;
313     intbuff = atoi(data);
314    
315     temp = intbuff; // til temperatur case
316     pot = intbuff; // til potmeter case
317 kevin 30 switch (intbuff)
318     {
319     case 110: /*Led 1 Slukket*/
320     {
321     m_led1.SetCheck(0);
322     break;
323     }
324     case 111: /*Led 1 Tændt*/
325     {
326     m_led1.SetCheck(1);
327     break;
328     }
329     case 120: /*Led 2 Slukket*/
330     {
331     m_led2.SetCheck(0);
332     break;
333     }
334     case 121: /*Led 2 Tændt*/
335     {
336     m_led2.SetCheck(1);
337     break;
338     }
339     case 130: /*Led 3 Slukket*/
340     {
341     m_led3.SetCheck(0);
342     break;
343     }
344     case 131: /*Led 3 Tændt*/
345     {
346     m_led3.SetCheck(1);
347     break;
348     }
349     case 140: /*Knap 1 off*/
350     {
351     m_knap1.SetCheck(0);
352     break;
353     }
354     case 141: /*Knap 1 on*/
355     {
356     m_knap1.SetCheck(1);
357     break;
358     }
359     case 150: /*Knap 2 off*/
360     {
361     m_knap2.SetCheck(0);
362     break;
363     }
364     case 151: /*Knap 2 on*/
365     {
366     m_knap2.SetCheck(1);
367     break;
368     }
369     }
370     if (temp > 3999 && temp < 5000) //skriv temperaturen
371     {
372     CString strtemp;
373     temp = (temp-4000);
374     strtemp.Format("%d",temp);
375     m_temp.SetWindowText(strtemp);
376     }
377     if (pot > 1999 && pot < 3050) //skriv potmeter værdien
378     {
379     CString strpot;
380     pot = (pot-2000);
381     strpot.Format("%d",pot);
382 kevin 32 m_pot.SetWindowText(strpot);
383 kevin 30 }
384    
385     }
386     void CClientDlg::OnBnClickedLed1()
387     {
388 kevin 37 CString Updatevariable;
389 kevin 30 int led, code, send;
390     led = 110;
391     code = m_led1.GetCheck();
392     send = (led+code);
393     Updatevariable.Format("%d",send);
394 kevin 37 sockClient.Send(Updatevariable,Updatevariable.GetLength(),NULL);
395 kevin 30 }
396    
397     void CClientDlg::OnBnClickedLed2()
398     {
399 kevin 37 CString Updatevariable;
400 kevin 30 int led, code, send;
401     led = 120;
402     code = m_led2.GetCheck();
403     send = (led+code);
404     Updatevariable.Format("%d",send);
405 kevin 37 sockClient.Send(Updatevariable,Updatevariable.GetLength(),NULL);
406 kevin 30 }
407    
408     void CClientDlg::OnBnClickedLed3()
409     {
410 kevin 37 CString Updatevariable;
411 kevin 30 int led, code, send;
412     led = 130;
413     code = m_led3.GetCheck();
414     send = (led+code);
415     Updatevariable.Format("%d",send);
416 kevin 37 sockClient.Send(Updatevariable,Updatevariable.GetLength(),NULL);
417 kevin 30 }
418 kevin 32
419     void CClientDlg::OnBnClickedOk()
420     {
421     sockClient.Close();
422     continueThread = false;
423     OnOK();
424     }
425    
426     void CClientDlg::OnBnClickedCancel()
427     {
428     sockClient.Close();
429     continueThread = false;
430     OnCancel();
431     }

  ViewVC Help
Powered by ViewVC 1.1.20