--- branches/linux-serial/Serial.cpp 2007/08/06 12:39:10 75 +++ branches/linux-serial/Serial.cpp 2008/06/04 13:06:09 76 @@ -7,7 +7,6 @@ #include #endif -#include "stdafx.h" #include "Serial.h" #include @@ -179,58 +178,18 @@ int CSerial::readByte() { unsigned char out; - unsigned long size; + int size = readBytes(&out,1); - if (!mIsopen) - throw std::runtime_error("Port not opened"); -#ifdef _MSC_VER - ReadFile(mComport,&out,1,&size,0); - if (size != 1) - { - std::string error = writeLastError(); - CloseHandle(mComport); - throw std::exception(error.c_str()); - } -#else //linux readByte() - size = read(mFiledescriptor, &out, 1); - if (size != 1) - { - return -1; - } -#endif + if (size == 0) + return 256; - printByte("Read", out); return out; } void CSerial::writeByte(unsigned char out) { - unsigned long size; - - printByte("Write", out); - if (!mIsopen) - throw std::runtime_error("Port not opened"); - -#ifdef _MSC_VER - 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()); - } -#else //linux writeByte() - //tcdrain(mFiledescriptor); - size = write(mFiledescriptor,&out,1); - Sleep(50); - //tcdrain(mFiledescriptor); - if (size != 1) - throw std::runtime_error(writeLastError() ); -#endif + writeBytes(&out,1); } int CSerial::convertBaudrate(Baudrate rate) @@ -351,35 +310,71 @@ } -/* -void CSerial::writeBytes(UCVector out) + +void CSerial::writeBytes(unsigned char* buf, unsigned int len) { - unsigned long bytesWritten; - unsigned int size = out.size(); - unsigned char *buf = new unsigned char[size]; + if (!mIsopen) + throw std::runtime_error("Port not opened"); +#ifdef _MSC_VER + writeBytesWindows(buf,len); +#else + writeBytesLinux(buf,len); +#endif +} + - for (int i=0; i0) + Sleep(2); - WriteFile(mComport,buf,size,&bytesWritten,NULL); - if (bytesWritten != size) + WriteFile(mComport,buf,len,&size, NULL); + if (size != len) { std::string error = writeLastError(); CloseHandle(mComport); throw std::exception(error.c_str()); } - delete[] buf; } -*/ +#else +void CSerial::writeBytesLinux(unsigned char* buf, unsigned int len) +{ + int size = write(mFiledescriptor, &buf, len); + Sleep(50); + //tcdrain(mFiledescriptor); + if (size != len) + throw std::runtime_error(writeLastError() ); +} +#endif -/* -UCVector CSerial::readBytes(int maxcount) +int CSerial::readBytes(unsigned char *buf, unsigned int maxLen) { - UCVector buf; + if (!mIsopen) + throw std::runtime_error("Port not opened"); +#ifdef _MSC_VER + return readBytesWindows(buf,maxLen); +#else + return readBytesLinux(buf,maxLen); +#endif +} + +#ifdef _MSC_VER +int CSerial::readBytesWindows(unsigned char* buf, unsigned int maxLen) +{ + unsigned long size; + ReadFile(mComport, buf, maxLen, &size, 0); + return size; +} +#else - return buf; +int CSerial::readBytesLinux(unsigned char* buf, unsigned int maxLen) +{ + return read(mFiledescriptor, buf, maxLen); } -*/ + +#endif