//=============================================================================
// General component library for WIN32
// Copyright (C) 2000, UAB BBDSoft ( http://www.bbdsoft.com/ )
//
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
//
// The author of this program may be contacted at [email protected]
//=============================================================================
#ifndef _COMPORT_
#include "ComPort.hpp"
#endif
#ifndef _WINDOWS_
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifndef _STDEXCEPT_
#include <stdexcept>
#endif
using namespace std;
//----------------------------------------------------------------------------
COMPort::COMPort ( const char * const portName )
: theDCB (NULL)
{
thePortHandle = (unsigned ) CreateFile ( portName
, GENERIC_READ | GENERIC_WRITE
, 0
, NULL
, OPEN_EXISTING
, FILE_FLAG_NO_BUFFERING
, NULL
);
if (thePortHandle == HFILE_ERROR)
{
throw runtime_error ("COMPort: failed to open.");
} // endif
theDCB = new char [sizeof(DCB)];
getState();
setBlockingMode();
setHandshaking();
} // end constructor
//----------------------------------------------------------------------------
COMPort::~COMPort()
{
delete [] theDCB;
// close serial port device
if (CloseHandle ((HANDLE)thePortHandle) == FALSE )
{
throw runtime_error ("COMPort: failed to close.");
} // endif
} // end destructor
//----------------------------------------------------------------------------
void COMPort::getState () const
{
if (!GetCommState ( (HANDLE) thePortHandle
, (LPDCB) theDCB
)
)
{
throw runtime_error ("COMPort: could not retrieve serial port state.");
} // endif
} // end COMPort::getState () const
//----------------------------------------------------------------------------
COMPort& COMPort::setState ()
{
if (!SetCommState ( (HANDLE) thePortHandle
, (LPDCB) theDCB
)
)
{
throw runtime_error ("COMPort: could not modify serial port state.");
} // endif
return *this;
} // end COMPort::setState ()
//-----------------------------------------------------------------------------
COMPort& COMPort::setBitRate ( unsigned long Param )
{
DCB & aDCB = *((LPDCB)theDCB);
aDCB.BaudRate = Param;
return setState();
} // end COMPort::setBitRate (..)
//-----------------------------------------------------------------------------
unsigned long COMPort::bitRate() const
{
DCB & aDCB = *((LPDCB)theDCB);
return aDCB.BaudRate;
} // end COMPort::bitRate () const
//-----------------------------------------------------------------------------
COMPort& COMPort::setLineCharacteristics( char * inConfig )
{
COMMTIMEOUTS aTimeout;
if ( !BuildCommDCBAndTimeouts ( inConfig
, (LPDCB)theDCB
, &aTimeout
)
)
{
throw runtime_error ("COMPort: could not set line characteristics.");
} // endif
if ( ! SetCommTimeouts ( (HANDLE(thePortHandle))
, &aTimeout
)
)
{
throw runtime_error ("COMPort: could not set line characteristics.");
} // endif
return setState();
}
//----------------------------------------------------------------------------
char COMPort::read ()
{
char buffer;
DWORD charsRead = 0;
do
{
if (! ReadFile ( (HANDLE(thePortHandle))
, &buffer
, sizeof(char)
, &charsRead
, NULL
)
)
{
throw runtime_error ("COMPort: read failed.");
} // endif
} while ( !charsRead );
return buffer;
} // end COMPort::read()
//----------------------------------------------------------------------------
unsigned long COMPort::read ( void *inBuffer
, const unsigned long inCharsReq
)
{
DWORD charsRead = 0;
if ( !ReadFile ( (HANDLE(thePortHandle))
, inBuffer
, inCharsReq
, &charsRead
, NULL
)
)
{
throw runtime_error ("COMPort: read failed.");
} // endif
return charsRead;
} // end COMPort::read (..)
//----------------------------------------------------------------------------
COMPort & COMPort::write ( const char inChar )
{
char buffer = inChar;
DWORD charsWritten = 0;
if ( !WriteFile ( (HANDLE(thePortHandle))
, &buffer
, sizeof(char)
, &charsWritten
, NULL
)
)
{
throw runtime_error ("COMPort: write failed.");
} // endif
return *this;
} // end COMPort::write (..)
//----------------------------------------------------------------------------
unsigned long COMPort::write ( const void *inBuffer
, const unsigned long inBufSize
)
{
DWORD charsWritten = 0;
if ( !WriteFile ( (HANDLE(thePortHandle))
, inBuffer
, inBufSize
, &charsWritten
, NULL
)
)
{
throw runtime_error ("COMPort: write failed.");
} // endif
return charsWritten;
} // end COMPort::write()
//-----------------------------------------------------------------------------
COMPort& COMPort::setxONxOFF ( bool Param )
{
DCB & aDCB = *((LPDCB)theDCB);
aDCB.fOutX = Param ? 1 : 0;
aDCB.fInX = Param ? 1 : 0;
return setState();
} // end COMPort::setxONxOFF (..)
//-----------------------------------------------------------------------------
bool COMPort::isxONxOFF () const
{
DCB & aDCB = *((LPDCB)theDCB);
return (aDCB.fOutX && aDCB.fInX);
} // end COMPort::isxONxOFF () const
//----------------------------------------------------------------------------
COMPort& COMPort::setBlockingMode ( unsigned long inReadInterval
, unsigned long inReadMultiplyer
, unsigned long inReadConstant
)
{
COMMTIMEOUTS commTimeout;
if ( !GetCommTimeouts ( (HANDLE(thePortHandle))
, &commTimeout
)
)
{
throw runtime_error ("COMPort: failed to retrieve timeouts.");
} // endif
commTimeout.ReadIntervalTimeout = inReadInterval;
if ( inReadInterval==MAXDWORD )
{
commTimeout.ReadTotalTimeoutMultiplier = 0;
commTimeout.ReadTotalTimeoutConstant = 0;
}
else
{
commTimeout.ReadTotalTimeoutMultiplier = inReadMultiplyer;
commTimeout.ReadTotalTimeoutConstant = inReadConstant;
} // endifelse
if ( !SetCommTimeouts ( (HANDLE(thePortHandle))
, &commTimeout
)
)
{
throw runtime_error ("COMPort: failed to modify timeouts.");
} // endif
return *this;
} // end COMPort::setBlockingMode (..)
//-----------------------------------------------------------------------------
COMPort & COMPort::setHandshaking ( bool inHandshaking )
{
DCB & aDCB = *((LPDCB)theDCB);
if (i
Com_Class.zip_NT serial_class
版权申诉
141 浏览量
2022-09-24
14:50:36
上传
评论
收藏 4KB ZIP 举报
小贝德罗
- 粉丝: 70
- 资源: 1万+
最新资源
- html动态爱心代码一(附源码)
- c40539bc-071a-486c-9d52-9d0c18d62dac 4.html
- 基于物理的非视域成像(NLOS)算法,利用了nerf+python源码+文档说明
- yuluer知更鸟.7z(1).001
- python课程设计-基于tensorflow实现的图文生成程序,数据集flickr30k-images+源代码+文档说明+截图
- python作业-基于Flickr30k数据集实现图像文本跨模态搜索python源码+数据集+测试界面+项目说明(高分课程设计)
- 基于Qt实现医院信息管理系统c++源码+文档说明+数据库(期末大作业)
- 基于python实现的医院信息管理系统完整源码+sql数据库+详细注释(高分课程设计)
- 基于python的眼底图像视杯视盘分割项目源码+文档说明+截图演示+详细注释(高分课程设计)
- ImageBasedModellingEdu-贰壹贰叁零
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈