⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memoryeditorlist.cpp

📁 用于开发Modbus工业级通信
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////
//
// FILE: MemoryEditorList.cpp : implementation file
//
// See "_README.CPP"
//
// Implements a owner data (callback) style CListCtrl
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "mod_RSsim.h"
#include "MemoryEditorList.h"
#include "mod_RSsimdlg.h"
#include "EditMemoryDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CHAR * PLCViewerFormats[7] =
{
   "",
   "%d",    //   VIEWFORMAT_DECIMAL =1,
   "%04X",  //   VIEWFORMAT_HEX,
   "%u",    //   VIEWFORMAT_WORD,
   "%u",    //   VIEWFORMAT_DWORD,
   "%ld",   //   VIEWFORMAT_LONG,
   "%f"     //   VIEWFORMAT_FLOAT
};

CHAR * ViewerFormatNames[7] =
{
   "",
   "decimal",     //   VIEWFORMAT_DECIMAL =1,
   "hex",         //   VIEWFORMAT_HEX,
   "word 16",     //   VIEWFORMAT_WORD,
   "dword 32",    //   VIEWFORMAT_DWORD,
   "long 32",     //   VIEWFORMAT_LONG,
   "float 32"     //   VIEWFORMAT_FLOAT
};
/*
// --------------- SwopWords (Global Generic) --------------------------------
// PURPOSE : Converts DWORD from large endian to small endian
// (and back if called again)
DWORD SwopWords(DWORD * x)
{
DWORD loWord;
DWORD hiWord;

  loWord = LOWORD(*x);
  hiWord = HIWORD(*x);
  *x = (loWord<<16) + hiWord ;
  return(*x);
} // SwopWords*/

// ------------------------- GetPLCMemoryLimit --------------------------------
DWORD CMOD_simDlg::GetPLCMemoryLimit(DWORD area)
{
   //if (m_busyCreatingServers)
   //   return(0);
   return PLCMemory[area].GetSize();
}

// ---------------------------- GetPLCMemoryValue -----------------------------
DWORD CMOD_simDlg::GetPLCMemoryValue(DWORD area,DWORD offset, WORD type)
{
DWORD dwValue=0;

   ASSERT(area < (DWORD)GetNumMemoryAreas());
   if (offset >= MAX_MOD_MEMWORDS)
      return (0);
   //ASSERT(offset < MAX_MOD_MEMWORDS);
   switch(type)
   {
   case CMemoryEditorList::VIEWFORMAT_DECIMAL:
   case CMemoryEditorList::VIEWFORMAT_HEX:
   case CMemoryEditorList::VIEWFORMAT_WORD:
      dwValue = PLCMemory[area][offset];
      break;
   case CMemoryEditorList::VIEWFORMAT_DWORD:
   case CMemoryEditorList::VIEWFORMAT_LONG:
      dwValue = PLCMemory[area][offset]<<16;
      if (offset < MAX_MOD_MEMWORDS)
         dwValue += PLCMemory[area][offset+1];
      break;
   case CMemoryEditorList::VIEWFORMAT_FLOAT:
      dwValue = PLCMemory[area][offset]<<16;
      if (offset < MAX_MOD_MEMWORDS)
         dwValue += PLCMemory[area][offset+1];
      if (pGlobalDialog->IsClone())
         SwopWords(&dwValue);          //clone PLC's have a swapped float
      break;
   default:
      ASSERT(0);
      break;
   }
   return (dwValue);
} // GetPLCMemoryValue

/////////////////////////////////////////////////////////////////////////////
// CMemoryEditorList
class CMOD_simDlg;

extern CMOD_simDlg *pGlobalDialog;

CMemoryEditorList::CMemoryEditorList()
{
   for (int i=0;i<sizeof(m_selectedListPos)/sizeof(m_selectedListPos[0])/*MAX_MOD_MEMTYPES */;i++)
      m_selectedListPos[i] = 0;

   m_memAreaDisplayed = (DWORD)-1;
   SetAreaDisplayed(MODBUS_MEM_REGISTERS);
   SetDisplayedWidth(10);
   m_addressFormatHEX = FALSE;
   SetDataViewFormat(VIEWFORMAT_DECIMAL);
}

CMemoryEditorList::~CMemoryEditorList()
{

}

// ------------------------------ SetAreaDisplayed -----------------------------
LONG CMemoryEditorList::SetAreaDisplayed(LONG PLCArea)
{
   // save the current place.
POSITION pos;
   
   // make sure that the control at least exists
   if ((DWORD)-1 !=m_memAreaDisplayed)
   {
      pos = GetFirstSelectedItemPosition();
      m_selectedListPos[m_memAreaDisplayed] = GetNextSelectedItem(pos);
#ifdef _UI_DEBUGGING
      CString msg;
      msg.Format("Selection was at %d in %d.\n", m_selectedListPos[m_memAreaDisplayed],m_memAreaDisplayed);
      OutputDebugString(msg);
#endif // _UI_DEBUGGING
   }
   else
      m_selectedListPos[m_memAreaDisplayed] = 0; // first time in here
   m_memAreaDisplayed = PLCArea;
   return (m_memAreaDisplayed);
} // SetAreaDisplayed

// ---------------------------------- GetViewFormat ----------------------------
// return a suitable format for the current data type
CMemoryEditorList::ListViewFormats_ CMemoryEditorList::GetViewFormat()
{
   if ((pGlobalDialog->m_selectedProtocol == PROTOCOL_SELJOY232) ||
       (pGlobalDialog->m_selectedProtocol == PROTOCOL_SELAB232))
      return(m_viewFormat);

   //
   switch (m_memAreaDisplayed)
   {
   case MODBUS_MEM_OUTPUTS:
   case MODBUS_MEM_INPUTS:
      return (VIEWFORMAT_DECIMAL);
      break;
   default:
      break;
   }
   return (m_viewFormat);
}

// ----------------------------- GetViewFormatString ---------------------------
LPCTSTR CMemoryEditorList::GetViewFormatString()
{
   // call our member Fn
   return (PLCViewerFormats[GetViewFormat()]);
}

// --------------------------------- GetFormatName ----------------------------
LPCTSTR CMemoryEditorList::GetFormatName()
{
   // call our member Fn
   return (ViewerFormatNames[GetViewFormat()]);
}

// ------------------------------ SetupColumns --------------------------------
// populate the control's headdings
void CMemoryEditorList::SetupColumns(LONG width, BOOL cloneAddressing)
{
int nColumnCount;

   m_displayableWidth = (WORD)width-1;
   m_cloneAddressing = cloneAddressing;
   // start out fresh
   nColumnCount = GetHeaderCtrl()->GetItemCount();

   // Delete all of the columns.
   for (int i=0;i < nColumnCount;i++)
   {
      DeleteColumn(0);
   }

   // set up the column headings on the list-view control
   {   
      LV_COLUMN   lvColumn;
      int         i;
      TCHAR       szString[NUMBER_LISTCOLS][20] = {TEXT(" Address"),  
                                                   TEXT(" +0"), 
                                                   TEXT(" +1"), 
                                                   TEXT(" +2"), 
                                                   TEXT(" +3"), 
                                                   TEXT(" +4"), 
                                                   TEXT(" +5"), 
                                                   TEXT(" +6"), 
                                                   TEXT(" +7"), 
                                                   TEXT(" +8"), 
                                                   TEXT(" +9")};
      if (pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed))
      {
         // provide headdings for bit addresses
         strcpy(szString[0],  TEXT(" Address") );
         for (int i=0 ; i<=15 ; i++)
         {
            if (cloneAddressing)
               sprintf(szString[i+1], TEXT(" +%d"), i );
            else
               sprintf(szString[i+1], TEXT(" +%d"), 15-i );
         }
         strcpy(szString[i+1],  TEXT(" Total") );
         m_displayableWidth++;
      }

      // initialize the columns
      lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
      lvColumn.fmt = LVCFMT_LEFT;
      lvColumn.cx = WIDTH_LISTCOL1;   // 1st col is wider
      for(i = 0; i < (int)m_displayableWidth+1; i++)
      {
         lvColumn.pszText = szString[i];
         
         // work out width for all columns except the "address" column
         if (i!=0)
         {
            if (pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed))
            { // col width for all bits
               if (i!=17)  // all columns but the last one "total"
               {
                  lvColumn.cx = WIDTH_LISTCOLBIT;  // digitals are narrower
                  if (strlen(szString[i])==3)    // Make 0..9 narrower still
                     lvColumn.cx -=6;   // make it narrower by 5 DLU's
               }
               else
                  lvColumn.cx = WIDTH_LISTCOLTOTAL; // "total" column
            }
            else
            {
               // col width for a register
               lvColumn.cx = WIDTH_LISTCOLS;
            }
         }
         SendMessage(LVM_INSERTCOLUMN, (WPARAM)i, (LPARAM)&lvColumn);
      }

      if (pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed))
         m_displayableWidth--;     // total column is not data

   }
   // set listview item count to maximum
   DWORD    maximumOffset = pGlobalDialog->GetPLCMemoryLimit(m_memAreaDisplayed);

   WORD numListItem = (WORD)ceil(maximumOffset/*MAX_MOD_MEMWORDS*//((float)m_displayableWidth));
   SendMessage(LVM_SETITEMCOUNT, (WPARAM)(numListItem), (LPARAM)LVSICF_NOINVALIDATEALL);
   
   EnsureVisible(m_selectedListPos[m_memAreaDisplayed], FALSE);
} // SetupColumns


BEGIN_MESSAGE_MAP(CMemoryEditorList, CListCtrl)
	//{{AFX_MSG_MAP(CMemoryEditorList)
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	ON_NOTIFY_REFLECT(LVN_GETDISPINFO, OnGetdispinfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CMemoryEditorList message handlers

// --------------------------- OnDblclk ---------------------------------
void CMemoryEditorList::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{
POINT pt;
RECT  rect;
CHAR position[80];
LVHITTESTINFO htInfo;
LONG absItemNumber;

	// TODO: Add your control notification handler code here
   // get cursor position

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -