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

📄 serialdlg.cpp

📁 实现了串口通信
💻 CPP
字号:
// serialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "serial.h"
#include "serialDlg.h"
#include "COM_control.h"
#include "afxmt.h"

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


_COM mycom;

HANDLE	m_hCommPort;		//保存打开的串行口设备句柄

UINT ReadChar(PVOID hWnd);	//读取字符线程

char RecvBuf[BUFFER_SIZE], SendBuf[BUFFER_SIZE];
UINT RecvPTR;

CEvent	SendEvent(0,true,0,0),RecvEvent(0,true,0,0);
OVERLAPPED	SendOV,RecvOV;

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CSerialDlg dialog

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

void CSerialDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerialDlg)
	DDX_Control(pDX, IDC_SEND, m_Send);
	DDX_Control(pDX, IDOK, m_OK);
	DDX_Control(pDX, IDC_RECV, m_Receive);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSerialDlg, CDialog)
	//{{AFX_MSG_MAP(CSerialDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDOK2, OnOk2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerialDlg message handlers

BOOL CSerialDlg::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

	mycom.ComInitial("COM5");	
	AfxBeginThread(ReadChar,NULL,THREAD_PRIORITY_NORMAL);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

UINT ReadChar(PVOID hWnd)
{

	CString ShowStr="RECV:";
    PACKAGE* recvpck=NULL;
	CFile writefile;
	CSerialApp* pApp=(CSerialApp*)AfxGetApp();
	CSerialDlg* pDlg=(CSerialDlg*)pApp->m_pMainWnd;

	while (true)		//只要线程运行,就监视端口是否收到数据
	{	
    	recvpck=mycom.ReadCom();
		if (recvpck!=NULL)
		{
			if (recvpck->iPckType==NORMAL_MESSAGE)
			{					
				recvpck->pData[recvpck->iDataLen]=NULL;
				CString tmp;
				tmp.Format("%s",recvpck->pData);
				ShowStr=ShowStr+tmp;
				pDlg->m_Receive.InsertString (0,ShowStr);
				continue;
			}
			else{

				CString recvfilename;
				recvfilename.Format("%s",recvpck->iPckName);

				if (recvpck->iType==FILE_BEGINNING||recvpck->iType==file_both)
				{					
					writefile.Open(recvfilename,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary|CFile::modeNoTruncate);
					//myfile=fopen("test.bmp","wb");
				}
				
				writefile.Write(recvpck->pData,recvpck->iDataLen);
				//fwrite(recvpck->pData,sizeof(char),recvpck->iDataLen,myfile);
				
				if (recvpck->iType==FILE_ENDING||recvpck->iType==file_both)
				{
					writefile.Close();
					//fclose(myfile);
					
					CString tmp="文件接收完成!";
					ShowStr=ShowStr+recvfilename+tmp;
					pDlg->m_Receive.InsertString (0,ShowStr);	
					AfxMessageBox(ShowStr);
			    	pDlg->filename=recvfilename;					
					pDlg->loadimage();
				}
			}	
		}
	}
	return 0;
}

void CSerialDlg::OnOK()
{
	ULONG SendLen;
    CString ShowStr="SEND:";
	PACKAGE pck;
	ULONG pcktype;
	
	//获取要发送的字符串
	SendLen=m_Send.GetWindowText(SendBuf,BUFFER_SIZE-1);
    if (SendLen==0)
	{
		AfxMessageBox("请输入要传输的数据");
		return;
	}
	SendBuf[SendLen++]=NULL;
	
	m_Send.EnableWindow(FALSE);
	m_OK.EnableWindow (FALSE);

	pcktype=NORMAL_MESSAGE;
	pck.SetPck(pcktype,NULL,0,SendBuf,SendLen);
	mycom.WriteCom(pck);
	
	m_Send.EnableWindow(TRUE);
	m_OK.EnableWindow (TRUE);
	
	ShowStr=ShowStr+SendBuf;		//显示发送的消息
	m_Receive.InsertString (0,(LPCTSTR)ShowStr);
    m_Send.SetWindowText(NULL);
	m_Send.SetFocus();
}

BOOL CSerialDlg::DestroyWindow() 
{

	CloseHandle(m_hCommPort); //关闭串行口

	return CDialog::DestroyWindow();
}

void CSerialDlg::OnBrowse() 
{
}

void CSerialDlg::loadimage()
{
    hwnd = GetDlgItem(IDC_PIC);
 	hDesDC = hwnd->GetDC()->m_hDC;
 	hSrcDC = CreateCompatibleDC(hDesDC);
 	hBitmap=(HBITMAP)LoadImage(AfxGetInstanceHandle(),filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
 	
	GetObject(hBitmap, sizeof BITMAP, &bm);
 	SelectObject(hSrcDC, hBitmap);
 	
 	hwnd->GetClientRect(&rect);
 	::SetStretchBltMode(hDesDC,COLORONCOLOR);       
 	::StretchBlt(hDesDC, rect.left, rect.top, rect.right, rect.bottom, hSrcDC, 0, 0, bm.bmWidth, bm.bmHeight,+SRCCOPY);
}

void CSerialDlg::OnOk2() 
{		
	CFileDialog mydolg(TRUE);
	if(mydolg.DoModal()==IDOK)
	{	
	    filename=mydolg.GetFileName();
		LPTSTR lpsz =(LPTSTR)(LPCTSTR)filename;  
		CFile readfile;
		readfile.Open(filename,CFile::modeRead|CFile::typeBinary|CFile::modeNoTruncate);	
		int fileLength=readfile.GetLength();
		int totalRead=0;	
		int SendLen;
		
		PACKAGE pck;
		int pcktype=FILE_PACKAGE;
		int type;
		while(totalRead<fileLength) 
		{	
			type=FILE_MIDDLE;			
			if (totalRead==0)
			{
				type=FILE_BEGINNING;
			}
			
			SendLen=readfile.Read(SendBuf,BUFFER_SIZE);	
			totalRead+=SendLen;			
			
			if(totalRead==fileLength)
			{
				if(type!=FILE_BEGINNING)
					type=FILE_ENDING;
				else type=file_both;
			}
			else{
				if (type!=FILE_BEGINNING&&type!=file_both)
					type=FILE_MIDDLE;							
			}

			pck.SetPck(pcktype,lpsz,type,SendBuf,SendLen);			
			Sleep(100);
			mycom.WriteCom(pck);
		}
		readfile.Close();

		CString ShowStr="SEND:";
		CSerialApp* pApp=(CSerialApp*)AfxGetApp();
     	CSerialDlg* pDlg=(CSerialDlg*)pApp->m_pMainWnd;
		CString tmp="文件发送完成";
		ShowStr=ShowStr+filename+tmp;
		pDlg->m_Receive.InsertString(0,ShowStr);
	}	
}

⌨️ 快捷键说明

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