/[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 11 - (hide annotations) (download)
Mon Jan 29 16:18:31 2007 UTC (17 years, 4 months ago) by hedin
File size: 7780 byte(s)
Added init from regedit, and made some "spam" for kevin to test on:P
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 3
10     #ifdef _DEBUG
11     #define new DEBUG_NEW
12     #endif
13    
14    
15 hedin 6 std::vector<CString> GetAvailableComPorts()
16     {
17     std::vector<CString> ports;
18    
19     for (int i = 1; i < 20; i++)
20     {
21     CString port;
22     port.Format("COM%d", i);
23    
24    
25     HANDLE handle = CreateFile(port,
26     GENERIC_READ | GENERIC_WRITE,
27     0,
28     0,
29     OPEN_EXISTING,
30     0,
31     NULL);
32    
33     if (handle != INVALID_HANDLE_VALUE)
34     {
35     CloseHandle(handle);
36     ports.push_back(port);
37     }
38     }
39     return ports;
40     }
41    
42 hedin 3 // CAboutDlg dialog used for App About
43    
44     class CAboutDlg : public CDialog
45     {
46     public:
47     CAboutDlg();
48    
49     // Dialog Data
50     enum { IDD = IDD_ABOUTBOX };
51    
52     protected:
53     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
54    
55     // Implementation
56     protected:
57     DECLARE_MESSAGE_MAP()
58     };
59    
60     CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
61     {
62     }
63    
64     void CAboutDlg::DoDataExchange(CDataExchange* pDX)
65     {
66     CDialog::DoDataExchange(pDX);
67     }
68    
69     BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
70     END_MESSAGE_MAP()
71    
72    
73     // CH7ServerDlg dialog
74    
75    
76    
77     CH7ServerDlg::CH7ServerDlg(CWnd* pParent /*=NULL*/)
78     : CDialog(CH7ServerDlg::IDD, pParent)
79     , m_Start(0)
80     {
81     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
82     }
83    
84     void CH7ServerDlg::DoDataExchange(CDataExchange* pDX)
85     {
86     CDialog::DoDataExchange(pDX);
87     DDX_Control(pDX, IDC_STATUS, m_Status);
88     DDX_Control(pDX, IDC_BaudRate, m_BaudRate);
89     DDX_Control(pDX, IDC_SERIAL, m_Serial);
90     DDX_Control(pDX, IDC_STOP, m_Stop);
91     DDX_Control(pDX, IDC_PORT, m_Port);
92     DDX_Control(pDX, IDC_RESTART, m_Restart);
93     }
94    
95     BEGIN_MESSAGE_MAP(CH7ServerDlg, CDialog)
96     ON_WM_SYSCOMMAND()
97     ON_WM_PAINT()
98     ON_WM_QUERYDRAGICON()
99     //}}AFX_MSG_MAP
100     ON_BN_CLICKED(IDC_Start, OnBnClickedStart)
101 hedin 6 ON_BN_CLICKED(IDC_TEST, OnBnClickedTest)
102 hedin 8 ON_BN_CLICKED(IDC_RESTART, OnBnClickedRestart)
103 hedin 3 END_MESSAGE_MAP()
104    
105    
106     // CH7ServerDlg message handlers
107    
108     BOOL CH7ServerDlg::OnInitDialog()
109     {
110     CDialog::OnInitDialog();
111    
112     // Add "About..." menu item to system menu.
113    
114     // IDM_ABOUTBOX must be in the system command range.
115     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
116     ASSERT(IDM_ABOUTBOX < 0xF000);
117    
118     CMenu* pSysMenu = GetSystemMenu(FALSE);
119     if (pSysMenu != NULL)
120     {
121     CString strAboutMenu;
122     strAboutMenu.LoadString(IDS_ABOUTBOX);
123     if (!strAboutMenu.IsEmpty())
124     {
125     pSysMenu->AppendMenu(MF_SEPARATOR);
126     pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
127     }
128     }
129    
130     // Set the icon for this dialog. The framework does this automatically
131     // when the application's main window is not a dialog
132     SetIcon(m_hIcon, TRUE); // Set big icon
133     SetIcon(m_hIcon, FALSE); // Set small icon
134    
135     /**********************************************************************/
136     // TODO: Add extra initialization here
137 hedin 8
138 hedin 11 ServerInit();
139 hedin 3
140     /**********************************************************************/
141     return TRUE; // return TRUE unless you set the focus to a control
142     }
143    
144     void CH7ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
145     {
146     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
147     {
148     CAboutDlg dlgAbout;
149     dlgAbout.DoModal();
150     }
151     else
152     {
153     CDialog::OnSysCommand(nID, lParam);
154     }
155     }
156    
157     // If you add a minimize button to your dialog, you will need the code below
158     // to draw the icon. For MFC applications using the document/view model,
159     // this is automatically done for you by the framework.
160    
161     void CH7ServerDlg::OnPaint()
162     {
163     if (IsIconic())
164     {
165     CPaintDC dc(this); // device context for painting
166    
167     SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
168    
169     // Center icon in client rectangle
170     int cxIcon = GetSystemMetrics(SM_CXICON);
171     int cyIcon = GetSystemMetrics(SM_CYICON);
172     CRect rect;
173     GetClientRect(&rect);
174     int x = (rect.Width() - cxIcon + 1) / 2;
175     int y = (rect.Height() - cyIcon + 1) / 2;
176    
177     // Draw the icon
178     dc.DrawIcon(x, y, m_hIcon);
179     }
180     else
181     {
182     CDialog::OnPaint();
183     }
184     }
185    
186     // The system calls this function to obtain the cursor to display while the user drags
187     // the minimized window.
188     HCURSOR CH7ServerDlg::OnQueryDragIcon()
189     {
190     return static_cast<HCURSOR>(m_hIcon);
191     }
192    
193     void CH7ServerDlg::OnBnClickedStart()
194     {
195 hedin 11 // Get TCp port
196 hedin 6 CRegKey reg;
197     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
198     unsigned long Port;
199     reg.QueryDWORDValue("Port", Port);
200    
201 hedin 11 // Add's user defined Baud-Rate to regedit before the server start.
202     CString Baud;
203     m_BaudRate.GetWindowText(Baud);
204     reg.SetDWORDValue("BaudRate", atoi(Baud) );
205    
206     StartTcp();
207 hedin 6 }
208    
209     int CH7ServerDlg::StartTcp(void)
210     {
211 hedin 11 // Gets the TCP Port number from regedit.
212 hedin 6 unsigned long Port;
213     CRegKey reg;
214     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
215     reg.QueryDWORDValue("Port", Port);
216 hedin 11 reg.Close();
217    
218     // Restats the TCP Connection
219 hedin 8 TcpServer.Close();
220    
221 hedin 6 TcpServer.Create(Port);
222     TcpServer.Listen();
223 hedin 11
224     // Prints TCP Port number to the status box.
225     CString tmp;
226     tmp.Format("%d", Port);
227     m_Port.SetWindowText(tmp);
228    
229 hedin 6 return 0;
230     }
231    
232 hedin 11 void CH7ServerDlg::OnBnClickedRestart()
233 hedin 6 {
234 hedin 11 CWaitCursor wait; // Musen viser timeglas.
235     CString Port;
236 hedin 3 m_Port.GetWindowText(Port);
237 hedin 5 CRegKey reg;
238     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
239 hedin 6 reg.SetDWORDValue("Port", atoi(Port) );
240 hedin 11
241     CString tmp;
242     m_Serial.GetWindowText(tmp);
243     reg.SetStringValue("Serial",tmp);
244 hedin 5 reg.Close();
245 hedin 6 StartTcp();
246 hedin 3 }
247    
248 hedin 11 void CH7ServerDlg::OnBnClickedTest()
249 hedin 8 {
250 hedin 11
251     }
252     void CH7ServerDlg::ServerInit(void)
253     {
254     int i;
255 hedin 8 CRegKey reg;
256 hedin 11 // Sets values in baud-rate combo box
257     m_BaudRate.AddString("1200");
258     m_BaudRate.AddString("2400");
259     m_BaudRate.AddString("4800"); // Don't work, used for error test.
260     m_BaudRate.AddString("9600");
261     m_BaudRate.AddString("19200");
262    
263     unsigned long Baud;
264     CString GetBaud;
265 hedin 8 reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
266 hedin 11 reg.QueryDWORDValue("BaudRate", Baud);
267    
268     switch (Baud)
269     {
270     case 1200:
271     m_BaudRate.SetCurSel(0);
272     break;
273     case 2400:
274     m_BaudRate.SetCurSel(1);
275     break;
276     case 4800:
277     m_BaudRate.SetCurSel(2);
278     break;
279     case 9600:
280     m_BaudRate.SetCurSel(3);
281     break;
282     case 19200:
283     m_BaudRate.SetCurSel(4);
284     break;
285     default:
286     m_BaudRate.SetCurSel(3);
287     break;
288     }
289    
290     m_BaudRate.GetWindowText(GetBaud);
291     reg.SetDWORDValue("BaudRate", atoi(GetBaud) );
292    
293     // Checks how many COM ports there are.
294     std::vector<CString> Serial = GetAvailableComPorts();
295     for (i=0; i<Serial.size(); i++)
296     m_Serial.AddString(Serial[i]);
297    
298     // Load saved settings from regedit.
299     unsigned long Port;
300     unsigned long BaudRate;
301    
302     // Used to format the loaded values.
303     CString TPort;
304     CString TBaudRate;
305    
306     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
307     reg.QueryDWORDValue("Port", Port);
308     reg.QueryDWORDValue("BaudRate", BaudRate);
309    
310     char LoadCom[20];
311     unsigned long size = 20;
312     reg.QueryStringValue("Serial", LoadCom, &size);
313    
314     TPort.Format("%d", Port);
315     TBaudRate.Format("%d", BaudRate);
316    
317     m_Port.SetWindowText(TPort);
318     m_BaudRate.SetWindowText(TBaudRate);
319     m_Serial.SetWindowText(LoadCom);
320    
321     // Checks for valid COM port in regedit.
322     for (i=0; i< Serial.size() ; i++)
323     {
324     if (LoadCom == Serial[i])
325     {
326     m_Serial.SetCurSel(i);
327     break;
328     }
329     }
330     if (i == Serial.size() )
331     {
332     m_Serial.SetCurSel(0);
333     reg.SetStringValue("Serial", Serial[0]);
334     }
335     reg.Close();
336    
337 hedin 8 }

  ViewVC Help
Powered by ViewVC 1.1.20