[MFC] Serial통신 코드

2015.09.25 10:02

MFC 로 시리얼 통신하는 코드의 일부이다. 

이번에 공부를 하면서 참고한 코드이다.

WriteByte()함수에서 버퍼크기를 수정하여 사용하면 될듯싶다.


프로그램 종료시 시리얼도 닫아주는걸 잊지말자.


SerialPort.h

class CSerialPort
{
public:
	CSerialPort();
	~CSerialPort();
private:
	HANDLE	m_hComm;
	DCB		m_dcb;
	COMMTIMEOUTS m_CommTimeouts;
	BOOL	m_bPortReady;
	bool	m_bWriteRC;
	bool	m_bReadRC;
	DWORD	m_iBytesWritten;
	DWORD	m_iBytesRead;
	DWORD	m_dwBytesRead;
	BYTE	m_nWriteData[DATA_SIZE];

public:
	void ClosePort();
	bool ReadByte(BYTE &resp);
	bool ReadByte(BYTE* &resp, UINT size);
	bool WriteByte(BYTE *pBuff);
	int SetLevel(int nChannel, int nValue);
	int LightOnOff(int nChannel, bool bOn);
	bool OpenPort(CString portname);
	bool SetCommunicationTimeouts(DWORD ReadIntervalTimeout,
		DWORD ReadTotalTimeoutMultiplier, DWORD ReadTotalTimeoutConstant,
		DWORD WriteTotalTimeoutMultiplier, DWORD WriteTotalTimeoutConstant);
	bool ConfigurePort(DWORD BaudRate, BYTE ByteSize, DWORD fParity,
		BYTE  Parity, BYTE StopBits);
	BOOL ch_state[4];
	BOOL comm_status = OFF;
};

extern CSerialPort m_serial;



SerialPort.cpp

#include "stdafx.h" #include "SerialPort.h" CSerialPort m_serial; CSerialPort::CSerialPort() { } CSerialPort::~CSerialPort() { } bool CSerialPort::OpenPort(CString portname) { m_hComm = CreateFile(L"//./" + portname, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if (m_hComm == INVALID_HANDLE_VALUE) { return false; } else return true; } bool CSerialPort::ConfigurePort(DWORD BaudRate, BYTE ByteSize, DWORD fParity, BYTE Parity, BYTE StopBits) { if ((m_bPortReady = GetCommState(m_hComm, &m_dcb)) == 0) { //MessageBox(L"GetCommState Error", L"Error", MB_OK + MB_ICONERROR); CloseHandle(m_hComm); return false; } m_dcb.BaudRate = BaudRate; m_dcb.ByteSize = ByteSize; m_dcb.Parity = Parity; m_dcb.StopBits = StopBits; m_dcb.fBinary = true; m_dcb.fDsrSensitivity = false; m_dcb.fParity = fParity; m_dcb.fOutX = false; m_dcb.fInX = false; m_dcb.fNull = false; m_dcb.fAbortOnError = true; m_dcb.fOutxCtsFlow = false; m_dcb.fOutxDsrFlow = false; m_dcb.fDtrControl = DTR_CONTROL_DISABLE; m_dcb.fDsrSensitivity = false; m_dcb.fRtsControl = RTS_CONTROL_DISABLE; m_dcb.fOutxCtsFlow = false; m_dcb.fOutxCtsFlow = false; m_bPortReady = SetCommState(m_hComm, &m_dcb); if (m_bPortReady == 0) { MessageBox((HWND)"SetCommState Error",NULL, L"Error", MB_OK + MB_ICONERROR); CloseHandle(m_hComm); return false; } return true; } bool CSerialPort::SetCommunicationTimeouts(DWORD ReadIntervalTimeout, DWORD ReadTotalTimeoutMultiplier, DWORD ReadTotalTimeoutConstant, DWORD WriteTotalTimeoutMultiplier, DWORD WriteTotalTimeoutConstant) { if ((m_bPortReady = GetCommTimeouts(m_hComm, &m_CommTimeouts)) == 0) return false; m_CommTimeouts.ReadIntervalTimeout = ReadIntervalTimeout; m_CommTimeouts.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant; m_CommTimeouts.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier; m_CommTimeouts.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant; m_CommTimeouts.WriteTotalTimeoutMultiplier = WriteTotalTimeoutMultiplier; m_bPortReady = SetCommTimeouts(m_hComm, &m_CommTimeouts); if (m_bPortReady == 0) { MessageBox((HWND)"StCommTimeouts function failed", NULL, L"Com Port Error", MB_OK + MB_ICONERROR); CloseHandle(m_hComm); return false; } return true; } bool CSerialPort::WriteByte(BYTE *pBuff) { m_iBytesWritten = 0; BYTE temp[9] = { NULL }; for (int i = 0; i < 9;i++) { temp[i] = pBuff[i]; } if (WriteFile(m_hComm, temp, 9, &m_iBytesWritten, NULL) == 0) return false; else return true; } bool CSerialPort::ReadByte(BYTE &resp) { BYTE rx; resp = 0; DWORD dwBytesTransferred = 0; if (ReadFile(m_hComm, &rx, 1, &dwBytesTransferred, 0)) { if (dwBytesTransferred == 1) { resp = rx; return true; } } return false; } bool CSerialPort::ReadByte(BYTE* &resp, UINT size) { DWORD dwBytesTransferred = 0; if (ReadFile(m_hComm, resp, size, &dwBytesTransferred, 0)) { if (dwBytesTransferred == size) return true; } return false; } void CSerialPort::ClosePort() { CloseHandle(m_hComm); return; }