RflySimSDK v3.05
RflySimSDK说明文档
载入中...
搜索中...
未找到
vrpn_SerialPort.h
浏览该文件的文档.
1
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
34class VRPN_API vrpn_SerialPort {
35public:
36 typedef int file_handle_type;
40 vrpn_SerialPort(const char *portname, long baud, int charsize = 8,
41 vrpn_SER_PARITY parity = vrpn_SER_PARITY_NONE);
42
45
48
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
60 void close();
62
67 int write(std::string const &buffer);
68 int write(const unsigned char *buffer, int bytes);
70
76 int read_available_characters(unsigned char *buffer, int count);
77
80 std::string read_available_characters(int count = -1);
81
84 int read_available_characters(unsigned char *buffer, int count,
85 struct timeval &timeout);
86
89 std::string read_available_characters(int count, struct timeval &timeout);
91
94
98
102
107
116 void set_rts();
117 void clear_rts();
118 void assign_rts(bool set);
120
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;
133
134private:
135 void requiresOpen() const;
139 vrpn_SerialPort const &operator=(vrpn_SerialPort const &);
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...