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

📄 embednetcdlg.cpp

📁 计算机与嵌入式系统或者是单片机之间网络传输时
💻 CPP
字号:
// EmbedNetCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EmbedNetC.h"
#include "EmbedNetCDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEmbedNetCDlg dialog

CDialog *g_dlg;
BYTE recv_data[1024*200];
long recv_len=0;


CEmbedNetCDlg::CEmbedNetCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEmbedNetCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEmbedNetCDlg)
	m_servip = _T("192.168.0.100");
	m_port = 8080;
	m_fname = _T("");
	m_recvText = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bConnected=FALSE;
	m_packet=0;
}

void CEmbedNetCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEmbedNetCDlg)
	DDX_Control(pDX, IDC_STATIC_STATUS, m_ctlStatus);
	DDX_Text(pDX, IDC_EDIT_SERVIP, m_servip);
	DDX_Text(pDX, IDC_EDIT_PORT, m_port);
	DDX_Text(pDX, IDC_EDIT_FNAME, m_fname);
	DDX_Text(pDX, IDC_EDIT_RECV, m_recvText);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEmbedNetCDlg, CDialog)
	//{{AFX_MSG_MAP(CEmbedNetCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_FILENAME, OnButtonFilename)
    ON_MESSAGE(WSA_SOCK_ASYNC, OnSockAsynch)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_UPDATE_RECV,OnUpdateRecv)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEmbedNetCDlg message handlers

BOOL CEmbedNetCDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		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.  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
	g_dlg=this;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEmbedNetCDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

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

void CEmbedNetCDlg::OnButtonConnect() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	char str_ip[80];
	wsprintf(str_ip,"%s",m_servip);
	m_sockClient.Create();

    if(m_sockClient.Connect(str_ip, m_port))
	{
       m_bConnected=TRUE;
	   m_sockClient.AsyncSelect(FD_CONNECT|FD_READ);
	}
}

void CEmbedNetCDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	if(!m_bConnected)
		return;
	CString msg;
	UINT port;

	CFile myFile;
	myFile.Open(m_fname, CFile::modeRead | CFile::typeBinary);

	int myFileLength = myFile.GetLength();


	msg.Format("Send %d bytes to %s:%d",myFileLength,m_servip,m_port);
	m_ctlStatus.SetWindowText(msg);	
/*	
	if(m_sockClient.Send(&myFileLength, 4)==SOCKET_ERROR)
	{
        msg.Format("socket send() error!");
		m_ctlStatus.SetWindowText(msg);
	    return ;
	}
	
*/
	
	BYTE* data = new BYTE[myFileLength];

	myFile.Read(data, myFileLength);
	
	SaveToCFile(data,myFileLength);
	
	if(m_sockClient.Send(data, myFileLength)==SOCKET_ERROR)
	{
		msg.Format("socket send() error!");
		m_ctlStatus.SetWindowText(msg);
		return ;
	}


	myFile.Close();
	delete data;
}

void CEmbedNetCDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	m_sockClient.Close();
	CDialog::OnClose();
}

void CEmbedNetCDlg::OnSockAsynch(WPARAM wParam, LPARAM lParam)
{

	CString msg;

	
	CString str_ip;
	UINT port;
	

	
	int WSAEvent ;
	WSAEvent=wParam;
    TRACE("\nlParam:%d",WSAEvent);
	switch (WSAEvent)
	{
	case FD_CONNECT:
         
		 break;
	case FD_READ:
	  
	  if(m_packet==0)
	  	  recv_len=0;
	  else
		  KillTimer(TimerNumber);
	  m_packet++;
      recv_len+=m_sockClient.Receive(recv_data+recv_len,sizeof(recv_data));
	  msg.Format("received %d bytes from server [%d pkt].",recv_len,m_packet);
	  m_ctlStatus.SetWindowText(msg);
      TimerNumber=SetTimer(WM_USER+11,500,NULL);
		break;
	case FD_CLOSE:
		break;
	default:
	   msg.Format("Unhandle Socket Event!");
	   m_ctlStatus.SetWindowText(msg);
       
	}

}

void CEmbedNetCDlg::OnButtonFilename() 
{
	// TODO: Add your control notification handler code here
	CFileDialog file_dlg(TRUE);
	file_dlg.DoModal();
	m_fname=file_dlg.GetPathName();
	UpdateData(FALSE);
}

void CEmbedNetCDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_packet)
	{ 
		m_packet=0;
	  //  MessageBox("recv finish.");
        SendMessage(WM_UPDATE_RECV,0,0);
	}
	CDialog::OnTimer(nIDEvent);
}

void CEmbedNetCDlg::OnUpdateRecv(WPARAM wParam,LPARAM lParam)
{
	CString ch;
	m_recvText="\r\n";
	for(int i=0;i<(recv_len<260?recv_len:260);i++)
	{
		ch.Format("%c",recv_data[i]);
		m_recvText+=ch;
	}
    ch="\r\n...";
	m_recvText+=ch;
    UpdateData(FALSE);

	CFile file;
	file.Open("recv.dat", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
    file.WriteHuge(recv_data,recv_len);
	file.Close();

}

void CEmbedNetCDlg::SaveToCFile(BYTE *pdata, int datalen)
{
	CFile file;
	file.Open("send.c", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
	char sz_data[18];
	for(int i=0;i<datalen;i++)
	{
        if(i%15==0&&i>0)
		{           
			sprintf(sz_data,"0x%-2x,\n",pdata[i]);
			file.Write(sz_data,6);
		}
		else
		{
			sprintf(sz_data,"0x%-2x,",pdata[i]);
            file.Write(sz_data,5);
		}
		
	}
	file.Close();

}

⌨️ 快捷键说明

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