// H7 ServerDlg.cpp : implementation file // #include "stdafx.h" #include "H7 Server.h" #include "H7 ServerDlg.h" #include ".\h7 serverdlg.h" #include #include "Define.h" #ifdef _DEBUG #define new DEBUG_NEW #endif void WriteLastError(char *format) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, NULL ); printf(format, lpMsgBuf); CString Tmp; Tmp.Format( "%s", lpMsgBuf); MessageBox(0,Tmp,"",MB_OK); } 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); DDX_Control(pDX, IDC_Start, m_StartB); } 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) ON_BN_CLICKED(IDC_STOP, OnBnClickedStop) 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 // if there is no free com ports, the program shut down. if (!ServerInit() ) { OnOK(); return false; } m_Restart.EnableWindow(false); m_Stop.EnableWindow(false); //m_BaudRate.EnableWindow(false); m_First = true; /**********************************************************************/ 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::OnBnClickedRestart() { StartSerial(); } void CH7ServerDlg::OnBnClickedStart() { // Add's user defined Baud-Rate to regedit before the server start. // CRegKey reg; // CString Baud = "9600"; // m_BaudRate.GetWindowText(Baud); // reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server"); // reg.SetDWORDValue("BaudRate", atoi(Baud) ); // StartTcp(); StartSerial(); m_Stop.EnableWindow(true); m_Restart.EnableWindow(true); m_BaudRate.EnableWindow(true); m_StartB.EnableWindow(false); } int CH7ServerDlg::StartTcp(void) { // Get the TCP Port number from regedit. CString Current_Port; unsigned long Port; m_Port.GetWindowText(Current_Port); CRegKey reg; reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Server"); reg.SetDWORDValue("Port",atoi(Current_Port) ); reg.QueryDWORDValue("Port", Port); reg.Close(); // Restarts the TCP Connection TcpServer.Close(); if (!TcpServer.Create(Port)) MessageBox("Crap"); if (!TcpServer.Listen()) MessageBox("Loads of crap"); // Prints TCP Port number to the status box. CString tmp; tmp.Format("%d", Port); m_Port.SetWindowText(tmp); return 0; } void CH7ServerDlg::OnBnClickedTest() { //CWaitCursor wait; //UpdateClient(); } int CH7ServerDlg::StartSerial(void) { int Baud; int dBaud; CString SerialPort; Baud = m_BaudRate.GetCurSel(); m_Serial.GetWindowText(SerialPort); switch (Baud) { case 0: dBaud = 1200; break; case 1: dBaud = 2400; break; case 2: dBaud = 4800; break; case 3: dBaud = 9600; break; case 4: dBaud = 19200; break; default: //this should not be possible, but ... dBaud = 9600; break; } if( H7Serial.isOpen() ){ try { H7Serial.writeTarget(10, Baud); } catch (std::exception e) { MessageBox( "Pic could not use selected baud rate." ); return 0; } H7Serial.close(); H7Serial.open( SerialPort, dBaud ); } else { H7Serial.open( SerialPort, dBaud ); } Sleep(50); try { int pot = H7Serial.readTarget(5); CString debug; debug.Format("%d", pot); MessageBox(debug); } catch (...) { MessageBox("Major fuck-up"); } return 0; } void CH7ServerDlg::UpdateClient(TcpClientClass* caller) { // Updater status vindue. m_Status.SetWindowText(""); UpdateStatus( "LED3: ", caller->mLed3 ); UpdateStatus( "LED4: ", caller->mLed4 ); UpdateStatus( "LED5: ",caller->mLed5 ); UpdateStatus( "Switch2: ",caller->mSw2 ); UpdateStatus( "Switch3: ", caller->mSw3 ); UpdateStatus( "Potmeter: ", caller->mPot ); UpdateStatus( "Temperatur: ", caller->mTemp ); } void CH7ServerDlg::NewLine(void) { // Laver linieskift i Status vinduet. CString nLine = ""; m_Status.GetWindowText(nLine); if( nLine.GetLength() != 0 ) nLine += "\r\n"; m_Status.SetWindowText(nLine); } CString CH7ServerDlg::OnOff(int value) { // undersøger om LED3-5 er tændt eller slukket. CString Test; if( value == 0 ) Test = "Slukket"; else Test = "Tændt"; return CString(Test); } void CH7ServerDlg::UpdateStatus(CString name, short value) { CString tmp,StatusWindow; tmp.Format( "%d", value ); m_Status.GetWindowText(StatusWindow); StatusWindow += name; StatusWindow += tmp; m_Status.SetWindowText(StatusWindow); NewLine(); } void CH7ServerDlg::OnBnClickedStop() { // Close TCP and Sreial connections TcpServer.Close(); H7Serial.close(); // Close server app. OnOK(); } void CH7ServerDlg::OnCancel() { TcpServer.Close(); H7Serial.close(); // Close server app. CDialog::OnCancel(); }