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 |
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 |
// 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 |
ON_BN_CLICKED(IDC_TEST, OnBnClickedTest)
|
102 |
END_MESSAGE_MAP()
|
103 |
|
104 |
|
105 |
// CH7ServerDlg message handlers
|
106 |
|
107 |
BOOL CH7ServerDlg::OnInitDialog()
|
108 |
{
|
109 |
CDialog::OnInitDialog();
|
110 |
|
111 |
// Add "About..." menu item to system menu.
|
112 |
|
113 |
// IDM_ABOUTBOX must be in the system command range.
|
114 |
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
|
115 |
ASSERT(IDM_ABOUTBOX < 0xF000);
|
116 |
|
117 |
CMenu* pSysMenu = GetSystemMenu(FALSE);
|
118 |
if (pSysMenu != NULL)
|
119 |
{
|
120 |
CString strAboutMenu;
|
121 |
strAboutMenu.LoadString(IDS_ABOUTBOX);
|
122 |
if (!strAboutMenu.IsEmpty())
|
123 |
{
|
124 |
pSysMenu->AppendMenu(MF_SEPARATOR);
|
125 |
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
|
126 |
}
|
127 |
}
|
128 |
|
129 |
// Set the icon for this dialog. The framework does this automatically
|
130 |
// when the application's main window is not a dialog
|
131 |
SetIcon(m_hIcon, TRUE); // Set big icon
|
132 |
SetIcon(m_hIcon, FALSE); // Set small icon
|
133 |
|
134 |
/**********************************************************************/
|
135 |
// TODO: Add extra initialization here
|
136 |
// Sets values in baud-rate combo box
|
137 |
m_BaudRate.AddString("1200");
|
138 |
m_BaudRate.AddString("2400");
|
139 |
m_BaudRate.AddString("4800");
|
140 |
m_BaudRate.AddString("9600");
|
141 |
m_BaudRate.AddString("19200");
|
142 |
|
143 |
std::vector<CString> Serial = GetAvailableComPorts();
|
144 |
for (int i=0;i<Serial.size();i++)
|
145 |
m_Serial.AddString(Serial[i]);
|
146 |
m_Serial.SetCurSel(0);
|
147 |
|
148 |
/**********************************************************************/
|
149 |
return TRUE; // return TRUE unless you set the focus to a control
|
150 |
}
|
151 |
|
152 |
void CH7ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
|
153 |
{
|
154 |
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
155 |
{
|
156 |
CAboutDlg dlgAbout;
|
157 |
dlgAbout.DoModal();
|
158 |
}
|
159 |
else
|
160 |
{
|
161 |
CDialog::OnSysCommand(nID, lParam);
|
162 |
}
|
163 |
}
|
164 |
|
165 |
// If you add a minimize button to your dialog, you will need the code below
|
166 |
// to draw the icon. For MFC applications using the document/view model,
|
167 |
// this is automatically done for you by the framework.
|
168 |
|
169 |
void CH7ServerDlg::OnPaint()
|
170 |
{
|
171 |
if (IsIconic())
|
172 |
{
|
173 |
CPaintDC dc(this); // device context for painting
|
174 |
|
175 |
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
176 |
|
177 |
// Center icon in client rectangle
|
178 |
int cxIcon = GetSystemMetrics(SM_CXICON);
|
179 |
int cyIcon = GetSystemMetrics(SM_CYICON);
|
180 |
CRect rect;
|
181 |
GetClientRect(&rect);
|
182 |
int x = (rect.Width() - cxIcon + 1) / 2;
|
183 |
int y = (rect.Height() - cyIcon + 1) / 2;
|
184 |
|
185 |
// Draw the icon
|
186 |
dc.DrawIcon(x, y, m_hIcon);
|
187 |
}
|
188 |
else
|
189 |
{
|
190 |
CDialog::OnPaint();
|
191 |
}
|
192 |
}
|
193 |
|
194 |
// The system calls this function to obtain the cursor to display while the user drags
|
195 |
// the minimized window.
|
196 |
HCURSOR CH7ServerDlg::OnQueryDragIcon()
|
197 |
{
|
198 |
return static_cast<HCURSOR>(m_hIcon);
|
199 |
}
|
200 |
|
201 |
void CH7ServerDlg::OnBnClickedStart()
|
202 |
{
|
203 |
CRegKey reg;
|
204 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
|
205 |
unsigned long Port;
|
206 |
reg.QueryDWORDValue("Port", Port);
|
207 |
|
208 |
CString tmp;
|
209 |
tmp.Format("%d", Port);
|
210 |
m_Port.SetWindowText(tmp);
|
211 |
}
|
212 |
|
213 |
int CH7ServerDlg::StartTcp(void)
|
214 |
{
|
215 |
unsigned long Port;
|
216 |
CRegKey reg;
|
217 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
|
218 |
reg.QueryDWORDValue("Port", Port);
|
219 |
TcpServer.Create(Port);
|
220 |
TcpServer.Listen();
|
221 |
return 0;
|
222 |
}
|
223 |
|
224 |
void CH7ServerDlg::OnBnClickedTest()
|
225 |
{
|
226 |
//**************** <TCP Port section> ****************/
|
227 |
CString Port = "";
|
228 |
|
229 |
m_Port.GetWindowText(Port);
|
230 |
// Saving Port number in regedit as int
|
231 |
CRegKey reg;
|
232 |
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
|
233 |
reg.SetDWORDValue("Port", atoi(Port) );
|
234 |
reg.Close();
|
235 |
// Debug info to the status edit box.
|
236 |
m_Status.SetWindowText(Port);
|
237 |
//**************** </TCP Port section> ****************/
|
238 |
StartTcp();
|
239 |
}
|
240 |
|