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

  ViewVC Help
Powered by ViewVC 1.1.20