📄 uart.cpp
字号:
/*----------------------------------------------------------------------------
* Copyright (c) 2001 by National Semiconductor Corporation
* National Semiconductor Corporation
* 2900 Semiconductor Drive
* Santa Clara, California 95051
*
* All rights reserved
*
*<<<-------------------------------------------------------------------------
* File Contents:
* Uart.cpp - Overlapped serial IO communication class.
*
* Project: USB Demo Application
* Author : Yan Nosovitsky
* Date : Dec 2001
*----------------------------------------------------------------------->>>*/
#include "stdafx.h"
#include "Uart.h"
Uart::Uart()
{
uartHandle = 0;
}
BOOL Uart::OpenUART(LPCSTR lpszPortNum, DWORD dwBaudRate, BYTE byParity, BYTE byStopBits, BYTE byByteSize)
{
DCB dcb; // structure that defines the control setting for a serial communications device
uartHandle = CreateFile(lpszPortNum, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH| FILE_FLAG_OVERLAPPED, NULL); // hTemplate must be NULL for comm devices
if ( uartHandle == INVALID_HANDLE_VALUE )
return FALSE;
PurgeComm(uartHandle,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
if ( !GetCommState(uartHandle, &dcb) )
{
CloseUART();
return FALSE;
}
dcb.BaudRate = dwBaudRate;
dcb.ByteSize = byByteSize;
dcb.Parity = byParity;
dcb.StopBits = byStopBits;
COMMTIMEOUTS CommTimeouts;
BOOL bRetVal;
bRetVal = GetCommTimeouts(uartHandle, &CommTimeouts);
if(bRetVal)
{
CommTimeouts.ReadIntervalTimeout = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 1;
bRetVal = SetCommTimeouts(uartHandle, &CommTimeouts);
}
if ( ! SetCommState(uartHandle, &dcb) )
{
CloseUART();
return FALSE;
}
//
// set comm buffer sizes
//
SetupComm(uartHandle, 1024, 1024);
return TRUE;
}
void Uart::CloseUART()
{
if ( uartHandle > 0 )
{
PurgeComm(uartHandle,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
CloseHandle(uartHandle);
}
uartHandle = 0;
}
bool Uart::WriteData(BYTE* pdata, DWORD len)
{
DWORD written = 0;
OVERLAPPED m_ov;
if ( len < 1 )
return FALSE;
memset( &m_ov, 0, sizeof( OVERLAPPED ) ) ;
// create event for overlapped I/O
m_ov.hEvent = CreateEvent(NULL, FALSE, FALSE, ""); // pointer to event-object name
if ( m_ov.hEvent == INVALID_HANDLE_VALUE )
return FALSE;
WriteFile(uartHandle, pdata, len, &written, &m_ov);
if ( ! GetOverlappedResult(uartHandle, &m_ov, &written, TRUE) )
{
CloseHandle(m_ov.hEvent);
return FALSE;
}
CloseHandle(m_ov.hEvent);
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError( uartHandle, &dwErrorFlags, &ComStat );
return TRUE;
}
DWORD Uart::ReadData(BYTE* pdest, DWORD len, DWORD dwMaxWait)
{
DWORD result = 0;
DWORD read = 0; // num read bytes
DWORD mask = 0; // a 32-bit variable that receives a mask indicating the type of event that occurred
DWORD dwCommErrors = 0;
OVERLAPPED m_ov;
DWORD i;
COMSTAT ComStat;
memset( &m_ov, 0, sizeof( OVERLAPPED ) ) ;
if ( len < 1 ) return(0);
// create event for overlapped I/O
m_ov.hEvent = CreateEvent(NULL, FALSE, FALSE, "");
if ( m_ov.hEvent == INVALID_HANDLE_VALUE )
return -1;
for (i = 0; i < dwMaxWait; i += 5)
{
Sleep(5);
ClearCommError(uartHandle, &dwCommErrors, &ComStat);
if (ComStat.cbInQue > 0)
{
if (!ReadFile(uartHandle, pdest, len, &read, &m_ov))
{
ClearCommError(uartHandle, &dwCommErrors, NULL);
CloseHandle(m_ov.hEvent);
return -1;
}
CloseHandle(m_ov.hEvent);
return read;
}
}
CloseHandle(m_ov.hEvent);
return -1;
}
BOOL Uart::IsNT(VOID)
{
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
return TRUE;
}
else
{
return FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -