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

📄 6ksendrecedlg.cpp

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

#include "stdafx.h"
#include "6KSendRece.h"
#include "6KSendReceDlg.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

/////////////////////////////////////////////////////////////////////////////
// CMy6KSendReceDlg dialog

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

void CMy6KSendReceDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy6KSendReceDlg)
	DDX_Text(pDX, IDC_EDIT_IPADDRESS, m_strIPAddress);
	DDX_Text(pDX, IDC_EDIT_MODBUSRESPONSE, m_strModbusResponse);
	DDX_Text(pDX, IDC_EDIT_MODBUSSEND, m_strModbusSend);
	DDX_Text(pDX, IDC_EDIT_RESPONSE, m_strResponse);
	DDX_Text(pDX, IDC_EDIT_SEND, m_strSend);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy6KSendReceDlg, CDialog)
	//{{AFX_MSG_MAP(CMy6KSendReceDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_EXIT, OnExit)
	ON_BN_CLICKED(ID_SENDCOMMAND, OnSendcommand)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy6KSendReceDlg message handlers

BOOL CMy6KSendReceDlg::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="172.16.2.200";
	m_strSend="$01F";
	SetDlgItemText(IDC_EDIT_IPADDRESS,m_strIPAddress);
	SetDlgItemText(IDC_EDIT_SEND,m_strSend);

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

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

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

char      szCmd[200],szResponse[200];
char      szModbusCmd[200],szModbusResponse[200];
void CMy6KSendReceDlg::OnSendcommand() 
{
	// TODO: Add your control notification handler code here
    CString   strText,strTemp;
	char      szIPAddress[20];

	int       iRetVal;
	char      szDump[80];

    //--- 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;
    }

	//--- Get the command send to 5000/TCP ---
	GetDlgItemText(IDC_EDIT_SEND,szCmd,20);
    
    //--- send a command to 5000/TCP and receive response ---        
    iRetVal=ADAMTCP_SendReceive6KTCPCmd(szIPAddress, szCmd, szResponse,
                                              szModbusCmd, szModbusResponse);
    if( iRetVal )
    {
        sprintf(szDump,"ADAMTCP_SendReceive5KTCPCmd() Failure !!!   code=%d\n",iRetVal);
        MessageBox(szDump);
        ADAMTCP_Disconnect();
        return;
    }

    //--- display various message ---
	SetDlgItemText(IDC_EDIT_RESPONSE,szResponse);
	SetDlgItemText(IDC_EDIT_MODBUSSEND,szModbusCmd);
	SetDlgItemText(IDC_EDIT_MODBUSRESPONSE,szModbusResponse);

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

⌨️ 快捷键说明

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