/
vrpn_SerialPort.C
204 lines (179 loc) · 4.75 KB
/
vrpn_SerialPort.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
@file
@brief Implementation
@date 2012
@author
Ryan Pavlik
<rpavlik@iastate.edu> and <abiryan@ryand.net>
http://academic.cleardefinition.com/
Iowa State University Virtual Reality Applications Center
Human-Computer Interaction Graduate Program
*/
// Copyright Iowa State University 2012.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Internal Includes
#include "vrpn_SerialPort.h"
#include "vrpn_Serial.h"
// Library/third-party includes
// - none
// Standard includes
#include <stdio.h>
#include <vector>
#include <exception>
#include <algorithm>
#include <limits>
typedef std::vector<unsigned char> DynamicBufferType;
vrpn_SerialPort::vrpn_SerialPort(const char *portname, long baud, int charsize,
vrpn_SER_PARITY parity)
: _comm(vrpn_open_commport(portname, baud, charsize, parity))
, _rts_status(false)
{
if (!is_open()) {
throw OpenFailure();
}
}
vrpn_SerialPort::vrpn_SerialPort()
: _comm(-1)
, _rts_status(false)
{
}
vrpn_SerialPort::~vrpn_SerialPort()
{
if (is_open()) {
try {
close();
}
catch (std::exception &) {
fprintf(stderr, "Error when closing serial port in destructor.\n");
}
}
}
void vrpn_SerialPort::open(const char *portname, long baud, int charsize,
vrpn_SER_PARITY parity)
{
if (is_open()) {
throw AlreadyOpen();
}
_comm = vrpn_open_commport(portname, baud, charsize, parity);
if (!is_open()) {
throw OpenFailure();
}
}
void vrpn_SerialPort::close()
{
requiresOpen();
int ret = vrpn_close_commport(_comm);
if (ret != 0) {
throw CloseFailure();
}
}
int vrpn_SerialPort::write(std::string const &buffer)
{
if (buffer.size() > 0) {
DynamicBufferType buf(buffer.c_str(), buffer.c_str() + buffer.size());
return write(&(buf[0]), static_cast<int>(buffer.size()));
}
return 0;
}
int vrpn_SerialPort::write(const unsigned char *buffer, int bytes)
{
requiresOpen();
int ret = vrpn_write_characters(_comm, buffer, bytes);
if (ret == -1) {
throw WriteFailure();
}
return ret;
}
int vrpn_SerialPort::read_available_characters(unsigned char *buffer, int count)
{
requiresOpen();
int ret = vrpn_read_available_characters(_comm, buffer, count);
if (ret == -1) {
throw ReadFailure();
}
return ret;
}
std::string vrpn_SerialPort::read_available_characters(int count)
{
std::string retString;
unsigned int numRead = 0;
unsigned int thisRead = 0;
static const unsigned int BUFSIZE = 256;
unsigned char buf[BUFSIZE];
unsigned int needed = BUFSIZE - 1;
do {
if (count > -1) {
needed = (std::min)(count - numRead, BUFSIZE - 1);
}
thisRead = read_available_characters(buf, needed);
if (thisRead != 0) {
retString.append(&(buf[0]), &(buf[0]) + thisRead);
numRead += thisRead;
}
} while (thisRead != 0 &&
(static_cast<int>(numRead) < count || count == -1));
return retString;
}
int vrpn_SerialPort::read_available_characters(unsigned char *buffer, int count,
struct timeval &timeout)
{
requiresOpen();
int ret = vrpn_read_available_characters(_comm, buffer, count, &timeout);
if (ret == -1) {
throw ReadFailure();
}
return ret;
}
std::string vrpn_SerialPort::read_available_characters(int count,
struct timeval &timeout)
{
if (count == std::numeric_limits<int>::max()) {
/// overflow safety
throw ReadFailure();
}
DynamicBufferType buf(count + 1);
int ret = read_available_characters(&(buf[0]), count, timeout);
return std::string(&(buf[0]), &(buf[0]) + ret);
}
void vrpn_SerialPort::flush_input_buffer()
{
requiresOpen();
int ret = vrpn_flush_input_buffer(_comm);
if (ret == -1) {
throw FlushFailure();
}
}
void vrpn_SerialPort::flush_output_buffer()
{
requiresOpen();
int ret = vrpn_flush_output_buffer(_comm);
if (ret == -1) {
throw FlushFailure();
}
}
void vrpn_SerialPort::drain_output_buffer()
{
requiresOpen();
int ret = vrpn_drain_output_buffer(_comm);
if (ret == -1) {
throw DrainFailure();
}
}
void vrpn_SerialPort::set_rts()
{
requiresOpen();
int ret = vrpn_set_rts(_comm);
if (ret == -1) {
throw RTSFailure();
}
}
void vrpn_SerialPort::clear_rts()
{
requiresOpen();
int ret = vrpn_clear_rts(_comm);
if (ret == -1) {
throw RTSFailure();
}
}