/[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 28 - (show annotations) (download)
Wed Jan 31 12:42:51 2007 UTC (17 years, 3 months ago) by hedin
File size: 9889 byte(s)
Communication established between PIC and client:D
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 }
115
116 BEGIN_MESSAGE_MAP(CH7ServerDlg, CDialog)
117 ON_WM_SYSCOMMAND()
118 ON_WM_PAINT()
119 ON_WM_QUERYDRAGICON()
120 //}}AFX_MSG_MAP
121 ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
122 ON_BN_CLICKED(IDC_TEST, OnBnClickedTest)
123 ON_BN_CLICKED(IDC_RESTART, OnBnClickedRestart)
124 END_MESSAGE_MAP()
125
126
127 // CH7ServerDlg message handlers
128
129 BOOL CH7ServerDlg::OnInitDialog()
130 {
131 CDialog::OnInitDialog();
132
133 // Add "About..." menu item to system menu.
134
135 // IDM_ABOUTBOX must be in the system command range.
136 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
137 ASSERT(IDM_ABOUTBOX < 0xF000);
138
139 CMenu* pSysMenu = GetSystemMenu(FALSE);
140 if (pSysMenu != NULL)
141 {
142 CString strAboutMenu;
143 strAboutMenu.LoadString(IDS_ABOUTBOX);
144 if (!strAboutMenu.IsEmpty())
145 {
146 pSysMenu->AppendMenu(MF_SEPARATOR);
147 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
148 }
149 }
150
151 // Set the icon for this dialog. The framework does this automatically
152 // when the application's main window is not a dialog
153 SetIcon(m_hIcon, TRUE); // Set big icon
154 SetIcon(m_hIcon, FALSE); // Set small icon
155
156 /**********************************************************************/
157 // TODO: Add extra initialization here
158
159 // if there is no free com ports, the program shut down.
160 if (!ServerInit() )
161 {
162 OnOK();
163 return false;
164 }
165
166 /**********************************************************************/
167 return TRUE; // return TRUE unless you set the focus to a control
168 }
169
170 void CH7ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
171 {
172 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
173 {
174 CAboutDlg dlgAbout;
175 dlgAbout.DoModal();
176 }
177 else
178 {
179 CDialog::OnSysCommand(nID, lParam);
180 }
181 }
182
183 // If you add a minimize button to your dialog, you will need the code below
184 // to draw the icon. For MFC applications using the document/view model,
185 // this is automatically done for you by the framework.
186
187 void CH7ServerDlg::OnPaint()
188 {
189 if (IsIconic())
190 {
191 CPaintDC dc(this); // device context for painting
192
193 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
194
195 // Center icon in client rectangle
196 int cxIcon = GetSystemMetrics(SM_CXICON);
197 int cyIcon = GetSystemMetrics(SM_CYICON);
198 CRect rect;
199 GetClientRect(&rect);
200 int x = (rect.Width() - cxIcon + 1) / 2;
201 int y = (rect.Height() - cyIcon + 1) / 2;
202
203 // Draw the icon
204 dc.DrawIcon(x, y, m_hIcon);
205 }
206 else
207 {
208 CDialog::OnPaint();
209 }
210 }
211
212 // The system calls this function to obtain the cursor to display while the user drags
213 // the minimized window.
214 HCURSOR CH7ServerDlg::OnQueryDragIcon()
215 {
216 return static_cast<HCURSOR>(m_hIcon);
217 }
218
219 void CH7ServerDlg::OnBnClickedStart()
220 {
221 // Add's user defined Baud-Rate to regedit before the server start.
222 CRegKey reg;
223 CString Baud;
224 m_BaudRate.GetWindowText(Baud);
225
226 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
227 reg.SetDWORDValue("BaudRate", atoi(Baud) );
228
229 StartTcp();
230 StartSerial();
231 }
232
233 int CH7ServerDlg::StartTcp(void)
234 {
235 // Get the TCP Port number from regedit.
236 unsigned long Port;
237 CRegKey reg;
238 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
239 reg.QueryDWORDValue("Port", Port);
240 reg.Close();
241
242 // Restats the TCP Connection
243 TcpServer.Close();
244
245 TcpServer.Create(Port);
246 TcpServer.Listen();
247
248 // Prints TCP Port number to the status box.
249 CString tmp;
250 tmp.Format("%d", Port);
251 m_Port.SetWindowText(tmp);
252
253 return 0;
254 }
255
256 void CH7ServerDlg::OnBnClickedRestart()
257 {
258 CWaitCursor wait; // Musen viser timeglas.
259 CString Port;
260 m_Port.GetWindowText(Port);
261 CRegKey reg;
262 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
263 reg.SetDWORDValue("Port", atoi(Port) );
264
265 CString tmp;
266 m_Serial.GetWindowText(tmp);
267 reg.SetStringValue("Serial",tmp);
268 reg.Close();
269 StartTcp();
270 StartSerial();
271 }
272
273 void CH7ServerDlg::OnBnClickedTest()
274 {
275
276 }
277 bool CH7ServerDlg::ServerInit(void)
278 {
279 unsigned int i;
280 CRegKey reg;
281 // Sets values in baud-rate combo box
282 m_BaudRate.AddString("1200");
283 m_BaudRate.AddString("2400");
284 m_BaudRate.AddString("4800"); // Don't work, used for error test.
285 m_BaudRate.AddString("9600");
286 m_BaudRate.AddString("19200");
287
288 unsigned long Baud;
289 CString GetBaud;
290 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
291 reg.QueryDWORDValue("BaudRate", Baud);
292
293 switch (Baud)
294 {
295 case 1200:
296 m_BaudRate.SetCurSel(0);
297 break;
298 case 2400:
299 m_BaudRate.SetCurSel(1);
300 break;
301 case 4800:
302 m_BaudRate.SetCurSel(2);
303 break;
304 case 9600:
305 m_BaudRate.SetCurSel(3);
306 break;
307 case 19200:
308 m_BaudRate.SetCurSel(4);
309 break;
310 default:
311 m_BaudRate.SetCurSel(3);
312 break;
313 }
314
315 m_BaudRate.GetWindowText(GetBaud);
316 reg.SetDWORDValue("BaudRate", atoi(GetBaud) );
317
318 // Checks how many COM ports there are.
319 std::vector<CString> Serial = GetAvailableComPorts();
320 for (i=0; i<Serial.size(); i++)
321 m_Serial.AddString(Serial[i]);
322
323 // Load saved settings from regedit.
324 unsigned long Port;
325 unsigned long BaudRate;
326
327 // Used to format the loaded values.
328 CString TPort;
329 CString TBaudRate;
330
331 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
332 reg.QueryDWORDValue("Port", Port);
333 reg.QueryDWORDValue("BaudRate", BaudRate);
334
335 char LoadCom[20];
336 unsigned long size = 20;
337 reg.QueryStringValue("Serial", LoadCom, &size);
338
339 TPort.Format("%d", Port);
340 TBaudRate.Format("%d", BaudRate);
341
342 m_Port.SetWindowText(TPort);
343 m_BaudRate.SetWindowText(TBaudRate);
344 m_Serial.SetWindowText(LoadCom);
345
346 if( Serial.size() == 0 ){
347 MessageBox( "No (free) serial port's found" );
348 return false;
349 }
350 // Checks for valid COM port in regedit.
351 for (i=0; i< Serial.size() ; i++)
352 {
353 if (LoadCom == Serial[i])
354 {
355 m_Serial.SetCurSel(i);
356 break;
357 }
358 }
359 if (i == Serial.size() )
360 {
361 m_Serial.SetCurSel(0);
362 reg.SetStringValue("Serial", Serial[0]);
363 }
364 reg.Close();
365 return true;
366 }
367
368 int CH7ServerDlg::StartSerial(void)
369 {
370 char SerialPort[6];
371 unsigned long size = 6;
372 unsigned long Baud;
373
374 CRegKey reg;
375
376
377 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
378
379 reg.QueryStringValue("Serial", SerialPort, &size);
380 reg.QueryDWORDValue("BaudRate", Baud);
381
382 // Starter Serial forbindelsen, hvis den ikke allerede er startet.
383 if (!H7Serial.isOpen())
384 H7Serial.open(SerialPort,Baud);
385
386 // Updater status vindue.
387 UpdateStatus( "LED3: ",H7Serial.readTarget(0) );
388 UpdateStatus( "LED4: ",H7Serial.readTarget(1) );
389 UpdateStatus( "LED5: ",H7Serial.readTarget(2) );
390 UpdateStatus( "Switch2: ",H7Serial.readTarget(3) );
391 UpdateStatus( "Switch3: ", H7Serial.readTarget(4) );
392 UpdateStatus( "Potmeter: ", H7Serial.readTarget(5) );
393 UpdateStatus( "Temperatur: ", H7Serial.readTarget(6) );
394
395
396
397 H7Serial.writeTarget(LED3,OFF);
398 return 0;
399 }
400
401 void CH7ServerDlg::NewLine(void)
402 {
403 CString nLine = "";
404 m_Status.GetWindowText(nLine);
405 if( nLine.GetLength() != 0 )
406 nLine += "\r\n";
407 m_Status.SetWindowText(nLine);
408 }
409
410 CString CH7ServerDlg::OnOff(int value)
411 {
412 CString Test;
413 if( value == 0 )
414 Test = "Slukket";
415 else
416 Test = "Tændt";
417
418 return CString(Test);
419 }
420
421 void CH7ServerDlg::UpdateStatus(CString name, short value)
422 {
423 CString tmp,StatusWindow;
424 tmp.Format( "%d", value );
425
426 m_Status.GetWindowText(StatusWindow);
427 StatusWindow += name;
428 StatusWindow += tmp;
429 m_Status.SetWindowText(StatusWindow);
430 NewLine();
431
432 }

  ViewVC Help
Powered by ViewVC 1.1.20