#include "StdAfx.h" #include #include "Serial.h" #include #include #include #include std::string writeLastError() { LPVOID lpMsgBuf; UINT error = GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (LPSTR) &lpMsgBuf, 0, NULL); std::ostringstream out; out << "Error" << lpMsgBuf; return out.str(); } CSerial::CSerial() { mIsopen = false; } CSerial::CSerial(char* port, int bitrate) { mPortstr = port; mBitrate = bitrate; mIsopen = false; open(); } CSerial::~CSerial(void) { close(); } void CSerial::open(CString port, int bitrate) { if (mIsopen) throw std::exception("Port already opened"); mPortstr = port; mBitrate = bitrate; open(); } void CSerial::open() { // Åbner Serial porten. mComport = CreateFile( mPortstr, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // Error check. if (mComport == INVALID_HANDLE_VALUE) { throw std::exception(writeLastError().c_str()); } DCB dcb; dcb.DCBlength = sizeof(DCB); mDcbRestore.DCBlength = sizeof(DCB); // Error check. if (!GetCommState(mComport,&dcb) || !GetCommState(mComport,&mDcbRestore)) { std::string error = writeLastError(); CloseHandle(mComport); throw std::exception(error.c_str()); } // Definerer Serial Conf. dcb.BaudRate = mBitrate; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fRtsControl = RTS_CONTROL_TOGGLE; dcb.fParity = false; dcb.fDsrSensitivity = false; // Error check. if (!SetCommState(mComport,&dcb)) { std::string error = writeLastError(); CloseHandle(mComport); throw std::exception(error.c_str()); } mIsopen = true; } void CSerial::close() { if (mIsopen) { while (getComstat().cbOutQue >0) { Sleep(5); } SetCommState(mComport,&mDcbRestore); CloseHandle(mComport); mIsopen = false; } } unsigned char CSerial::readByte() { unsigned char out; unsigned long size; if (!mIsopen) { throw std::exception("Port not opened"); } ReadFile(mComport,&out,1,&size,0); // Error check. if (size != 1) { std::string error = writeLastError(); CloseHandle(mComport); throw std::exception(error.c_str()); } //printByte("Read", out); return out; } void CSerial::writeByte(unsigned char out) { unsigned long size; //printByte("Write", out); if (!mIsopen) throw std::exception("Port not opened"); while (getComstat().cbOutQue >0) Sleep(2); WriteFile(mComport,&out,1,&size, NULL); if (size ==0) { std::string error = writeLastError(); CloseHandle(mComport); throw std::exception(error.c_str()); } } COMSTAT CSerial::getComstat() const { if (!mIsopen) throw std::exception("Port not opened"); COMSTAT stat; DWORD x; ClearCommError(mComport,&x,&stat); return stat; } int CSerial::bytesReady() const { return getComstat().cbInQue; } int CSerial::outQueueSize() const { return getComstat().cbOutQue; } void CSerial::printByte(char* description, unsigned char byte) { std::cout << description << " : " << (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl; std::cout << std::dec; }