vrpn  07.33
Virtual Reality Peripheral Network
vrpn_SerialPort.C
Go to the documentation of this file.
1 
15 // Copyright Iowa State University 2012.
16 // Distributed under the Boost Software License, Version 1.0.
17 // (See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19 
20 // Internal Includes
21 #include "vrpn_SerialPort.h"
22 #include "vrpn_Serial.h"
23 
24 // Library/third-party includes
25 // - none
26 
27 // Standard includes
28 #include <stdio.h>
29 #include <vector>
30 #include <exception>
31 #include <algorithm>
32 #include <limits>
33 
34 typedef std::vector<unsigned char> DynamicBufferType;
35 
36 vrpn_SerialPort::vrpn_SerialPort(const char *portname, long baud, int charsize,
37  vrpn_SER_PARITY parity)
38  : _comm(vrpn_open_commport(portname, baud, charsize, parity))
39  , _rts_status(false)
40 {
41  if (!is_open()) {
42  throw OpenFailure();
43  }
44 }
45 
47  : _comm(-1)
48  , _rts_status(false)
49 {
50 }
51 
53 {
54  if (is_open()) {
55  try {
56  close();
57  }
58  catch (std::exception &) {
59  fprintf(stderr, "Error when closing serial port in destructor.\n");
60  }
61  }
62 }
63 
64 void vrpn_SerialPort::open(const char *portname, long baud, int charsize,
65  vrpn_SER_PARITY parity)
66 {
67  if (is_open()) {
68  throw AlreadyOpen();
69  }
70  _comm = vrpn_open_commport(portname, baud, charsize, parity);
71  if (!is_open()) {
72 
73  throw OpenFailure();
74  }
75 }
76 
78 {
79  requiresOpen();
80  int ret = vrpn_close_commport(_comm);
81  if (ret != 0) {
82  throw CloseFailure();
83  }
84 }
85 
86 int vrpn_SerialPort::write(std::string const &buffer)
87 {
88  if (buffer.size() > 0) {
89  DynamicBufferType buf(buffer.c_str(), buffer.c_str() + buffer.size());
90  return write(&(buf[0]), static_cast<int>(buffer.size()));
91  }
92  return 0;
93 }
94 
95 int vrpn_SerialPort::write(const unsigned char *buffer, int bytes)
96 {
97  requiresOpen();
98  int ret = vrpn_write_characters(_comm, buffer, bytes);
99  if (ret == -1) {
100  throw WriteFailure();
101  }
102  return ret;
103 }
104 
105 int vrpn_SerialPort::read_available_characters(unsigned char *buffer, int count)
106 {
107  requiresOpen();
108  int ret = vrpn_read_available_characters(_comm, buffer, count);
109  if (ret == -1) {
110  throw ReadFailure();
111  }
112 
113  return ret;
114 }
115 
117 {
118  std::string retString;
119  unsigned int numRead = 0;
120  unsigned int thisRead = 0;
121  static const unsigned int BUFSIZE = 256;
122  unsigned char buf[BUFSIZE];
123  unsigned int needed = BUFSIZE - 1;
124  do {
125  if (count > -1) {
126  needed = (std::min)(count - numRead, BUFSIZE - 1);
127  }
128  thisRead = read_available_characters(buf, needed);
129  if (thisRead != 0) {
130  retString.append(&(buf[0]), &(buf[0]) + thisRead);
131  numRead += thisRead;
132  }
133  } while (thisRead != 0 &&
134  (static_cast<int>(numRead) < count || count == -1));
135  return retString;
136 }
137 
138 int vrpn_SerialPort::read_available_characters(unsigned char *buffer, int count,
139  struct timeval &timeout)
140 {
141  requiresOpen();
142  int ret = vrpn_read_available_characters(_comm, buffer, count, &timeout);
143  if (ret == -1) {
144  throw ReadFailure();
145  }
146  return ret;
147 }
148 
150  struct timeval &timeout)
151 {
152  if (count == std::numeric_limits<int>::max()) {
154  throw ReadFailure();
155  }
156  DynamicBufferType buf(count + 1);
157  int ret = read_available_characters(&(buf[0]), count, timeout);
158  return std::string(&(buf[0]), &(buf[0]) + ret);
159 }
160 
162 {
163  requiresOpen();
164  int ret = vrpn_flush_input_buffer(_comm);
165  if (ret == -1) {
166  throw FlushFailure();
167  }
168 }
169 
171 {
172  requiresOpen();
173  int ret = vrpn_flush_output_buffer(_comm);
174  if (ret == -1) {
175  throw FlushFailure();
176  }
177 }
178 
180 {
181  requiresOpen();
182  int ret = vrpn_drain_output_buffer(_comm);
183  if (ret == -1) {
184  throw DrainFailure();
185  }
186 }
187 
189 {
190  requiresOpen();
191  int ret = vrpn_set_rts(_comm);
192  if (ret == -1) {
193  throw RTSFailure();
194  }
195 }
196 
198 {
199  requiresOpen();
200  int ret = vrpn_clear_rts(_comm);
201  if (ret == -1) {
202  throw RTSFailure();
203  }
204 }
#define min(x, y)
Definition: vrpn_WiiMote.C:47
int vrpn_write_characters(int comm, const unsigned char *buffer, size_t bytes)
Write the buffer to the serial port.
Definition: vrpn_Serial.C:643
int vrpn_close_commport(int comm)
Definition: vrpn_Serial.C:345
int vrpn_open_commport(const char *portname, long baud, int charsize, vrpn_SER_PARITY parity, bool rts_flow)
Open a serial port, given its name and baud rate.
Definition: vrpn_Serial.C:54
int write(std::string const &buffer)
bool is_open() const
vrpn_SER_PARITY
Definition: vrpn_Serial.h:15
int vrpn_flush_input_buffer(int comm)
Throw out any characters within the input buffer.
Definition: vrpn_Serial.C:435
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
void flush_input_buffer()
Throw out any characters within the input buffer.
void drain_output_buffer()
Wait until all of the characters in the output buffer are sent, then return.
vrpn_SerialPort()
Construct without opening.
void flush_output_buffer()
Throw out any characters (do not send) within the output buffer.
int vrpn_drain_output_buffer(int comm)
Wait until all of the characters in the output buffer are sent, then return.
Definition: vrpn_Serial.C:485
int vrpn_set_rts(int comm)
Definition: vrpn_Serial.C:365
int vrpn_read_available_characters(int comm, unsigned char *buffer, size_t bytes)
Definition: vrpn_Serial.C:512
int vrpn_clear_rts(int comm)
Definition: vrpn_Serial.C:399
int vrpn_flush_output_buffer(int comm)
Throw out any characters (do not send) within the output buffer.
Definition: vrpn_Serial.C:462
~vrpn_SerialPort()
Destructor - closes port if open.
int read_available_characters(unsigned char *buffer, int count)
void open(const char *portname, long baud, int charsize=8, vrpn_SER_PARITY parity=vrpn_SER_PARITY_NONE)
std::vector< unsigned char > DynamicBufferType
void close()
Close the serial port.