Lots of updates and refactoring.
This commit is contained in:
263
AVR Working Controller/SerialPort/SerialPort.cpp
Normal file
263
AVR Working Controller/SerialPort/SerialPort.cpp
Normal file
@@ -0,0 +1,263 @@
|
||||
//
|
||||
//
|
||||
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
|
||||
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER
|
||||
// REMAINS UNCHANGED.
|
||||
//
|
||||
// Email: yetiicb@hotmail.com
|
||||
//
|
||||
// Copyright (C) 2002-2003 Idael Cardoso.
|
||||
//
|
||||
|
||||
//#include "Test.h"
|
||||
#include "SerialPort.h"
|
||||
#include "stdafx.h"
|
||||
|
||||
#ifdef _DEBUG
|
||||
#undef THIS_FILE
|
||||
static char THIS_FILE[]=__FILE__;
|
||||
#define new DEBUG_NEW
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Construction/Destruction
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
CSerialPort::CSerialPort()
|
||||
: m_PortHandle(INVALID_HANDLE_VALUE)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CSerialPort::~CSerialPort()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
BOOL CSerialPort::Open(LPCTSTR PortName, DWORD BaudRate, BYTE ByteSize, BYTE Parity, BYTE StopBits, DWORD DesiredAccess)
|
||||
{
|
||||
Close();
|
||||
m_PortHandle = CreateFile(PortName, DesiredAccess, 0, NULL, OPEN_EXISTING, 0, 0);
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DCB dcb;
|
||||
CString s;
|
||||
dcb.DCBlength = sizeof(dcb);
|
||||
GetCommState(m_PortHandle, &dcb);
|
||||
//printf("dcb: BaudRate before=%d\n", dcb.BaudRate);
|
||||
//printf("dcb: ByteSize before=%d\n", dcb.ByteSize);
|
||||
//printf("dcb: StopBits before=%d\n", dcb.StopBits);
|
||||
//printf("dcb: Parity before=%d\n", dcb.Parity);
|
||||
dcb.BaudRate = BaudRate;
|
||||
dcb.ByteSize = ByteSize;
|
||||
dcb.StopBits = StopBits;
|
||||
dcb.Parity = Parity;
|
||||
//printf("dcb: fDsrSensitivity before=%d\n", dcb.fDsrSensitivity);
|
||||
//printf("dcb: fOutxCtsFlow before=%d\n", dcb.fOutxCtsFlow);
|
||||
//printf("dcb: fOutxDsrFlow before=%d\n", dcb.fOutxDsrFlow);
|
||||
//printf("dcb: fInX before=%d\n", dcb.fInX);
|
||||
//printf("dcb: fOutX before=%d\n", dcb.fOutX);
|
||||
//printf("dcb: fDtrControl before=%d\n", dcb.fDtrControl);
|
||||
//printf("dcb: fRtsControl before=%d\n", dcb.fRtsControl);
|
||||
dcb.fDsrSensitivity = 0;
|
||||
dcb.fOutxCtsFlow = 0;
|
||||
dcb.fOutxDsrFlow = 0;
|
||||
dcb.fInX = 0;
|
||||
dcb.fOutX = 0;
|
||||
dcb.fDtrControl = DTR_CONTROL_DISABLE; //DTR and RTS 0
|
||||
dcb.fRtsControl = RTS_CONTROL_DISABLE;
|
||||
|
||||
SetCommState(m_PortHandle, &dcb);
|
||||
|
||||
COMMTIMEOUTS touts;
|
||||
touts.ReadIntervalTimeout = MAXDWORD; // This, plus the zero timeouts causes immediate return
|
||||
touts.ReadTotalTimeoutMultiplier = 0;
|
||||
touts.ReadTotalTimeoutConstant = 0;
|
||||
touts.WriteTotalTimeoutConstant = 1;
|
||||
touts.WriteTotalTimeoutMultiplier = 0;
|
||||
SetCommTimeouts(m_PortHandle, &touts);
|
||||
|
||||
//SetCommMask (m_PortHandle, EV_CTS | EV_DSR | EV_RING | EV_RLSD);
|
||||
PurgeComm(m_PortHandle, PURGE_TXCLEAR | PURGE_RXCLEAR);
|
||||
return TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Open COM Error: %i\n", GetLastError());
|
||||
return FALSE; // Use GetLastError() to know the reason
|
||||
}
|
||||
}
|
||||
|
||||
void CSerialPort::Close()
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(m_PortHandle);
|
||||
m_PortHandle = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL CSerialPort::IsOpen()
|
||||
{
|
||||
return (m_PortHandle != INVALID_HANDLE_VALUE);
|
||||
}
|
||||
|
||||
DWORD CSerialPort::Read(LPVOID Buffer, DWORD BufferSize)
|
||||
{
|
||||
DWORD Res(0);
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
ReadFile(m_PortHandle, Buffer, BufferSize, &Res, NULL);
|
||||
}
|
||||
return Res;
|
||||
}
|
||||
|
||||
DWORD CSerialPort::Write(const LPVOID Buffer, DWORD BufferSize)
|
||||
{
|
||||
DWORD Res(0);
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
(void)WriteFile(m_PortHandle, Buffer, BufferSize, &Res, NULL);
|
||||
}
|
||||
return Res;
|
||||
}
|
||||
|
||||
BOOL CSerialPort::Get_CD_State()
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DWORD ModemStat;
|
||||
if (GetCommModemStatus(m_PortHandle, &ModemStat))
|
||||
{
|
||||
return (ModemStat & MS_RLSD_ON) > 0; //Not sure
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL CSerialPort::Get_CTS_State()
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DWORD ModemStat;
|
||||
if (GetCommModemStatus(m_PortHandle, &ModemStat))
|
||||
{
|
||||
return (ModemStat & MS_CTS_ON) > 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL CSerialPort::Get_DSR_State()
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DWORD ModemStat;
|
||||
if (GetCommModemStatus(m_PortHandle, &ModemStat))
|
||||
{
|
||||
return (ModemStat & MS_DSR_ON) > 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL CSerialPort::Get_RI_State()
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DWORD ModemStat;
|
||||
if (GetCommModemStatus(m_PortHandle, &ModemStat))
|
||||
{
|
||||
return (ModemStat & MS_RING_ON) > 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
void CSerialPort::Set_DTR_State(BOOL state)
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
EscapeCommFunction(m_PortHandle, (state ? SETDTR : CLRDTR));
|
||||
}
|
||||
}
|
||||
|
||||
BOOL CSerialPort::Get_DTR_State()
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DWORD ModemStat;
|
||||
if (GetCommModemStatus(m_PortHandle, &ModemStat))
|
||||
{
|
||||
return (ModemStat & MS_DSR_ON) > 0; //Not sure
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
void CSerialPort::Set_RTS_State(BOOL state)
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (0 == EscapeCommFunction(m_PortHandle, (state ? SETRTS : CLRRTS))) {
|
||||
printf("Set_RTS_State(%d) failed.\n", state ? 1 : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
BOOL CSerialPort::Get_RTS_State()
|
||||
{
|
||||
if (m_PortHandle != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DWORD ModemStat;
|
||||
if (GetCommModemStatus(m_PortHandle, &ModemStat))
|
||||
{
|
||||
return (ModemStat & MS_CTS_ON) > 0; //Not sure
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
48
AVR Working Controller/SerialPort/SerialPort.h
Normal file
48
AVR Working Controller/SerialPort/SerialPort.h
Normal file
@@ -0,0 +1,48 @@
|
||||
//
|
||||
//
|
||||
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
|
||||
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
||||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
|
||||
// PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER
|
||||
// REMAINS UNCHANGED.
|
||||
//
|
||||
// Email: yetiicb@hotmail.com
|
||||
//
|
||||
// Copyright (C) 2002-2003 Idael Cardoso.
|
||||
//
|
||||
|
||||
|
||||
#if !defined(AFX_SERIALPORT_H__731AC17A_665D_4C64_AAA7_6D284B3B7AE8__INCLUDED_)
|
||||
#define AFX_SERIALPORT_H__731AC17A_665D_4C64_AAA7_6D284B3B7AE8__INCLUDED_
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
#if _MSC_VER > 1000
|
||||
#pragma once
|
||||
#endif // _MSC_VER > 1000
|
||||
|
||||
class CSerialPort
|
||||
{
|
||||
public:
|
||||
void Set_RTS_State(BOOL state);
|
||||
void Set_DTR_State(BOOL state);
|
||||
BOOL Get_RTS_State();
|
||||
BOOL Get_DTR_State();
|
||||
BOOL Get_RI_State();
|
||||
BOOL Get_DSR_State();
|
||||
BOOL Get_CTS_State();
|
||||
BOOL Get_CD_State();
|
||||
virtual DWORD Write(const LPVOID Buffer, DWORD BufferSize);
|
||||
virtual DWORD Read(LPVOID Buffer, DWORD BufferSize);
|
||||
virtual BOOL IsOpen();
|
||||
virtual void Close();
|
||||
// Use PortName usually "COM1:" ... "COM4:" note that the name must end by ":"
|
||||
virtual BOOL Open(LPCTSTR PortName, DWORD BaudRate, BYTE ByteSize, BYTE Parity, BYTE StopBits, DWORD DesiredAccess = GENERIC_READ|GENERIC_WRITE);
|
||||
CSerialPort();
|
||||
virtual ~CSerialPort();
|
||||
|
||||
protected:
|
||||
HANDLE m_PortHandle;
|
||||
};
|
||||
|
||||
#endif // !defined(AFX_SERIALPORT_H__731AC17A_665D_4C64_AAA7_6D284B3B7AE8__INCLUDED_)
|
||||
27
AVR Working Controller/SerialPort/StdAfx.h
Normal file
27
AVR Working Controller/SerialPort/StdAfx.h
Normal file
@@ -0,0 +1,27 @@
|
||||
// stdafx.h : include file for standard system include files,
|
||||
// or project specific include files that are used frequently, but
|
||||
// are changed infrequently
|
||||
//
|
||||
|
||||
#if !defined(AFX_STDAFX_H__C610F99E_12F2_4FE0_849F_B52D1051ECFA__INCLUDED_)
|
||||
#define AFX_STDAFX_H__C610F99E_12F2_4FE0_849F_B52D1051ECFA__INCLUDED_
|
||||
|
||||
#if _MSC_VER > 1000
|
||||
#pragma once
|
||||
#endif // _MSC_VER > 1000
|
||||
|
||||
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
|
||||
|
||||
#include <afxwin.h> // MFC core and standard components
|
||||
#include <afxext.h> // MFC extensions
|
||||
#include <afxdisp.h> // MFC Automation classes
|
||||
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
|
||||
#ifndef _AFX_NO_AFXCMN_SUPPORT
|
||||
#include <afxcmn.h> // MFC support for Windows Common Controls
|
||||
#endif // _AFX_NO_AFXCMN_SUPPORT
|
||||
|
||||
|
||||
//{{AFX_INSERT_LOCATION}}
|
||||
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
|
||||
|
||||
#endif // !defined(AFX_STDAFX_H__C610F99E_12F2_4FE0_849F_B52D1051ECFA__INCLUDED_)
|
||||
Reference in New Issue
Block a user