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

📄 6kreadaidlg.cpp

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

#include "stdafx.h"
#include "6KReadAI.h"
#include "6KReadAIDlg.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

/////////////////////////////////////////////////////////////////////////////
// CMy6KReadAIDlg dialog

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

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

BEGIN_MESSAGE_MAP(CMy6KReadAIDlg, CDialog)
	//{{AFX_MSG_MAP(CMy6KReadAIDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_EXIT, OnExit)
	ON_BN_CLICKED(ID_ANALOGINPUT, OnAnaloginput)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy6KReadAIDlg message handlers

BOOL CMy6KReadAIDlg::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="6015";
	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 CMy6KReadAIDlg::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 CMy6KReadAIDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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

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

void CMy6KReadAIDlg::OnAnaloginput() 
{
	// TODO: Add your control notification handler code here
    CString   strText,strTemp;
	char      szIPAddress[20];
	int       i,iRetVal, iChannel;
	WORD      wDeviceID;
    WORD      wModuleName;
	WORD      wGain[9];
	char      szDump[80];
	WORD      wHex[16];
	double    fValue[16];

	//--- the module name, ex 6017 ---
	//--- Notice: support 6015, 6017 in DLL Ver 2.01 ---
	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;
    }

	//--- setting the Gain code for every channel ---
	//--- please refer to Utility to get the gain code for every channel ---
    if (wModuleName == 6015)
    {
        iChannel = 7;
        wGain[0]=ADAMTCP_BI_50TO150C;   // the gain code for channel:0
        wGain[1]=ADAMTCP_BI_50TO150C;   // the gain code for channel:1
      	wGain[2]=ADAMTCP_BI_50TO150C;   // the gain code for channel:2
        wGain[3]=ADAMTCP_BI_50TO150C;   // the gain code for channel:3
        wGain[4]=ADAMTCP_BI_50TO150C;   // the gain code for channel:4
      	wGain[5]=ADAMTCP_BI_50TO150C;   // the gain code for channel:5
        wGain[6]=ADAMTCP_BI_50TO150C;   // the gain code for channel:6
      	wGain[7]=ADAMTCP_BI_50TO150C;   // the gain code for 6015's averaging channel
    }
    else
    {
        iChannel = 8;
		wGain[0]=ADAMTCP_BI_5V;		// the gain code for channel:0
		wGain[1]=ADAMTCP_BI_150mV;	// the gain code for channel:1
		wGain[2]=ADAMTCP_UNI_4TO20mA; // the gain code for channel:2
		wGain[3]=ADAMTCP_BI_10V;		// the gain code for channel:3
		wGain[4]=ADAMTCP_BI_10V;		// the gain code for channel:4
		wGain[5]=ADAMTCP_BI_10V;		// the gain code for channel:5
		wGain[6]=ADAMTCP_BI_10V;		// the gain code for channel:6
		wGain[7]=ADAMTCP_BI_10V;		// the gain code for channel:7
		wGain[8]=ADAMTCP_BI_10V;		// the gain code for 6017's averaging channel
	}


    //--- reading analog input ---
    iRetVal=ADAMTCP_Read6KAI(szIPAddress,  wModuleName, wDeviceID, wGain, wHex, fValue);
    if( iRetVal )
    {
        sprintf(szDump,"ADAMTCP_Read6KDIO() Failure !!!   code=%d\n",iRetVal);
        MessageBox(szDump);
        ADAMTCP_Disconnect();
        return;
    }

	//----- show the Analog Input value for channel 0 to 7 -------
	((CListBox*)GetDlgItem(IDC_LIST_AIVALUE))->ResetContent();
	for(i=0; i<iChannel; i++)
	{
		strText.Format( "Channel:%02d  ->  Data:%04x(hex)   Data:%05u(dec)  Value:%5.3f", i,wHex[i],wHex[i],fValue[i]);
		((CListBox*)GetDlgItem(IDC_LIST_AIVALUE))->InsertString( -1, strText );
	}
	//----- show the Analog Input Value for Averaging Channel ----
	strText.Format( "Averaging channel:  ->  Data:%04x(hex)  Data:%05u(dec)  Value:%5.3f", wHex[8],wHex[8],fValue[8]);
	((CListBox*)GetDlgItem(IDC_LIST_AIVALUE))->InsertString( -1, strText );

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

⌨️ 快捷键说明

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