TactileVestProject  V1.0
Tactile API documentation
 All Classes Functions
Serial_Unix.h
1 #ifdef __linux__
2  #define UNIX
3 #elif __APPLE__
4  #define UNIX
5 #endif
6 #ifdef UNIX
7 #ifndef _SERIAL_Unix_H_
8 #define _SERIAL_Unix_H_
9 #include "Serial.h"
10 #include <termios.h>
11 #include <string>
12 
13 // Based on the serial class by Brian Baumhover
14 using namespace std;
15 class Serial_Unix: public Serial
16 {
17 public:
18  Serial_Unix()
19  ~Serial_Unix();
20 
21  bool Open(char *);
22  bool Close( void );
23 
24  int ReadData( void *, int );
25  int SendData(char *);
26 private:
27  int mBaudRate;
28  string mDeviceName;
29  int mCflag;
30  int mIflag;
31  int mOflag;
32  int mLflag;
33 
34  bool mAcquired;
35 
36  int mFd;
37 
38  struct termios mOldtio;
39 
40 
41  void initSerial(string deviceName, int baudrate)
42  {
43  mDeviceName = deviceName;
44 
45  switch( baudrate )
46  {
47  case 9600:
48  baudrate = B9600;
49  break;
50 
51  case 19200:
52  baudrate = B19200;
53  break;
54 
55  case 38400:
56  baudrate = B38400;
57  break;
58 
59  case 57600:
60  baudrate = B57600;
61  break;
62 
63  case 115200:
64  baudrate = B115200;
65  break;
66 
67  //case 125000:
68  //baudrate = B125000;
69  //break;
70 
71  default:
72  printf( "invalid baud rate for serial port %s\n", deviceName.c_str() );
73  baudrate = B9600;
74  break;
75  }
76 
77  mBaudRate = baudrate;
78  mCflag = baudrate | SERIAL_CFLAGS_COMMON;
79  mIflag = SERIAL_IFLAGS_COMMON;
80  mOflag = SERIAL_OFLAGS_COMMON;
81  mLflag = SERIAL_LFLAGS_COMMON;
82  }
83 
84 
85  void initSerial( string deviceName, int cflag, int iflag, int oflag, int lflag):
86  mAcquired(false),
87  mFd(-1)
88  {
89  mDeviceName = deviceName;
90  mCflag = cflag;
91  mIflag = iflag;
92  mOflag = oflag;
93  mLflag = lflag;
94  }
95 
96  bool acquire()
97  {
98  if(!mAcquired)
99  {
100  mFd = open(mDeviceName.c_str(), mCflag, mIflag, mOflag, mLflag);
101  if(this->valid())
102  mAcquired = true;
103  else
104  printf("couldn't acquire serial port\n");//Debug::Instance()->notice( SERIAL, ERROR, "Serial::acquire: opening serial port failed\n" );// problem here!
105  }
106  else
107  {
108  printf("called acquire when already acquired\n");//Debug::Instance()->notice( SERIAL, NOTICE, "Serial::acquire: called acquire when already acquired\n" );// problem here!
109  }
110  return mAcquired;
111  }
112  bool release()
113  {
114  if(mAcquired)
115  {
116  if(this->valid())
117  {
118  if( tcsetattr( mFd, TCSANOW, &mOldtio ) < 0) // reset the port settings
119  {
121  }
122  close( mFd );
123  }
124  else
125  {
127  }
128  mAcquired = false;
129  }
130  else
131  {
133  }
134  return !mAcquired;
135  }
136 
137  // Functions
138  bool valid(){ return mFd != -1; }
139  int read( char* buffer, short numBytes, int waitTime=0 );
140  int write( char*, unsigned short );
141  void flush();
142 
143  int open(unsigned char port,int cflag, int iflag, int oflag, int lflag);
144  int open(const char* device,int cflag, int iflag, int oflag, int lflag);
145 };
146 
147 
148 
149 #endif
150 #endif
Definition: Serial.h:35