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

📄 transbmp.cpp

📁 嵌入式LINUX9应用程序开发详解中串口编程实例
💻 CPP
字号:
// TransBmp.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "TransBmp.h"

#include "MainFrm.h"
#include "TransBmpDoc.h"
#include "TransBmpView.h"
#include "common.h"

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


static HANDLE					hCom;                     //串口句柄
int		g_nBaudRate = 28800;
/////////////////////////////////////////////////////////////////////////////
// CTransBmpApp

BEGIN_MESSAGE_MAP(CTransBmpApp, CWinApp)
	//{{AFX_MSG_MAP(CTransBmpApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTransBmpApp construction

CTransBmpApp::CTransBmpApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CTransBmpApp object

CTransBmpApp theApp;

// CTransBmpApp initialization
//////////////////////////////////////////////////////////////////////////////////////////////
void convert_long ( unsigned char * buffer )
{
	unsigned char Temp;

	Temp = *( ( unsigned char * ) buffer );
	*( ( unsigned char * ) buffer ) = *( ( unsigned char * ) ( buffer + 3 ) );
	*( ( unsigned char * ) ( buffer + 3 ) ) = Temp;

	Temp = *( ( unsigned char * ) ( buffer + 1 ) );
	*( ( unsigned char * ) ( buffer + 1 ) ) = *( ( unsigned char * ) ( buffer + 2 ) );
	*( ( unsigned char * ) ( buffer + 2 ) ) = Temp;
}

int  InitSerial()
{
	DCB Comdcb; //串口通信控制块
	COMMTIMEOUTS ComTimeOut; //超时控制块
    
	hCom = CreateFile("COM1",GENERIC_READ | GENERIC_WRITE, 0, NULL, 
		OPEN_EXISTING,0, NULL);//FILE_FLAG_OVERLAPPED FILE_ATTRIBUTE_NORMAL
    if (hCom == INVALID_HANDLE_VALUE)
	{
		TRACE("Failed to open COM1\n");
		return 1;
	}
	PurgeComm(hCom,PURGE_TXABORT|PURGE_RXABORT|
		           PURGE_TXCLEAR|PURGE_RXCLEAR);


	if(!GetCommState(hCom, &Comdcb))
   		return 2;
    //设置串口通信参数
	Comdcb.BaudRate = g_nBaudRate;//4800;	//57600;//38400;//9600
	Comdcb.ByteSize = 8;
	Comdcb.Parity = NOPARITY;
	Comdcb.StopBits = ONESTOPBIT;

	if(!SetCommState(hCom, &Comdcb))
   		return 3;

	ComTimeOut.ReadIntervalTimeout = 200;//50;
	ComTimeOut.ReadTotalTimeoutMultiplier = 0;
	ComTimeOut.ReadTotalTimeoutConstant = 0;

	ComTimeOut.WriteTotalTimeoutMultiplier=0;
	ComTimeOut.WriteTotalTimeoutConstant=0;


	if(!SetCommTimeouts(hCom,&ComTimeOut))
   		return 4;

	return 0;
}

BOOL CTransBmpApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)
	/////////////////////////////////////////
	//从参数行读取参数
	char buffer[480];
	char tempbuffer[480];
	strcpy(buffer,GetCommandLine());
	sscanf(buffer,"%s %d",tempbuffer,&g_nBaudRate);
	///////////////////////////////////////////////////////////////////////////////////////////
	//等待连接,连接成功后显示界面显示传来的位图
	InitSerial();
	///////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////
	CString strFileName(_T("test.bmp")); 
	CFile File(strFileName, CFile::modeReadWrite|CFile::modeCreate);
	///////////////////////////////////////////////////////////////////////////////////////////
	DWORD SendRetVal = 0;

	char szRecvBuf[LINESIZE];
	DWORD dwBytesRead = 0;

	//先要传一个文件长度过来,
	int nFileLen = 0;

	//ReadFile(hCom,(char *)&nFileLen,4,&dwBytesRead,NULL);
	//convert_long((unsigned char *)&nFileLen);
	//TRACE("文件长度%d\n",nFileLen);
	int nBytesHaveRecv = 0;
	for(;;)
	{
		dwBytesRead = 0;
		memset(szRecvBuf,0,LINESIZE);
		ReadFile(hCom,szRecvBuf,LINESIZE,&dwBytesRead,NULL);
		TRACE("收到%d字节:\n",dwBytesRead);
		///////////////////////////////////////////////////////////////////////////////////////
		File.Write((void *)szRecvBuf,dwBytesRead);
		nBytesHaveRecv+=dwBytesRead;
		//if(nBytesHaveRecv >= nFileLen)
		if(dwBytesRead < LINESIZE)
		{
			File.Close();
			TRACE("BMP文件传输完毕\n");
			break;
		}
	}
	CloseHandle(hCom);
	///////////////////////////////////////////////////////////////////////////////////////////
	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CTransBmpDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CTransBmpView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	/*
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;
	*///用新的命令行代替
	OnFileNew();
	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// 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)
		// No message handlers
	//}}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()

// App command to run the dialog
void CTransBmpApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CTransBmpApp message handlers

⌨️ 快捷键说明

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