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

📄 6ksetdodlg.cpp

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

#include "stdafx.h"
#include "6KSetDO.h"
#include "6KSetDODlg.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

/////////////////////////////////////////////////////////////////////////////
// CMy6KSetDODlg dialog

CMy6KSetDODlg::CMy6KSetDODlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy6KSetDODlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy6KSetDODlg)
	m_nDO0 = -1;
	m_nDO1 = -1;
	m_nDO2 = -1;
	m_nDO3 = -1;
	m_nDO4 = -1;
	m_nDO5 = -1;
	m_nDO6 = -1;
	m_nDO7 = -1;
	m_nDO8 = -1;
	m_nDO9 = -1;
	m_nDO10 = -1;
	m_nDO11 = -1;
	m_nDO12 = -1;
	m_nDO13 = -1;
	m_nDO14 = -1;
	m_nDO15 = -1;
	m_strIPAddress = _T("");
	m_strDeviceID = _T("");
	m_strModuleName = _T("");
	m_strNoOfWrite = _T("");
	m_strStartDO = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy6KSetDODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy6KSetDODlg)
	DDX_Radio(pDX, IDC_RADIO1, m_nDO0);
	DDX_Radio(pDX, IDC_RADIO3, m_nDO1);
	DDX_Radio(pDX, IDC_RADIO5, m_nDO2);
	DDX_Radio(pDX, IDC_RADIO7, m_nDO3);
	DDX_Radio(pDX, IDC_RADIO9, m_nDO4);
	DDX_Radio(pDX, IDC_RADIO11, m_nDO5);
	DDX_Radio(pDX, IDC_RADIO13, m_nDO6);
	DDX_Radio(pDX, IDC_RADIO15, m_nDO7);
	DDX_Radio(pDX, IDC_RADIO17, m_nDO8);
	DDX_Radio(pDX, IDC_RADIO19, m_nDO9);
	DDX_Radio(pDX, IDC_RADIO21, m_nDO10);
	DDX_Radio(pDX, IDC_RADIO23, m_nDO11);
	DDX_Radio(pDX, IDC_RADIO25, m_nDO12);
	DDX_Radio(pDX, IDC_RADIO27, m_nDO13);
	DDX_Radio(pDX, IDC_RADIO29, m_nDO14);
	DDX_Radio(pDX, IDC_RADIO31, m_nDO15);
	DDX_Text(pDX, IDC_EDIT_IPADDRESS, m_strIPAddress);
	DDX_Text(pDX, IDC_EDIT_DEVICEID, m_strDeviceID);
	DDX_CBString(pDX, IDC_MODULENAME, m_strModuleName);
	DDX_Text(pDX, IDC_EDIT_NOOFWRITE, m_strNoOfWrite);
	DDX_Text(pDX, IDC_EDIT_STARTDO, m_strStartDO);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy6KSetDODlg, CDialog)
	//{{AFX_MSG_MAP(CMy6KSetDODlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_EXIT, OnExit)
	ON_BN_CLICKED(ID_SETDO, OnSetdo)
	ON_CBN_EDITCHANGE(IDC_MODULENAME, OnEditchangeModulename)
	ON_CBN_EDITUPDATE(IDC_MODULENAME, OnEditupdateModulename)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy6KSetDODlg message handlers

BOOL CMy6KSetDODlg::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";
	m_strStartDO="0";
	m_strNoOfWrite="2";
	SetDlgItemText(IDC_EDIT_IPADDRESS,m_strIPAddress);
	SetDlgItemText(IDC_MODULENAME,m_strModuleName);
	SetDlgItemText(IDC_EDIT_DEVICEID,m_strDeviceID);
	SetDlgItemText(IDC_EDIT_STARTDO,m_strStartDO);
	SetDlgItemText(IDC_EDIT_NOOFWRITE,m_strNoOfWrite);

    //--- initial all the DO to 0 ---
	CButton*  pRadioButton;
	int       nID,i;

    nID=IDC_RADIO1;
	for(i=0; i<16; i++)
	{

	    pRadioButton=(CButton *)GetDlgItem(nID+2*i);
		pRadioButton->SetCheck(BST_CHECKED);
	}

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

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

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

void CMy6KSetDODlg::OnSetdo() 
{
	// TODO: Add your control notification handler code here
    CString   strText,strTemp;
	char      szIPAddress[20];
	int       i,nID,iRetVal;
	WORD      wDeviceID;
	WORD      wModuleName;
    WORD      wStartDO;
    WORD      wCount;
    BYTE      byData[128];  
	int       DOValue[16];
	CButton*  pRadioButton;
	char      szDump[80];

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

    nID=IDC_RADIO1;
	for(i=0; i<16; i++)
	{

	    pRadioButton=(CButton *)GetDlgItem(nID+2*i);
		if( pRadioButton->GetCheck()==BST_CHECKED )
			DOValue[i]=0;
		else
			DOValue[i]=1;
	}

	// How many DO to write
	GetDlgItemText(IDC_EDIT_NOOFWRITE,strTemp);
	wCount=atoi(strTemp);
	if( wCount<0 || wCount>16 )
	{
		MessageBox("The # of D/O must between 1 to 16 !");
		return;
	}

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

	// the starting D/O to write
	GetDlgItemText(IDC_EDIT_STARTDO,strTemp);
	wStartDO=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);
        ADAMTCP_Disconnect();
        return;
    }

    //--- prepare data to write to coil ---
	for(i=0; i<wCount; i++)
		byData[i]=DOValue[wStartDO+i];

    //--- writing the coils ---
	iRetVal=ADAMTCP_Write6KDO(szIPAddress, wModuleName, wDeviceID, wStartDO, wCount, byData);
    if( iRetVal )
    {
        sprintf(szDump,"ADAMTCP_WriteCoil() Failure !!!   code=%d\n",iRetVal);
        MessageBox(szDump);
        ADAMTCP_Disconnect();
        return;
    }

    //--- lastly, disconnet connection to 5000/TCP ---
    ADAMTCP_Disconnect();	
}

void CMy6KSetDODlg::OnEditchangeModulename() 
{
	// TODO: Add your control notification handler code here
	//MessageBox("Device Name Editchange");
}

void CMy6KSetDODlg::OnEditupdateModulename() 
{
	// TODO: Add your control notification handler code here
	//MessageBox("Device Name Editupdate");	
}

/***
void CMy6KSetDODlg::OnSelchangeModulename() 
{
	// TODO: Add your control notification handler code here
    CString   strText,strTemp;
	char      szIPAddress[20];
	int       i,nID,iRetVal;
	WORD      wDeviceID;
	WORD      wModuleName;
    WORD      wStartDO;
    WORD      wCount;

	// TODO: Add your control notification handler code here

	//--- the module name, ex 6050, 6051,... ---
	GetDlgItemText(IDC_MODULENAME,strTemp);
	wModuleName=(WORD)atoi(strTemp);
    switch( wModuleName )
	{
		case 6050:
			MessageBox("6050");
			m_strNoOfWrite="12";
			break;
		case 6051:
			MessageBox("6051");
			m_strNoOfWrite="2";
			break;
		case 6060:
			MessageBox("6060");
			m_strNoOfWrite="6";
			break;
	}
	SetDlgItemText(IDC_EDIT_NOOFWRITE,m_strNoOfWrite);
}
***/

⌨️ 快捷键说明

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