// SinglePortDlg.cpp : implementation file // #include "stdafx.h" #include "SinglePort.h" #include "SinglePortDlg.h" #include "PswdDlg.h" #include "InfoDlg.h" //#include "IPSerial.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif int port_opened = 0; // 0: close 1:open HANDLE g_ahExitEvent; HANDLE g_hMoniterEventThread; CSinglePortDlg *pointer; UINT EditBoard( LPVOID param ); UINT LineModemStatus( LPVOID param ); DWORD WINAPI MoniterEventThread( LPVOID param ); DWORD WINAPI MoniterEventThread(LPVOID param) { int port_id; port_id = *(PINT) param; CString szLogMsg = _T(""); szLogMsg.Format( "MoniterEventThread Arg Value = %d \n", port_id ); AfxTrace(szLogMsg); if( WaitForSingleObject(g_ahExitEvent, INFINITE) != WAIT_TIMEOUT) { slan_close(port_id); CIPAddressCtrl *pIpAddrCtrl = NULL; pIpAddrCtrl = (CIPAddressCtrl *)(pointer->GetDlgItem(IDC_SERVER_IPADDRESS)); pIpAddrCtrl->EnableWindow( TRUE ); // GetDlgItem(IDC_SERVER_IPADDRESS).EnableWindow(TRUE); pointer->m_PortCtrl.EnableWindow(TRUE); pointer->m_OpenCtrl.EnableWindow(TRUE); pointer->m_ResetServerCtrl.EnableWindow(TRUE); pointer->m_CloseCtrl.EnableWindow(FALSE); AfxMessageBox("Stopped!!"); } return (DWORD) -1; } UINT EditBoard(LPVOID param) { CString szLogMsg = _T(""); static int nCount=0; int ret; while( TRUE ) { //Sleep(1000); if( port_opened == 1) { char szStr[3072]; CString strTemp; CString strResult; int len = 2048; ret = slan_read(pointer->port_id, szStr, &len); if(ret<0) { SetEvent(g_ahExitEvent); Sleep(1000); CloseHandle(g_hMoniterEventThread); break; } if(!len) Sleep(1000); if( len > 0) { szStr[len] = '\0'; pointer->m_Transceiver.GetWindowText(strTemp); strResult.Format("%s%s", strTemp, szStr); pointer->m_Transceiver.SetWindowText(strResult); } } else break; } return 0; } UINT LineModemStatus( LPVOID param ) { static int nCount=0; int ret =0; CString szLogMsg = _T(""); while( TRUE ) { Sleep(8000); if( port_opened == 1) { { ret = slan_modemstatus(pointer->port_id); /* MSR Register Bit Notes ============================ Bit 7 Carrier Detect Bit 6 Ring Indicator Bit 5 Data Set Ready Bit 4 Clear To Send Bit 3 Delta Data Carrier Detect Bit 2 Trailing Edge Ring Indicator Bit 1 Delta Data Set Ready Bit 0 Delta Clear to Send */ if( ret >= 0) { szLogMsg.Format(" ModemStatus 0X%0x \n",ret); AfxTrace(szLogMsg); if( ret & 0x10 ) { pointer->m_CTSCtrl.SetWindowText("On"); } else { pointer->m_CTSCtrl.SetWindowText("Off"); } if( ret & 0x20 ) { pointer->m_DSRCtrl.SetWindowText("On"); } else { pointer->m_DSRCtrl.SetWindowText("Off"); } if( ret & 0x80 ) { pointer->m_DCDCtrl.SetWindowText("On"); } else { pointer->m_DCDCtrl.SetWindowText("Off"); } } else { SetEvent(g_ahExitEvent); Sleep(1000); CloseHandle(g_hMoniterEventThread); break; } ret = slan_linestatus(pointer->port_id); /*LSR Register Bit Notes ======================================= Bit 7 Error in Received FIFO Bit 6 Empty Data Holding Registers Bit 5 Empty Transmitter Holding Register Bit 4 Break Interrupt Bit 3 Framing Error Bit 2 Parity Error Bit 1 Overrun Error Bit 0 Data Ready */ if( ret >= 0) { szLogMsg.Format(" lineStatus 0X%0x \n",ret); AfxTrace(szLogMsg); if( ret & 0x02 )//ox01 { pointer->m_LineStatusCtrl.SetWindowText("OverRun Error"); } if( ret & 0x04 )//0x02 { pointer->m_LineStatusCtrl.SetWindowText("Parity Error"); } if( ret & 0x08 )//0x04 { pointer->m_LineStatusCtrl.SetWindowText("Framing Error"); } if( ret & 0x10 )//0x08 { pointer->m_LineStatusCtrl.SetWindowText("Break Error"); } } else { SetEvent(g_ahExitEvent); Sleep(1000); CloseHandle(g_hMoniterEventThread); break; } }// end block }//end if( port_opened ==1) else break; }//end while return 0; }// end main function ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSinglePortDlg dialog CSinglePortDlg::CSinglePortDlg(CWnd* pParent /*=NULL*/) : CDialog(CSinglePortDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSinglePortDlg) m_Port = 0; // m_ServerIP = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSinglePortDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSinglePortDlg) DDX_Control(pDX, IDC_RESET_SERVER, m_ResetServerCtrl); DDX_Control(pDX, IDC_DSR_CTRL, m_DSRCtrl); DDX_Control(pDX, IDC_DCD_CTRL, m_DCDCtrl); DDX_Control(pDX, IDC_CTS_CTRL, m_CTSCtrl); DDX_Control(pDX, IDC_LINE_STATUS, m_LineStatusCtrl); DDX_Control(pDX, IDC_OPEN, m_OpenCtrl); DDX_Control(pDX, IDC_CLOSE, m_CloseCtrl); DDX_Control(pDX, IDC_PORT, m_PortCtrl); DDX_Control(pDX, IDC_TRANSCEIVER, m_Transceiver); DDX_Text(pDX, IDC_PORT, m_Port); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSinglePortDlg, CDialog) //{{AFX_MSG_MAP(CSinglePortDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_OPEN, OnOpen) ON_BN_CLICKED(IDC_CLOSE, OnClose) ON_BN_CLICKED(IDC_RESET_SERVER, OnResetServer) ON_BN_CLICKED(IDC_CHECK_ALIVE, OnCheckAlive) ON_EN_CHANGE(IDC_TRANSCEIVER, OnChangeTransceiver) ON_BN_CLICKED(IDC_BTN_EXIT, OnBtnExit) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSinglePortDlg message handlers BOOL CSinglePortDlg::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 //m_ServerIP = "192.168.4.105"; // Test Only //m_Port = 1; // Test Only m_LineStatusCtrl.SetWindowText("Off"); m_CTSCtrl.SetWindowText("Off"); m_DSRCtrl.SetWindowText("Off"); m_DCDCtrl.SetWindowText("Off"); UpdateData(FALSE); pointer = this; m_CloseCtrl.EnableWindow(FALSE); TotalBreakCount = 0; return TRUE; // return TRUE unless you set the focus to a control } void CSinglePortDlg::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 CSinglePortDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) 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 to obtain the cursor to display while the user drags // the minimized window. HCURSOR CSinglePortDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSinglePortDlg::GetInputData() { UpdateData(TRUE); } // ===================================================================== void CSinglePortDlg::OnOpen() { // TODO: Add your control notification handler code here GetInputData(); char szTemp[128]; DWORD dwThreadId = 0; OPMODE eMode; INPUTS sParam; CString szServerIp = _T(""); CIPAddressCtrl *pIpAddrCtrl = NULL; pIpAddrCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_SERVER_IPADDRESS); pIpAddrCtrl->GetWindowText( szServerIp ); //Validate the inputs... if( szServerIp.IsEmpty() || 0==pointer->m_Port) { AfxMessageBox( "Enter the proper Inputs !!" ); return; } eMode = TCP_SRVR; sprintf(sParam.tcp.ip,szServerIp); sParam.tcp.portno = m_Port; sprintf(szTemp, szServerIp); TotalBreakCount = 0; //port_id = slan_open( szTemp, m_Port, 3000); port_id = slan_open(eMode, sParam); if( port_id >= 0 ) { AfxMessageBox("OK"); port_opened = 1; // port is opened // slan_SetReadTimeouts(port_id, MAXDWORD); slan_SetReadTimeouts(port_id, 0); CIPAddressCtrl *pIpAddrCtrl = NULL; pIpAddrCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_SERVER_IPADDRESS); pIpAddrCtrl->EnableWindow(FALSE); // m_ServerIPCtrl.EnableWindow(FALSE); m_PortCtrl.EnableWindow(FALSE); m_OpenCtrl.EnableWindow(FALSE); m_ResetServerCtrl.EnableWindow(FALSE); // m_LineStatusCtrl.EnableWindow(TRUE); m_CloseCtrl.EnableWindow(TRUE); HWND hWndEditBoard = GetSafeHwnd(); HWND hWndLineModemStatus = GetSafeHwnd(); g_ahExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL ); AfxBeginThread( EditBoard, hWndEditBoard, THREAD_PRIORITY_NORMAL ); AfxBeginThread( LineModemStatus, hWndLineModemStatus, THREAD_PRIORITY_NORMAL ); g_hMoniterEventThread = CreateThread(NULL, 0, MoniterEventThread, (LPVOID)&port_id, 0, &dwThreadId); m_Transceiver.SetFocus(); } else AfxMessageBox("Fail"); } void CSinglePortDlg::OnClose() { // TODO: Add your control notification handler code here if( port_opened == 1) { port_opened = 0; // port is closed SetEvent(g_ahExitEvent); //Sleep(1000); //CloseHandle(g_hMoniterEventThread); } } /* This function will be called when USER click ResetServer button, It will Check the Password entered by user is valid/not and prompts a msg "Invalid Password Entered" if it is incorrect one. And will Prompt a msg whether the ResetServer successed or Failed at the End. */ void CSinglePortDlg::OnResetServer() { _tcscpy(m_szServerPswd, _T("")); int ret; // To Collect the Server Password we are Launching Password Dialog CPswdDlg objPswd; if( objPswd.DoModal() == IDOK ) { // Collect ServerIP GetInputData(); char szServerIp[128]; CString szServerIpAddr = _T(""); CIPAddressCtrl *pIpAddrCtrl = NULL; pIpAddrCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_SERVER_IPADDRESS); pIpAddrCtrl->GetWindowText( szServerIpAddr ); sprintf(szServerIp, szServerIpAddr); // Call Server Reset Function with ServerIP and Server Password ret = slan_resetserver(szServerIp, m_szServerPswd); // Analyse the Return value and Prompt Msgs to User if( ret == SLAN_OK ) { AfxMessageBox(_T("Server Reset OK")); } else if(ret == SLAN_INVALID_PASSWORD ) { AfxMessageBox(_T("Invalid Password Entered..!")); } else if(ret == SLAN_RESET_TIMEOUT ) { AfxMessageBox(_T("Resetting Slan Server Timed Out..!")); } else { AfxMessageBox(_T("Server Reset Failed")); } } } /* This function will be called when USER click CheckAlive button And will Prompt a msg whether the Server is Alive or Not. */ void CSinglePortDlg::OnCheckAlive() { // TODO: Add your control notification handler code here char szServerIp[128]; int ret; // Collect ServerIP GetInputData(); CString szServerIpAddr = _T(""); CIPAddressCtrl *pIpAddrCtrl = NULL; pIpAddrCtrl = (CIPAddressCtrl *)GetDlgItem(IDC_SERVER_IPADDRESS); pIpAddrCtrl->GetWindowText( szServerIpAddr ); sprintf(szServerIp, szServerIpAddr); // Call Slan Check Alive Function ret = slan_checkalive( szServerIp, CHECK_ALIVETIMEOUT); // Analyse the Return value and Prompt Msgs to User if( ret == SLAN_OK ) AfxMessageBox(_T("Server is alive")); else if(ret == SLAN_TIMEOUT) AfxMessageBox(_T("Slan Check alive Timed Out")); else if(ret == SLAN_NOT_ALIVE) AfxMessageBox(_T("Server is not alive")); } // ===================================================================== void CSinglePortDlg::OnChangeTransceiver() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // TODO: Add your control notification handler code here if( port_opened == 1) { char cSendChar; findCharToSend(&cSendChar); int ret = slan_write(port_id, &cSendChar, 1 ); } else AfxMessageBox("Port not open!"); } void CSinglePortDlg::OnCancel() { // TODO: Add extra cleanup here } void CSinglePortDlg::OnOK() { // TODO: Add extra cleanup here } void CSinglePortDlg::OnBtnExit() { // TODO: Add your control notification handler code here if( port_opened == 1) OnClose(); CDialog::OnCancel(); } void CSinglePortDlg::findCharToSend(char* pcSendChar) { WORD wLineIndex = 0; WORD dwGetSel = 0; WORD wStart = 0; DWORD wColNumber = 0; DWORD wLineNumber = 0; DWORD cbText = 0; char szBuf[2048] = {0}; CEdit* pEdit = (CEdit*)GetDlgItem(IDC_TRANSCEIVER); wLineNumber = pEdit->SendMessage(EM_LINEFROMCHAR, -1, 0L); dwGetSel = (WORD)pEdit->SendMessage(EM_GETSEL,0, 0L); wStart = LOWORD(dwGetSel); wLineIndex = (WORD)pEdit->SendMessage(EM_LINEINDEX, -1, 0L); wColNumber = wStart - wLineIndex; *(WORD *)szBuf = sizeof(szBuf); cbText = pEdit->SendMessage(EM_GETLINE, wLineNumber,(DWORD)(LPSTR) szBuf); szBuf[cbText] = '\0'; *pcSendChar = szBuf[wColNumber-1]; } BOOL CSinglePortDlg::PreTranslateMessage(MSG* pMsg) { if ((pMsg->message == WM_KEYDOWN) && (pMsg->hwnd == GetDlgItem( IDC_TRANSCEIVER )->m_hWnd)) { int nVirtKey = (int) pMsg->wParam; if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK) return TRUE; } return CDialog::PreTranslateMessage(pMsg); }