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

📄 usartdrv.cpp

📁 全是好东西啊 欢迎观看,共勉,加油啊
💻 CPP
字号:
//*-----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name               : Usartdrv.cpp
//* Object                  : Usart Driver
//*
//* 1.0 06/06/01 IH         : Creation
//*-----------------------------------------------------------------------------

//*----- Files to be included Definition -----*/
#include "stdafx.h"

#include <windows.h>
#include <stdio.h>
#include "usartdrv.h"
#include "dfu.h"

//*----- External variables defined in usartdrv.h -----*/


//* --------------------------------------------------------------------------
//* Function Name        : OpenCom
//* Object               : Open COM port
//* Input Parameters     : COM Port name
//* Output Parameters    : Com Port Handle 
//* Functions called     : 
//* --------------------------------------------------------------------------
HANDLE Usart_Open(const char *ComName)
{
    DWORD sizeCommConfig = sizeof(COMMCONFIG);
    COMMTIMEOUTS CommTimeouts;
	int error = 0;
    
	HANDLE HComPort;
	COMMCONFIG commConfig;

	commConfig.dwSize = sizeof(COMMCONFIG);
	commConfig.wVersion = 0x100;

    HComPort = CreateFile((LPCTSTR)ComName,
							GENERIC_READ | GENERIC_WRITE,
							0,							// comm devices must be opened w/exclusive-access
							NULL,						// no security attrs
							OPEN_EXISTING,				// comm devices must use OPEN_EXISTING
							0,							// not overlapped I/O
							NULL						// hTemplate must be NULL for comm devices
						);

    // If CreateFile failed return GetLastError
    if (HComPort == INVALID_HANDLE_VALUE )
		return (HANDLE) INVALID_HANDLE_VALUE;

	    // GeT commConfig properties to this COM
    if (! GetCommConfig(HComPort, &commConfig, &sizeCommConfig))
		return (HANDLE) INVALID_HANDLE_VALUE;
	//if (! SetCommConfig(HComPort, &commConfig, sizeof(COMMCONFIG)) )	
	//	return (HANDLE) INVALID_HANDLE_VALUE;
	if(!SetCommState(HComPort, (LPDCB) &(commConfig.dcb)) )
		return (HANDLE) INVALID_HANDLE_VALUE;

	//* Set the Timeouts
	CommTimeouts.ReadIntervalTimeout = 1000;			//* Time between two characters
	CommTimeouts.ReadTotalTimeoutMultiplier = 40;		//* Multiplier
	CommTimeouts.ReadTotalTimeoutConstant = 1000;		//* Constant additting
	CommTimeouts.WriteTotalTimeoutMultiplier = 0;		//* Total time-out period for write
	CommTimeouts.WriteTotalTimeoutConstant=   0;

	if (! SetCommTimeouts(HComPort,&CommTimeouts))
		return (HANDLE) INVALID_HANDLE_VALUE;
	
	if (! PurgeComm(HComPort,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR))
		return (HANDLE) INVALID_HANDLE_VALUE;

	return(HComPort);
}

//*----------------------------------------------------------------------------
//* Function Name       : writeCom
//* Object              : Format print info
//* Input Parameters    : Address of message, Len
//* Output Parameters   : FALSE or TRUE
//* Functions called    : CloseHandle
//*----------------------------------------------------------------------------
BOOL Usart_Close(HANDLE hcom)
{
	if(!CloseHandle(hcom))
		return FALSE;
    return TRUE;
}


//*----------------------------------------------------------------------------
//* Function Name       : writeCom
//* Object              : Format print info
//* Input Parameters    : Address of message, Len
//* Output Parameters   : FALSE or TRUE
//* Functions called    : ReadFile, displayError,closeCom
//*----------------------------------------------------------------------------
BOOL Usart_Write(IN HANDLE hcom, PVOID IN buffer, IN DWORD len)
{
	DWORD bytesWritten;

    // Tranfert file (return if error)
	BOOL status = WriteFile(hcom, buffer, len, &bytesWritten , NULL);
	if (!status)
		return status;

	if (bytesWritten != len) {
		SetLastError(ERROR_TX_NOT_COMPLETE);
		return FALSE;
	}
	else
		return TRUE;
}


//*----------------------------------------------------------------------------
//* Function Name       : ReadCom
//* Object              : 
//* Input Parameters    : 
//* Output Parameters   : 
//* Functions called    : 
//*----------------------------------------------------------------------------
BOOL Usart_Read(IN HANDLE hcom, PVOID buffer, IN DWORD len)
{
	DWORD bytesRead;

    BOOL status = ReadFile(hcom, buffer, len, &bytesRead, NULL);
	if (!status)
		return status;

	if (bytesRead != len) {
		SetLastError(ERROR_RX_NOT_COMPLETE);
		return FALSE;
	}
	else
		return TRUE;
}


//*----------------------------------------------------------------------------
//* Function Name       : Usart_Download
//* Object              : 
//* Input Parameters    : 
//* Output Parameters   : 
//* Functions called    : 
//*----------------------------------------------------------------------------
ULONG Usart_Download(const char *com ,PVOID IN buffer, IN ULONG bufferSize)
{	
	ULONG   bytesWritten = bufferSize;
	CHAR    errorMessage[100];
	PIPE usartPipe;

	// Open the communication channel and init pipe structure
	usartPipe.handle = Usart_Open(com);
	if(usartPipe.handle == INVALID_HANDLE_VALUE) 
	{
		sprintf(errorMessage, "Can not open device: %X", GetLastError());
		MessageBox(NULL, errorMessage, "ERROR", MB_OK);
		return 0;
	}

	usartPipe.read   = Usart_Read;
	usartPipe.write  = Usart_Write;

	// Download the buffer
	if (!DownloadUploadFirmware(&usartPipe, buffer, bufferSize, FALSE)) {
		sprintf(errorMessage, "Error downloading firmware: %X", GetLastError());
		MessageBox(NULL, errorMessage, "ERROR", MB_OK);
		bytesWritten = 0;
	}

	// Close the handle
	Usart_Close(usartPipe.handle);
	return bufferSize;
}

⌨️ 快捷键说明

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