// H7 ServerDlg.cpp : implementation file // #include "stdafx.h" #include "H7 Server.h" #include "H7 ServerDlg.h" #include ".\h7 serverdlg.h" #include #ifdef _DEBUG #define new DEBUG_NEW #endif std::vector GetAvailableComPorts() { std::vector ports; for (int i = 1; i < 20; i++) { CString port; port.Format("COM%d", i); HANDLE handle = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, NULL); if (handle != INVALID_HANDLE_VALUE) { CloseHandle(handle); ports.push_back(port); } } return ports; } // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CH7ServerDlg dialog CH7ServerDlg::CH7ServerDlg(CWnd* pParent /*=NULL*/) : CDialog(CH7ServerDlg::IDD, pParent) , m_Start(0) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CH7ServerDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_STATUS, m_Status); DDX_Control(pDX, IDC_BaudRate, m_BaudRate); DDX_Control(pDX, IDC_SERIAL, m_Serial); DDX_Control(pDX, IDC_STOP, m_Stop); DDX_Control(pDX, IDC_PORT, m_Port); DDX_Control(pDX, IDC_RESTART, m_Restart); } BEGIN_MESSAGE_MAP(CH7ServerDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_Start, OnBnClickedStart) ON_BN_CLICKED(IDC_TEST, OnBnClickedTest) ON_BN_CLICKED(IDC_RESTART, OnBnClickedRestart) END_MESSAGE_MAP() // CH7ServerDlg message handlers BOOL CH7ServerDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon /**********************************************************************/ // TODO: Add extra initialization here ServerInit(); /**********************************************************************/ return TRUE; // return TRUE unless you set the focus to a control } void CH7ServerDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CH7ServerDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CH7ServerDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } void CH7ServerDlg::OnBnClickedStart() { // Get TCp port CRegKey reg; reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server"); unsigned long Port; reg.QueryDWORDValue("Port", Port); // Add's user defined Baud-Rate to regedit before the server start. CString Baud; m_BaudRate.GetWindowText(Baud); reg.SetDWORDValue("BaudRate", atoi(Baud) ); StartTcp(); } int CH7ServerDlg::StartTcp(void) { // Gets the TCP Port number from regedit. unsigned long Port; CRegKey reg; reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server"); reg.QueryDWORDValue("Port", Port); reg.Close(); // Restats the TCP Connection TcpServer.Close(); TcpServer.Create(Port); TcpServer.Listen(); // Prints TCP Port number to the status box. CString tmp; tmp.Format("%d", Port); m_Port.SetWindowText(tmp); return 0; } void CH7ServerDlg::OnBnClickedRestart() { CWaitCursor wait; // Musen viser timeglas. CString Port; m_Port.GetWindowText(Port); CRegKey reg; reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server"); reg.SetDWORDValue("Port", atoi(Port) ); CString tmp; m_Serial.GetWindowText(tmp); reg.SetStringValue("Serial",tmp); reg.Close(); StartTcp(); } void CH7ServerDlg::OnBnClickedTest() { } void CH7ServerDlg::ServerInit(void) { int i; CRegKey reg; // Sets values in baud-rate combo box m_BaudRate.AddString("1200"); m_BaudRate.AddString("2400"); m_BaudRate.AddString("4800"); // Don't work, used for error test. m_BaudRate.AddString("9600"); m_BaudRate.AddString("19200"); unsigned long Baud; CString GetBaud; reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server"); reg.QueryDWORDValue("BaudRate", Baud); switch (Baud) { case 1200: m_BaudRate.SetCurSel(0); break; case 2400: m_BaudRate.SetCurSel(1); break; case 4800: m_BaudRate.SetCurSel(2); break; case 9600: m_BaudRate.SetCurSel(3); break; case 19200: m_BaudRate.SetCurSel(4); break; default: m_BaudRate.SetCurSel(3); break; } m_BaudRate.GetWindowText(GetBaud); reg.SetDWORDValue("BaudRate", atoi(GetBaud) ); // Checks how many COM ports there are. std::vector Serial = GetAvailableComPorts(); for (i=0; i