--- branches/linux-serial/Serial.cpp 2007/02/04 20:11:41 43 +++ branches/linux-serial/Serial.cpp 2007/02/04 21:22:44 44 @@ -1,14 +1,19 @@ +#ifdef _WINDOWS #include "StdAfx.h" +#else //linux +#include +#endif #include "Serial.h" -#include +#include #include #include #include std::string writeLastError() { +#ifdef _WINDOWS LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, @@ -21,6 +26,11 @@ std::ostringstream out; out << "Error" << lpMsgBuf; return out.str(); +#else //linux + char message[256]; + strerror_r(errno, message, 255); + return std::string(message); +#endif } @@ -35,7 +45,11 @@ mBitrate = bitrate; mIsopen = false; - open(); +#ifdef _WINDOWS + openWindows(); +#else + openLinux(); +#endif } CSerial::~CSerial(void) @@ -46,21 +60,26 @@ void CSerial::open(char* port, int bitrate) { if (mIsopen) - throw std::exception("Port already opened"); + throw std::runtime_error("Port already opened"); mPortstr = port; mBitrate = bitrate; - - open(); + +#ifdef _WINDOWS + openWindows(); +#else + openLinux(); +#endif } -void CSerial::open() +#ifdef _WINDOWS +void CSerial::openWindows() { 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()); + throw std::runtime_error(writeLastError().c_str()); } DCB dcb; @@ -92,15 +111,26 @@ mIsopen = true; } +#endif + + +#ifndef _WINDOWS +void CSerial::openLinux() +{ +} +#endif void CSerial::close() { if (mIsopen) { +#ifdef _WINDOWS while (getComstat().cbOutQue >0) Sleep(5); SetCommState(mComport,&mDcbRestore); CloseHandle(mComport); +#else // linux close() +#endif mIsopen = false; } } @@ -112,8 +142,8 @@ unsigned long size; if (!mIsopen) - throw std::exception("Port not opened"); - + throw std::runtime_error("Port not opened"); +#ifdef _WINDOWS ReadFile(mComport,&out,1,&size,0); if (size != 1) { @@ -121,9 +151,10 @@ CloseHandle(mComport); throw std::exception(error.c_str()); } +#else //linux readByte() +#endif - - printByte("Read", out); + //printByte("Read", out); return out; } @@ -132,10 +163,11 @@ { unsigned long size; - printByte("Write", out); + //printByte("Write", out); if (!mIsopen) - throw std::exception("Port not opened"); + throw std::runtime_error("Port not opened"); +#ifdef _WINDOWS while (getComstat().cbOutQue >0) Sleep(2); @@ -146,8 +178,10 @@ CloseHandle(mComport); throw std::exception(error.c_str()); } +#else //linux writeByte() +#endif } - +#ifdef _WINDOWS COMSTAT CSerial::getComstat() const { if (!mIsopen) @@ -158,23 +192,33 @@ ClearCommError(mComport,&x,&stat); return stat; } +#endif int CSerial::bytesReady() const { +#ifdef _WINDOWS return getComstat().cbInQue; +#else + return 0; +#endif } int CSerial::outQueueSize() const { +#ifdef _WINDOWS return getComstat().cbOutQue; +#else + return 0; +#endif } - +/* Debug function 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)