#include "StdAfx.h" #include "Serial.h" #include #include #include #include std::string writeLastError() { LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 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(char* port, int bitrate) { if (mIsopen) throw std::exception("Port already opened"); mPortstr = port; mBitrate = bitrate; open(); } void CSerial::open() { mComport = CreateFile( mPortstr, GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); if (mComport == INVALID_HANDLE_VALUE) { throw std::exception(writeLastError().c_str()); } DCB dcb; dcb.DCBlength = sizeof(DCB); mDcbRestore.DCBlength = sizeof(DCB); if (!GetCommState(mComport,&dcb) || !GetCommState(mComport,&mDcbRestore)) { std::string error = writeLastError(); CloseHandle(mComport); throw std::exception(error.c_str()); } dcb.BaudRate = mBitrate; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fParity = false; dcb.fDsrSensitivity = false; 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); 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; } /* void CSerial::writeBytes(UCVector out) { unsigned long bytesWritten; unsigned int size = out.size(); unsigned char *buf = new unsigned char[size]; for (int i=0; i