// ClientDlg.cpp : implementation file // #include "stdafx.h" #include "Client.h" #include "ClientDlg.h" #include ".\clientdlg.h" #ifdef _DEBUG #define new DEBUG_NEW #endif //Globale variabler osv. :-S CString Ip, Delay, Updatevariable; unsigned long Port; UINT Status( LPVOID Param ); UINT Update( LPVOID Param ); void Buffcheck(CString); void Start(); CButton m_led1; CButton m_led2; CButton m_led3; CButton m_knap1; CButton m_knap2; CEdit m_temp; CEdit m_pot; // CAboutDlg dialog used for App About bool continueThread; 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() // CClientDlg dialog CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/) : CDialog(CClientDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CClientDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_LED1, m_led1); DDX_Control(pDX, IDC_LED2, m_led2); DDX_Control(pDX, IDC_LED3, m_led3); DDX_Control(pDX, IDC_KNAP1, m_knap1); DDX_Control(pDX, IDC_KNAP2, m_knap2); DDX_Control(pDX, IDC_TEMP, m_temp); DDX_Control(pDX, IDC_POT, m_pot); DDX_Control(pDX, IDC_IPADDRESS1, m_ip); DDX_Control(pDX, IDC_PORT, m_port); DDX_Control(pDX, IDC_DELAY, m_delay); } BEGIN_MESSAGE_MAP(CClientDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_IPPORT, OnBnClickedIpport) ON_BN_CLICKED(IDC_LED1, OnBnClickedLed1) ON_BN_CLICKED(IDC_LED2, OnBnClickedLed2) ON_BN_CLICKED(IDC_LED3, OnBnClickedLed3) ON_BN_CLICKED(IDOK, OnBnClickedOk) ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel) END_MESSAGE_MAP() // CClientDlg message handlers /*Client socket class*/ class ClientSocket : public CSocket{ public: ClientSocket() { }; virtual void OnReceive(int nErrorCode); }; BOOL CClientDlg::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 // Get data from regedit CString strip, strport; char chaip[16]; char chadelay[16]; CRegKey reg; unsigned long size = 16; LONG res=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ); if (res == ERROR_SUCCESS) { res=reg.QueryStringValue("IP", chaip, &size); } Ip = chaip; m_ip.SetWindowText(Ip); LONG res2=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ); if (res2 == ERROR_SUCCESS) { res2=reg.QueryDWORDValue("Port", Port); } strport.Format("%d",Port); m_port.SetWindowText(strport); LONG res3=reg.Open(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client",KEY_READ); if (res3 == ERROR_SUCCESS) { res3=reg.QueryStringValue("Delay",chadelay,&size); } Delay = chadelay; m_delay.SetWindowText(Delay); reg.Close(); //connect til server Start(); continueThread = true; AfxBeginThread(Status,0,THREAD_PRIORITY_NORMAL,0,0,NULL); return TRUE; // return TRUE unless you set the focus to a control } void CClientDlg::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 CClientDlg::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 CClientDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } ClientSocket sockClient; CString strIp, strPort, buff; /*Clientsocket class used for client socket connections*/ void ClientSocket::OnReceive( int nErrorCode ) { char buffer[4096]; int size; size = Receive(buffer,4095); buffer[size] = 0; buff = buffer; OutputDebugString(buff + "\n"); while (1) //homebrewed tokenizer { int pos = buff.Find('|',0); if (pos != -1) { CString token = buff.Left(pos); buff = buff.Right( buff.GetLength() - pos -1); Buffcheck(token); } else break; } //Buffcheck(buff); //Close(); } void CClientDlg::OnBnClickedIpport() { //Set new port and ip for the server in regedit m_ip.GetWindowText(strIp); m_port.GetWindowText(strPort); m_delay.GetWindowText(Delay); int test; test = atoi(Delay); if(test < 10000 && test > 100) { CRegKey reg; reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client"); reg.SetStringValue("","Project-data"); reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client"); reg.SetStringValue("IP",strIp); reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client"); reg.SetDWORDValue("Port",atoi(strPort)); reg.Create(HKEY_LOCAL_MACHINE, "SOFTWARE\\Projekt\\Client"); reg.SetStringValue("Delay",Delay); reg.Close(); } else { MessageBox("fejl i delay",0,MB_OK); } } void Start() { sockClient.Create(); if (sockClient.Connect(Ip,Port)) { } else { int error = GetLastError(); CString tmp; tmp.Format("%d", error); } } UINT Status( LPVOID Param ) { while(continueThread) { CString status; int statusint = 100; status.Format("%d",statusint); sockClient.Send(status,status.GetLength(),NULL); Sleep(atoi(Delay)); } return TRUE; } UINT Update( LPVOID param ) { sockClient.Send(Updatevariable,Updatevariable.GetLength(),NULL); return TRUE; } void Buffcheck(CString data) { int intbuff=0, temp, pot; intbuff = atoi(data); temp = intbuff; // til temperatur case pot = intbuff; // til potmeter case switch (intbuff) { case 110: /*Led 1 Slukket*/ { m_led1.SetCheck(0); break; } case 111: /*Led 1 Tændt*/ { m_led1.SetCheck(1); break; } case 120: /*Led 2 Slukket*/ { m_led2.SetCheck(0); break; } case 121: /*Led 2 Tændt*/ { m_led2.SetCheck(1); break; } case 130: /*Led 3 Slukket*/ { m_led3.SetCheck(0); break; } case 131: /*Led 3 Tændt*/ { m_led3.SetCheck(1); break; } case 140: /*Knap 1 off*/ { m_knap1.SetCheck(0); break; } case 141: /*Knap 1 on*/ { m_knap1.SetCheck(1); break; } case 150: /*Knap 2 off*/ { m_knap2.SetCheck(0); break; } case 151: /*Knap 2 on*/ { m_knap2.SetCheck(1); break; } } if (temp > 3999 && temp < 5000) //skriv temperaturen { CString strtemp; temp = (temp-4000); strtemp.Format("%d",temp); m_temp.SetWindowText(strtemp); } if (pot > 1999 && pot < 3050) //skriv potmeter værdien { CString strpot; pot = (pot-2000); strpot.Format("%d",pot); m_pot.SetWindowText(strpot); } } void CClientDlg::OnBnClickedLed1() { int led, code, send; led = 110; code = m_led1.GetCheck(); send = (led+code); Updatevariable.Format("%d",send); AfxBeginThread(Update,0,THREAD_PRIORITY_NORMAL,0,0,NULL); } void CClientDlg::OnBnClickedLed2() { int led, code, send; led = 120; code = m_led2.GetCheck(); send = (led+code); Updatevariable.Format("%d",send); AfxBeginThread(Update,0,THREAD_PRIORITY_NORMAL,0,0,NULL); } void CClientDlg::OnBnClickedLed3() { int led, code, send; led = 130; code = m_led3.GetCheck(); send = (led+code); Updatevariable.Format("%d",send); AfxBeginThread(Update,0,THREAD_PRIORITY_NORMAL,0,0,NULL); } void CClientDlg::OnBnClickedOk() { sockClient.Close(); continueThread = false; OnOK(); } void CClientDlg::OnBnClickedCancel() { sockClient.Close(); continueThread = false; OnCancel(); }