/[H7]/trunk/H7 Server/H7 ServerDlg.cpp
ViewVC logotype

Contents of /trunk/H7 Server/H7 ServerDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 64 - (show annotations) (download)
Tue Feb 6 14:19:39 2007 UTC (17 years, 3 months ago) by hedin
File size: 9083 byte(s)
Rewritten Baud-Rate function...
1 // H7 ServerDlg.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "H7 Server.h"
6 #include "H7 ServerDlg.h"
7 #include ".\h7 serverdlg.h"
8 #include <vector>
9 #include "Define.h"
10
11 #ifdef _DEBUG
12 #define new DEBUG_NEW
13 #endif
14
15
16 void WriteLastError(char *format)
17 {
18 LPVOID lpMsgBuf;
19 FormatMessage(
20 FORMAT_MESSAGE_ALLOCATE_BUFFER |
21 FORMAT_MESSAGE_FROM_SYSTEM |
22 FORMAT_MESSAGE_IGNORE_INSERTS,
23 NULL,
24 GetLastError(),
25 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
26 (LPTSTR) &lpMsgBuf,
27 0,
28 NULL
29 );
30 printf(format, lpMsgBuf);
31 CString Tmp;
32 Tmp.Format( "%s", lpMsgBuf);
33 MessageBox(0,Tmp,"",MB_OK);
34 }
35
36 std::vector<CString> GetAvailableComPorts()
37 {
38 std::vector<CString> ports;
39
40 for (int i = 1; i < 20; i++)
41 {
42 CString port;
43 port.Format("COM%d", i);
44
45
46 HANDLE handle = CreateFile(port,
47 GENERIC_READ | GENERIC_WRITE,
48 0,
49 0,
50 OPEN_EXISTING,
51 0,
52 NULL);
53
54 if (handle != INVALID_HANDLE_VALUE)
55 {
56 CloseHandle(handle);
57 ports.push_back(port);
58 }
59 }
60 return ports;
61 }
62
63 // CAboutDlg dialog used for App About
64
65 class CAboutDlg : public CDialog
66 {
67 public:
68 CAboutDlg();
69
70 // Dialog Data
71 enum { IDD = IDD_ABOUTBOX };
72
73 protected:
74 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
75
76 // Implementation
77 protected:
78 DECLARE_MESSAGE_MAP()
79 };
80
81 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
82 {
83 }
84
85 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
86 {
87 CDialog::DoDataExchange(pDX);
88 }
89
90 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
91 END_MESSAGE_MAP()
92
93
94 // CH7ServerDlg dialog
95
96
97
98 CH7ServerDlg::CH7ServerDlg(CWnd* pParent /*=NULL*/)
99 : CDialog(CH7ServerDlg::IDD, pParent)
100 , m_Start(0)
101 {
102 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
103 }
104
105 void CH7ServerDlg::DoDataExchange(CDataExchange* pDX)
106 {
107 CDialog::DoDataExchange(pDX);
108 DDX_Control(pDX, IDC_STATUS, m_Status);
109 DDX_Control(pDX, IDC_BaudRate, m_BaudRate);
110 DDX_Control(pDX, IDC_SERIAL, m_Serial);
111 DDX_Control(pDX, IDC_STOP, m_Stop);
112 DDX_Control(pDX, IDC_PORT, m_Port);
113 DDX_Control(pDX, IDC_RESTART, m_Restart);
114 DDX_Control(pDX, IDC_Start, m_StartB);
115 }
116
117 BEGIN_MESSAGE_MAP(CH7ServerDlg, CDialog)
118 ON_WM_SYSCOMMAND()
119 ON_WM_PAINT()
120 ON_WM_QUERYDRAGICON()
121 //}}AFX_MSG_MAP
122 ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
123 ON_BN_CLICKED(IDC_TEST, OnBnClickedTest)
124 ON_BN_CLICKED(IDC_RESTART, OnBnClickedRestart)
125 ON_BN_CLICKED(IDC_STOP, OnBnClickedStop)
126 END_MESSAGE_MAP()
127
128
129 // CH7ServerDlg message handlers
130
131 BOOL CH7ServerDlg::OnInitDialog()
132 {
133 CDialog::OnInitDialog();
134
135 // Add "About..." menu item to system menu.
136
137 // IDM_ABOUTBOX must be in the system command range.
138 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
139 ASSERT(IDM_ABOUTBOX < 0xF000);
140
141 CMenu* pSysMenu = GetSystemMenu(FALSE);
142 if (pSysMenu != NULL)
143 {
144 CString strAboutMenu;
145 strAboutMenu.LoadString(IDS_ABOUTBOX);
146 if (!strAboutMenu.IsEmpty())
147 {
148 pSysMenu->AppendMenu(MF_SEPARATOR);
149 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
150 }
151 }
152
153 // Set the icon for this dialog. The framework does this automatically
154 // when the application's main window is not a dialog
155 SetIcon(m_hIcon, TRUE); // Set big icon
156 SetIcon(m_hIcon, FALSE); // Set small icon
157
158 /**********************************************************************/
159 // TODO: Add extra initialization here
160
161 // if there is no free com ports, the program shut down.
162 if (!ServerInit() )
163 {
164 OnOK();
165 return false;
166 }
167 m_Restart.EnableWindow(false);
168 m_Stop.EnableWindow(false);
169 //m_BaudRate.EnableWindow(false);
170 m_First = true;
171 /**********************************************************************/
172 return TRUE; // return TRUE unless you set the focus to a control
173 }
174
175 void CH7ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
176 {
177 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
178 {
179 CAboutDlg dlgAbout;
180 dlgAbout.DoModal();
181 }
182 else
183 {
184 CDialog::OnSysCommand(nID, lParam);
185 }
186 }
187
188 // If you add a minimize button to your dialog, you will need the code below
189 // to draw the icon. For MFC applications using the document/view model,
190 // this is automatically done for you by the framework.
191
192 void CH7ServerDlg::OnPaint()
193 {
194 if (IsIconic())
195 {
196 CPaintDC dc(this); // device context for painting
197
198 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
199
200 // Center icon in client rectangle
201 int cxIcon = GetSystemMetrics(SM_CXICON);
202 int cyIcon = GetSystemMetrics(SM_CYICON);
203 CRect rect;
204 GetClientRect(&rect);
205 int x = (rect.Width() - cxIcon + 1) / 2;
206 int y = (rect.Height() - cyIcon + 1) / 2;
207
208 // Draw the icon
209 dc.DrawIcon(x, y, m_hIcon);
210 }
211 else
212 {
213 CDialog::OnPaint();
214 }
215 }
216
217 // The system calls this function to obtain the cursor to display while the user drags
218 // the minimized window.
219 HCURSOR CH7ServerDlg::OnQueryDragIcon()
220 {
221 return static_cast<HCURSOR>(m_hIcon);
222 }
223
224 void CH7ServerDlg::OnBnClickedRestart()
225 {
226 StartSerial();
227 }
228
229 void CH7ServerDlg::OnBnClickedStart()
230 {
231 // Add's user defined Baud-Rate to regedit before the server start.
232 // CRegKey reg;
233 // CString Baud = "9600";
234 // m_BaudRate.GetWindowText(Baud);
235
236 // reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
237 // reg.SetDWORDValue("BaudRate", atoi(Baud) );
238
239 // StartTcp();
240 StartSerial();
241 m_Stop.EnableWindow(true);
242 m_Restart.EnableWindow(true);
243 m_BaudRate.EnableWindow(true);
244 m_StartB.EnableWindow(false);
245 }
246
247 int CH7ServerDlg::StartTcp(void)
248 {
249 // Get the TCP Port number from regedit.
250 CString Current_Port;
251 unsigned long Port;
252 m_Port.GetWindowText(Current_Port);
253 CRegKey reg;
254 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
255 reg.SetDWORDValue("Port",atoi(Current_Port) );
256 reg.QueryDWORDValue("Port", Port);
257 reg.Close();
258
259 // Restarts the TCP Connection
260 TcpServer.Close();
261
262 if (!TcpServer.Create(Port))
263 MessageBox("Crap");
264 if (!TcpServer.Listen())
265 MessageBox("Loads of crap");
266
267 // Prints TCP Port number to the status box.
268 CString tmp;
269 tmp.Format("%d", Port);
270 m_Port.SetWindowText(tmp);
271
272 return 0;
273 }
274
275 void CH7ServerDlg::OnBnClickedTest()
276 {
277 //CWaitCursor wait;
278 //UpdateClient();
279 }
280
281
282 int CH7ServerDlg::StartSerial(void)
283 {
284 int Baud;
285 int dBaud;
286 CString SerialPort;
287
288 Baud = m_BaudRate.GetCurSel();
289 m_Serial.GetWindowText(SerialPort);
290
291 switch (Baud)
292 {
293 case 0:
294 dBaud = 1200;
295 break;
296 case 1:
297 dBaud = 2400;
298 break;
299 case 2:
300 dBaud = 4800;
301 break;
302 case 3:
303 dBaud = 9600;
304 break;
305 case 4:
306 dBaud = 19200;
307 break;
308
309 default: //this should not be possible, but ...
310 dBaud = 9600;
311 break;
312 }
313
314 if( H7Serial.isOpen() ){
315 try
316 {
317 H7Serial.writeTarget(10, Baud);
318 }
319 catch (std::exception e)
320 {
321 MessageBox( "Pic could not use selected baud rate." );
322 return 0;
323 }
324 H7Serial.close();
325 H7Serial.open( SerialPort, dBaud );
326 }
327 else {
328 H7Serial.open( SerialPort, dBaud );
329 }
330
331 Sleep(50);
332
333
334 try {
335 int pot = H7Serial.readTarget(5);
336 CString debug;
337 debug.Format("%d", pot);
338 MessageBox(debug);
339 }
340 catch (...)
341 {
342 MessageBox("Major fuck-up");
343 }
344
345
346 return 0;
347 }
348
349 void CH7ServerDlg::UpdateClient(TcpClientClass* caller)
350 {
351 // Updater status vindue.
352 m_Status.SetWindowText("");
353 UpdateStatus( "LED3: ", caller->mLed3 );
354 UpdateStatus( "LED4: ", caller->mLed4 );
355 UpdateStatus( "LED5: ",caller->mLed5 );
356 UpdateStatus( "Switch2: ",caller->mSw2 );
357 UpdateStatus( "Switch3: ", caller->mSw3 );
358 UpdateStatus( "Potmeter: ", caller->mPot );
359 UpdateStatus( "Temperatur: ", caller->mTemp );
360
361 }
362
363 void CH7ServerDlg::NewLine(void)
364 {
365 // Laver linieskift i Status vinduet.
366 CString nLine = "";
367 m_Status.GetWindowText(nLine);
368 if( nLine.GetLength() != 0 )
369 nLine += "\r\n";
370 m_Status.SetWindowText(nLine);
371 }
372
373 CString CH7ServerDlg::OnOff(int value)
374 {
375 // undersøger om LED3-5 er tændt eller slukket.
376 CString Test;
377 if( value == 0 )
378 Test = "Slukket";
379 else
380 Test = "Tændt";
381
382 return CString(Test);
383 }
384
385 void CH7ServerDlg::UpdateStatus(CString name, short value)
386 {
387 CString tmp,StatusWindow;
388 tmp.Format( "%d", value );
389 m_Status.GetWindowText(StatusWindow);
390 StatusWindow += name;
391 StatusWindow += tmp;
392 m_Status.SetWindowText(StatusWindow);
393 NewLine();
394 }
395
396 void CH7ServerDlg::OnBnClickedStop()
397 {
398 // Close TCP and Sreial connections
399 TcpServer.Close();
400 H7Serial.close();
401 // Close server app.
402 OnOK();
403 }
404
405 void CH7ServerDlg::OnCancel()
406 {
407 TcpServer.Close();
408 H7Serial.close();
409 // Close server app.
410
411 CDialog::OnCancel();
412 }
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458

  ViewVC Help
Powered by ViewVC 1.1.20