RflySimSDK v3.08
RflySimSDK说明文档
载入中...
搜索中...
未找到
vrpn_SerialPort.h
浏览该文件的文档.
1/** @file
2 @brief Header
3
4 @date 2012
5
6 @author
7 Ryan Pavlik
8 <rpavlik@iastate.edu> and <abiryan@ryand.net>
9 http://academic.cleardefinition.com/
10 Iowa State University Virtual Reality Applications Center
11 Human-Computer Interaction Graduate Program
12*/
13
14// Copyright Iowa State University 2012.
15// Distributed under the Boost Software License, Version 1.0.
16// (See accompanying file LICENSE_1_0.txt or copy at
17// http://www.boost.org/LICENSE_1_0.txt)
18
19#pragma once
20
21// Internal Includes
22#include "vrpn_Configure.h" // for VRPN_API
23#include "vrpn_Serial.h" // for ::vrpn_SER_PARITY_NONE, etc
24
25// Library/third-party includes
26// - none
27
28// Standard includes
29#include <stdexcept> // for runtime_error, logic_error
30#include <string> // for string
31
32/// @brief A simple class wrapping the functionality of vrpn_Serial.h with
33/// RAII, object-orientation, and optional STL types
34class VRPN_API vrpn_SerialPort {
35public:
36 typedef int file_handle_type;
37 /// @brief Construct and open port
38 /// @sa vrpn_open_commport
39 /// @throws OpenFailure
40 vrpn_SerialPort(const char *portname, long baud, int charsize = 8,
41 vrpn_SER_PARITY parity = vrpn_SER_PARITY_NONE);
42
43 /// @brief Construct without opening
45
46 /// @brief Destructor - closes port if open.
48
49 /// @name Open/Close Methods
50 /// @{
51 /// @brief Open serial port
52 /// @sa vrpn_open_commport
53 /// @throws OpenFailure, AlreadyOpen
54 void open(const char *portname, long baud, int charsize = 8,
55 vrpn_SER_PARITY parity = vrpn_SER_PARITY_NONE);
56 bool is_open() const;
57
58 /// @brief Close the serial port.
59 /// @throws NotOpen, CloseFailure
60 void close();
61 /// @}
62
63 /// @name Write
64 /// @returns number of bytes written
65 /// @throws WriteFailure, NotOpen
66 /// @{
67 int write(std::string const &buffer);
68 int write(const unsigned char *buffer, int bytes);
69 /// @}
70
71 /// @name Read
72 /// @throws ReadFailure, NotOpen
73 /// @{
74 /// @brief Read available characters from input buffer, up to indicated
75 /// count.
76 int read_available_characters(unsigned char *buffer, int count);
77
78 /// @brief Read available characters from input buffer, up to indicated
79 /// count (or -1 for no limit)
80 std::string read_available_characters(int count = -1);
81
82 /// @brief Read available characters from input buffer, and wait up to the
83 /// indicated timeout for those remaining, up to indicated count.
84 int read_available_characters(unsigned char *buffer, int count,
85 struct timeval &timeout);
86
87 /// @brief Read available characters from input buffer, and wait up to the
88 /// indicated timeout for those remaining, up to indicated count.
89 std::string read_available_characters(int count, struct timeval &timeout);
90 /// @}
91
92 /// @name Buffer manipulation
93 /// @{
94
95 /// @brief Throw out any characters within the input buffer.
96 /// @throws FlushFailure, NotOpen
98
99 /// @brief Throw out any characters (do not send) within the output buffer.
100 /// @throws FlushFailure, NotOpen
102
103 /// @brief Wait until all of the characters in the output buffer are sent,
104 /// then return.
106 /// @}
107
108 /// @name RTS
109 /// @brief Set and clear functions for the RTS ("ready to send") hardware
110 /// flow- control bit.
111 ///
112 /// These are used on a port that is already open. Some devices (like the
113 /// Ascension Flock of Birds) use this to reset the device.
114 /// @throws RTSFailure, NotOpen
115 /// @{
116 void set_rts();
117 void clear_rts();
118 void assign_rts(bool set);
119 /// @}
120
121 /// @name Serial Port Exceptions
122 /// @{
123 struct AlreadyOpen;
124 struct CloseFailure;
125 struct DrainFailure;
126 struct FlushFailure;
127 struct NotOpen;
128 struct OpenFailure;
129 struct RTSFailure;
130 struct ReadFailure;
131 struct WriteFailure;
132 /// @}
133
134private:
135 void requiresOpen() const;
136 /// @name Non-copyable
137 /// @{
139 vrpn_SerialPort const &operator=(vrpn_SerialPort const &);
140 /// @}
141 file_handle_type _comm;
142 bool _rts_status;
143};
144
145struct vrpn_SerialPort::AlreadyOpen : std::logic_error {
147 : std::logic_error("Tried to open a serial port that was already open.")
148 {
149 }
150};
151
152struct vrpn_SerialPort::NotOpen : std::logic_error {
153 NotOpen()
154 : std::logic_error("Tried to use a serial port that was not yet open.")
155 {
156 }
157};
158
159struct vrpn_SerialPort::OpenFailure : std::runtime_error {
161 : std::runtime_error(
162 "Received an error when trying to open serial port.")
163 {
164 }
165};
166
167struct vrpn_SerialPort::CloseFailure : std::runtime_error {
169 : std::runtime_error(
170 "Received an error when trying to close serial port.")
171 {
172 }
173};
174
175struct vrpn_SerialPort::RTSFailure : std::runtime_error {
176 RTSFailure()
177 : std::runtime_error("Failed to modify serial port RTS status.")
178 {
179 }
180};
181
182struct vrpn_SerialPort::ReadFailure : std::runtime_error {
184 : std::runtime_error("Failure on serial port read.")
185 {
186 }
187};
188
189struct vrpn_SerialPort::WriteFailure : std::runtime_error {
191 : std::runtime_error("Failure on serial port write.")
192 {
193 }
194};
195
196struct vrpn_SerialPort::FlushFailure : std::runtime_error {
198 : std::runtime_error("Failure on serial port flush.")
199 {
200 }
201};
202
203struct vrpn_SerialPort::DrainFailure : std::runtime_error {
205 : std::runtime_error("Failure on serial port drain.")
206 {
207 }
208};
209
210inline bool vrpn_SerialPort::is_open() const { return _comm != -1; }
211
212inline void vrpn_SerialPort::assign_rts(bool set)
213{
214 if (set) {
215 set_rts();
216 }
217 else {
218 clear_rts();
219 }
220}
221
222inline void vrpn_SerialPort::requiresOpen() const
223{
224 if (!is_open()) {
225 throw NotOpen();
226 }
227}
A simple class wrapping the functionality of vrpn_Serial.h with RAII, object-orientation,...
定义 vrpn_SerialPort.h:34
int read_available_characters(unsigned char *buffer, int count, struct timeval &timeout)
Read available characters from input buffer, and wait up to the indicated timeout for those remaining...
void flush_output_buffer()
Throw out any characters (do not send) within the output buffer.
void drain_output_buffer()
Wait until all of the characters in the output buffer are sent, then return.
~vrpn_SerialPort()
Destructor - closes port if open.
void close()
Close the serial port.
void flush_input_buffer()
Throw out any characters within the input buffer.
std::string read_available_characters(int count, struct timeval &timeout)
Read available characters from input buffer, and wait up to the indicated timeout for those remaining...
std::string read_available_characters(int count=-1)
Read available characters from input buffer, up to indicated count (or -1 for no limit)
vrpn_SerialPort(const char *portname, long baud, int charsize=8, vrpn_SER_PARITY parity=vrpn_SER_PARITY_NONE)
Construct and open port
vrpn_SerialPort()
Construct without opening
定义 vrpn_SerialPort.h:145
定义 vrpn_SerialPort.h:167
定义 vrpn_SerialPort.h:203
定义 vrpn_SerialPort.h:196
定义 vrpn_SerialPort.h:152
定义 vrpn_SerialPort.h:159
定义 vrpn_SerialPort.h:175
定义 vrpn_SerialPort.h:182
定义 vrpn_SerialPort.h:189
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...