/[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 58 by torben, Mon Feb 5 10:43:49 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    #include <unistd.h>
5  #include <errno.h>  #include <errno.h>
6    #include <termios.h>
7    #include <fcntl.h>
8  #endif  #endif
9    
10    #include "stdafx.h"
11  #include "Serial.h"  #include "Serial.h"
12    
13  #include <stdexcept>  #include <stdexcept>
14  #include <string>  #include <string>
15  #include <sstream>  #include <sstream>
16    
17    
18    #ifdef DEBUG
19    #include <iostream>
20  #include <iomanip>  #include <iomanip>
21    #endif
22    
23    
24    
25    
26  std::string writeLastError()  std::string writeLastError()
27  {  {
28  #ifdef _WINDOWS  #ifdef _MSC_VER
29          LPVOID lpMsgBuf;          LPVOID lpMsgBuf;
30          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,
31                          NULL,                          NULL,
# Line 24  std::string writeLastError() Line 36  std::string writeLastError()
36                          NULL);                          NULL);
37    
38          std::ostringstream out;          std::ostringstream out;
39          out << "Error" << lpMsgBuf;          out << "Error " << lpMsgBuf;
40          return out.str();          return out.str();
41  #else //linux  #else //linux
42          char message[256];          return std::string( strerror(errno) );
         strerror_r(errno, message, 255);  
         return std::string(message);  
43  #endif  #endif
44  }  }
45    
# Line 39  CSerial::CSerial() Line 49  CSerial::CSerial()
49          mIsopen = false;          mIsopen = false;
50  }  }
51    
52  CSerial::CSerial(char* port, int bitrate)  
53    CSerial::CSerial(char* port, Baudrate bitrate)
54  {  {
55          mPortstr = port;          mPortstr = port;
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 57  CSerial::~CSerial(void) Line 68  CSerial::~CSerial(void)
68          close();          close();
69  }  }
70    
71  void CSerial::open(char* port, int bitrate)  void CSerial::open(char* port, Baudrate bitrate)
72  {  {
73          if (mIsopen)          if (mIsopen)
74                  throw std::runtime_error("Port already opened");                  throw std::runtime_error("Port already opened");
# 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 93  void CSerial::openWindows() Line 104  void CSerial::openWindows()
104                  throw std::exception(error.c_str());                  throw std::exception(error.c_str());
105          }          }
106    
107          dcb.BaudRate = mBitrate;          dcb.BaudRate = convertBaudrate(mBitrate);
108          dcb.ByteSize = 8;          dcb.ByteSize = 8;
109          dcb.Parity = NOPARITY;          dcb.Parity = NOPARITY;
110          dcb.StopBits = ONESTOPBIT;          dcb.StopBits = ONESTOPBIT;
# 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            mFiledescriptor = ::open(mPortstr, O_RDWR | O_NOCTTY | O_NONBLOCK);
134            if (mFiledescriptor < 0)
135                    throw std::runtime_error( writeLastError() );
136    
137            bzero(&newtio, sizeof(newtio) );
138    
139            // use a std. 8N1 config
140            newtio.c_cflag = convertBaudrate(mBitrate) | CRTSCTS | CS8 | CLOCAL | CREAD;
141            newtio.c_iflag = IGNPAR;
142            newtio.c_oflag = 0;
143    
144            // set input mode (non-canonical, no echo,...)
145            newtio.c_lflag = 0;
146                            
147            newtio.c_cc[VTIME]    = 0;   // inter-character timer unused
148            newtio.c_cc[VMIN]     = 0;   // blocking read until 1 chars received
149            
150                                            
151            tcflush(mFiledescriptor, TCIFLUSH);
152            tcsetattr(mFiledescriptor, TCSANOW, &newtio);
153            
154            mIsopen = true;
155  }  }
156  #endif  #endif
157    
# Line 124  void CSerial::close() Line 159  void CSerial::close()
159  {  {
160          if (mIsopen)          if (mIsopen)
161          {          {
162  #ifdef _WINDOWS  #ifdef _MSC_VER
163                  while (getComstat().cbOutQue >0)                  while (getComstat().cbOutQue >0)
164                          Sleep(5);                          Sleep(5);
165                  SetCommState(mComport,&mDcbRestore);                  SetCommState(mComport,&mDcbRestore);
166                  CloseHandle(mComport);                  CloseHandle(mComport);
167  #else // linux close()  #else // linux close()
168                    tcdrain(mFiledescriptor);
169                    tcsetattr(mFiledescriptor, TCSADRAIN, &mOldtio); //restore settings, when all data is written
170                    ::close(mFiledescriptor); //close()== system-call
171  #endif  #endif
172                  mIsopen = false;                  mIsopen = false;
173          }          }
174  }  }
175    
176    
177  unsigned char CSerial::readByte()  int CSerial::readByte()
178  {  {
179          unsigned char out;          unsigned char out;
180          unsigned long size;          unsigned long size;
181    
182          if (!mIsopen)          if (!mIsopen)
183                  throw std::runtime_error("Port not opened");                  throw std::runtime_error("Port not opened");
184  #ifdef _WINDOWS  #ifdef _MSC_VER
185          ReadFile(mComport,&out,1,&size,0);          ReadFile(mComport,&out,1,&size,0);
186          if (size != 1)          if (size != 1)
187          {          {
# Line 152  unsigned char CSerial::readByte() Line 190  unsigned char CSerial::readByte()
190                  throw std::exception(error.c_str());                  throw std::exception(error.c_str());
191          }          }
192  #else //linux readByte()  #else //linux readByte()
193            size = read(mFiledescriptor, &out, 1);
194            if (size != 1)
195            {
196                    return -1;
197            }
198  #endif  #endif
199    
200          //printByte("Read", out);          printByte("Read", out);
201          return out;          return out;
202  }  }
203    
# Line 163  void CSerial::writeByte(unsigned char ou Line 206  void CSerial::writeByte(unsigned char ou
206  {  {
207          unsigned long size;          unsigned long size;
208    
209          //printByte("Write", out);          printByte("Write", out);
210          if (!mIsopen)          if (!mIsopen)
211                  throw std::runtime_error("Port not opened");                  throw std::runtime_error("Port not opened");
212    
213  #ifdef _WINDOWS  #ifdef _MSC_VER
214          while (getComstat().cbOutQue >0)          while (getComstat().cbOutQue >0)
215                  Sleep(2);                  Sleep(2);
216    
# Line 179  void CSerial::writeByte(unsigned char ou Line 222  void CSerial::writeByte(unsigned char ou
222                  throw std::exception(error.c_str());                  throw std::exception(error.c_str());
223          }          }
224  #else //linux writeByte()  #else //linux writeByte()
225            //tcdrain(mFiledescriptor);
226            size = write(mFiledescriptor,&out,1);
227            Sleep(50);
228            //tcdrain(mFiledescriptor);
229            if (size != 1)
230                    throw std::runtime_error(writeLastError() );
231    #endif
232    }
233    
234    int CSerial::convertBaudrate(Baudrate rate)
235    {
236            int retval=0;
237    #ifdef _MSC_VER
238            switch( rate )
239            {
240                    case Baud300:
241                            retval = 300;
242                            break;
243                    case Baud600:
244                            retval = 600;
245                            break;
246                    case Baud1200:
247                            retval = 1200;
248                            break;
249                    case Baud2400:
250                            retval = 2400;
251                            break;
252                    case Baud4800:
253                            retval = 4800;
254                            break;
255                    case Baud9600:
256                            retval = 9600;
257                            break;
258                    case Baud19200:
259                            retval = 19200;
260                            break;
261                    case Baud38400:
262                            retval = 38400;
263                            break;
264                    case Baud57600:
265                            retval = 57600;
266                            break;
267                    case Baud115200:
268                            retval = 115200;
269                            break;                  
270            }
271    #else
272            switch (rate)
273            {
274                    case Baud300:
275                            retval = B300;
276                            break;
277                    case Baud600:
278                            retval = B600;
279                            break;
280                    case Baud1200:
281                            retval = B1200;
282                            break;
283                    case Baud2400:
284                            retval = B2400;
285                            break;
286                    case Baud4800:
287                            retval = B4800;
288                            break;
289                    case Baud9600:
290                            retval = B9600;
291                            break;
292                    case Baud19200:
293                            retval = B19200;
294                            break;
295                    case Baud38400:
296                            retval = B38400;
297                            break;
298                    case Baud57600:
299                            retval = B57600;
300                            break;
301                    case Baud115200:
302                            retval = B115200;
303                            break;
304            }
305  #endif  #endif
306    
307            return retval;
308  }  }
309  #ifdef _WINDOWS  
310    #ifdef _MSC_VER
311  COMSTAT CSerial::getComstat() const  COMSTAT CSerial::getComstat() const
312  {  {
313          if (!mIsopen)          if (!mIsopen)
# Line 196  COMSTAT CSerial::getComstat() const Line 322  COMSTAT CSerial::getComstat() const
322    
323  int CSerial::bytesReady() const  int CSerial::bytesReady() const
324  {  {
325  #ifdef _WINDOWS  #ifdef _MSC_VER
326          return getComstat().cbInQue;          return getComstat().cbInQue;
327  #else  #else
328          return 0;          return 0;
# Line 205  int CSerial::bytesReady() const Line 331  int CSerial::bytesReady() const
331    
332  int CSerial::outQueueSize() const  int CSerial::outQueueSize() const
333  {  {
334  #ifdef _WINDOWS  #ifdef _MSC_VER
335          return getComstat().cbOutQue;          return getComstat().cbOutQue;
336  #else  #else
337          return 0;          return 0;
338  #endif  #endif
339  }  }
340    
341  /* Debug function  // Debug function
342    //
343  void CSerial::printByte(char* description, unsigned char byte)  void CSerial::printByte(char* description, unsigned char byte)
344  {  {
345    #ifdef DEBUG
346          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;
347          std::cout << std::dec;          std::cout << std::dec;
348    #endif
349  }  }
350  */  
351    
352  /*  /*
353  void CSerial::writeBytes(UCVector out)  void CSerial::writeBytes(UCVector out)

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

  ViewVC Help
Powered by ViewVC 1.1.20