// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // + FileName: CalPollDlg.cpp Author: S.Hammond // + // + Version: 1 Date: 23/June/1999 // + // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + // + This file contains the code to control the application dialog. // + // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #include "stdafx.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include #include #include #include #define _CALPOLLDLG_C_ #include "CalPoll.h" #include "Comms.h" #include "CalPollDlg.h" #include "Status.h" #include "About.h" #include "CommsDevice.h" #include "ModBus.h" // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + CCalPollDlg dialog // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CCalPollDlg::CCalPollDlg(CWnd* pParent /*=NULL*/) : CDialog(CCalPollDlg::IDD, pParent) { // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Setup global point to class // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CalPollDlg=this; //{{AFX_DATA_INIT(CCalPollDlg) //}}AFX_DATA_INIT m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCalPollDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCalPollDlg) DDX_Control(pDX, IDC_DEBUG2, m_Debug2); DDX_Control(pDX, IDC_TO, m_To); DDX_Control(pDX, IDC_FROM, m_From); DDX_Control(pDX, IDC_DEBUG, m_Debug); DDX_Control(pDX, IDC_STATUS, m_Status); DDX_Control(pDX, IDC_ADDR, m_Addr); DDX_Control(pDX, IDC_LIST1, m_List); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCalPollDlg, CDialog) //{{AFX_MSG_MAP(CCalPollDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDOK, OnPoll) ON_BN_CLICKED(IDCANCEL, OnClose) ON_LBN_DBLCLK(IDC_LIST1, OnStatus) ON_LBN_SELCHANGE(IDC_LIST1, OnListChange) ON_BN_CLICKED(IDC_STATUS, OnStatus) ON_BN_CLICKED(IDC_COMMS, OnComms) //}}AFX_MSG_MAP END_MESSAGE_MAP() // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Reset the CALPoll main dialog // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ BOOL CCalPollDlg::OnInitDialog() { CDialog::OnInitDialog(); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Add "About..." menu item to system menu. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 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); } } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Setup icon images // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Set the list extent // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ m_List.SetHorizontalExtent(350); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Reset the scan range // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ m_From.SetWindowText("1"); m_To.SetWindowText("10"); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Disable the status button until after a scan // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ m_Status.EnableWindow(FALSE); m_Debug.SetWindowText(""); m_Debug2.SetWindowText(""); return(TRUE); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Handle system messages // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void CCalPollDlg::OnSysCommand(UINT nID, LPARAM lParam) { CAboutDlg dlgAbout; if ((nID & 0xFFF0) == IDM_ABOUTBOX) { 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 CCalPollDlg::OnPaint() { if (IsIconic()) { // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + device context for painting // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CPaintDC dc(this); 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(); } } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Return the icon handle for dragging // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ HCURSOR CCalPollDlg::OnQueryDragIcon() { return( (HCURSOR) m_hIcon ); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Handle to close operation // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void CCalPollDlg::OnClose() { CommsDevice.CloseComms(); CDialog::OnOK(); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + The user has pressed the Poll button // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void CCalPollDlg::OnPoll() { BYTE Addr; char TempString[256]; BYTE From; BYTE To; // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Do we have an open communications port ? // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if ( CommsDevice.CheckComms()==FALSE ) { OnComms(); if ( CommsDevice.CheckComms()==FALSE ) { return; } } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Show hourglass while polling // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CWaitCursor ShowGlass; // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Reset listbox // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ m_List.ResetContent(); m_Status.EnableWindow(FALSE); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Fetch scanning range from dialog and limit to 1 to 247 // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ m_From.GetWindowText(TempString,256); From=(BYTE)max(1,atoi(TempString)); m_To.GetWindowText(TempString,256); To=(BYTE)min(247,atoi(TempString)); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Scan the required modbus address range // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ for (Addr=From;Addr<(To+1);Addr++) { // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Show the current Modbus address // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sprintf(TempString,"%u",Addr); m_Addr.SetWindowText(TempString); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Read the Comms (LEVEL C) Address register // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ModBus.ModReadSingle(Addr,MODBUS_CAL_LEVC_ADDR); if( ModBus.ValidResponse() ) { // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Does it match the address we are talking to ? // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if ( ModBus.GetByte(4)==Addr ) { // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Second check using the Comms baud rate register // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ModBus.ModReadSingle(Addr,MODBUS_CAL_LEVC_BAUD); if( ModBus.ValidResponse() ) { // +++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Does it match the baud rate we are using ? // +++++++++++++++++++++++++++++++++++++++++++++++++++++++ if ( CommsDevice.CheckBaud(ModBus.GetByte(4)) ) { // +++++++++++++++++++++++++++++++++++++++++++++++++++ // + Fetch model infomation // +++++++++++++++++++++++++++++++++++++++++++++++++++ ModBus.QueryDevice(Addr,TempString); m_List.AddString(TempString); } } } } } } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + The user has pressed the Status button // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void CCalPollDlg::OnStatus() { CStatus StatusDialog; int Index; char TempString[1024]; char *ptr; int Slave; // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Fetch the index into the list of ModBus devices // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Index=m_List.GetCurSel(); if ( Index==LB_ERR ) { AfxMessageBox("Invalid index in list box"); return; } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Fetch the line of text from that selection // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ m_List.GetText(Index,TempString); strlwr(TempString); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Find the slave address number // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ptr=strstr(TempString,"0x"); if ( ptr==NULL ) { AfxMessageBox("Invalid entry in list box"); return; } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + And convert back into a number // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ptr+=2; if ( *ptr>'9' ) { Slave=*ptr-'a'+10; } else { Slave=*ptr-'0'; } Slave*=16; ptr++; if ( *ptr>'9' ) { Slave+=*ptr-'a'+10; } else { Slave+=*ptr-'0'; } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Set the slave address into the dialog, and run the dialog // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ StatusDialog.SetSlave(Slave); StatusDialog.DoModal(); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Called when the list selection changes // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void CCalPollDlg::OnListChange() { int Index; // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + If the selection is valid then enable the Status button // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Index=m_List.GetCurSel(); if ( Index==LB_ERR ) { m_Status.EnableWindow(FALSE); } else { m_Status.EnableWindow(TRUE); } } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Handle the "Setup Comms" button action. // + First get the required comms setup from the dialog and // + then setup the hardware device. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void CCalPollDlg::OnComms() { CComms CommsDialog; COMMSSETUP Setup; // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Ask the user for the communications setup // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if ( CommsDialog.DoModal()!=IDOK ) { // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + User aborted changes // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ return; } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Fetch a copy of the required communications setup // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CommsDialog.GetCommsSetup(&Setup); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // + Open a new communications handle // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CommsDevice.OpenComms(&Setup); }