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 |
6 |
// Sets values in baud-rate combo box
|
139 |
|
|
m_BaudRate.AddString("1200");
|
140 |
|
|
m_BaudRate.AddString("2400");
|
141 |
hedin |
8 |
m_BaudRate.AddString("4800"); // Don't work, used for error test.
|
142 |
hedin |
6 |
m_BaudRate.AddString("9600");
|
143 |
|
|
m_BaudRate.AddString("19200");
|
144 |
hedin |
3 |
|
145 |
hedin |
8 |
// Checks how many COM ports there are.
|
146 |
hedin |
6 |
std::vector<CString> Serial = GetAvailableComPorts();
|
147 |
hedin |
8 |
for (unsigned int i=0; i<Serial.size(); i++)
|
148 |
hedin |
6 |
m_Serial.AddString(Serial[i]);
|
149 |
|
|
m_Serial.SetCurSel(0);
|
150 |
hedin |
3 |
|
151 |
hedin |
8 |
// Load saved settings from regedit.
|
152 |
|
|
unsigned long Port;
|
153 |
|
|
unsigned long BaudRate;
|
154 |
|
|
// CString LoadCom;
|
155 |
|
|
|
156 |
|
|
// Used to format the loaded values.
|
157 |
|
|
CString TPort;
|
158 |
|
|
CString TBaudRate;
|
159 |
|
|
|
160 |
|
|
CRegKey reg;
|
161 |
|
|
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
|
162 |
|
|
reg.QueryDWORDValue("Port", Port);
|
163 |
|
|
reg.QueryDWORDValue("BaudRate", BaudRate);
|
164 |
|
|
|
165 |
|
|
char LoadCom[20];
|
166 |
|
|
unsigned long size = 20;
|
167 |
|
|
reg.QueryStringValue("Serial", LoadCom, &size);
|
168 |
|
|
|
169 |
|
|
TPort.Format("%d", Port);
|
170 |
|
|
TBaudRate.Format("%d", BaudRate);
|
171 |
|
|
// TLoadCom.Format("%s", TLoadCom);
|
172 |
|
|
|
173 |
|
|
m_Port.SetWindowText(TPort);
|
174 |
|
|
m_BaudRate.SetWindowText(TBaudRate);
|
175 |
|
|
m_Serial.SetWindowText(LoadCom);
|
176 |
|
|
MessageBox(LoadCom);
|
177 |
hedin |
3 |
/**********************************************************************/
|
178 |
|
|
return TRUE; // return TRUE unless you set the focus to a control
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
void CH7ServerDlg::OnSysCommand(UINT nID, LPARAM lParam)
|
182 |
|
|
{
|
183 |
|
|
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
|
184 |
|
|
{
|
185 |
|
|
CAboutDlg dlgAbout;
|
186 |
|
|
dlgAbout.DoModal();
|
187 |
|
|
}
|
188 |
|
|
else
|
189 |
|
|
{
|
190 |
|
|
CDialog::OnSysCommand(nID, lParam);
|
191 |
|
|
}
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
// If you add a minimize button to your dialog, you will need the code below
|
195 |
|
|
// to draw the icon. For MFC applications using the document/view model,
|
196 |
|
|
// this is automatically done for you by the framework.
|
197 |
|
|
|
198 |
|
|
void CH7ServerDlg::OnPaint()
|
199 |
|
|
{
|
200 |
|
|
if (IsIconic())
|
201 |
|
|
{
|
202 |
|
|
CPaintDC dc(this); // device context for painting
|
203 |
|
|
|
204 |
|
|
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
|
205 |
|
|
|
206 |
|
|
// Center icon in client rectangle
|
207 |
|
|
int cxIcon = GetSystemMetrics(SM_CXICON);
|
208 |
|
|
int cyIcon = GetSystemMetrics(SM_CYICON);
|
209 |
|
|
CRect rect;
|
210 |
|
|
GetClientRect(&rect);
|
211 |
|
|
int x = (rect.Width() - cxIcon + 1) / 2;
|
212 |
|
|
int y = (rect.Height() - cyIcon + 1) / 2;
|
213 |
|
|
|
214 |
|
|
// Draw the icon
|
215 |
|
|
dc.DrawIcon(x, y, m_hIcon);
|
216 |
|
|
}
|
217 |
|
|
else
|
218 |
|
|
{
|
219 |
|
|
CDialog::OnPaint();
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
// The system calls this function to obtain the cursor to display while the user drags
|
224 |
|
|
// the minimized window.
|
225 |
|
|
HCURSOR CH7ServerDlg::OnQueryDragIcon()
|
226 |
|
|
{
|
227 |
|
|
return static_cast<HCURSOR>(m_hIcon);
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
void CH7ServerDlg::OnBnClickedStart()
|
231 |
|
|
{
|
232 |
hedin |
6 |
CRegKey reg;
|
233 |
|
|
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
|
234 |
|
|
unsigned long Port;
|
235 |
|
|
reg.QueryDWORDValue("Port", Port);
|
236 |
|
|
|
237 |
|
|
CString tmp;
|
238 |
|
|
tmp.Format("%d", Port);
|
239 |
|
|
m_Port.SetWindowText(tmp);
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
int CH7ServerDlg::StartTcp(void)
|
243 |
|
|
{
|
244 |
|
|
unsigned long Port;
|
245 |
|
|
CRegKey reg;
|
246 |
|
|
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
|
247 |
|
|
reg.QueryDWORDValue("Port", Port);
|
248 |
hedin |
8 |
TcpServer.Close();
|
249 |
|
|
|
250 |
hedin |
6 |
TcpServer.Create(Port);
|
251 |
|
|
TcpServer.Listen();
|
252 |
|
|
return 0;
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
void CH7ServerDlg::OnBnClickedTest()
|
256 |
|
|
{
|
257 |
|
|
//**************** <TCP Port section> ****************/
|
258 |
|
|
CString Port = "";
|
259 |
|
|
|
260 |
hedin |
3 |
m_Port.GetWindowText(Port);
|
261 |
hedin |
6 |
// Saving Port number in regedit as int
|
262 |
hedin |
5 |
CRegKey reg;
|
263 |
|
|
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
|
264 |
hedin |
6 |
reg.SetDWORDValue("Port", atoi(Port) );
|
265 |
hedin |
5 |
reg.Close();
|
266 |
hedin |
6 |
// Debug info to the status edit box.
|
267 |
hedin |
3 |
m_Status.SetWindowText(Port);
|
268 |
hedin |
6 |
//**************** </TCP Port section> ****************/
|
269 |
|
|
StartTcp();
|
270 |
hedin |
3 |
}
|
271 |
|
|
|
272 |
hedin |
8 |
|
273 |
|
|
void CH7ServerDlg::OnBnClickedRestart()
|
274 |
|
|
{
|
275 |
|
|
CWaitCursor wait; // Musen viser timeglas.
|
276 |
|
|
CString Port;
|
277 |
|
|
m_Port.GetWindowText(Port);
|
278 |
|
|
CRegKey reg;
|
279 |
|
|
reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server");
|
280 |
|
|
reg.SetDWORDValue("Port", atoi(Port) );
|
281 |
|
|
StartTcp();
|
282 |
|
|
}
|