cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Anton Deguet
6 Created on: 2004-12-06
7
8 (C) Copyright 2004-2023 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19
24
25#ifndef _osaSerialPort_h
26#define _osaSerialPort_h
27
28
32
33#if (CISST_OS == CISST_WINDOWS)
34#include <windows.h>
35#else
36#include <termios.h>
37#endif
38
39#include <sstream>
40
41
49{
55
56public:
59#if (CISST_OS == CISST_WINDOWS)
60 // Windows defines standard baud rates in WinBase.h, which does not include
61 // CBR_230400 or CBR_460800.
62 #ifndef CBR_230400
63 #define CBR_230400 230400
64 #endif
65 #ifndef CBR_460800
66 #define CBR_460800 460800
67 #endif
68 enum BaudRateType {BaudRate300 = CBR_300,
69 BaudRate1200 = CBR_1200,
70 BaudRate9600 = CBR_9600,
71 BaudRate19200 = CBR_19200,
72 BaudRate38400 = CBR_38400,
73 BaudRate57600 = CBR_57600,
74 BaudRate115200 = CBR_115200,
77#else
78 // MacOS doesn't define some of the baud rates
79 #ifndef B460800
80 #define B460800 460800
81 #endif
82 enum BaudRateType {BaudRate300 = B300,
83 BaudRate1200 = B1200,
84 BaudRate9600 = B9600,
85 BaudRate19200 = B19200,
86 BaudRate38400 = B38400,
87 BaudRate57600 = B57600,
88 BaudRate115200 = B115200,
89 BaudRate230400 = B230400,
90 BaudRate460800 = B460800};
91#endif
92
93
96#if (CISST_OS == CISST_WINDOWS)
101#else
102 enum CharacterSizeType {CharacterSize5 = CS5,
103 CharacterSize6 = CS6,
104 CharacterSize7 = CS7,
105 CharacterSize8 = CS8};
106#endif
107
108
111#if (CISST_OS == CISST_WINDOWS)
113 ParityCheckingEven = EVENPARITY,
114 ParityCheckingOdd = ODDPARITY};
115#else
116 enum ParityCheckingType {ParityCheckingNone,
117 ParityCheckingEven,
118 ParityCheckingOdd};
119#endif
120
121
127
132
133
139 PortName("Undefined"),
140 IsOpenedFlag(false),
141 BaudRate(BaudRate9600),
142 CharacterSize(CharacterSize8),
143 ParityChecking(ParityCheckingNone),
144 StopBits(StopBitsOne),
145 FlowControl(FlowControlNone)
146 {
147#if (CISST_OS == CISST_WINDOWS)
148 memset(&OverlappedStructureRead, 0, sizeof(OverlappedStructureRead));
149 memset(&OverlappedStructureWrite, 0, sizeof(OverlappedStructureWrite));
150#else // Unix
151 FileDescriptor = -1;
152#endif
153 }
154
155
157 virtual ~osaSerialPort(void);
158
160 inline void SetBaudRate(const BaudRateType & baudRate) {
161 this->BaudRate = baudRate;
162 CMN_LOG_CLASS_INIT_VERBOSE << "Baud rate modified to " << this->BaudRate
163 << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
164 }
165
167 inline void SetCharacterSize(const CharacterSizeType & characterSize) {
168 this->CharacterSize = characterSize;
169 CMN_LOG_CLASS_INIT_VERBOSE << "Character size modified to " << this->CharacterSize
170 << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
171 }
172
174 inline void SetParityChecking(const ParityCheckingType & parityChecking) {
175 this->ParityChecking = parityChecking;
176 CMN_LOG_CLASS_INIT_VERBOSE << "Parity checking modified to " << this->ParityChecking
177 << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
178 }
179
181 inline void SetStopBits(const StopBitsType & stopBits) {
182 this->StopBits = stopBits;
183 CMN_LOG_CLASS_INIT_VERBOSE << "Stop bits modified to " << this->StopBits
184 << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
185 }
186
188 inline void SetFlowControl(const FlowControlType & flowControl) {
189 this->FlowControl = flowControl;
190 CMN_LOG_CLASS_INIT_VERBOSE << "Flow control modified to " << this->FlowControl
191 << " on port " << this->PortName << " (not effective until Configure)" << std::endl;
192 }
193
195 inline void SetPortName(const std::string & portName) {
196 this->PortName = portName;
197 }
198
200 inline std::string GetPortName(void) const {
201 return this->PortName;
202 }
203
205 std::string SetPortNumber(unsigned int portNumber);
206
211 bool Open(bool blocking = false);
212
213 bool Configure(void);
214
215 bool Close(void);
216
218 inline bool IsOpened(void) const {
219 return this->IsOpenedFlag;
220 }
221
223 // PK: why overload for char and uchar?
224 int Write(const char * data, int nBytes);
225 int Write(const unsigned char * data, int nBytes);
226 inline int Write(const std::string & data) {
227 return this->Write(data.c_str(), static_cast<int>(data.size()));
228 }
229
231 // PK: why overload for char and uchar?
232 int Read(char * data, int nBytes);
233 int Read(unsigned char * data, int nBytes);
234
249 bool WriteBreak(double breakLengthInSeconds);
250
252 bool Flush(void);
253
254private:
255 // full port name
256 std::string PortName;
257 bool IsOpenedFlag;
258
259 // parameters
260 BaudRateType BaudRate;
261 CharacterSizeType CharacterSize;
262 ParityCheckingType ParityChecking;
263 StopBitsType StopBits;
264 FlowControlType FlowControl;
265
266#if (CISST_OS == CISST_WINDOWS)
267 HANDLE PortHandle;
268 OVERLAPPED OverlappedStructureRead, OverlappedStructureWrite;
269 COMMTIMEOUTS TimeOuts;
270 bool isBlocking;
271#else // Unix
272 int FileDescriptor;
273#endif
274};
275
276
278
279
280#endif // _osaSerialPort_h
Base class for high level objects.
Definition cmnGenericObject.h:53
Serial port.
Definition osaSerialPort.h:49
FlowControlType
Definition osaSerialPort.h:124
@ FlowControlSoftware
Definition osaSerialPort.h:125
@ FlowControlNone
Definition osaSerialPort.h:124
@ FlowControlHardware
Definition osaSerialPort.h:126
void SetParityChecking(const ParityCheckingType &parityChecking)
Definition osaSerialPort.h:174
bool Close(void)
bool IsOpened(void) const
Definition osaSerialPort.h:218
void SetBaudRate(const BaudRateType &baudRate)
Definition osaSerialPort.h:160
BaudRateType
Definition osaSerialPort.h:68
@ BaudRate300
Definition osaSerialPort.h:68
@ BaudRate19200
Definition osaSerialPort.h:71
@ BaudRate230400
Definition osaSerialPort.h:75
@ BaudRate1200
Definition osaSerialPort.h:69
@ BaudRate115200
Definition osaSerialPort.h:74
@ BaudRate460800
Definition osaSerialPort.h:76
@ BaudRate57600
Definition osaSerialPort.h:73
@ BaudRate38400
Definition osaSerialPort.h:72
@ BaudRate9600
Definition osaSerialPort.h:70
virtual ~osaSerialPort(void)
ParityCheckingType
Definition osaSerialPort.h:112
@ ParityCheckingOdd
Definition osaSerialPort.h:114
@ ParityCheckingEven
Definition osaSerialPort.h:113
@ ParityCheckingNone
Definition osaSerialPort.h:112
int Write(const char *data, int nBytes)
CharacterSizeType
Definition osaSerialPort.h:97
@ CharacterSize6
Definition osaSerialPort.h:98
@ CharacterSize7
Definition osaSerialPort.h:99
@ CharacterSize5
Definition osaSerialPort.h:97
@ CharacterSize8
Definition osaSerialPort.h:100
StopBitsType
Definition osaSerialPort.h:130
@ StopBitsTwo
Definition osaSerialPort.h:131
@ StopBitsOne
Definition osaSerialPort.h:130
int Write(const unsigned char *data, int nBytes)
void SetPortName(const std::string &portName)
Definition osaSerialPort.h:195
int Read(char *data, int nBytes)
int Write(const std::string &data)
Definition osaSerialPort.h:226
bool Configure(void)
void SetCharacterSize(const CharacterSizeType &characterSize)
Definition osaSerialPort.h:167
bool WriteBreak(double breakLengthInSeconds)
std::string GetPortName(void) const
Definition osaSerialPort.h:200
bool Open(bool blocking=false)
void SetStopBits(const StopBitsType &stopBits)
Definition osaSerialPort.h:181
bool Flush(void)
osaSerialPort(void)
Definition osaSerialPort.h:138
int Read(unsigned char *data, int nBytes)
void SetFlowControl(const FlowControlType &flowControl)
Definition osaSerialPort.h:188
std::string SetPortNumber(unsigned int portNumber)
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition cmnClassRegisterMacros.h:199
const int CMN_NO_DYNAMIC_CREATION
Definition cmnClassRegisterMacros.h:325
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Defines cmnGenericObject.
#define CMN_LOG_ALLOW_DEFAULT
Definition cmnLogLoD.h:76
#define CMN_LOG_CLASS_INIT_VERBOSE
Definition cmnLogger.h:115
Portability across compilers and operating systems tools.
Macros to export the symbols of cisstOSAbstraction (in a Dll).
#define CBR_230400
Definition osaSerialPort.h:63
#define CBR_460800
Definition osaSerialPort.h:66