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

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

  ViewVC Help
Powered by ViewVC 1.1.20