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

📄 filecomdlg.cpp

📁 这个是串口通信的
💻 CPP
字号:
// FileComDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileCom.h"
#include "FileComDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE g_hCom;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CFileComDlg dialog

CFileComDlg::CFileComDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileComDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileComDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileComDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileComDlg)
	DDX_Control(pDX, IDC_INFO, m_Info);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileComDlg, CDialog)
	//{{AFX_MSG_MAP(CFileComDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileComDlg message handlers

BOOL CFileComDlg::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
	InitCom();
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CFileComDlg::InitCom()
{
	DCB dcb;
	g_hCom=CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,NULL);
	GetCommState(g_hCom,&dcb);
	dcb.BaudRate=CBR_115200;
	dcb.ByteSize=8;
	dcb.Parity=NOPARITY;
	dcb.StopBits=ONESTOPBIT;
	dcb.fParity=FALSE;
	SetCommState(g_hCom,&dcb);
	SetupComm(g_hCom,256,256);
	PurgeComm(g_hCom,PURGE_RXCLEAR|PURGE_TXCLEAR);
}
BYTE CheckSum(BYTE *data,DWORD len)
{
	BYTE r;
	while(len--)
	{
		r+=data[0];
		data++;
	}
	return r;
}
DWORD WINAPI SendFile(LPVOID lpVoid)
{
	CFileComDlg *pDlg=(CFileComDlg *)lpVoid;
	CFile hfile;	
	if(hfile.Open(pDlg->m_strFN,OF_READ)==FALSE)
	{
		pDlg->AppendInfo("打开文件失败!");
		return 0;
	}
	OVERLAPPED SendOV,ReadOV;
	DWORD dwWrite,dwRead;
	BYTE ENQ[3]={0x16,0x16,0x03},EOT[3]={0x16,0x16,0x04};
	BYTE ACK[4];
	BYTE DataP[128];
	BYTE PackageID=0;
	ZeroMemory(&ReadOV,sizeof(OVERLAPPED));
	ReadOV.hEvent=CreateEvent(0,true,0,0);
	ZeroMemory(&SendOV,sizeof(OVERLAPPED));
	SendOV.hEvent=CreateEvent(0,true,0,0);
	pDlg->AppendInfo("ENQ");
	WriteFile(g_hCom,ENQ,sizeof(ENQ),&dwWrite,&SendOV);
	ReadFile(g_hCom,ACK,sizeof(ACK),&dwRead,&ReadOV);
	WaitForSingleObject(ReadOV.hEvent,600);
	GetOverlappedResult(g_hCom,&ReadOV,&dwRead,FALSE);
	if(dwRead<3)//ENQ ACK LOST
	{
		pDlg->AppendInfo("对方响应错误!");
		return 0;
	}else
	{
		if(ACK[0]==0x16 && ACK[1]==0x16)
		{
			if(ACK[3]==0x06)
			{
				if(ACK[2]>1)
				{
					pDlg->AppendInfo("对方响应错误!");
					return 0;
				}
				pDlg->AppendInfo("ACK",ACK[2]);
			}else
			{
				pDlg->AppendInfo("对方响应错误!");
				return 0;
			}
		}else
		{
			pDlg->AppendInfo("对方响应错误!");
			return 0;
		}
	}
PackageID=1;
	while(TRUE)
	{
		DataP[0]=0x02;
		DataP[1]=PackageID;
		DWORD fl=hfile.Read(&DataP[2],sizeof(DataP)-4);		
		DataP[fl+2]=CheckSum(&DataP[2],fl);
		DataP[fl+3]=0x03;
		if(fl==0)
		{
			break;
		}
		DWORD bSendAgain=FALSE;
		do
		{
			pDlg->AppendInfo("发送信息包",PackageID);
			WriteFile(g_hCom,DataP,sizeof(DataP),&dwWrite,&SendOV);
			*((DWORD*)ACK)=0xFF0000;
			ReadFile(g_hCom,ACK,sizeof(ACK),&dwRead,&ReadOV);
			WaitForSingleObject(ReadOV.hEvent,1000);
			GetOverlappedResult(g_hCom,&ReadOV,&dwRead,FALSE);
			if(dwRead<3)//ENQ ACK LOST
			{
				pDlg->AppendInfo("对方响应错误,重新发送!");	
				bSendAgain=TRUE;
			}else
			{
				if(ACK[0]==0x16 && ACK[1]==0x16)
				{
					if(ACK[3]==0x06)
					{
						if(ACK[2]!=PackageID)
						{
							pDlg->AppendInfo("对方响应错误,重新发送!");
							bSendAgain=TRUE;
						}else
						{
							pDlg->AppendInfo("ACK",ACK[2]);
						}
					}else if(ACK[2]==0x15)
					{
						pDlg->AppendInfo("对方响应错误,重新发送!");
						bSendAgain=TRUE;
					
					}
				}else
				{
					pDlg->AppendInfo("对方响应错误,重新发送!");
					bSendAgain=TRUE;
				}
			}
		}while(bSendAgain);

		PackageID=1-PackageID;
	}
	WriteFile(g_hCom,EOT,sizeof(EOT),&dwWrite,&SendOV);
	GetOverlappedResult(g_hCom,&SendOV,&dwWrite,TRUE);
	pDlg->AppendInfo("EOT\n发送结束");

	return 0;
}
void CFileComDlg::OnOK() 
{
	// TODO: Add extra validation here
	DWORD dwID;
	CFileDialog fdlg(TRUE,".txt",NULL,0,NULL,this);
	if(fdlg.DoModal()==IDOK)
	{
		m_strFN=fdlg.GetPathName();
		CreateThread(NULL,0,SendFile,this,0,&dwID);
	}
	
}

void CFileComDlg::AppendInfo(CString str,int a)
{
	CString str2;
	m_Info.SetSel(m_Info.GetWindowTextLength(),m_Info.GetWindowTextLength());
	str2.Format("%s %d\r\n",str,a);
	m_Info.ReplaceSel(str2);
}
void CFileComDlg::AppendInfo(CString str)
{
		CString str2;

	m_Info.SetSel(m_Info.GetWindowTextLength(),m_Info.GetWindowTextLength());
	str2.Format("%s\r\n",str);
	m_Info.ReplaceSel(str2);
}

⌨️ 快捷键说明

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