--- branches/linux-serial/Serial.cpp 2007/02/04 21:33:40 45 +++ branches/linux-serial/Serial.cpp 2007/02/04 23:11:02 46 @@ -1,7 +1,13 @@ #ifdef _WINDOWS #include "StdAfx.h" #else //linux +#include +#include + +#include #include +#include +#include #endif #include "Serial.h" @@ -9,8 +15,16 @@ #include #include #include +#include #include +#define _POSIX_SOURCE 1 /* POSIX compliant source */ +#define BAUDRATE B38400 + +#ifndef _WINDOWS // ugly hack, else will gcc not accept this constant in openLinux() +const int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; +#endif + std::string writeLastError() { #ifdef _WINDOWS @@ -27,9 +41,7 @@ out << "Error" << lpMsgBuf; return out.str(); #else //linux - char message[256]; - strerror_r(errno, message, 255); - return std::string(message); + return std::string( strerror(errno) ); #endif } @@ -106,7 +118,7 @@ { std::string error = writeLastError(); CloseHandle(mComport); - throw std::exception(error.c_str()); + throw std::exception( error ); } mIsopen = true; @@ -117,6 +129,32 @@ #ifndef _WINDOWS void CSerial::openLinux() { + termios newtio; + + std::cout << "opening port " << std::endl; + mFiledescriptor = ::open(mPortstr, flags); + if (mFiledescriptor < 0) + throw std::runtime_error( writeLastError() ); + + std::cout << "port opened" << std::endl; + bzero(&newtio, sizeof(newtio) ); + + // use a std. 8N1 config + newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; + newtio.c_iflag = IGNPAR; + newtio.c_oflag = 0; + + /* set input mode (non-canonical, no echo,...) */ + newtio.c_lflag = 0; + + newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ + newtio.c_cc[VMIN] = 1; /* blocking read until 1 chars received */ + + tcflush(mFiledescriptor, TCIFLUSH); + tcsetattr(mFiledescriptor, TCSANOW, &newtio); + + std::cout << "port configured " << std::endl; + mIsopen = true; } #endif @@ -130,6 +168,9 @@ SetCommState(mComport,&mDcbRestore); CloseHandle(mComport); #else // linux close() + tcdrain(mFiledescriptor); + tcsetattr(mFiledescriptor, TCSADRAIN, &mOldtio); //restore settings, when all data is written + ::close(mFiledescriptor); //close()== system-call #endif mIsopen = false; } @@ -152,9 +193,12 @@ throw std::exception(error.c_str()); } #else //linux readByte() + size = read(mFiledescriptor, &out, 1); + if (size != 1) + throw std::runtime_error( writeLastError() ); #endif - //printByte("Read", out); + printByte("Read", out); return out; } @@ -179,8 +223,12 @@ throw std::exception(error.c_str()); } #else //linux writeByte() + size = write(mFiledescriptor,&out,1); + if (size != 1) + throw std::runtime_error(writeLastError() ); #endif } + #ifdef _WINDOWS COMSTAT CSerial::getComstat() const { @@ -212,13 +260,14 @@ #endif } -/* Debug function +// 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)