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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 65 - (hide annotations) (download)
Tue Feb 6 16:44:32 2007 UTC (17 years, 4 months ago) by hedin
File size: 8934 byte(s)
serial port, baud rate and tcp port is printed to the status window, when you start the server.
1 hedin 3 // 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 hedin 6 #include <vector>
9 hedin 28 #include "Define.h"
10 hedin 3
11     #ifdef _DEBUG
12     #define new DEBUG_NEW
13     #endif
14    
15    
16 hedin 19 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 hedin 6 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 hedin 3 // 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 hedin 36 DDX_Control(pDX, IDC_Start, m_StartB);
115 hedin 3 }
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 hedin 6 ON_BN_CLICKED(IDC_TEST, OnBnClickedTest)
124 hedin 8 ON_BN_CLICKED(IDC_RESTART, OnBnClickedRestart)
125 hedin 33 ON_BN_CLICKED(IDC_STOP, OnBnClickedStop)
126 hedin 3 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 hedin 8
161 hedin 19 // if there is no free com ports, the program shut down.
162     if (!ServerInit() )
163     {
164     OnOK();
165     return false;
166     }
167 hedin 36 m_Restart.EnableWindow(false);
168     m_Stop.EnableWindow(false);
169 hedin 33 m_First = true;
170 hedin 3 /**********************************************************************/
171     return TRUE; // return TRUE unless you set the focus to a control
172     }
173    
174     void CH7ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
175     {
176     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
177     {
178     CAboutDlg dlgAbout;
179     dlgAbout.DoModal();
180     }
181     else
182     {
183     CDialog::OnSysCommand(nID, lParam);
184     }
185     }
186    
187     // If you add a minimize button to your dialog, you will need the code below
188     // to draw the icon. For MFC applications using the document/view model,
189     // this is automatically done for you by the framework.
190    
191     void CH7ServerDlg::OnPaint()
192     {
193     if (IsIconic())
194     {
195     CPaintDC dc(this); // device context for painting
196    
197     SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
198    
199     // Center icon in client rectangle
200     int cxIcon = GetSystemMetrics(SM_CXICON);
201     int cyIcon = GetSystemMetrics(SM_CYICON);
202     CRect rect;
203     GetClientRect(&rect);
204     int x = (rect.Width() - cxIcon + 1) / 2;
205     int y = (rect.Height() - cyIcon + 1) / 2;
206    
207     // Draw the icon
208     dc.DrawIcon(x, y, m_hIcon);
209     }
210     else
211     {
212     CDialog::OnPaint();
213     }
214     }
215    
216     // The system calls this function to obtain the cursor to display while the user drags
217     // the minimized window.
218     HCURSOR CH7ServerDlg::OnQueryDragIcon()
219     {
220     return static_cast<HCURSOR>(m_hIcon);
221     }
222    
223 hedin 33 void CH7ServerDlg::OnBnClickedRestart()
224     {
225     StartSerial();
226     }
227    
228 hedin 3 void CH7ServerDlg::OnBnClickedStart()
229     {
230 hedin 65 StartTcp();
231 hedin 19 StartSerial();
232 hedin 36 m_Stop.EnableWindow(true);
233     m_Restart.EnableWindow(true);
234     m_StartB.EnableWindow(false);
235 hedin 6 }
236    
237     int CH7ServerDlg::StartTcp(void)
238     {
239 hedin 19 // Get the TCP Port number from regedit.
240 hedin 33 CString Current_Port;
241 hedin 6 unsigned long Port;
242 hedin 33 m_Port.GetWindowText(Current_Port);
243 hedin 6 CRegKey reg;
244     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
245 hedin 33 reg.SetDWORDValue("Port",atoi(Current_Port) );
246 hedin 6 reg.QueryDWORDValue("Port", Port);
247 hedin 11 reg.Close();
248    
249 hedin 33 // Restarts the TCP Connection
250 hedin 8 TcpServer.Close();
251    
252 hedin 33 if (!TcpServer.Create(Port))
253 hedin 65 MessageBox("TCP Crap");
254 hedin 33 if (!TcpServer.Listen())
255 hedin 65 MessageBox("Loads of TCP crap");
256 hedin 11
257     // Prints TCP Port number to the status box.
258 hedin 65 CString Status = "TCP Port: ";
259     UpdateStatus(Status, Port);
260 hedin 11
261 hedin 65 // CString tmp;
262     // tmp.Format("%d", Port);
263     // m_Port.SetWindowText(tmp);
264    
265 hedin 6 return 0;
266     }
267    
268 hedin 11 void CH7ServerDlg::OnBnClickedTest()
269 hedin 8 {
270 hedin 39 //CWaitCursor wait;
271 hedin 35 //UpdateClient();
272 hedin 11 }
273    
274    
275 hedin 19 int CH7ServerDlg::StartSerial(void)
276     {
277 hedin 64 int Baud;
278     int dBaud;
279 hedin 34 CString SerialPort;
280 hedin 28
281 hedin 64 Baud = m_BaudRate.GetCurSel();
282 hedin 34 m_Serial.GetWindowText(SerialPort);
283 hedin 19
284 hedin 64 switch (Baud)
285 hedin 33 {
286     case 0:
287 hedin 64 dBaud = 1200;
288 hedin 33 break;
289     case 1:
290 hedin 64 dBaud = 2400;
291 hedin 33 break;
292     case 2:
293 hedin 64 dBaud = 4800;
294 hedin 33 break;
295     case 3:
296 hedin 64 dBaud = 9600;
297 hedin 33 break;
298     case 4:
299 hedin 64 dBaud = 19200;
300 hedin 33 break;
301    
302     default: //this should not be possible, but ...
303 hedin 64 dBaud = 9600;
304 hedin 33 break;
305     }
306 hedin 64
307     if( H7Serial.isOpen() ){
308     try
309     {
310     H7Serial.writeTarget(10, Baud);
311     }
312     catch (std::exception e)
313     {
314     MessageBox( "Pic could not use selected baud rate." );
315     return 0;
316     }
317 hedin 33 H7Serial.close();
318 hedin 64 H7Serial.open( SerialPort, dBaud );
319 hedin 33 }
320 hedin 64 else {
321     H7Serial.open( SerialPort, dBaud );
322     }
323 hedin 33
324 hedin 65 // Udskriver Serial Port og Baud Rate til Status vinduet.
325     UpdateStatus(SerialPort, dBaud);
326     /*
327 hedin 64 Sleep(50);
328 hedin 19
329 hedin 64
330     try {
331     int pot = H7Serial.readTarget(5);
332     CString debug;
333     debug.Format("%d", pot);
334     MessageBox(debug);
335     }
336     catch (...)
337     {
338     MessageBox("Major fuck-up");
339     }
340 hedin 65 */
341 hedin 64
342     return 0;
343 hedin 33 }
344    
345 hedin 35 void CH7ServerDlg::UpdateClient(TcpClientClass* caller)
346 hedin 33 {
347 hedin 28 // Updater status vindue.
348 hedin 33 m_Status.SetWindowText("");
349 hedin 35 UpdateStatus( "LED3: ", caller->mLed3 );
350     UpdateStatus( "LED4: ", caller->mLed4 );
351     UpdateStatus( "LED5: ",caller->mLed5 );
352     UpdateStatus( "Switch2: ",caller->mSw2 );
353     UpdateStatus( "Switch3: ", caller->mSw3 );
354     UpdateStatus( "Potmeter: ", caller->mPot );
355     UpdateStatus( "Temperatur: ", caller->mTemp );
356 hedin 36
357 hedin 28 }
358 hedin 19
359 hedin 28 void CH7ServerDlg::NewLine(void)
360     {
361 hedin 39 // Laver linieskift i Status vinduet.
362 hedin 28 CString nLine = "";
363     m_Status.GetWindowText(nLine);
364     if( nLine.GetLength() != 0 )
365     nLine += "\r\n";
366     m_Status.SetWindowText(nLine);
367     }
368 hedin 19
369 hedin 28 CString CH7ServerDlg::OnOff(int value)
370     {
371 hedin 39 // undersøger om LED3-5 er tændt eller slukket.
372 hedin 28 CString Test;
373     if( value == 0 )
374     Test = "Slukket";
375     else
376     Test = "Tændt";
377 hedin 19
378 hedin 28 return CString(Test);
379     }
380 hedin 19
381 hedin 28 void CH7ServerDlg::UpdateStatus(CString name, short value)
382     {
383 hedin 65 CString Format,StatusWindow;
384     Format.Format( "%d", value );
385 hedin 28 m_Status.GetWindowText(StatusWindow);
386     StatusWindow += name;
387 hedin 65 StatusWindow += Format;
388 hedin 28 m_Status.SetWindowText(StatusWindow);
389     NewLine();
390 hedin 8 }
391 hedin 33
392     void CH7ServerDlg::OnBnClickedStop()
393     {
394     // Close TCP and Sreial connections
395     TcpServer.Close();
396     H7Serial.close();
397     // Close server app.
398     OnOK();
399     }
400    
401 hedin 36 void CH7ServerDlg::OnCancel()
402 hedin 33 {
403 hedin 36 TcpServer.Close();
404     H7Serial.close();
405     // Close server app.
406 hedin 33
407 hedin 36 CDialog::OnCancel();
408 hedin 33 }
409 hedin 39
410    
411    
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    

  ViewVC Help
Powered by ViewVC 1.1.20