/[H7]/branches/linux-serial/Serial.cpp
ViewVC logotype

Diff of /branches/linux-serial/Serial.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 48 by torben, Mon Feb 5 00:48:02 2007 UTC revision 49 by torben, Mon Feb 5 07:06:43 2007 UTC
# Line 1  Line 1 
1  #ifdef _WINDOWS  #ifndef _MSC_VER //linux
 #include "StdAfx.h"  
 #else //linux  
2  #include <sys/types.h>  #include <sys/types.h>
3  #include <sys/stat.h>  #include <sys/stat.h>
4    
# Line 10  Line 8 
8  #include <fcntl.h>  #include <fcntl.h>
9  #endif  #endif
10    
11    #include "StdAfx.h"
12  #include "Serial.h"  #include "Serial.h"
13    
14  #include <stdexcept>  #include <stdexcept>
# Line 21  Line 20 
20  #define _POSIX_SOURCE 1 /* POSIX compliant source */  #define _POSIX_SOURCE 1 /* POSIX compliant source */
21  #define BAUDRATE B9600  #define BAUDRATE B9600
22    
23  #ifndef _WINDOWS // ugly hack, else will gcc not accept this constant in openLinux()  #ifndef _MSC_VER // ugly hack, else will gcc not accept this constant in openLinux()
24  const int flags = O_RDWR | O_NOCTTY ; //| O_NONBLOCK;  const int flags = O_RDWR | O_NOCTTY ; //| O_NONBLOCK;
25  #endif  #endif
26    
27  std::string writeLastError()  std::string writeLastError()
28  {  {
29  #ifdef _WINDOWS  #ifdef _MSC_VER
30          LPVOID lpMsgBuf;          LPVOID lpMsgBuf;
31          FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,          FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
32                          NULL,                          NULL,
# Line 57  CSerial::CSerial(char* port, int bitrate Line 56  CSerial::CSerial(char* port, int bitrate
56          mBitrate = bitrate;          mBitrate = bitrate;
57          mIsopen = false;          mIsopen = false;
58    
59  #ifdef _WINDOWS  #ifdef _MSC_VER
60          openWindows();          openWindows();
61  #else  #else
62          openLinux();          openLinux();
# Line 77  void CSerial::open(char* port, int bitra Line 76  void CSerial::open(char* port, int bitra
76          mPortstr = port;          mPortstr = port;
77          mBitrate = bitrate;          mBitrate = bitrate;
78                    
79  #ifdef _WINDOWS  #ifdef _MSC_VER
80          openWindows();          openWindows();
81  #else  #else
82          openLinux();          openLinux();
83  #endif  #endif
84  }  }
85    
86  #ifdef _WINDOWS  #ifdef _MSC_VER
87  void CSerial::openWindows()  void CSerial::openWindows()
88  {  {
89          mComport = CreateFile( mPortstr, GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);          mComport = CreateFile( mPortstr, GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
# Line 118  void CSerial::openWindows() Line 117  void CSerial::openWindows()
117          {          {
118                  std::string error = writeLastError();                  std::string error = writeLastError();
119                  CloseHandle(mComport);                  CloseHandle(mComport);
120                  throw std::exception( error );                  throw std::runtime_error( error );
121          }          }
122    
123          mIsopen = true;          mIsopen = true;
# Line 126  void CSerial::openWindows() Line 125  void CSerial::openWindows()
125  #endif  #endif
126    
127    
128  #ifndef _WINDOWS  #ifndef _MSC_VER
129  void CSerial::openLinux()  void CSerial::openLinux()
130  {  {
131          termios newtio;          termios newtio;
# Line 166  void CSerial::close() Line 165  void CSerial::close()
165  {  {
166          if (mIsopen)          if (mIsopen)
167          {          {
168  #ifdef _WINDOWS  #ifdef _MSC_VER
169                  while (getComstat().cbOutQue >0)                  while (getComstat().cbOutQue >0)
170                          Sleep(5);                          Sleep(5);
171                  SetCommState(mComport,&mDcbRestore);                  SetCommState(mComport,&mDcbRestore);
# Line 188  int CSerial::readByte() Line 187  int CSerial::readByte()
187    
188          if (!mIsopen)          if (!mIsopen)
189                  throw std::runtime_error("Port not opened");                  throw std::runtime_error("Port not opened");
190  #ifdef _WINDOWS  #ifdef _MSC_VER
191          ReadFile(mComport,&out,1,&size,0);          ReadFile(mComport,&out,1,&size,0);
192          if (size != 1)          if (size != 1)
193          {          {
# Line 218  void CSerial::writeByte(unsigned char ou Line 217  void CSerial::writeByte(unsigned char ou
217          if (!mIsopen)          if (!mIsopen)
218                  throw std::runtime_error("Port not opened");                  throw std::runtime_error("Port not opened");
219    
220  #ifdef _WINDOWS  #ifdef _MSC_VER
221          while (getComstat().cbOutQue >0)          while (getComstat().cbOutQue >0)
222                  Sleep(2);                  Sleep(2);
223    
# Line 239  void CSerial::writeByte(unsigned char ou Line 238  void CSerial::writeByte(unsigned char ou
238  #endif  #endif
239  }  }
240    
241  #ifdef _WINDOWS  #ifdef _MSC_VER
242  COMSTAT CSerial::getComstat() const  COMSTAT CSerial::getComstat() const
243  {  {
244          if (!mIsopen)          if (!mIsopen)
# Line 254  COMSTAT CSerial::getComstat() const Line 253  COMSTAT CSerial::getComstat() const
253    
254  int CSerial::bytesReady() const  int CSerial::bytesReady() const
255  {  {
256  #ifdef _WINDOWS  #ifdef _MSC_VER
257          return getComstat().cbInQue;          return getComstat().cbInQue;
258  #else  #else
259          return 0;          return 0;
# Line 263  int CSerial::bytesReady() const Line 262  int CSerial::bytesReady() const
262    
263  int CSerial::outQueueSize() const  int CSerial::outQueueSize() const
264  {  {
265  #ifdef _WINDOWS  #ifdef _MSC_VER
266          return getComstat().cbOutQue;          return getComstat().cbOutQue;
267  #else  #else
268          return 0;          return 0;

Legend:
Removed from v.48  
changed lines
  Added in v.49

  ViewVC Help
Powered by ViewVC 1.1.20