vrpn
07.35
Virtual Reality Peripheral Network
Toggle main menu visibility
Loading...
Searching...
No Matches
vrpn_SerialPort.C
Go to the documentation of this file.
1
14
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
46
vrpn_SerialPort::vrpn_SerialPort
()
47
: _comm(-1)
48
, _rts_status(false)
49
{
50
}
51
52
vrpn_SerialPort::~vrpn_SerialPort
()
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
77
void
vrpn_SerialPort::close
()
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
116
std::string
vrpn_SerialPort::read_available_characters
(
int
count)
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
149
std::string
vrpn_SerialPort::read_available_characters
(
int
count,
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
161
void
vrpn_SerialPort::flush_input_buffer
()
162
{
163
requiresOpen();
164
int
ret =
vrpn_flush_input_buffer
(_comm);
165
if
(ret == -1) {
166
throw
FlushFailure
();
167
}
168
}
169
170
void
vrpn_SerialPort::flush_output_buffer
()
171
{
172
requiresOpen();
173
int
ret =
vrpn_flush_output_buffer
(_comm);
174
if
(ret == -1) {
175
throw
FlushFailure
();
176
}
177
}
178
179
void
vrpn_SerialPort::drain_output_buffer
()
180
{
181
requiresOpen();
182
int
ret =
vrpn_drain_output_buffer
(_comm);
183
if
(ret == -1) {
184
throw
DrainFailure
();
185
}
186
}
187
188
void
vrpn_SerialPort::set_rts
()
189
{
190
requiresOpen();
191
int
ret =
vrpn_set_rts
(_comm);
192
if
(ret == -1) {
193
throw
RTSFailure
();
194
}
195
}
196
197
void
vrpn_SerialPort::clear_rts
()
198
{
199
requiresOpen();
200
int
ret =
vrpn_clear_rts
(_comm);
201
if
(ret == -1) {
202
throw
RTSFailure
();
203
}
204
}
vrpn_SerialPort::flush_output_buffer
void flush_output_buffer()
Throw out any characters (do not send) within the output buffer.
Definition
vrpn_SerialPort.C:170
vrpn_SerialPort::close
void close()
Close the serial port.
Definition
vrpn_SerialPort.C:77
vrpn_SerialPort::read_available_characters
int read_available_characters(unsigned char *buffer, int count)
Definition
vrpn_SerialPort.C:105
vrpn_SerialPort::clear_rts
void clear_rts()
Definition
vrpn_SerialPort.C:197
vrpn_SerialPort::vrpn_SerialPort
vrpn_SerialPort()
Construct without opening.
Definition
vrpn_SerialPort.C:46
vrpn_SerialPort::write
int write(std::string const &buffer)
Definition
vrpn_SerialPort.C:86
vrpn_SerialPort::set_rts
void set_rts()
Definition
vrpn_SerialPort.C:188
vrpn_SerialPort::is_open
bool is_open() const
Definition
vrpn_SerialPort.h:210
vrpn_SerialPort::flush_input_buffer
void flush_input_buffer()
Throw out any characters within the input buffer.
Definition
vrpn_SerialPort.C:161
vrpn_SerialPort::~vrpn_SerialPort
~vrpn_SerialPort()
Destructor - closes port if open.
Definition
vrpn_SerialPort.C:52
vrpn_SerialPort::open
void open(const char *portname, long baud, int charsize=8, vrpn_SER_PARITY parity=vrpn_SER_PARITY_NONE)
Definition
vrpn_SerialPort.C:64
vrpn_SerialPort::drain_output_buffer
void drain_output_buffer()
Wait until all of the characters in the output buffer are sent, then return.
Definition
vrpn_SerialPort.C:179
vrpn_SerialPort::AlreadyOpen
Definition
vrpn_SerialPort.h:145
vrpn_SerialPort::CloseFailure
Definition
vrpn_SerialPort.h:167
vrpn_SerialPort::DrainFailure
Definition
vrpn_SerialPort.h:203
vrpn_SerialPort::FlushFailure
Definition
vrpn_SerialPort.h:196
vrpn_SerialPort::OpenFailure
Definition
vrpn_SerialPort.h:159
vrpn_SerialPort::RTSFailure
Definition
vrpn_SerialPort.h:175
vrpn_SerialPort::ReadFailure
Definition
vrpn_SerialPort.h:182
vrpn_SerialPort::WriteFailure
Definition
vrpn_SerialPort.h:189
DynamicBufferType
std::vector< unsigned char > DynamicBufferType
Definition
vrpn_SerialPort.C:34
vrpn_SerialPort.h
Header.
vrpn_write_characters
int vrpn_write_characters(int comm, const unsigned char *buffer, size_t bytes)
Write the buffer to the serial port.
Definition
vrpn_Serial.C:651
vrpn_set_rts
int vrpn_set_rts(int comm)
Definition
vrpn_Serial.C:368
vrpn_close_commport
int vrpn_close_commport(int comm)
Definition
vrpn_Serial.C:348
vrpn_flush_input_buffer
int vrpn_flush_input_buffer(int comm)
Throw out any characters within the input buffer.
Definition
vrpn_Serial.C:438
vrpn_drain_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:488
vrpn_read_available_characters
int vrpn_read_available_characters(int comm, unsigned char *buffer, size_t bytes)
Definition
vrpn_Serial.C:515
vrpn_flush_output_buffer
int vrpn_flush_output_buffer(int comm)
Throw out any characters (do not send) within the output buffer.
Definition
vrpn_Serial.C:465
vrpn_clear_rts
int vrpn_clear_rts(int comm)
Definition
vrpn_Serial.C:402
vrpn_open_commport
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
vrpn_Serial.h
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
vrpn_SER_PARITY
vrpn_SER_PARITY
Definition
vrpn_Serial.h:15
vrpn_SerialPort.C
Generated by
1.17.0