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

📄 socketfiledlg.cpp

📁 柴油机的控制和状态检测使用的是电子设备ECU,ECU内运行的程序(生产文件)要在柴油机下线前装入。装入程序使用的是罐装设备
💻 CPP
字号:
// SocketFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SocketFile.h"
#include "SocketFileDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSocketFileDlg dialog

CSocketFileDlg::CSocketFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSocketFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSocketFileDlg)
	m_nEdit1 = 0;
	m_strEdit3 = _T("");
	m_strEdit2 = _T("");
	m_strEdit4 = _T("");
	m_strEdit5 = _T("");
	m_strEdit6 = _T("");
	m_slider = 0;
	m_nEdit7 = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSocketFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSocketFileDlg)
	DDX_Control(pDX, IDC_LIST1, m_cList1);
	DDX_Control(pDX, IDC_IPADDRESS1, m_ServerIP);
	DDX_Text(pDX, IDC_EDIT1, m_nEdit1);
	DDX_Text(pDX, IDC_EDIT3, m_strEdit3);
	DDX_Text(pDX, IDC_EDIT2, m_strEdit2);
	DDX_Text(pDX, IDC_EDIT4, m_strEdit4);
	DDX_Text(pDX, IDC_EDIT5, m_strEdit5);
	DDX_Text(pDX, IDC_EDIT6, m_strEdit6);
	DDX_Slider(pDX, IDC_SLIDER1, m_slider);
	DDX_Text(pDX, IDC_EDIT7, m_nEdit7);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSocketFileDlg, CDialog)
	//{{AFX_MSG_MAP(CSocketFileDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDCOK, OnCok)
	ON_BN_CLICKED(IDC_ConnectServer, OnConnectServer)
	ON_BN_CLICKED(IDC_ServerDisConnect, OnServerDisConnect)
	ON_MESSAGE(CLI_MESSAGE,OnClientMessage)
	ON_BN_CLICKED(IDC_Send, OnSend)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSocketFileDlg message handlers

BOOL CSocketFileDlg::OnInitDialog()
{
	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
	
	m_nEdit1=5010;
    //m_ServerIP.set
	this->UpdateData(false);
	//初始化
	WSADATA wsaData;
	WORD version = MAKEWORD(2, 0);
	int ret = WSAStartup(version, &wsaData);
	if(ret != 0)
	{
		TRACE("Initilize Error!\n");  //初始化失败
		AfxMessageBox("Failed in initial socket");
	}

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

void CSocketFileDlg::OnOK() 
{
	CDialog::OnOK();
}

void CSocketFileDlg::OnCancel() 
{
	CDialog::OnCancel();
}

void CSocketFileDlg::OnCok() 
{
	CDialog::OnOK();
}


void CSocketFileDlg::OnConnectServer() 
{
	BYTE  f0,f1,f2,f3;
	m_ServerIP.GetAddress(f0,f1,f2,f3);
	CString addr;
	addr.Format("%d.%d.%d.%d",f0,f1,f2,f3);
    UpdateData(true);
	UINT m_uPort=m_nEdit1;
    m_nClient.InitAndConnect(this->m_hWnd,m_uPort,addr);	
}

void CSocketFileDlg::OnServerDisConnect() 
{
	m_strEdit3="下线";
	UpdateData(false);
	m_nClient.DisConnect_Server();
	
}


LRESULT CSocketFileDlg::OnClientMessage(WPARAM wParam, LPARAM lParam)
{
	UINT j,len1=12;
	char buf[1023];
    
	char s[1024];
	int  i,len;
	int FileName_Len,FileLengthStr_len;
	BYTE cmd;
	char FileName[80],FileLength[20];
	switch(lParam)
	{
		case FD_CONNECT:
			len=GetLastError();
			if(len!=0)
			{
				AfxMessageBox("Error in Connecting");
			}
			m_strEdit3="在线";
			UpdateData(false);
			return 0;
		case FD_READ:
			len=recv(m_nClient.m_hSocket,s,1024,0);
			s[len]=NULL;
			if((s[0]==0x50)||(s[len]==0x05))
			{
	           cmd=(byte)s[1];    //Command
			   switch(cmd)
			   {
			   case 1:    
                    //FileName
				    FileName_Len=(int)s[4+1];
				    for(i=0;i<FileName_Len;i++) 
					 { 
						 FileName[i]=s[4+1+1+i];
					 }
                         FileName[FileName_Len]='\0';
                         m_strEdit4=FileName;
					 //FileLength
                     FileLengthStr_len=(int)s[4+1+1+FileName_Len];         
				    for(i=0;i<FileLengthStr_len;i++) 
					 { 
						 FileLength[i]=s[4+1+1+1+FileName_Len+i];
					 }
                         FileLength[FileLengthStr_len]='\0';
                         m_strEdit5=FileLength;
                     //Open File
                      	fp.Open(FileName,CFile::modeCreate|CFile::modeWrite);
                     //发送取文件数据命令
						DataBlockNo=0;
                        buf[0]=0x50;  //head
                        buf[1]=0x02;  //Command
	                    buf[2]=0x00;  //encryption Select
                        buf[3]=0x02;  //Data_Length
                        buf[4]=0x00;  //Data_Length
						buf[5]=DataBlockNo-(DataBlockNo/256)*256;
                        buf[6]=(DataBlockNo/256);
                        buf[7]=0x05;  //tail
	                  //发送取数据
                        len1=8;
	                    m_nClient.SendData(buf,len1);
                        m_strEdit6="取文件数据";
				     break;
	 		   case 2:    //Data   
                        len1=(BYTE)s[4]*256+(BYTE)s[3];
                        for(j=0;j<len1;j++)
						{
							buf[j]=s[5+j];
						}
						fp.Write(buf,len1);
				        DataBlockNo++;	
 
                        buf[0]=0x50;  //head
                        buf[1]=0x02;  //Command
	                    buf[2]=0x00;  //encryption Select
                        buf[3]=0x02;  //Data_Length
                        buf[4]=0x00;  //Data_Length
						buf[5]=DataBlockNo-(DataBlockNo/256)*256;
                        buf[6]=(DataBlockNo/256);
                        buf[7]=0x05;  //tail
	                    len1=8;
	                    m_nClient.SendData(buf,len1);
                        m_strEdit6="取文件数据";
						m_nEdit7=DataBlockNo;
				   break;
			   case 3:    //Data  End
                        len1=(BYTE)s[4]*256+(BYTE)s[3];
                        for(j=0;j<len1;j++)
						{
							buf[j]=s[5+j];
						}
						fp.Write(buf,len1);
        				fp.Close();
				   break;
			   }
			}
			UpdateData(false);
            //m_cList1.AddString(s);
			//随时跟踪滚动条的位置 
			return 0;
		case FD_WRITE:
			return 0;
		case FD_CLOSE:
             closesocket(m_nClient.m_hSocket);
  			 m_strEdit3="下线";
             UpdateData(false);
			 return 0;
		default:
     		return 0;
	}
		 
}

void CSocketFileDlg::OnSend() 
{
	CString str;
	UpdateData(true);
	str=m_strEdit2;
    m_nClient.SendString(str);
	
}

BOOL CSocketFileDlg::DestroyWindow() 
{
	return CDialog::DestroyWindow();
}

void CSocketFileDlg::OnButton2() 
{
	UINT len=12;
	char buf[1023];
    buf[0]=0x50;  //head
    buf[1]=0x01;  //Command
	buf[2]=0x00;  //encryption Select
    buf[3]=0x06;  //Data_Length
    buf[4]=0x00;
    buf[5]='E';    //Data
    buf[6]='C';    
    buf[7]='U';    
    buf[8]='_';    
    buf[9]='N';    
	buf[10]='O';    
	buf[11]=0x05;  //tail
	//发送ECU号
    m_nClient.SendData(buf,len);
    m_strEdit6="查询ECU号";
	
}

void CSocketFileDlg::OnButton1() 
{
	char buf[1024];
    UINT Len=11;
	buf[0]='0';
	buf[1]='2';
	buf[2]='0';
	buf[3]='2';
	buf[4]='0';
	buf[5]='1';
	buf[6]='0';
	buf[7]='1';
	buf[8]='0';
	buf[9]='0';
    buf[10]='0';
	fp.Open("1.txt",CFile::modeCreate|CFile::modeWrite);
	fp.Write(buf,Len);
	fp.Close();
}

⌨️ 快捷键说明

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