vrpn  07.33
Virtual Reality Peripheral Network
vrpn_SerialPort.h
Go to the documentation of this file.
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 
35 public:
36  typedef int file_handle_type;
40  vrpn_SerialPort(const char *portname, long baud, int charsize = 8,
42 
45 
47  ~vrpn_SerialPort();
48 
54  void open(const char *portname, long baud, int charsize = 8,
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 
97  void flush_input_buffer();
98 
101  void flush_output_buffer();
102 
105  void drain_output_buffer();
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 
134 private:
135  void requiresOpen() const;
139  vrpn_SerialPort const &operator=(vrpn_SerialPort const &);
141  file_handle_type _comm;
142  bool _rts_status;
143 };
144 
145 struct vrpn_SerialPort::AlreadyOpen : std::logic_error {
147  : std::logic_error("Tried to open a serial port that was already open.")
148  {
149  }
150 };
151 
152 struct vrpn_SerialPort::NotOpen : std::logic_error {
154  : std::logic_error("Tried to use a serial port that was not yet open.")
155  {
156  }
157 };
158 
159 struct vrpn_SerialPort::OpenFailure : std::runtime_error {
161  : std::runtime_error(
162  "Received an error when trying to open serial port.")
163  {
164  }
165 };
166 
167 struct vrpn_SerialPort::CloseFailure : std::runtime_error {
169  : std::runtime_error(
170  "Received an error when trying to close serial port.")
171  {
172  }
173 };
174 
175 struct vrpn_SerialPort::RTSFailure : std::runtime_error {
177  : std::runtime_error("Failed to modify serial port RTS status.")
178  {
179  }
180 };
181 
182 struct vrpn_SerialPort::ReadFailure : std::runtime_error {
184  : std::runtime_error("Failure on serial port read.")
185  {
186  }
187 };
188 
189 struct vrpn_SerialPort::WriteFailure : std::runtime_error {
191  : std::runtime_error("Failure on serial port write.")
192  {
193  }
194 };
195 
196 struct vrpn_SerialPort::FlushFailure : std::runtime_error {
198  : std::runtime_error("Failure on serial port flush.")
199  {
200  }
201 };
202 
203 struct vrpn_SerialPort::DrainFailure : std::runtime_error {
205  : std::runtime_error("Failure on serial port drain.")
206  {
207  }
208 };
209 
210 inline bool vrpn_SerialPort::is_open() const { return _comm != -1; }
211 
212 inline void vrpn_SerialPort::assign_rts(bool set)
213 {
214  if (set) {
215  set_rts();
216  }
217  else {
218  clear_rts();
219  }
220 }
221 
222 inline void vrpn_SerialPort::requiresOpen() const
223 {
224  if (!is_open()) {
225  throw NotOpen();
226  }
227 }
bool is_open() const
vrpn_SER_PARITY
Definition: vrpn_Serial.h:15
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
void assign_rts(bool set)
#define VRPN_API
A simple class wrapping the functionality of vrpn_Serial.h with RAII, object-orientation,...