cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
osaSerialPort.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2004-12-06
8 
9  (C) Copyright 2004-2009 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 
27 #ifndef _osaSerialPort_h
28 #define _osaSerialPort_h
29 
30 
34 
35 #if (CISST_OS == CISST_WINDOWS)
36 #include <windows.h>
37 #else
38 #include <termios.h>
39 #endif
40 
41 #include <sstream>
42 
43 
51 {
57 
58 public:
61 #if (CISST_OS == CISST_WINDOWS)
62  enum BaudRateType {BaudRate300 = CBR_300,
63  BaudRate1200 = CBR_1200,
64  BaudRate9600 = CBR_9600,
65  BaudRate19200 = CBR_19200,
66  BaudRate38400 = CBR_38400,
67  BaudRate57600 = CBR_57600,
68  BaudRate115200 = CBR_115200};
69 #else
70  enum BaudRateType {BaudRate300 = B300,
71  BaudRate1200 = B1200,
72  BaudRate9600 = B9600,
73  BaudRate19200 = B19200,
74  BaudRate38400 = B38400,
75  BaudRate57600 = B57600,
76  BaudRate115200 = B115200};
77 #endif
78 
79 
82 #if (CISST_OS == CISST_WINDOWS)
83  enum CharacterSizeType {CharacterSize5 = 5,
84  CharacterSize6 = 6,
85  CharacterSize7 = 7,
86  CharacterSize8 = 8};
87 #else
88  enum CharacterSizeType {CharacterSize5 = CS5,
89  CharacterSize6 = CS6,
90  CharacterSize7 = CS7,
91  CharacterSize8 = CS8};
92 #endif
93 
94 
97 #if (CISST_OS == CISST_WINDOWS)
98  enum ParityCheckingType {ParityCheckingNone = NOPARITY,
99  ParityCheckingEven = EVENPARITY,
100  ParityCheckingOdd = ODDPARITY};
101 #else
102  enum ParityCheckingType {ParityCheckingNone,
103  ParityCheckingEven,
104  ParityCheckingOdd};
105 #endif
106 
107 
110  enum FlowControlType {FlowControlNone,
112  FlowControlHardware};
113 
116  enum StopBitsType {StopBitsOne,
117  StopBitsTwo};
118 
119 
125  PortName("Undefined"),
126  IsOpenedFlag(false),
127  BaudRate(BaudRate9600),
128  CharacterSize(CharacterSize8),
129  ParityChecking(ParityCheckingNone),
130  StopBits(StopBitsOne),
131  FlowControl(FlowControlNone)
132  {
133 #if (CISST_OS == CISST_WINDOWS)
134  memset(&OverlappedStructureRead, 0, sizeof(OverlappedStructureRead));
135  memset(&OverlappedStructureWrite, 0, sizeof(OverlappedStructureWrite));
136 #else // Unix
137  FileDescriptor = -1;
138 #endif
139  }
140 
141 
143  virtual ~osaSerialPort(void);
144 
146  inline void SetBaudRate(const BaudRateType & baudRate) {
147  this->BaudRate = baudRate;
148  CMN_LOG_CLASS_INIT_VERBOSE << "Baud rate modified to " << this->BaudRate
149  << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
150  }
151 
153  inline void SetCharacterSize(const CharacterSizeType & characterSize) {
154  this->CharacterSize = characterSize;
155  CMN_LOG_CLASS_INIT_VERBOSE << "Character size modified to " << this->CharacterSize
156  << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
157  }
158 
160  inline void SetParityChecking(const ParityCheckingType & parityChecking) {
161  this->ParityChecking = parityChecking;
162  CMN_LOG_CLASS_INIT_VERBOSE << "Parity checking modified to " << this->ParityChecking
163  << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
164  }
165 
167  inline void SetStopBits(const StopBitsType & stopBits) {
168  this->StopBits = stopBits;
169  CMN_LOG_CLASS_INIT_VERBOSE << "Stop bits modified to " << this->StopBits
170  << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
171  }
172 
174  inline void SetFlowControl(const FlowControlType & flowControl) {
175  this->FlowControl = flowControl;
176  CMN_LOG_CLASS_INIT_VERBOSE << "Flow control modified to " << this->FlowControl
177  << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
178  }
179 
181  inline void SetPortName(const std::string & portName) {
182  this->PortName = portName;
183  }
184 
186  inline std::string GetPortName(void) const {
187  return this->PortName;
188  }
189 
191  std::string SetPortNumber(unsigned int portNumber);
192 
197  bool Open(bool blocking = false);
198 
199  bool Configure(void);
200 
201  bool Close(void);
202 
204  inline bool IsOpened(void) const {
205  return this->IsOpenedFlag;
206  }
207 
209  // PK: why overload for char and uchar?
210  int Write(const char * data, int nBytes);
211  int Write(const unsigned char * data, int nBytes);
212  inline int Write(const std::string & data) {
213  return this->Write(data.c_str(), static_cast<int>(data.size()));
214  }
215 
217  // PK: why overload for char and uchar?
218  int Read(char * data, int nBytes);
219  int Read(unsigned char * data, int nBytes);
220 
235  bool WriteBreak(double breakLengthInSeconds);
236 
238  bool Flush(void);
239 
240 private:
241  // full port name
242  std::string PortName;
243  bool IsOpenedFlag;
244 
245  // parameters
246  BaudRateType BaudRate;
247  CharacterSizeType CharacterSize;
248  ParityCheckingType ParityChecking;
249  StopBitsType StopBits;
250  FlowControlType FlowControl;
251 
252 #if (CISST_OS == CISST_WINDOWS)
253  HANDLE PortHandle;
254  OVERLAPPED OverlappedStructureRead, OverlappedStructureWrite;
255  COMMTIMEOUTS TimeOuts;
256  bool isBlocking;
257 #else // Unix
258  int FileDescriptor;
259 #endif
260 };
261 
262 
264 
265 
266 #endif // _osaSerialPort_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
void SetStopBits(const StopBitsType &stopBits)
Definition: osaSerialPort.h:167
bool IsOpened(void) const
Definition: osaSerialPort.h:204
BaudRateType
Definition: osaSerialPort.h:62
Portability across compilers and operating systems tools.
ParityCheckingType
Definition: osaSerialPort.h:98
void SetCharacterSize(const CharacterSizeType &characterSize)
Definition: osaSerialPort.h:153
Base class for high level objects.
Definition: cmnGenericObject.h:51
std::string GetPortName(void) const
Definition: osaSerialPort.h:186
void SetBaudRate(const BaudRateType &baudRate)
Definition: osaSerialPort.h:146
void SetPortName(const std::string &portName)
Definition: osaSerialPort.h:181
FlowControlType
Definition: osaSerialPort.h:110
CharacterSizeType
Definition: osaSerialPort.h:83
osaSerialPort(void)
Definition: osaSerialPort.h:124
CMN_DECLARE_SERVICES_INSTANTIATION(osaSerialPort)
int Write(const std::string &data)
Definition: osaSerialPort.h:212
Defines cmnGenericObject.
Macros to export the symbols of cisstOSAbstraction (in a Dll).
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
StopBitsType
Definition: osaSerialPort.h:116
Definition: osaSerialPort.h:111
void SetFlowControl(const FlowControlType &flowControl)
Definition: osaSerialPort.h:174
#define CMN_LOG_CLASS_INIT_VERBOSE
Definition: cmnLogger.h:115
void SetParityChecking(const ParityCheckingType &parityChecking)
Definition: osaSerialPort.h:160
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
Serial port.
Definition: osaSerialPort.h:50
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76