/[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 44 by torben, Sun Feb 4 21:22:44 2007 UTC revision 50 by torben, Mon Feb 5 07:14:33 2007 UTC
# Line 1  Line 1 
1  #ifdef _WINDOWS  #ifndef _MSC_VER //linux
2  #include "StdAfx.h"  #include <sys/types.h>
3  #else //linux  #include <sys/stat.h>
4    
5    #include <unistd.h>
6  #include <errno.h>  #include <errno.h>
7    #include <termios.h>
8    #include <fcntl.h>
9  #endif  #endif
10    
11    #include "StdAfx.h"
12  #include "Serial.h"  #include "Serial.h"
13    
14  #include <stdexcept>  #include <stdexcept>
15  #include <string>  #include <string>
16  #include <sstream>  #include <sstream>
17    #include <iostream>
18  #include <iomanip>  #include <iomanip>
19    
20    #define _POSIX_SOURCE 1 /* POSIX compliant source */
21    #define BAUDRATE B9600
22    
23    #ifndef _MSC_VER // ugly hack, else will gcc not accept this constant in openLinux()
24    const int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
25    #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 27  std::string writeLastError() Line 40  std::string writeLastError()
40          out << "Error" << lpMsgBuf;          out << "Error" << lpMsgBuf;
41          return out.str();          return out.str();
42  #else //linux  #else //linux
43          char message[256];          return std::string( strerror(errno) );
         strerror_r(errno, message, 255);  
         return std::string(message);  
44  #endif  #endif
45  }  }
46    
# Line 45  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 65  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 106  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.c_str());                  throw std::runtime_error( error );
121          }          }
122    
123          mIsopen = true;          mIsopen = true;
# Line 114  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;
132    
133            std::cout << "opening port " << std::endl;      
134            mFiledescriptor = ::open(mPortstr, flags);
135            if (mFiledescriptor < 0)
136                    throw std::runtime_error( writeLastError() );
137    
138            std::cout << "port opened" << std::endl;
139            bzero(&newtio, sizeof(newtio) );
140    
141            // use a std. 8N1 config
142            newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
143            newtio.c_iflag = IGNPAR;
144            newtio.c_oflag = 0;
145    
146            /* set input mode (non-canonical, no echo,...) */
147            newtio.c_lflag = 0;
148                            
149            newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
150            newtio.c_cc[VMIN]     = 0;   /* blocking read until 1 chars received */
151    
152            cfmakeraw(&newtio);
153            cfsetospeed(&newtio, BAUDRATE);
154            
155                                            
156            tcflush(mFiledescriptor, TCIFLUSH);
157            tcsetattr(mFiledescriptor, TCSANOW, &newtio);
158            
159            std::cout << "port configured " << std::endl;
160            mIsopen = true;
161  }  }
162  #endif  #endif
163    
# Line 124  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);
172                  CloseHandle(mComport);                  CloseHandle(mComport);
173  #else // linux close()  #else // linux close()
174                    tcdrain(mFiledescriptor);
175                    tcsetattr(mFiledescriptor, TCSADRAIN, &mOldtio); //restore settings, when all data is written
176                    ::close(mFiledescriptor); //close()== system-call
177  #endif  #endif
178                  mIsopen = false;                  mIsopen = false;
179          }          }
180  }  }
181    
182    
183  unsigned char CSerial::readByte()  int CSerial::readByte()
184  {  {
185          unsigned char out;          unsigned char out;
186          unsigned long size;          unsigned long size;
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 152  unsigned char CSerial::readByte() Line 196  unsigned char CSerial::readByte()
196                  throw std::exception(error.c_str());                  throw std::exception(error.c_str());
197          }          }
198  #else //linux readByte()  #else //linux readByte()
199            size = read(mFiledescriptor, &out, 1);
200            if (size != 1)
201            {
202                    std::cout << writeLastError() << std::endl;
203                    return -1;
204            }
205  #endif  #endif
206    
207          //printByte("Read", out);          printByte("Read", out);
208          return out;          return out;
209  }  }
210    
# Line 163  void CSerial::writeByte(unsigned char ou Line 213  void CSerial::writeByte(unsigned char ou
213  {  {
214          unsigned long size;          unsigned long size;
215    
216          //printByte("Write", out);          printByte("Write", out);
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 179  void CSerial::writeByte(unsigned char ou Line 229  void CSerial::writeByte(unsigned char ou
229                  throw std::exception(error.c_str());                  throw std::exception(error.c_str());
230          }          }
231  #else //linux writeByte()  #else //linux writeByte()
232            //tcdrain(mFiledescriptor);
233            size = write(mFiledescriptor,&out,1);
234            Sleep(50);
235            //tcdrain(mFiledescriptor);
236            if (size != 1)
237                    throw std::runtime_error(writeLastError() );
238  #endif  #endif
239  }  }
240  #ifdef _WINDOWS  
241    #ifdef _MSC_VER
242  COMSTAT CSerial::getComstat() const  COMSTAT CSerial::getComstat() const
243  {  {
244          if (!mIsopen)          if (!mIsopen)
# Line 196  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 205  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;
269  #endif  #endif
270  }  }
271    
272  /* Debug function  // Debug function
273    //
274  void CSerial::printByte(char* description, unsigned char byte)  void CSerial::printByte(char* description, unsigned char byte)
275  {  {
276          std::cout << description << " : " <<  (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl;          std::cout << description << " : " <<  (int) byte << "/" << std::setw(2) << std::setfill('0') << std::hex << (int) byte << std::endl;
277          std::cout << std::dec;          std::cout << std::dec;
278  }  }
279  */  
280    
281  /*  /*
282  void CSerial::writeBytes(UCVector out)  void CSerial::writeBytes(UCVector out)

Legend:
Removed from v.44  
changed lines
  Added in v.50

  ViewVC Help
Powered by ViewVC 1.1.20