?? itkplcdlg.cpp
字號:
// @dlg
//
// @module itkPlcDlg.cpp
//
//
// Maintenance:
//
// Version Date Who What
// ------- -------- --- -------------------------------------
// 7.0 06/01/97 jra Created
// 7.1 04/30/98 mvs Added TCP/IP Support
// 7.1 05/20/98 mvs Added UDP Support
//
#include "stdafx.h"
#include "ItkPlc.h"
#include "ItkPlcDlg.h"
#include "PlcMemDlg.h"
#include "CommStatsDlg.h"
#include "AboutDlg.h"
// Callback function for Tcp Server
void EXPORT32 CALLBACK TcpSrvCallback(LPVOID, SOCKET, DWORD);
void EXPORT32 CALLBACK UdpSrvCallback(LPVOID, SOCKADDR, DWORD);
//
// Union used to convert a 32-bit hex number to an IEEE
// floating point value.
//
union LFConvert
{
long l;
float f;
} long2float[5];
#define WAIT_TIME 5L
//----(Member Function)-------------------------------------------------------
//
// @mfunc | CItkPlcDlg | CItkPlcDlg |
//
// Constructor
// Perform any memory allocation here.
//
CItkPlcDlg::CItkPlcDlg(CWnd* pParent /*=NULL*/)
: CDialog(CItkPlcDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CItkPlcDlg)
m_datatype = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//
// Initialize data members
//
this->m_dwTransmitIndex = 0;
this->m_dwProtocol = SERIAL_PROTOCOL;
this->m_hIoThread = NULL;
this->m_bShutdown = FALSE;
this->m_nTimerRefreshRate = 1000;
this->m_dwLocalStation = 0;
this->m_dwRemoteStation = 1;
this->m_szPortName = "COM1";
this->m_szPortMode = "9600,n,8,1";
this->m_bUnsolicited = FALSE;
this->m_bSourceStn = FALSE;
this->m_bTransNum = FALSE;
this->m_nTransactionNumber = 0x0FF0;
this->m_szIpAddress = "3.4.5.6";
this->m_uPortNumber = 20000;
this->m_pTcp = (CTcpIp *)NULL;
//
// Initialize the Timer Map to False
//
memset(&this->m_nTimerMap, 0, (PLC_MEMORY_SIZE * sizeof(int)));
//
// Get the memory for the Timer Dialog box
//
m_pdlgTriggers = new CTriggerDlg(PLC_MEMORY_SIZE);
m_pdlgTriggers->InitTimerMap();
//
// Get the memory for the Settings Dialog box
//
m_pdlgSettings = new CSettingsDlg;
m_pdlgSettings->SetProtocol(this->m_dwProtocol);
m_pdlgSettings->SetPortName(this->m_szPortName);
m_pdlgSettings->SetPortMode(this->m_szPortMode);
m_pdlgSettings->SetIpAddress(this->m_szIpAddress);
m_pdlgSettings->SetPortNumber(this->m_uPortNumber);
m_pdlgSettings->SetSendUnsol(this->m_bUnsolicited);
m_pdlgSettings->SetRemoteStation(this->m_dwRemoteStation);
m_pdlgSettings->SetLocalStation(this->m_dwLocalStation);
//
// Get the memory for the Comm Stats Dialog box
//
m_pdlgCommStats = new CCommStatsDlg;
//
// Initialize a critical section to be used by the various
// threads that may attempt to send at the same time
//
InitializeCriticalSection(&this->m_Lock);
}
//----(Member Function)-------------------------------------------------------
//
// @mfunc | CItkPlcDlg | ~CItkPlcDlg |
//
// Destructor
// Perform and dynamically allocated memory cleanup here.
//
CItkPlcDlg::~CItkPlcDlg()
{
if (m_pdlgTriggers)
{
delete m_pdlgTriggers;
}
if (m_pdlgSettings)
{
delete m_pdlgSettings;
}
if (m_pdlgCommStats)
{
delete m_pdlgCommStats;
}
}
//----(Member Function)-------------------------------------------------------
//
// @mfunc void | CItkPlcDlg | DoDataExchange |
//
//
void CItkPlcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CItkPlcDlg)
DDX_Control(pDX, IDC_PLC_DATA, m_DataList);
DDX_Radio(pDX, IDC_DATATYPE_UNSIGNED, m_datatype);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CItkPlcDlg, CDialog)
//{{AFX_MSG_MAP(CItkPlcDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_SETTINGS_BUTTON, OnSettingsButton)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_TRIGGERS, OnTriggers)
ON_LBN_DBLCLK(IDC_PLC_DATA, OnDblclkPlcData)
ON_BN_CLICKED(IDC_DATATYPE_FLOAT, OnDatatypeFloat)
ON_BN_CLICKED(IDC_DATATYPE_HEX, OnDatatypeHex)
ON_BN_CLICKED(IDC_DATATYPE_LONG, OnDatatypeLong)
ON_BN_CLICKED(IDC_DATATYPE_SIGNED, OnDatatypeSigned)
ON_BN_CLICKED(IDC_DATATYPE_UNSIGNED, OnDatatypeUnsigned)
ON_BN_CLICKED(IDC_CLEAR_MEMORY, OnClearMemory)
ON_BN_CLICKED(IDC_DATATYPE_ASCII, OnDatatypeAscii)
ON_BN_CLICKED(IDC_SHOW_STATS, OnShowStats)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//----(Member Function)-------------------------------------------------------
//
// @mfunc BOOL | CItkPlcDlg | OnInitDialog |
//
// Initializes class members.
// Place any class specific initialization here.
//
BOOL CItkPlcDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
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.
//
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
//
// Initialize the data array to equal there register number
// (zero based)
//
for (int i = 0; i < PLC_MEMORY_SIZE; i++ )
{
this->PlcMemory[i] = i;
}
//
// Initialize the list box - 68 rows * 15 registers = 1020. Add 4 more
// to last row to bring it to 1024
//
int nNumberOfRows = PLC_MEMORY_SIZE / REGISTERS_PER_ROW;
for (i = 0; i < nNumberOfRows; i++)
{
UpdateRow(i);
}
this->m_DataList.AddString("01020 01021 01022 01023");
m_datatype = DT_UNSIGNED;
UpdateData(FALSE);
//
// seed the random number generator for the transaction number
//
srand( (unsigned)time( NULL ) );
return(TRUE);
}
//----(Member Function)-------------------------------------------------------
//
// @mfunc void | CItkPlcDlg | OnSysCommand |
//
// Default system command handler.
//
void CItkPlcDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
//----(Member Function)-------------------------------------------------------
//
// @mfunc void | CItkPlcDlg | OnPaint |
//
// Default OnPaint handler. This function is executed when the application
// receives a WM_PAINT message
//
void CItkPlcDlg::OnPaint()
{
if (IsIconic())
{
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;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//----(Member Function)-------------------------------------------------------
//
// @mfunc void | CItkPlcDlg | OnQueryDragIcon |
//
// Default OnQueryDragIcon() handler.
//
HCURSOR CItkPlcDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//----(Member Function)-------------------------------------------------------
//
// @mfunc void | CItkPlcDlg | OnTimer |
//
// Called when the application reveives a WM_TIMER message. The timer is
// used to update all PLC registers in the timer map.
//
void CItkPlcDlg::OnTimer(UINT nIDEvent)
{
switch (nIDEvent)
{
case REGISTER_TIMER:
{
int nCurrentRow = 0;
BOOL bUpdate = FALSE;
for (int i = 0; i < PLC_MEMORY_SIZE; i++)
{
if (this->m_nTimerMap[i] == TRUE)
{
//
// Update the register in the PLC memory map
//
PlcMemory[i]++;
if (i > PLC_MEMORY_SIZE)
{
break;
}
//
// Set the flag to say something has changed in the row
//
bUpdate = TRUE;
}
//
// If we have checked every register in this row, and
// something has changed in the row, then update the row.
//
if (bUpdate && (nCurrentRow < (int)(i / REGISTERS_PER_ROW)))
{
UpdateRow(nCurrentRow);
//
// Clear the flag because we have updated the row
//
bUpdate = FALSE;
}
nCurrentRow = i / REGISTERS_PER_ROW;
}
//
// If the bUpdate flag is still set, it's because the update
// was for the last row and the above for loop will miss it,
// so update it here.
//
if (bUpdate)
{
UpdateRow(nLastRow);
}
}
break;
}
CDialog::OnTimer(nIDEvent);
}
//----(Member Function)-------------------------------------------------------
//
// @mfunc void | CItkPlcDlg | UpdateRow |
//
// Resposible for updating a row in the PLC's memory map.
//
void CItkPlcDlg::UpdateRow(int row)
{
CString str;
int nRow;
m_DataList.SetRedraw(FALSE);
nRow = m_DataList.GetTopIndex();
//
// Format the data dependant on the selected datatype view
//
switch (m_datatype)
{
case DT_ASCII:
//
// If the character is printable, then display it. If it is not
// a printable character, then display a period ('.').
//
if (row == nLastRow)
{
str.Format("%2c2%c %2c%2c %2c%2c %2c%2c",
isprint((unsigned char)(PlcMemory[(row*10)+0])) ? (unsigned char)(PlcMemory[(row*10)+0]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+0] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+0] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+1])) ? (unsigned char)(PlcMemory[(row*10)+1]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+1] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+1] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+2])) ? (unsigned char)(PlcMemory[(row*10)+2]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+2] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+2] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+3])) ? (unsigned char)(PlcMemory[(row*10)+3]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+3] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+3] >> 8) : '.');
}
else
{
str.Format("%2c%2c %2c%2c %2c%2c %2c%2c %2c%2c %2c%2c %2c%2c %2c%2c %2c%2c %2c%2c",
isprint((unsigned char)(PlcMemory[(row*10)+0])) ? (unsigned char)(PlcMemory[(row*10)+0]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+0] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+0] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+1])) ? (unsigned char)(PlcMemory[(row*10)+1]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+1] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+1] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+2])) ? (unsigned char)(PlcMemory[(row*10)+2]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+2] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+2] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+3])) ? (unsigned char)(PlcMemory[(row*10)+3]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+3] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+3] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+4])) ? (unsigned char)(PlcMemory[(row*10)+4]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+4] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+4] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+5])) ? (unsigned char)(PlcMemory[(row*10)+5]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+5] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+5] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+6])) ? (unsigned char)(PlcMemory[(row*10)+6]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+6] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+6] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+7])) ? (unsigned char)(PlcMemory[(row*10)+7]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+7] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+7] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+8])) ? (unsigned char)(PlcMemory[(row*10)+8]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+8] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+8] >> 8) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+9])) ? (unsigned char)(PlcMemory[(row*10)+9]) : '.',
isprint((unsigned char)(PlcMemory[(row*10)+9] >> 8)) ? (unsigned char)(PlcMemory[(row*10)+9] >> 8) : '.');
}
break;
case DT_FLOAT:
//
// This is a union used to convert a LONG value into an
// IEEE floating point value
//
long2float[0].l = (PlcMemory[(row*10)+1] << 16) + PlcMemory[(row*10)+0];
long2float[1].l = (PlcMemory[(row*10)+3] << 16) + PlcMemory[(row*10)+2];
long2float[2].l = (PlcMemory[(row*10)+5] << 16) + PlcMemory[(row*10)+4];
long2float[3].l = (PlcMemory[(row*10)+7] << 16) + PlcMemory[(row*10)+6];
long2float[4].l = (PlcMemory[(row*10)+9] << 16) + PlcMemory[(row*10)+8];
if (row == nLastRow)
{
str.Format("%06e %06e", long2float[0].f, long2float[1].f);
}
else
{
str.Format("%06e %06e %06e %06e %06e",
long2float[0].f, long2float[1].f, long2float[2].f,
long2float[3].f, long2float[4].f);
}
break;
case DT_LONG:
if (row == nLastRow)
{
str.Format(" %011lu %011lu",
(long)((PlcMemory[(row*10)+1] << 16) + (PlcMemory[(row*10)+0])),
(long)((PlcMemory[(row*10)+3] << 16) + (PlcMemory[(row*10)+2])));
}
else
{
str.Format(" %011lu %011lu %011lu %011lu %011lu",
(long)((PlcMemory[(row*10)+1] << 16) + (PlcMemory[(row*10)+0])),
(long)((PlcMemory[(row*10)+3] << 16) + (PlcMemory[(row*10)+2])),
(long)((PlcMemory[(row*10)+5] << 16) + (PlcMemory[(row*10)+4])),
(long)((PlcMemory[(row*10)+7] << 16) + (PlcMemory[(row*10)+6])),
(long)((PlcMemory[(row*10)+9] << 16) + (PlcMemory[(row*10)+8])));
}
break;
case DT_HEX:
if (row == nLastRow)
{
str.Format("%04X %04X %04X %04X",
PlcMemory[(row*10)+0],PlcMemory[(row*10)+1],PlcMemory[(row*10)+2],
PlcMemory[(row*10)+3]);
}
else
{
str.Format("%04X %04X %04X %04X %04X %04X %04X %04X %04X %04X",
PlcMemory[(row*10)+0],PlcMemory[(row*10)+1],PlcMemory[(row*10)+2],
PlcMemory[(row*10)+3],PlcMemory[(row*10)+4],PlcMemory[(row*10)+5],
PlcMemory[(row*10)+6],PlcMemory[(row*10)+7],PlcMemory[(row*10)+8],
PlcMemory[(row*10)+9]);
}
break;
case DT_SIGNED:
if (row == nLastRow)
{
str.Format("%+06hd %+06hd %+06hd %+06hd",
(signed short int)PlcMemory[(row*10)+0], (signed short int)PlcMemory[(row*10)+1],
(signed short int)PlcMemory[(row*10)+2], (signed short int)PlcMemory[(row*10)+3]);
}
else
{
str.Format("%+06hd %+06hd %+06hd %+06hd %+06hd %+06hd %+06hd %+06hd %+06hd %+06hd",
(signed short int)PlcMemory[(row*10)+0], (signed short int)PlcMemory[(row*10)+1],
(signed short int)PlcMemory[(row*10)+2], (signed short int)PlcMemory[(row*10)+3],
(signed short int)PlcMemory[(row*10)+4], (signed short int)PlcMemory[(row*10)+5],
(signed short int)PlcMemory[(row*10)+6], (signed short int)PlcMemory[(row*10)+7],
(signed short int)PlcMemory[(row*10)+8], (signed short int)PlcMemory[(row*10)+9]);
}
break;
case DT_UNSIGNED:
default:
if (row == nLastRow)
{
str.Format("%05u %05u %05u %05u",
PlcMemory[(row*10)+0], PlcMemory[(row*10)+1],
PlcMemory[(row*10)+2], PlcMemory[(row*10)+3]);
}
else
{
str.Format("%05u %05u %05u %05u %05u %05u %05u %05u %05u %05u",
PlcMemory[(row*10)+0],PlcMemory[(row*10)+1],PlcMemory[(row*10)+2],
PlcMemory[(row*10)+3],PlcMemory[(row*10)+4],PlcMemory[(row*10)+5],
PlcMemory[(row*10)+6],PlcMemory[(row*10)+7],PlcMemory[(row*10)+8],
PlcMemory[(row*10)+9]);
}
break;
}
m_DataList.DeleteString(row);
m_DataList.InsertString(row,str);
m_DataList.SetTopIndex(nRow);
m_DataList.SetRedraw(TRUE);
}
//----(Member Function)-------------------------------------------------------
//
// @mfunc void | CItkPlcDlg | OnExit |
//
// Responsible for cleaning up the threads and timers.
//
void CItkPlcDlg::OnExit()
{
//
// Set the shutdown flag to end the threads
//
this->m_bShutdown = TRUE;
//
// Hide the window because it may take a second or
// two to clean everything up.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -