Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Kestrel Collaboration
Kestrel Firmware
serial
Commits
a6922147
Commit
a6922147
authored
13 years ago
by
John Harrison
Browse files
Options
Download
Email Patches
Plain Diff
Adding a read_until option where you can insert a deliminator.
parent
5ef78dde
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
61 additions
and
0 deletions
+61
-0
include/serial.h
include/serial.h
+3
-0
src/serial.cpp
src/serial.cpp
+58
-0
No files found.
include/serial.h
View file @
a6922147
...
...
@@ -175,6 +175,9 @@ public:
*/
const
std
::
string
read
(
int
size
=
1
);
std
::
string
read_until
(
char
delim
,
size_t
size
=
-
1
);
std
::
string
read_until
(
std
::
string
delim
,
size_t
size
=
-
1
);
/** Write length bytes from buffer to the serial port.
*
* @param data A char[] with data to be written to the serial port.
...
...
This diff is collapsed.
Click to expand it.
src/serial.cpp
View file @
a6922147
...
...
@@ -239,6 +239,64 @@ const std::string Serial::read(int size) {
return
return_str
;
}
std
::
string
Serial
::
read_until
(
char
delim
,
size_t
size
)
{
this
->
reading
=
true
;
boost
::
asio
::
streambuf
b
(
size
==
-
1
?
size
:
(
std
::
numeric_limits
<
std
::
size_t
>::
max
)());
boost
::
asio
::
async_read_until
(
*
this
->
serial_port
,
b
,
delim
,
boost
::
bind
(
&
Serial
::
read_complete
,
this
,
boost
::
asio
::
placeholders
::
error
,
boost
::
asio
::
placeholders
::
bytes_transferred
));
if
(
this
->
timeout
>
timeout_zero_comparison
)
{
// Only set a timeout_timer if there is a valid timeout
this
->
timeout_timer
.
expires_from_now
(
this
->
timeout
);
this
->
timeout_timer
.
async_wait
(
boost
::
bind
(
&
Serial
::
timeout_callback
,
this
,
boost
::
asio
::
placeholders
::
error
));
}
else
if
(
this
->
nonblocking
)
{
this
->
timeout_timer
.
expires_from_now
(
boost
::
posix_time
::
milliseconds
(
1
));
this
->
timeout_timer
.
async_wait
(
boost
::
bind
(
&
Serial
::
timeout_callback
,
this
,
boost
::
asio
::
placeholders
::
error
));
}
while
(
this
->
reading
)
this
->
io_service
.
run_one
();
b
.
commit
(
bytes_read
);
std
::
istream
is
(
&
b
);
std
::
string
s
;
is
>>
s
;
return
s
;
}
std
::
string
Serial
::
read_until
(
std
::
string
delim
,
size_t
size
)
{
this
->
reading
=
true
;
boost
::
asio
::
streambuf
b
(
size
==
-
1
?
size
:
(
std
::
numeric_limits
<
std
::
size_t
>::
max
)());
boost
::
asio
::
async_read_until
(
*
this
->
serial_port
,
b
,
delim
,
boost
::
bind
(
&
Serial
::
read_complete
,
this
,
boost
::
asio
::
placeholders
::
error
,
boost
::
asio
::
placeholders
::
bytes_transferred
));
if
(
this
->
timeout
>
timeout_zero_comparison
)
{
// Only set a timeout_timer if there is a valid timeout
this
->
timeout_timer
.
expires_from_now
(
this
->
timeout
);
this
->
timeout_timer
.
async_wait
(
boost
::
bind
(
&
Serial
::
timeout_callback
,
this
,
boost
::
asio
::
placeholders
::
error
));
}
else
if
(
this
->
nonblocking
)
{
this
->
timeout_timer
.
expires_from_now
(
boost
::
posix_time
::
milliseconds
(
1
));
this
->
timeout_timer
.
async_wait
(
boost
::
bind
(
&
Serial
::
timeout_callback
,
this
,
boost
::
asio
::
placeholders
::
error
));
}
while
(
this
->
reading
)
this
->
io_service
.
run_one
();
b
.
commit
(
bytes_read
);
std
::
istream
is
(
&
b
);
std
::
string
s
;
is
>>
s
;
return
s
;
}
void
Serial
::
read_complete
(
const
boost
::
system
::
error_code
&
error
,
std
::
size_t
bytes_transferred
)
{
if
(
!
error
||
error
!=
boost
::
asio
::
error
::
operation_aborted
)
{
// If there was no error OR the error wasn't operation aborted (canceled), Cancel the timer
this
->
timeout_timer
.
cancel
();
// will cause timeout_callback to fire with an error
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment