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

📄 6kdiodlg.cpp

📁 关于台湾研华远程以太网模块上位机控制源代码
💻 CPP
字号:
// 6KDIODlg.cpp : implementation file
//

#include "stdafx.h"
#include "6KDIO.h"
#include "6KDIODlg.h"

#include <winsock2.h>
#include "..\..\..\include\adamtcp.h"

#define DeviceID       0x01  // 0x01 indicate the 5000/TCP
#define DEFAULT_PORT   502  // Port for Modbus/TCP
//-------- default timeout ------
int     iConnectionTimeout=2000;
int     iSendTimeout=2000;
int     iReceiveTimeout=2000;

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

/////////////////////////////////////////////////////////////////////////////
// CMy6KDIODlg dialog

CMy6KDIODlg::CMy6KDIODlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy6KDIODlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy6KDIODlg)
	m_strModuleName = _T("");
	m_strDeviceID = _T("");
	m_strIPAddress = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy6KDIODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy6KDIODlg)
	DDX_CBString(pDX, IDC_MODULENAME, m_strModuleName);
	DDX_Text(pDX, IDC_EDIT_DEVICEID, m_strDeviceID);
	DDX_Text(pDX, IDC_EDIT_IPADDRESS, m_strIPAddress);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy6KDIODlg, CDialog)
	//{{AFX_MSG_MAP(CMy6KDIODlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_EXIT, OnExit)
	ON_BN_CLICKED(ID_READDIO, OnReaddio)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy6KDIODlg message handlers

BOOL CMy6KDIODlg::OnInitDialog()
{
	int    iRetVal;
	char   szDump[80];

	CDialog::OnInitDialog();

	// 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_strIPAddress="192.168.1.2";
	m_strModuleName="6050";
	m_strDeviceID="1";
	SetDlgItemText(IDC_EDIT_IPADDRESS,m_strIPAddress);
	SetDlgItemText(IDC_MODULENAME,m_strModuleName);
	SetDlgItemText(IDC_EDIT_DEVICEID,m_strDeviceID);

	// get the DLL version 
	char   szCh[20];
    int    iVersion;    
 
	iVersion=ADAMTCP_GetDLLVersion();
	_ltoa( iVersion, szCh, 16 );
	SetDlgItemText(IDC_EDIT_DLLVER, szCh);

    //--- Firstly, initial DLL to working ---
    iRetVal=ADAMTCP_Open();
    if( iRetVal!=0 )
    {
        sprintf(szDump,"ADAMTCP_Open() Failure !!!   code=%d\n",iRetVal);
        MessageBox(szDump);
    }
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CMy6KDIODlg::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 CMy6KDIODlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMy6KDIODlg::OnExit() 
{
    //--- Finally, release some resource that DLL allocated---
	ADAMTCP_Close();

	// TODO: Add your control notification handler code here
	CDialog::OnOK();		
}

void CMy6KDIODlg::OnReaddio() 
{
	// TODO: Add your control notification handler code here
    CString   strText,strTemp;
	char      szIPAddress[20];
	int       i,iRetVal;
	WORD      wDeviceID;
    //WORD      wStartAddress;
    WORD      wModuleName;
    //BYTE      byData[128];
	char      szDump[80];
	BYTE      byDI[16], byDO[16];

	//--- the module name, ex 6050, 6051,... ---
	GetDlgItemText(IDC_MODULENAME,strTemp);
	wModuleName=(WORD)atoi(strTemp);

	// the Device ID
	GetDlgItemText(IDC_EDIT_DEVICEID,strTemp);
	wDeviceID=atoi(strTemp);

    // get the 5000/TCP's IP Address
	GetDlgItemText(IDC_EDIT_IPADDRESS,szIPAddress,20);
    
	//--- try to create a connection to 5000/TCP ---
    iRetVal=ADAMTCP_Connect(szIPAddress,DEFAULT_PORT,
				  iConnectionTimeout, iSendTimeout, iReceiveTimeout);

    if( iRetVal<0 )
    {
        sprintf(szDump,"ADAMTCP_Connect() Failure !!!   code=%d\n",iRetVal);
        MessageBox(szDump);
        return;
    }

	for(i=0; i<16; i++)
	{
		byDI[i]=byDO[i]=127;
	}
    //--- reading the coils ---
    iRetVal=ADAMTCP_Read6KDIO(szIPAddress,  wModuleName, wDeviceID, byDI, byDO);
    if( iRetVal )
    {
        sprintf(szDump,"ADAMTCP_Read6KDIO() Failure !!!   code=%d\n",iRetVal);
        MessageBox(szDump);
        ADAMTCP_Disconnect();
        return;
    }

	//----- show the DI value -------
	((CListBox*)GetDlgItem(IDC_LIST_DIVALUE))->ResetContent();
	for(i=0; i<16 && byDI[i]!=127; i++)
	{
		strText.Format( "DI:%02d -> %d", i,byDI[i]);
		((CListBox*)GetDlgItem(IDC_LIST_DIVALUE))->InsertString( -1, strText );
	}
	//----- show the DO value -----
	((CListBox*)GetDlgItem(IDC_LIST_DOVALUE))->ResetContent();
	for(i=0; i<16 && byDO[i]!=127; i++)
	{
		strText.Format( "DO:%02d -> %d", i,byDO[i]);
		((CListBox*)GetDlgItem(IDC_LIST_DOVALUE))->InsertString( -1, strText );
	}

    //--- disconnt connection to 5000/TCP ---
    ADAMTCP_Disconnect();
	
}

⌨️ 快捷键说明

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