/[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 33 - (hide annotations) (download)
Wed Jan 31 17:22:59 2007 UTC (17 years, 4 months ago) by hedin
File size: 8935 byte(s)
Made the selection of baud rate work (well, make everything work - including multiple clients)
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     }
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 hedin 6 ON_BN_CLICKED(IDC_TEST, OnBnClickedTest)
123 hedin 8 ON_BN_CLICKED(IDC_RESTART, OnBnClickedRestart)
124 hedin 33 ON_BN_CLICKED(IDC_STOP, OnBnClickedStop)
125 hedin 3 END_MESSAGE_MAP()
126    
127    
128     // CH7ServerDlg message handlers
129    
130     BOOL CH7ServerDlg::OnInitDialog()
131     {
132     CDialog::OnInitDialog();
133    
134     // Add "About..." menu item to system menu.
135    
136     // IDM_ABOUTBOX must be in the system command range.
137     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
138     ASSERT(IDM_ABOUTBOX < 0xF000);
139    
140     CMenu* pSysMenu = GetSystemMenu(FALSE);
141     if (pSysMenu != NULL)
142     {
143     CString strAboutMenu;
144     strAboutMenu.LoadString(IDS_ABOUTBOX);
145     if (!strAboutMenu.IsEmpty())
146     {
147     pSysMenu->AppendMenu(MF_SEPARATOR);
148     pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
149     }
150     }
151    
152     // Set the icon for this dialog. The framework does this automatically
153     // when the application's main window is not a dialog
154     SetIcon(m_hIcon, TRUE); // Set big icon
155     SetIcon(m_hIcon, FALSE); // Set small icon
156    
157     /**********************************************************************/
158     // TODO: Add extra initialization here
159 hedin 8
160 hedin 19 // if there is no free com ports, the program shut down.
161     if (!ServerInit() )
162     {
163     OnOK();
164     return false;
165     }
166 hedin 33 m_First = true;
167 hedin 3 /**********************************************************************/
168     return TRUE; // return TRUE unless you set the focus to a control
169     }
170    
171     void CH7ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
172     {
173     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
174     {
175     CAboutDlg dlgAbout;
176     dlgAbout.DoModal();
177     }
178     else
179     {
180     CDialog::OnSysCommand(nID, lParam);
181     }
182     }
183    
184     // If you add a minimize button to your dialog, you will need the code below
185     // to draw the icon. For MFC applications using the document/view model,
186     // this is automatically done for you by the framework.
187    
188     void CH7ServerDlg::OnPaint()
189     {
190     if (IsIconic())
191     {
192     CPaintDC dc(this); // device context for painting
193    
194     SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
195    
196     // Center icon in client rectangle
197     int cxIcon = GetSystemMetrics(SM_CXICON);
198     int cyIcon = GetSystemMetrics(SM_CYICON);
199     CRect rect;
200     GetClientRect(&rect);
201     int x = (rect.Width() - cxIcon + 1) / 2;
202     int y = (rect.Height() - cyIcon + 1) / 2;
203    
204     // Draw the icon
205     dc.DrawIcon(x, y, m_hIcon);
206     }
207     else
208     {
209     CDialog::OnPaint();
210     }
211     }
212    
213     // The system calls this function to obtain the cursor to display while the user drags
214     // the minimized window.
215     HCURSOR CH7ServerDlg::OnQueryDragIcon()
216     {
217     return static_cast<HCURSOR>(m_hIcon);
218     }
219    
220 hedin 33 void CH7ServerDlg::OnBnClickedRestart()
221     {
222     StartSerial();
223     }
224    
225 hedin 3 void CH7ServerDlg::OnBnClickedStart()
226     {
227 hedin 19 // Add's user defined Baud-Rate to regedit before the server start.
228 hedin 6 CRegKey reg;
229 hedin 33 CString Baud = "9600";
230     // m_BaudRate.GetWindowText(Baud);
231 hedin 19
232     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
233 hedin 11 reg.SetDWORDValue("BaudRate", atoi(Baud) );
234    
235     StartTcp();
236 hedin 19 StartSerial();
237 hedin 33 UpdateClient();
238 hedin 6 }
239    
240     int CH7ServerDlg::StartTcp(void)
241     {
242 hedin 19 // Get the TCP Port number from regedit.
243 hedin 33 CString Current_Port;
244 hedin 6 unsigned long Port;
245 hedin 33 m_Port.GetWindowText(Current_Port);
246 hedin 6 CRegKey reg;
247     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
248 hedin 33 reg.SetDWORDValue("Port",atoi(Current_Port) );
249 hedin 6 reg.QueryDWORDValue("Port", Port);
250 hedin 11 reg.Close();
251    
252 hedin 33 // Restarts the TCP Connection
253 hedin 8 TcpServer.Close();
254    
255 hedin 33 if (!TcpServer.Create(Port))
256     MessageBox("Crap");
257     if (!TcpServer.Listen())
258     MessageBox("Loads of crap");
259 hedin 11
260     // Prints TCP Port number to the status box.
261     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 33 CWaitCursor wait;
271     UpdateClient();
272 hedin 11 }
273    
274    
275 hedin 19 int CH7ServerDlg::StartSerial(void)
276     {
277     char SerialPort[6];
278     unsigned long size = 6;
279     unsigned long Baud;
280 hedin 28
281 hedin 33
282 hedin 19 CRegKey reg;
283    
284     reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
285     reg.QueryStringValue("Serial", SerialPort, &size);
286     reg.QueryDWORDValue("BaudRate", Baud);
287    
288 hedin 28 // Starter Serial forbindelsen, hvis den ikke allerede er startet.
289 hedin 33 if( m_First == 1 ){
290     Baud = 9600;
291     }
292     else
293     {
294     int selected = m_BaudRate.GetCurSel();
295     try
296     {
297     H7Serial.writeTarget(10,selected);
298     }
299     catch (std::exception e)
300     {
301     MessageBox("PIC could not use selected baudrate");
302     return 0;
303    
304     }
305     switch (selected)
306     {
307     case 0:
308     Baud = 1200;
309     break;
310     case 1:
311     Baud = 2400;
312     break;
313     case 2:
314     Baud = 4800;
315     break;
316     case 3:
317     Baud = 9600;;
318     break;
319     case 4:
320     Baud = 19200;
321     break;
322    
323     default: //this should not be possible, but ...
324     Baud = 9600;
325     break;
326     }
327     H7Serial.close();
328     }
329    
330 hedin 28 if (!H7Serial.isOpen())
331     H7Serial.open(SerialPort,Baud);
332 hedin 33
333 hedin 19
334 hedin 33 m_First = false;
335     // Nulstiller LED's
336     H7Serial.writeTarget(LED3,OFF);
337     H7Serial.writeTarget(LED4,OFF);
338     H7Serial.writeTarget(LED5,OFF);
339     return 0;
340     }
341    
342     void CH7ServerDlg::UpdateClient(void)
343     {
344 hedin 28 // Updater status vindue.
345 hedin 33 const int sleep = 10;
346    
347     m_Status.SetWindowText("");
348 hedin 28 UpdateStatus( "LED3: ",H7Serial.readTarget(0) );
349 hedin 33 Sleep(sleep);
350 hedin 28 UpdateStatus( "LED4: ",H7Serial.readTarget(1) );
351 hedin 33 Sleep(sleep);
352 hedin 28 UpdateStatus( "LED5: ",H7Serial.readTarget(2) );
353 hedin 33 Sleep(sleep);
354 hedin 28 UpdateStatus( "Switch2: ",H7Serial.readTarget(3) );
355 hedin 33 Sleep(sleep);
356 hedin 28 UpdateStatus( "Switch3: ", H7Serial.readTarget(4) );
357 hedin 33 Sleep(sleep);
358 hedin 28 UpdateStatus( "Potmeter: ", H7Serial.readTarget(5) );
359 hedin 33 Sleep(sleep);
360 hedin 28 UpdateStatus( "Temperatur: ", H7Serial.readTarget(6) );
361 hedin 33 Sleep(sleep);
362 hedin 28 }
363 hedin 19
364 hedin 28 void CH7ServerDlg::NewLine(void)
365     {
366     CString nLine = "";
367     m_Status.GetWindowText(nLine);
368     if( nLine.GetLength() != 0 )
369     nLine += "\r\n";
370     m_Status.SetWindowText(nLine);
371     }
372 hedin 19
373 hedin 28 CString CH7ServerDlg::OnOff(int value)
374     {
375     CString Test;
376     if( value == 0 )
377     Test = "Slukket";
378     else
379     Test = "Tændt";
380 hedin 19
381 hedin 28 return CString(Test);
382     }
383 hedin 19
384 hedin 28 void CH7ServerDlg::UpdateStatus(CString name, short value)
385     {
386     CString tmp,StatusWindow;
387     tmp.Format( "%d", value );
388     m_Status.GetWindowText(StatusWindow);
389     StatusWindow += name;
390     StatusWindow += tmp;
391     m_Status.SetWindowText(StatusWindow);
392     NewLine();
393 hedin 19
394 hedin 8 }
395 hedin 33
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::TcpStatus(void)
406     {
407    
408     }
409    

  ViewVC Help
Powered by ViewVC 1.1.20