//////////////////////////////////////////////////////////////
//
// 언제쯤인지 ㅋㅋㅋ 생각해보니 좀 됬나? 어데서 봐서 넣었던거 같은데 ㅡ.ㅡ;;
// 그래도 통신은 잘 되더군..
//
//////////////////////////////////////////////////////////////
#include "./serialctl.h"
CSerialCtl::CSerialCtl(void) { m_hSerialPort = NULL; m_bPortState = false; }
CSerialCtl::~CSerialCtl(void) { }
////////////////////////////////////////////////////////////////////////// // Serial Pont를 연다 bool CSerialCtl::OpenSerialPort(DCB dcbSet, char* pchrPortName) { if( isOpenPort() == false) { m_hSerialPort = CreateFile(pchrPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if(GetCommState(m_hSerialPort, &m_dcbconfig) == 0) { AfxMessageBox("Serial Port를 생성실패 \n"); return false; } m_dcbconfig.BaudRate = dcbSet.BaudRate; m_dcbconfig.StopBits = dcbSet.StopBits; m_dcbconfig.Parity = dcbSet.Parity; m_dcbconfig.ByteSize = dcbSet.ByteSize;
if(SetCommState(m_hSerialPort, &m_dcbconfig) == 0) { AfxMessageBox(" Serial Port를 초기화 실패 \n"); return false; }
COMMTIMEOUTS comTimeOut; comTimeOut.ReadIntervalTimeout = 3; comTimeOut.ReadTotalTimeoutMultiplier = 3; comTimeOut.ReadTotalTimeoutConstant = 2; comTimeOut.WriteTotalTimeoutMultiplier = 3; comTimeOut.WriteTotalTimeoutConstant = 2; SetCommTimeouts(m_hSerialPort, &comTimeOut); SetOpenPort(TRUE); return TRUE; }
return false; }
////////////////////////////////////////////////////////////////////////// // Open 한 Port를 닫는다. bool CSerialCtl::ClosePort() { if(isOpenPort()) { SetOpenPort(false); if(CloseHandle(m_hSerialPort) == 0) { AfxMessageBox("Close 실패"); return false; } return true; } return false; }
////////////////////////////////////////////////////////////////////////// // 현재 Port 상태 리턴 bool CSerialCtl::isOpenPort() { return m_bPortState; }
////////////////////////////////////////////////////////////////////////// // Port Open 성공 void CSerialCtl::SetOpenPort(bool bOpenState) { m_bPortState = bOpenState; }
////////////////////////////////////////////////////////////////////////// // Port를 읽는다. bool CSerialCtl::ReadPort(char* pchrInputData, const unsigned long& unsizeBuffer, unsigned long& unLength) { if(ReadFile(m_hSerialPort, pchrInputData, unsizeBuffer, &unLength, NULL) == 0 ) { AfxMessageBox("Reading False \n"); return false; }
if(unLength > 0) { pchrInputData[unLength] = NULL; return true; } return true; }
////////////////////////////////////////////////////////////////////////// // Write the Port bool CSerialCtl::WritePort(LPCVOID lpOutputData, const unsigned int& unsizeBuffer, unsigned long& unLength) { if(unLength > 0) { if(WriteFile(m_hSerialPort, lpOutputData, unsizeBuffer, &unLength, NULL) == 0) { AfxMessageBox("WritePort FALSE \n"); return false; } return true; } return false; }
|
댓글 없음:
댓글 쓰기