📄 usercom.cpp
字号:
// UserCOM.cpp: implementation of the CUserCOM class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "ugp_3000.h"
#include "UserCOM.h"
#include "memory.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CUserCOM::CUserCOM()
{
m_uCOMPort = 0;
m_hComm = NULL;
::memset(&m_Timeouts, 0, sizeof(COMMTIMEOUTS));
m_Timeouts.ReadIntervalTimeout = MAXDWORD;
}
CUserCOM::~CUserCOM()
{
if (m_hComm)
{
::CloseHandle(m_hComm);
m_hComm = NULL;
}
m_uCOMPort = 0;
}
BOOL CUserCOM::Init(unsigned uPort)
{
if (m_uCOMPort == uPort)
return TRUE;
if (m_hComm)
{
::CloseHandle(m_hComm);
m_hComm = NULL;
}
if (uPort == 0)
{
m_hComm = NULL;
m_uCOMPort = 0;
return TRUE;
}
static char* PortName[5] =
{
"",
"COM1",
"COM2",
"COM3",
"COM4"
};
m_hComm = ::CreateFile(PortName[uPort],
GENERIC_READ | GENERIC_WRITE,
NULL, NULL,
OPEN_EXISTING,
0,
NULL);
if (m_hComm == INVALID_HANDLE_VALUE)
{
m_hComm = NULL;
m_uCOMPort = 0;
return FALSE;
}
else
{
m_uCOMPort = uPort;
DCB dcb;
::GetCommState(m_hComm, &dcb);
switch(m_uCOMPort)
{
case 1:
dcb.BaudRate=57600;
dcb.ByteSize=8;
dcb.Parity=0;
dcb.fParity=0;
dcb.StopBits=0;
break;
case 2:
//::BuildCommDCB("COM2:1200,N,8,1",&dcb);
break;
case 3:
//::BuildCommDCB("COM3:1200,N,8,1",&dcb);
break;
case 4:
::BuildCommDCB("COM4:1200,N,8,1",&dcb);
break;
default:
m_hComm = NULL;
m_uCOMPort = 0;
return FALSE;
break;
}
//::GetCommState(m_hComm,&dcb);
::SetCommState(m_hComm,&dcb);
::SetCommTimeouts(m_hComm, &m_Timeouts);
return TRUE;
}
}
BOOL CUserCOM::Read()
{
if (!m_uCOMPort)
return FALSE;
return ::ReadFile(m_hComm, m_chRead, 256, &m_dwBytesRead, NULL);
}
BOOL CUserCOM::Write(char* chWrite)
{
if (!m_uCOMPort)
return FALSE;
DWORD dw;
return ::WriteFile(m_hComm, chWrite,1, &dw, NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -