TactileVestProject  V1.0
Tactile API documentation
 All Classes Functions
Serial.h
1 #ifndef SERIAL_H
2 #define SERIAL_H
3 #define FALSE 0
4 
5 #ifdef __linux__
6  #define UNIX
7 #elif __APPLE__
8  #define UNIX
9 #endif
10 
11 #ifdef _WIN32
12  #define WINDOWS
13 #elif _WIN64
14  #define WINDOWS
15 #endif
16 
17 #ifdef UNIX
18 #include <termios.h>
19 #endif
20 
21 #ifdef WINDOWS
22 #include <windows.h>
23 #define FC_DTRDSR 0x01
24 #define FC_RTSCTS 0x02
25 #define FC_XONXOFF 0x04
26 #define ASCII_BEL 0x07
27 #define ASCII_BS 0x08
28 #define ASCII_LF 0x0A
29 #define ASCII_CR 0x0D
30 #define ASCII_XON 0x11
31 #define ASCII_XOFF 0x13
32 #endif
33 #include<stdio.h>
34 #include "HardwareInterface.h"
35 class Serial : public HardwareInterface
36 {
37  public:
38  Serial();
39  virtual ~Serial();
40  Serial(const Serial& other);
41  Serial& operator=(const Serial& other);
45  virtual bool Open(char *);
49  virtual bool Close( void );
50 
54  virtual int SendData(char *);
55 
56 
57  bool IsOpened( void ) { return ( isOpened ); }
58  protected:
59  bool isOpened;
60  #ifdef WINDOWS
61  HANDLE deviceHandle;
62  TCHAR* comPort;
63  OVERLAPPED m_OverlappedRead, m_OverlappedWrite;
64  #endif
65  private:
66  #ifdef UNIX
67  int mBaudRate;
68  string mDeviceName;
69  int mCflag;
70  int mIflag;
71  int mOflag;
72  int mLflag;
73 
74  bool mAcquired;
75 
76  int mFd;
77 
78  struct termios mOldtio;
79 
80 
81  void initSerial(string deviceName, int baudrate)
82  {
83  mDeviceName = deviceName;
84 
85  switch( baudrate )
86  {
87  case 9600:
88  baudrate = B9600;
89  break;
90 
91  case 19200:
92  baudrate = B19200;
93  break;
94 
95  case 38400:
96  baudrate = B38400;
97  break;
98 
99  case 57600:
100  baudrate = B57600;
101  break;
102 
103  case 115200:
104  baudrate = B115200;
105  break;
106 
107  //case 125000:
108  //baudrate = B125000;
109  //break;
110 
111  default:
112  printf( "invalid baud rate for serial port %s\n", deviceName.c_str() );
113  baudrate = B9600;
114  break;
115  }
116 
117  mBaudRate = baudrate;
118  mCflag = baudrate | SERIAL_CFLAGS_COMMON;
119  mIflag = SERIAL_IFLAGS_COMMON;
120  mOflag = SERIAL_OFLAGS_COMMON;
121  mLflag = SERIAL_LFLAGS_COMMON;
122  }
123 
124 
125  void initSerial( string deviceName, int cflag, int iflag, int oflag, int lflag):
126  mAcquired(false),
127  mFd(-1)
128  {
129  mDeviceName = deviceName;
130  mCflag = cflag;
131  mIflag = iflag;
132  mOflag = oflag;
133  mLflag = lflag;
134  }
135 
136  bool acquire()
137  {
138  if(!mAcquired)
139  {
140  mFd = open(mDeviceName.c_str(), mCflag, mIflag, mOflag, mLflag);
141  if(this->valid())
142  mAcquired = true;
143  else
144  printf("couldn't acquire serial port\n");//Debug::Instance()->notice( SERIAL, ERROR, "Serial::acquire: opening serial port failed\n" );// problem here!
145  }
146  else
147  {
148  printf("called acquire when already acquired\n");//Debug::Instance()->notice( SERIAL, NOTICE, "Serial::acquire: called acquire when already acquired\n" );// problem here!
149  }
150  return mAcquired;
151  }
152  bool release()
153  {
154  if(mAcquired)
155  {
156  if(this->valid())
157  {
158  if( tcsetattr( mFd, TCSANOW, &mOldtio ) < 0) // reset the port settings
159  {
161  }
162  close( mFd );
163  }
164  else
165  {
167  }
168  mAcquired = false;
169  }
170  else
171  {
173  }
174  return !mAcquired;
175  }
176 
177  // Functions
178  bool valid(){ return mFd != -1; }
179  int read( char* buffer, short numBytes, int waitTime=0 );
180  int write( char*, unsigned short );
181  void flush();
182 
183  int open(unsigned char port,int cflag, int iflag, int oflag, int lflag);
184  int open(const char* device,int cflag, int iflag, int oflag, int lflag);
185  #endif
186 };
187 
188 #endif // SERIAL_H
189 
An interface to a hardware device, such as a serial connection.
Definition: HardwareInterface.h:5
virtual bool Close(void)
virtual int SendData(char *)
virtual bool Open(char *)
Definition: Serial.h:35