1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 11:44:53 +08:00

Compare commits

..

241 Commits
v0.2 ... main

Author SHA1 Message Date
Eric Fontaine
69e0372cf0
Move License text to separate LICENSE file (#261) 2022-03-08 16:16:57 -08:00
Maicol Castro
33e5a31ab7
Fix broken links (#244) 2021-06-10 15:15:44 -07:00
Robin Krens
ed9f89ca31
Add support for serial bluetooth ports on Linux (#237)
Added search blob /dev/rfcomm*. rfcomm* is a commonly used naming
convention for bluetooth ports on linux
2021-06-10 15:14:48 -07:00
Jacob Perron
9fc9e81fc1
Remove Boost dependency (#235)
It wasn't being used so there's no need to depend on it.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
2021-06-10 15:14:20 -07:00
Matthias Behr
57f72772a9
Add EINTR handling to SerialImpl::write (#233)
EINTR can still happen on write. Don't throw the exception in that
case but retry.
In case of other errors add more details to the SerialException.
2021-06-10 15:13:58 -07:00
Atomie CHEN
a93fc844d9
Solve issue Custom Baudrate OSX #139 of original repo; inspired by PySerial source code and #57 of github.com/npat-efault/picocom, we need to set custom baudrate after calling tcsetattr; tested on macOS Mojave 10.14.4 (#218) 2021-06-10 14:57:40 -07:00
Hannes Kamecke
7439db1228
Fix windows com port prefix (#179)
The check in `_prefix_port_if_needed` does not work, as it's currently comparing the whole input string to the prefix. As a consequence, port strings will be prefixed, even if they're already prefixed. This commit changes the call to `wstring::compare` to use an overload that compares a substring of the input string only.
2021-06-10 14:57:28 -07:00
Chenchen
9e331e7977
fix invalid memory access when eol size >1 (#220)
Co-authored-by: chenguojun <chenguojun@yogorobot.com>
2021-06-10 14:49:29 -07:00
Sean Yen
cbcca7c837 Install serial library to a portable location. (#216) 2020-01-06 11:15:17 -08:00
bsbaliga
683e12d2f6 Fix memory leak when exception is thrown by impl classes in (#198)
Serial::read() vector and string variants.
2019-03-25 15:25:33 -07:00
William Woodall
fba8d81b5d
whitespace 2018-11-12 12:03:16 -06:00
William Woodall
2ef29d7b10
whitespace 2018-11-12 12:01:58 -06:00
William Woodall
5a354eaab7
reduce the number of jobs on travis (#172)
* reduce the number of jobs on travis

* update usage of pip due to changes in Homebrew

* update pip and ensure the right one is being used with an alias

* force reinstall pip to get `pip` on PATH

* use python2 explicitly to run catkin_make

* force use of python2 executable by catkin packages

* python!

* simpler

* how to which

* Update .travis.yml

* make tests and run_tests

* test results

* disable timer tests on macOS
2018-02-05 19:44:04 -08:00
Mike Purvis
bdba3a80ad Fix CMake warning for rt and pthread. (#165) 2018-01-18 17:25:29 -08:00
William Woodall
17e3223e68
remove references to no longer available uninstall command (#171) 2018-01-13 13:37:26 -08:00
William Woodall
764fab8b5e sublime project file updates 2018-01-13 13:19:48 -08:00
Ben Moyer
235a5f716d print GetLastError() result instead of errno (#154) 2018-01-13 12:16:50 -08:00
Ben Moyer
534141aa8f implement flushInput and flushOutput for windows (#153) 2018-01-13 12:15:36 -08:00
José Manuel Díez
ce085ce88c Problem: hardware flow control uses RTS_CONTROL_TOGGLE (#132)
RTS_CONTROL_HANDSHAKE raises RTS when there is space in the input
buffer; RTS_CONTROL_TOGGLE only raises RTS when bytes are available for
transmission.

Also replace numeric constants with symbolic constants.
2018-01-13 12:10:11 -08:00
Mike Purvis
d3713af096 Support 500kbps serial ports. (#167) 2018-01-09 07:22:43 -08:00
rhd
827c4a784d Fix issue with write() and a timeout of 0. (#137)
* Fix issue with write() and a timeout of 0.

* fix up style
2017-01-20 17:34:49 -08:00
Stephane Poirier
771e62c220 Update documentation (#140)
* Fix typo and missing dependency in README

* [docs] Update docs: fix deprecation warnings + add missing deps to README
2017-01-20 15:29:03 -08:00
aleksey-sergey
02dfff7883 fixing unix timeouts handling ("timer_tests.short_interval" failure) (#147) 2016-11-28 17:38:53 -08:00
aleksey-sergey
4d69fb2e41 fix timeouts handling on Unix systems (#142)
fixed "singed long" overflow that took place on attempt
to use ~3000ms or bigger timeouts on Unix systems
2016-11-28 14:06:00 -08:00
dontsovcmc
c16faab6ea resource leak if exception in SerialImpl constructor (#146) 2016-11-21 14:48:07 -08:00
Christopher Baker
d76b7d6b7f Const corrections. (#141) 2016-10-17 13:54:11 +09:00
William Woodall
2d416f1560 Merge pull request #118 from Rimco/patch-1
Updated serial.cc for FreeBSD 9 compatibility.
2015-12-02 09:06:25 -08:00
Rimco
22dce33fa4 Updated serial.cc for FreeBSD 9 compatibility. 2015-11-26 22:15:48 +01:00
William Woodall
e12d81eadf Merge pull request #116 from pao/patch-1
Use CLOCK_MONOTONIC (Linux)/SYSTEM_CLOCK (OS X) to time select()
2015-11-10 15:35:05 -08:00
Patrick O'Leary
c5b4bbd181 on OS X, use SYSTEM_CLOCK, not CALENDAR_CLOCK
Analogously to using `CLOCK_MONOTONIC` on Linux to time events in favor of `CLOCK_REALTIME`, `SYSTEM_CLOCK` should be used in favor of `CALENDAR_CLOCK` on OS X.

Ref: http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx
2015-11-10 17:28:45 -06:00
Patrick O'Leary
98f1c31e81 on Linux, use CLOCK_MONOTONIC for clock_gettime()
On Linux systems which are being driven by an external time source (NTP or PTP), it is possible that time appears to slew in reverse under `CLOCK_REALTIME`. Since the timer function is used to time durations of events (calls to `select()`), it is better to use `CLOCK_MONOTONIC`, which isn't subject to slewing.
2015-11-10 09:40:30 -06:00
William Woodall
ef7f77d411 Merge pull request #113 from vladimirgamalian/patch-1
Comment unreferenced formal parameters
2015-09-27 14:14:03 -07:00
Vladimir Gamalian
402657574c Comment unreferenced formal parameter
Fix warning from static analysis tools.
2015-09-27 19:41:56 +07:00
William Woodall
a81087ec85 Merge pull request #112 from linquize/vs2015
Support VS2015
2015-09-23 14:35:04 -07:00
Linquize
9b80fc3fbf Can use the toolsets from Visual Studio 2010, 2012, 2013, 2015 2015-09-22 21:45:43 +08:00
William Woodall
c57285f30a Merge pull request #106 from ramirahikkala/master
AdditionalIncludeDirectories must be relative for project not solution
2015-08-14 10:11:13 -07:00
Rami
cd5053cb32 AdditionalIncludeDirectories must be relative for project not solution
Fixes #105

Signed-off-by: Rami <rami.rahikkala@jotautomation.com>
2015-08-14 08:37:27 +03:00
William Woodall
6144d579af Merge pull request #103 from drummist180/master
Fix include directory paths in Visual Studio projects.
2015-07-21 16:02:36 -07:00
Brandon Morton
1580f10d39 Fix include directory paths in Visual Studio projects.
Remove previously ignored *.user file.
2015-07-16 19:17:52 -07:00
William Woodall
99e57e633c Merge pull request #98 from wjwwood/fix_issue_97
fix warning on Windows
2015-04-27 15:20:05 -07:00
William Woodall
b8320d31a7 fix warning on Windows 2015-04-27 14:44:16 -07:00
William Woodall
10ac4e1c25 1.2.1 2015-04-21 21:50:41 -07:00
William Woodall
03ca3be6a8 Merge pull request #96 from wjwwood/issue_84
fix handling of COM ports over 10 on Windows, fixes #84
2015-04-21 21:38:04 -07:00
William Woodall
52197afbc8 [doc] fixup/clarify some throws documentation 2015-04-21 21:15:51 -07:00
William Woodall
e438077cba [style] whitespace and configs 2015-04-21 21:15:51 -07:00
William Woodall
985c3a5172 fix handling of COM ports over 10 on Windows, fixes #84 2015-04-21 20:43:54 -07:00
William Woodall
1a70b09bb1 Merge pull request #95 from wjwwood/issue_90_refresh
Release io_iterator_t object when finished with it in list_ports_osx.cc.
2015-04-21 20:42:02 -07:00
William Woodall
33bbde42c8 fix typo 2015-04-21 20:07:55 -07:00
William Woodall
3a9e6be834 [style] whitespace 2015-04-21 19:01:59 -07:00
Konstantina Kastanara
42aa78ae98 Release io_iterator_t object when finished with it in list_ports_osx.cc. 2015-04-21 18:59:58 -07:00
William Woodall
d7084ce775 Merge pull request #93 from legath/master
fix for clang build on macosx
2015-04-06 12:04:08 -07:00
Alexander Bulychev
e26a977173 fix for clang build on macosx 2015-04-06 16:53:33 +03:00
William Woodall
5b13be4d7c Merge pull request #92 from DarkDemiurg/master
Fix for mingw32
2015-03-24 16:56:05 -07:00
DarkDemiurg
29cb551c41 Fix for mingw32 2015-03-24 23:19:15 +03:00
William Woodall
e70643380d Merge pull request #91 from wjwwood/fix_osx
travis: fixup os x dep install
2015-03-09 13:04:19 -07:00
William Woodall
32792b3ee1 travis: fixup os x dep install 2015-03-09 11:48:31 -07:00
William Woodall
2473a8770f Merge pull request #86 from avian2/add-57600-baud
Add 576000 baud support for Unix
2015-01-12 10:34:06 -08:00
Tomaz Solc
dda5b6308e Add 576000 baud support for Unix 2015-01-06 15:15:31 +01:00
William Woodall
17da2dea31 Add CI status to README.md 2014-12-29 12:40:54 -08:00
William Woodall
c3d55880c8 Merge pull request #85 from wjwwood/travis_osx2
trying different travis osx settings
2014-12-29 12:37:37 -08:00
William Woodall
bd0980b69a trying different travis osx settings 2014-12-29 11:48:58 -08:00
William Woodall
31fa9c9e34 Merge pull request #83 from wjwwood/travis_osx
trying out osx support in travis
2014-12-29 11:22:22 -08:00
William Woodall
a622105543 trying out osx support in travis 2014-12-29 11:19:57 -08:00
William Woodall
8036553a1e improve error message when mark or space parity are not supported 2014-12-09 13:50:24 -08:00
David Hodo
6f464948f0 Make MARK/SPACE partiy support optional 2014-12-09 14:50:37 -06:00
William Woodall
6e47bdd0ae Merge pull request #81 from achronop/master
Add MARK/SPACE parity bit option
2014-12-07 15:20:49 -08:00
achronop
b376f85fb0 Add MARK/SPACE parity bit option 2014-12-07 21:33:53 +02:00
William Woodall
c19a5a3cc9 Merge pull request #75 from Konstantinacc/master
CloseHanle fd_ if SetCommState fails in function Serial::SerialImpl::reconfigurePort ().
2014-08-19 10:16:54 -07:00
Konstantina Kastanara
7bce3e0fdc CloseHanle fd_ if SetCommState fails in function Serial::SerialImpl::reconfigurePort (). 2014-08-19 18:24:48 +03:00
William Woodall
7de61c1603 Merge pull request #72 from dawid-aurobit/mxe-cross
support for mxe (http://mxe.cc)
2014-08-16 20:26:21 -07:00
Dawid Piotrowski
49b6762927 support for mxe 2014-08-16 22:08:56 +02:00
William Woodall
8fe085c45f Merge pull request #69 from linquize/vs
Many Visual Studio fixes
2014-07-27 14:38:49 -07:00
Linquize
b55e9f797f Can use the toolsets from Visual Studio 2010, 2012, 2013
The default is v100 (VS2010)
2014-07-26 22:00:10 +08:00
Linquize
54d7a9474a Fix warning in Win64 2014-07-26 21:59:53 +08:00
William Woodall
88ee4b3e3b Merge pull request #70 from bakercp/bugfix-availability-macros
Add include for OSX version checking macros.
2014-07-21 13:04:49 -07:00
Christopher Baker
3f0d49a602 Make sure AvailabilityMacros.h is included for build systems that do not include it by default.
- Required for macros such as MAC_OS_X_VERSION_10_3, MAC_OS_X_VERSION_MIN_REQUIRED, etc.
2014-07-21 12:29:38 -05:00
Linquize
124e601d2f Add VS related ignore files to .gitignore 2014-07-19 20:49:45 +08:00
Linquize
60bb787f93 Add x64 Platform to VS project 2014-07-19 20:49:08 +08:00
Linquize
d107487228 Add <SubSystem> tags to VS projects, specifically, test_serial is a console application 2014-07-19 20:48:23 +08:00
Linquize
e315dc5a48 Make Release|Win32 configuration compile 2014-07-19 20:13:56 +08:00
William Woodall
17aac9b77d Merge pull request #68 from bakercp/mingw_vs_text
Fixes to allow cross correct encoding on both codeblocks / mingw and VS
2014-07-15 13:35:28 -07:00
Christopher Baker
24564c59d3 Remove confusing comment. 2014-07-13 09:17:27 -05:00
William Woodall
1697fe2c56 Merge pull request #67 from bakercp/no_cpp11_codeblocks_win
Revert c++11 feature for better backward compatibility.
2014-07-12 14:16:31 -07:00
Christopher Baker
d9847ff87b Fixes to allow cross correct encoding on both codeblocsk / mingw and visual studio. 2014-07-11 09:28:55 -05:00
Christopher Baker
e16f43b1e4 Revert c++11 feature for better backward compatibility. 2014-07-10 23:57:19 -05:00
William Woodall
46802a9b3c 1.2.0 2014-07-02 15:59:39 -07:00
William Woodall
3d1f802242 changelog 2014-07-02 15:59:20 -07:00
William Woodall
dfc59a5f11 whitespace 2014-07-02 15:23:23 -07:00
William Woodall
de704369db fixup removal of read_cache_ 2014-07-02 15:17:02 -07:00
William Woodall
f1e53d17fb remove vestigial read_cache_ member variable 2014-07-02 15:07:38 -07:00
William Woodall
76c5575f80 Merge pull request #66 from bakercp/bugfix_osx_compiling
Add defines to new list_ports methods to compile on OSX.
2014-07-02 15:05:15 -07:00
William Woodall
033b009480 Merge pull request #65 from wjwwood/fix_locking
fix usage of scoped locks
2014-07-02 15:03:41 -07:00
William Woodall
23770f9581 Merge branch 'Konstantinacc-master' 2014-07-02 15:02:06 -07:00
William Woodall
051824894b style fixup 2014-07-02 15:01:53 -07:00
Christopher Baker
24a8722f3e Fix typo. 2014-07-02 11:18:11 -05:00
Christopher Baker
b46681e8cc Add defines to new list_ports methods to compile on OSX. 2014-07-02 10:47:45 -05:00
William Woodall
72af8435c5 fix usage of scoped locks
previously they were getting destroyed
immediately because they were not stored in a
temporary scope variable
2014-06-23 11:23:27 -07:00
Konstantina Kastanara
04d4763926 Added return value control in Serial::SerialImpl::close () in unix.cc and win.cc 2014-05-07 18:49:37 +03:00
William Woodall
2df3499e81 Merge pull request #49 from clearpathrobotics/wait-functions
Wait functions
2014-04-28 10:58:55 -07:00
Mike Purvis
2906a6fe90 Add missing stubs to serial/impl/win.h header 2014-04-28 07:30:40 -04:00
William Woodall
be80973ee7 Merge pull request #59 from LilleySavij/list_ports
List ports
2014-04-27 23:28:59 -07:00
Craig Lilley
10fa977dad Fixed a syntax error.
In list_ports_win.cc.
2014-04-28 00:01:42 +01:00
Craig Lilley
5d0c5335fa Updated documentation. 2014-04-26 13:22:45 +01:00
Craig Lilley
31a589e6e7 Added osx implementation to list_ports. 2014-04-26 12:49:30 +01:00
Craig Lilley
301a3d4b27 Changed the serial::PortDescription struct.
- Renamed to PortInfo.

- "friendly_name" field is now "description".
2014-04-24 02:23:26 +01:00
Craig Lilley
b8479822f7 Refactored list_ports to return a vector of serial::PortDescription. 2014-04-22 01:02:58 +01:00
William Woodall
9f89596e85 return a vector<array<string, 3> > from list_ports
instead of a vector<vector<string> >
2014-04-17 19:54:29 +01:00
Craig Lilley
72604cec0e Added ability to enumerate ports on linux and windows.
Updated serial_example.cc to show example of port enumeration.
2014-04-17 19:54:29 +01:00
William Woodall
4e5f143121 fix compile on VS2013 2014-04-16 14:32:28 -07:00
William Woodall
17afacc625 1.1.7 2014-02-20 17:37:00 -08:00
William Woodall
0fb1a6de87 update changelogs 2014-02-20 17:35:39 -08:00
Mike Purvis
5ec56d8294 Stubs for waitReadable and waitByteTimes on Windows. 2014-02-17 22:50:16 -05:00
William Woodall
15d37ac3aa Merge pull request #55 from dawid-aurobit/mingw
support for mingw (mxe.cc)
2014-01-05 23:34:52 -08:00
dawid
780f76c40d support for mingw (mxe.cc) 2014-01-05 18:29:04 +01:00
William Woodall
b1f25e884a Merge pull request #54 from nbigaouette/master
Fix compilation warning
2013-12-27 13:38:58 -08:00
Nicolas Bigaouette
feb667cc97 Fix compilation warning
See issue #53
2013-12-27 16:31:24 -05:00
William Woodall
f051c0a613 Merge pull request #45 from clearpathrobotics/timespec-refactor2
Timespec refactor (again)
2013-11-20 13:24:30 -08:00
Mike Purvis
d74c74bf15 Fill out waitByteTimes, integrate it into read as discussed in ticket #37 2013-11-19 12:27:56 -05:00
Mike Purvis
28025034bd Add waitReadable and waitByteTimes stubs. 2013-11-19 12:27:56 -05:00
Mike Purvis
a633418771 Build tests on Travis. 2013-11-18 21:35:13 -05:00
Mike Purvis
9c432f9bb1 Add unix timer tests, clarify tests are only on unix at present, move test builds into separate makefile. 2013-11-18 21:35:07 -05:00
Mike Purvis
a9bf8d804d Pre-fill buffer at start of read, to avoid the select if unnecessary. 2013-11-18 11:26:04 -05:00
Mike Purvis
c3855adbb0 Wrap nanoseconds when >= 1e9, rather than > 1e9. 2013-11-14 22:02:07 -05:00
Mike Purvis
fbffc18dd7 Fix for computing an expiry without rollover. 2013-11-08 10:03:27 -05:00
Mike Purvis
2e5e8f940b Divide by 1e9 to get seconds from nanoseconds, instead of 1e6. 2013-10-31 21:48:44 -04:00
William Woodall
cfac5bbcc9 use static casts rather than C-style casting
C-style casting can result in undesired reinterpret_casts
So we should avoid them, see:
http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used
2013-10-31 19:44:34 -04:00
Mike Purvis
3f2ed36849 Of course, the timespec_from_ms function must return its result. 2013-10-31 19:43:49 -04:00
Mike Purvis
5820056aef Remove TimerExpiredException. 2013-10-31 19:43:49 -04:00
Mike Purvis
589e7b9a3b Eliminate modulus operator. 2013-10-31 19:43:49 -04:00
Mike Purvis
d8a1ef4ecf Rename now function to timespec_now and timeout_time to expiry, for greater clarity. 2013-10-31 19:43:49 -04:00
Mike Purvis
6747711632 Implement MillisecondTimer in the unix read() and write() functions. 2013-10-31 19:43:49 -04:00
Mike Purvis
16a024691b Add MillisecondTimer class. 2013-10-31 19:43:49 -04:00
William Woodall
dd7259149f if not windows guard for unix.h 2013-10-30 11:54:59 -07:00
William Woodall
e8f913b4a4 Contributor notice 2013-10-30 11:53:06 -07:00
William Woodall
348cb9bdc5 fix broken ifdef _WIN32 2013-10-30 11:42:59 -07:00
William Woodall
092836d78f [style] whitespace changes only 2013-10-30 11:42:35 -07:00
William Woodall
5a43c628df Merge branch 'master' of https://github.com/bakercp/serial into bakercp-master 2013-10-30 10:49:07 -07:00
William Woodall
22f5e302be fix warnings about uninitialized class members 2013-10-28 15:10:38 -07:00
William Woodall
32b3631285 [style] white space clean up 2013-10-28 15:09:53 -07:00
Christopher Baker
0e5f7fa317 Fix broken ioctl calls, add exception handling. 2013-10-27 18:50:06 -05:00
Christopher Baker
9d04c09f10 Code guards for platform-specific implementations.
(when not using cmake / catkin)
2013-10-27 18:49:07 -05:00
Christopher Baker
5bcbbfb09c Explicit using. 2013-10-27 18:42:53 -05:00
William Woodall
d06b2f94dd "1.1.6" 2013-10-17 11:13:00 -07:00
William Woodall
3d0537ceb8 update changelog 2013-10-17 11:12:18 -07:00
William Woodall
863aee8795 Merge pull request #41 from clearpathrobotics/one-point-five-fix
Fix aliasing of stopbits_one_point_five
2013-10-09 10:47:57 -07:00
Mike Purvis
8f01d23249 Move stopbits_one_point_five to the end of the enum, so that it doesn't alias with stopbits_two. 2013-10-06 01:24:42 -04:00
William Woodall
0c7a1f59c3 1.1.5 2013-09-23 21:20:33 -07:00
William Woodall
dc9282862c updating changelogs 2013-09-23 21:20:22 -07:00
William Woodall
f1885c60c0 Merge pull request #38 from wmhilton/master
Windows build instructions
2013-08-28 16:30:17 -07:00
William Woodall
bed25fd8a2 Fix license labeling, I put BSD, but the license has always been MIT... 2013-08-28 15:01:13 -07:00
William Hilton
737cf2a04d Added Microsoft Visual Studio 2010 project to make compiling on Windows easier. 2013-08-25 19:22:11 -04:00
William Woodall
ae34a17364 Merge pull request #33 from wjwwood/fix_custom_baudrate_osx
Update how custom baudrates are handled on OS X
2013-08-13 13:53:14 -07:00
William Woodall
a20acb2a00 Merge pull request #35 from daniser/patch-1
Implemented Serial::available() for Windows
2013-08-06 17:38:24 -07:00
daniser
079615f11f Update win.cc 2013-08-06 02:08:22 +03:00
daniser
125c105e3f Implemented Serial::available() for Windows 2013-08-06 01:28:46 +03:00
William Woodall
c455d053bf Fixup unix case for strerror 2013-08-02 14:24:39 -07:00
William Woodall
e11abb04f2 Fix compiler warnings on Windows 2013-08-02 21:20:25 -07:00
William Woodall
3db36faa14 Update how custom baudrates are handled on OS X
This is taken from the example serial program
on Apple's developer website, see:

http://free-pascal-general.1045716.n5.nabble.com/Non-standard-baud-rates-in-OS-X-IOSSIOSPEED-IOCTL-td4699923.html
2013-07-31 16:54:15 -07:00
William Woodall
3292f2b682 Fix up linking on Linux for the tests
Conflicts:
	CMakeLists.txt
	tests/serial_tests.cc
2013-07-31 16:52:19 -07:00
William Woodall
e1138fda9e Merge pull request #32 from wjwwood/fix_timeout_windows
Setting timeout throws an exception on Windows
2013-07-30 16:17:00 -07:00
William Woodall
2143e8d475 Apply the timeout settings with reconfigurePort 2013-07-30 14:08:06 -07:00
William Woodall
a0a586cf5b [style] always use curly braces with if statements 2013-07-30 14:04:30 -07:00
William Woodall
329545b282 Merge pull request #30 from wjwwood/fix_wstring_windows
Fix wide string usage in Windows impl
2013-07-30 11:26:31 -07:00
William Woodall
9d20d1a07f convert wstring into string when printing 2013-07-30 11:06:47 -07:00
William Woodall
7e04501d99 Merge pull request #31 from nbigaouette/fix_doc
Fix a typo in documentation
2013-07-30 11:02:29 -07:00
Nicolas Bigaouette
1e09770d8b Fix typo in documentation 2013-07-30 13:45:47 -04:00
William Woodall
82884ca519 Pass LPCWSTR to CreateFile in Windows impl
This should fix #29
2013-07-30 10:31:15 -07:00
William Woodall
ffc3028289 Use wstring for port_ type in Windows impl 2013-07-30 10:30:46 -07:00
William Woodall
dca4bd163e 1.1.4 2013-06-20 14:10:49 -07:00
William Woodall
7f03d8d804 Fixing up CHANGELOG.rst for 1.1.4 2013-06-20 14:10:38 -07:00
William Woodall
db0947e242 Update package.xml 2013-06-12 00:13:18 -06:00
William Woodall
516a5c7e82 Fixed another Exception misspelling. 2013-06-11 17:05:24 -07:00
William Woodall
308be5b337 Merge pull request #25 from wjwwood/fix_exceptions
fix, by Thomas Hoppe <thomas.hoppe@cesys.com>
2013-06-11 14:40:18 -07:00
William Woodall
4291db0b30 Adding changelog 2013-06-11 14:18:23 -07:00
William Woodall
947f1937d6 Merge pull request #28 from wjwwood/issue_27
Potential timing fix for read and write.
2013-05-23 09:45:13 -07:00
William Woodall
060634bf91 Potential timing fix for read and write.
Fixes #27
2013-05-22 21:45:20 -07:00
William Woodall
4ed3889979 Merge pull request #26 from trainman419/master
Update list of exceptions thrown from constructor.
2013-05-22 11:43:39 -07:00
trainman419
31e07fdb99 Update list of exceptions thrown from constructor. 2013-05-21 00:00:31 -07:00
William Woodall
2b4bafbfd2 fix, by Thomas Hoppe <thomas.hoppe@cesys.com>
For SerialException's:

- The name was misspelled…
- Use std::string's for error messages to prevent corruption of messages on some platforms
2013-05-08 13:52:28 -07:00
William Woodall
46b06a3187 Update README.md 2013-03-14 16:04:11 -07:00
William Woodall
d6b97057ef Merge pull request #23 from bentley/master
OpenBSD ifdef
2013-02-19 14:19:06 -08:00
William Woodall
42fc29ada5 1.1.3 2013-01-09 10:54:34 -08:00
William Woodall
54987058ea install headers 2013-01-09 10:53:58 -08:00
William Woodall
63baf1cad8 1.1.2 2012-12-14 14:08:55 -08:00
William Woodall
df27a2bf9c Fix buildtool depends 2012-12-14 14:07:39 -08:00
William Woodall
788490f118 Version 1.1.1 2012-12-03 15:32:19 -08:00
William Woodall
72f43d9976 Small CMakeLists.txt touch ups. 2012-12-03 15:31:41 -08:00
William Woodall
2e9f850175 Merge branch 'master' of https://github.com/wjwwood/serial 2012-12-03 14:51:47 -08:00
William Woodall
fe00053372 Removed rt linking on OS X. Fixes #24. 2012-12-03 14:51:10 -08:00
William Woodall
c1866aeeed Update README.md
Added links to documentation.
2012-11-21 13:23:27 -08:00
William Woodall
c37d25a43c Added groovy source setup.bash to travis.yml 2012-11-06 19:22:06 -08:00
William Woodall
ae0eea6719 Change to the travis.yml file 2012-11-06 19:20:23 -08:00
William Woodall
b458793b19 Trying travis-ci out. 2012-11-06 16:41:41 -08:00
Anthony J. Bentley
b95b797de7 alloca.h does not exist on OpenBSD either. 2012-10-29 09:46:29 -06:00
William Woodall
d5429126c9 Added a link option specifically for Linux 2012-10-24 01:32:23 -07:00
William Woodall
f3a9767a1a Releasing version 1.1.0 of serial
Converted the build system to caktin
2012-10-24 00:11:21 -07:00
William Woodall
5c21be7d8f Updating the README.md 2012-10-24 00:08:46 -07:00
William Woodall
7174d62e1b Adding catkin build files and a new Makefile. 2012-10-23 23:47:11 -07:00
William Woodall
73371a144d Removing old build files. 2012-10-23 23:46:57 -07:00
William Woodall
5a2f127d26 Release v1.0.1 2012-08-27 11:36:26 -07:00
William Woodall
871f32fcb2 Fixed linking on Linux and OS X. 2012-08-01 20:09:39 -05:00
William Woodall
485a9fb9a3 Updated the tests for the new Serial API. 2012-08-01 20:09:20 -05:00
William Woodall
fbfd95dc63 Added the rt library to the serial_LIBRARIES variable when on Linux. 2012-08-01 20:08:47 -05:00
William Woodall
7e46129f7d Adding explicit keyword to Timeout constructor to prevent accidental conversion from int types. 2012-07-30 18:56:16 -05:00
William Woodall
c829e6a238 Merge pull request #21 from davidhodo/master
add option to not use clang so i can use this in bitbake
2012-07-21 13:56:45 -07:00
David Hodo
d7b12661e5 should have been if NOT defined 2012-07-21 11:13:13 -05:00
David Hodo
fdff84a168 add option to not use Clang, even if it is installed 2012-07-20 18:04:42 -05:00
William Woodall
c5a2d7b4bc Adding void return type for Arduino example in examples/serial_example.cc. Fixes #20 2012-07-09 15:51:38 -05:00
William Woodall
667eedeb2f Adding baudrates: 1000000, 11520000, 2000000, 2500000, 3000000, 3500000, and 4000000 to the unix.cc implementation. This is an attempting to work around issue #18, needs testing on a non-USB serial adapter that can handle higher baudrates. 2012-07-08 19:45:35 -05:00
William Woodall
8a4a9a78c4 Clean up of the build system. Added rt and util in certain places so compiling the tests on Linux succeed, which should fix #19. Also removed over verbose warnings on Linux when build stand-alone. 2012-07-08 19:09:21 -05:00
William Woodall
83ae470c78 Silly little bug in the custom baud rate code. Should fix #18. 2012-06-18 19:04:35 -05:00
William Woodall
da1207609f Fixing a bug in the unix write code which may have prevented it from writing in some cases, and updating some of the comments and error messages in the write function that still read like the read function 2012-06-14 18:51:24 -05:00
William Woodall
7e50dc7da6 Fixing compile and link problems in Linux and ROS 2012-06-13 20:13:57 -05:00
William Woodall
2ae490b3ac Fixes to flowcontrol that solve a wierd bug on Linux and fix an oversight on Windows. 2012-06-12 16:55:11 -05:00
William Woodall
95790064d8 Fixing warning in Linux 2012-06-12 00:25:28 -05:00
William Woodall
94eac90d7c Fixing a compile problem in Linux that got introduced in the Windows testing. 2012-06-12 00:23:39 -05:00
William Woodall
c55d7e2969 Adding link to the rosinstall file 2012-06-11 21:09:16 -05:00
William Woodall
80c0ce9b1b Updating docs and adding a rosinstall file 2012-06-11 20:51:18 -05:00
William Woodall
ada33af16e Merge commit 'e9999c9c7c801b8c60569f1b48792af8c050eac4' 2012-06-09 20:03:26 -05:00
William Woodall
120df13f2a Updating documentation 2012-06-09 19:52:05 -05:00
William Woodall
f4ce55e0be Update master 2012-06-09 18:21:34 -05:00
William Woodall
eb3ebc1dc7 Merge branch 'new_api' 2012-06-09 18:11:59 -05:00
William Woodall
e9999c9c7c Changes after testing on Windows. Everything seems to work, but it could use some more vetting. 2012-06-07 19:26:55 -05:00
William Woodall
493883da81 Fixing some warnings from compiling on Linux 2012-06-07 18:16:20 -05:00
William Woodall
e12955a560 Fixed some issues with the copy constructors 2012-06-07 18:09:51 -05:00
William Woodall
ef8265a535 A small change to quiet warnings in linux 2012-06-07 18:01:18 -05:00
William Woodall
7714615af7 Disabling more copy constructors 2012-06-07 17:54:11 -05:00
William Woodall
088fcda295 Disabling copy constructors on the expections 2012-06-07 17:52:14 -05:00
William Woodall
565ba66271 Sorted out the stdint stuff by including a header from the v8 project. 2012-06-07 17:49:05 -05:00
William Woodall
ead96705dd Still working on std types 2012-06-07 16:44:35 -05:00
William Woodall
520ccb54f4 Merge branch 'new_api' of github.com:wjwwood/serial into new_api
Conflicts:
	.gitignore
2012-06-06 18:58:51 -05:00
William Woodall
5ebd00cde1 Removing a vestigial file 2012-06-06 18:58:14 -05:00
William Woodall
6fac95d633 Adding initial windows support. 2012-06-06 18:56:44 -05:00
William Woodall
dcc73db6a8 Syntax fix 2012-05-11 15:28:23 -05:00
William Woodall
15f01a53a1 Adjustments to the read timeouts 2012-05-10 11:43:17 -05:00
William Woodall
937e834698 Updating the unix implementation to support the more sophisticated timeout system. 2012-05-09 19:51:56 -05:00
William Woodall
3039acbab9 Adding a convenience function for generating simple Timeouts based purely on the total time until a timeout occurs with no regard to inter byte timeout or timeout multipliers. 2012-05-07 13:06:28 -05:00
William Woodall
ba0c648119 Fixes #16. 2012-05-07 13:04:34 -05:00
William Woodall
0a66fa991d Changes to the documentation generation mechanism 2012-05-02 23:39:42 -05:00
William Woodall
194169e5e6 More updates to the documentation and new api after comments from john and michael. 2012-05-02 21:49:09 -05:00
William Woodall
8022c1b1ea Updating the API to fix some inconsistencies 2012-05-02 00:45:23 -05:00
39 changed files with 5278 additions and 2530 deletions

9
.gitignore vendored
View File

@ -23,3 +23,12 @@ patched
wiped wiped
msg_gen msg_gen
srv_gen srv_gen
doc/html
*sublime-workspace
*.user
*.suo
*.sdf
*.opensdf
ipch
Debug
Release

10
.travis.yml Normal file
View File

@ -0,0 +1,10 @@
os:
- linux
- osx
language: cpp
install:
- make install_deps
- source setup.bash
script:
- mkdir build && cd build && cmake .. -DPYTHON_EXECUTABLE=$(which python2) && make && make tests && make run_tests
- catkin_test_results .

85
CHANGELOG.rst Normal file
View File

@ -0,0 +1,85 @@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Changelog for package serial
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1.2.1 (2015-04-21)
------------------
* Removed the use of a C++11 feature for compatibility with older browsers.
* Fixed an issue with cross compiling with mingw on Windows.
* Restructured Visual Studio project layout.
* Added include of ``#include <AvailabilityMacros.h>`` on OS X (listing of ports).
* Fixed MXE for the listing of ports on Windows.
* Now closes file device if ``reconfigureDevice`` fails (Windows).
* Added the MARK/SPACE parity bit option, also made it optional.
Adding the enumeration values for MARK and SPACE was the only code change to an API header.
It should not affect ABI or API.
* Added support for 576000 baud on Linux.
* Now releases iterator properly in listing of ports code for OS X.
* Fixed the ability to open COM ports over COM10 on Windows.
* Fixed up some documentation about exceptions in ``serial.h``.
1.2.0 (2014-07-02)
------------------
* Removed vestigial ``read_cache_`` private member variable from Serial::Serial
* Fixed usage of scoped locks
Previously they were getting destroyed immediately because they were not stored in a temporary scope variable
* Added check of return value from close in Serial::SerialImpl::close () in unix.cc and win.cc
* Added ability to enumerate ports on linux and windows.
Updated serial_example.cc to show example of port enumeration.
* Fixed compile on VS2013
* Added functions ``waitReadable`` and ``waitByteTimes`` with implemenations for Unix to support high performance reading
* Contributors: Christopher Baker, Craig Lilley, Konstantina Kastanara, Mike Purvis, William Woodall
1.1.7 (2014-02-20)
------------------
* Improved support for mingw (mxe.cc)
* Fix compilation warning
See issue `#53 <https://github.com/wjwwood/serial/issues/53>`_
* Improved timer handling in unix implementation
* fix broken ifdef _WIN32
* Fix broken ioctl calls, add exception handling.
* Code guards for platform-specific implementations. (when not using cmake / catkin)
* Contributors: Christopher Baker, Mike Purvis, Nicolas Bigaouette, William Woodall, dawid
1.1.6 (2013-10-17)
------------------
* Move stopbits_one_point_five to the end of the enum, so that it doesn't alias with stopbits_two.
1.1.5 (2013-09-23)
------------------
* Fix license labeling, I put BSD, but the license has always been MIT...
* Added Microsoft Visual Studio 2010 project to make compiling on Windows easier.
* Implemented Serial::available() for Windows
* Update how custom baudrates are handled on OS X
This is taken from the example serial program on Apple's developer website, see:
http://free-pascal-general.1045716.n5.nabble.com/Non-standard-baud-rates-in-OS-X-IOSSIOSPEED-IOCTL-td4699923.html
* Timout settings are now applied by reconfigurePort
* Pass LPCWSTR to CreateFile in Windows impl
* Use wstring for ``port_`` type in Windows impl
1.1.4 (2013-06-12 00:13:18 -0600)
---------------------------------
* Timing calculation fix for read and write.
Fixes `#27 <https://github.com/wjwwood/serial/issues/27>`_
* Update list of exceptions thrown from constructor.
* fix, by Thomas Hoppe <thomas.hoppe@cesys.com>
For SerialException's:
* The name was misspelled...
* Use std::string's for error messages to prevent corruption of messages on some platforms
* alloca.h does not exist on OpenBSD either.
1.1.3 (2013-01-09 10:54:34 -0800)
---------------------------------
* Install headers
1.1.2 (2012-12-14 14:08:55 -0800)
---------------------------------
* Fix buildtool depends
1.1.1 (2012-12-03)
------------------
* Removed rt linking on OS X. Fixes `#24 <https://github.com/wjwwood/serial/issues/24>`_.
1.1.0 (2012-10-24)
------------------
* Previous history is unstructured and therefore has been truncated. See the commit messages for more info.

View File

@ -1,17 +1,81 @@
cmake_minimum_required(VERSION 2.4.6) cmake_minimum_required(VERSION 2.8.3)
project(serial)
set(ROS_ROOT $ENV{ROS_ROOT}) # Find catkin
find_package(catkin REQUIRED)
option(SERIAL_BUILD_WIHOUT_ROS "Build without ROS?" OFF) if(APPLE)
find_library(IOKIT_LIBRARY IOKit)
find_library(FOUNDATION_LIBRARY Foundation)
endif()
if(DEFINED ROS_ROOT AND NOT SERIAL_BUILD_WIHOUT_ROS) if(UNIX AND NOT APPLE)
# Build with ROS # If Linux, add rt and pthread
include(serial_ros.cmake) set(rt_LIBRARIES rt)
message("Building with ROS") set(pthread_LIBRARIES pthread)
build_serial() catkin_package(
else(DEFINED ROS_ROOT AND NOT SERIAL_BUILD_WIHOUT_ROS) LIBRARIES ${PROJECT_NAME}
# Build normally INCLUDE_DIRS include
include(serial.cmake) DEPENDS rt pthread
message("Building stand alone") )
build_serial() else()
endif(DEFINED ROS_ROOT AND NOT SERIAL_BUILD_WIHOUT_ROS) # Otherwise normal call
catkin_package(
LIBRARIES ${PROJECT_NAME}
INCLUDE_DIRS include
)
endif()
## Sources
set(serial_SRCS
src/serial.cc
include/serial/serial.h
include/serial/v8stdint.h
)
if(APPLE)
# If OSX
list(APPEND serial_SRCS src/impl/unix.cc)
list(APPEND serial_SRCS src/impl/list_ports/list_ports_osx.cc)
elseif(UNIX)
# If unix
list(APPEND serial_SRCS src/impl/unix.cc)
list(APPEND serial_SRCS src/impl/list_ports/list_ports_linux.cc)
else()
# If windows
list(APPEND serial_SRCS src/impl/win.cc)
list(APPEND serial_SRCS src/impl/list_ports/list_ports_win.cc)
endif()
## Add serial library
add_library(${PROJECT_NAME} ${serial_SRCS})
if(APPLE)
target_link_libraries(${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
elseif(UNIX)
target_link_libraries(${PROJECT_NAME} rt pthread)
else()
target_link_libraries(${PROJECT_NAME} setupapi)
endif()
## Uncomment for example
add_executable(serial_example examples/serial_example.cc)
add_dependencies(serial_example ${PROJECT_NAME})
target_link_libraries(serial_example ${PROJECT_NAME})
## Include headers
include_directories(include)
## Install executable
install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
## Install headers
install(FILES include/serial/serial.h include/serial/v8stdint.h
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}/serial)
## Tests
if(CATKIN_ENABLE_TESTING)
add_subdirectory(tests)
endif()

View File

@ -1,15 +0,0 @@
find_path(serial_INCLUDE_DIRS serial/serial.h /usr/include
/usr/local/include "$ENV{NAMER_ROOT}")
find_library(serial_LIBRARIES serial /usr/lib /usr/local/lib
"$ENV{NAMER_ROOT}")
set(serial_FOUND TRUE)
if (NOT serial_INCLUDE_DIRS)
set(serial_FOUND FALSE)
endif (NOT serial_INCLUDE_DIRS)
if (NOT serial_LIBRARIES)
set(serial_FOUND FALSE)
endif (NOT serial_LIBRARIES)

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright (c) 2012 William Woodall, John Harrison
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,5 +1,62 @@
ifdef ROS_ROOT all: serial
include $(shell rospack find mk)/cmake.mk
CMAKE_FLAGS := -DCMAKE_INSTALL_PREFIX=/tmp/usr/local
UNAME := $(shell uname -s)
install_deps:
ifeq ($(UNAME),Darwin)
brew tap ros/deps
brew update
brew outdated boost || brew upgrade boost || brew install boost
brew outdated python || brew upgrade python || brew install python
sudo -H python2 -m pip install -U pip setuptools
sudo -H python2 -m pip install --force-reinstall --no-deps -U pip
sudo -H python2 -m pip install rosinstall_generator wstool rosdep empy catkin_pkg
sudo -H rosdep init
rosdep update
mkdir catkin_ws
cd catkin_ws && rosinstall_generator catkin --rosdistro hydro --tar > catkin.rosinstall
cd catkin_ws && wstool init src catkin.rosinstall
cd catkin_ws && rosdep install --from-paths src --ignore-src -y
cd catkin_ws && python2 ./src/catkin/bin/catkin_make -DPYTHON_EXECUTABLE=`which python2` install
echo "source catkin_ws/install/setup.bash" > setup.bash
else else
include serial.makefile sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu precise main" > /etc/apt/sources.list.d/ros-latest.list'
wget http://packages.ros.org/ros.key -O - | sudo apt-key add -
sudo apt-get update
sudo apt-get install ros-hydro-catkin libboost-dev
echo "source /opt/ros/hydro/setup.bash" > setup.bash
endif
install:
cd build && make install
serial:
@mkdir -p build
cd build && cmake $(CMAKE_FLAGS) ..
ifneq ($(MAKE),)
cd build && $(MAKE)
else
cd build && make
endif
.PHONY: clean
clean:
rm -rf build
.PHONY: doc
doc:
@doxygen doc/Doxyfile
ifeq ($(UNAME),Darwin)
@open doc/html/index.html
endif
.PHONY: test
test:
@mkdir -p build
cd build && cmake $(CMAKE_FLAGS) ..
ifneq ($(MAKE),)
cd build && $(MAKE) run_tests
else
cd build && make run_tests
endif endif

106
README.md
View File

@ -1,91 +1,63 @@
# Serial Communication Library # Serial Communication Library
## Documentation [![Build Status](https://travis-ci.org/wjwwood/serial.svg?branch=master)](https://travis-ci.org/wjwwood/serial)*(Linux and OS X)* [![Build Status](https://ci.appveyor.com/api/projects/status/github/wjwwood/serial)](https://ci.appveyor.com/project/wjwwood/serial)*(Windows)*
Coming Soon! This is a cross-platform library for interfacing with rs-232 serial like ports written in C++. It provides a modern C++ interface with a workflow designed to look and feel like PySerial, but with the speed and control provided by C++.
## Dependencies This library is in use in several robotics related projects and can be built and installed to the OS like most unix libraries with make and then sudo make install, but because it is a catkin project it can also be built along side other catkin projects in a catkin workspace.
* CMake, for the build system: http://www.cmake.org/ Serial is a class that provides the basic interface common to serial libraries (open, close, read, write, etc..) and requires no extra dependencies. It also provides tight control over timeouts and control over handshaking lines.
* (Optional) ROS
## Stand Alone Installation ### Documentation
Get the source: Website: http://wjwwood.github.io/serial/
git clone git://github.com/wjwwood/serial.git API Documentation: http://wjwwood.github.io/serial/doc/1.1.0/index.html
cd serial
Compile the code: ### Dependencies
Required:
* [catkin](http://www.ros.org/wiki/catkin) - cmake and Python based buildsystem
* [cmake](http://www.cmake.org) - buildsystem
* [Python](http://www.python.org) - scripting language
* [empy](http://www.alcyone.com/pyos/empy/) - Python templating library
* [catkin_pkg](http://pypi.python.org/pypi/catkin_pkg/) - Runtime Python library for catkin
Optional (for documentation):
* [Doxygen](http://www.doxygen.org/) - Documentation generation tool
* [graphviz](http://www.graphviz.org/) - Graph visualization software
### Install
Get the code:
git clone https://github.com/wjwwood/serial.git
Build:
make make
Or run cmake youself: Build and run the tests:
mkdir build && cd build make test
cmake ..
make
Install the code (UNIX):
make
sudo make install
Uninstall the code (UNIX):
make
sudo make uninstall
Build the documentation: Build the documentation:
Comming Soon! make doc
## Using within ROS workspace Install:
Setup workspace (skip if you already have one): make install
mkdir some_ros_workspace ### License
rosws init some_ros_workspace
cd some_ros_workspace
source setup.bash
Add the rosinstall entry for this stack: [The MIT License](LICENSE)
echo "- git: {local-name: serial, uri: 'https://github.com/wjwwood/serial.git', version: 'master'}" >> .rosinstall
Rerun rosinstall (note this will change your $ROS_PACKAGE_PATH):
rosinstall .
source setup.bash
Build the unary stack: ### Authors
rosmake serial --rosdep-install William Woodall <wjwwood@gmail.com>
John Harrison <ash.gti@gmail.com>
Run the example: ### Contact
rosrun serial serial_example William Woodall <william@osrfoundation.org>
## License
The BSD License
Copyright (c) 2011 William Woodall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

10
changes.txt Normal file
View File

@ -0,0 +1,10 @@
# 1.1.0 10-24-2012
* Converted the build system to catkin
# v1.0.1 8-27-2012
* Added baudrates: 1000000, 11520000, 2000000, 2500000, 3000000, 3500000, and 4000000
* Linking related bug fixes on Linux and OS X
* Custom baudrate bug fix. Closes issue #18.

File diff suppressed because it is too large Load Diff

58
doc/serial.dox Normal file
View File

@ -0,0 +1,58 @@
/*!
\mainpage Serial Library
\author William Woodall <wjwwood@gmail.com>, John Harrison <ash@greaterthaninfinity.com>
\section what_is What is serial?
Serial is a cross-platform, simple to use library for using serial ports on computers. This library provides a C++, object oriented interface for interacting with RS-232 like devices on Linux and Windows.
Want to use it with ROS(Robot Operating System)? No problem, it compiles as a unary stack.
\section getting_started Getting Started
Ready to jump in?
- Checkout our examples: \ref serial_example.cc
- Look at the main class documentation: \ref serial::Serial
\section features Features
- Linux, Mac OS X, and Windows Support
- Easy to use interface (modeled after PySerial)
- Minimal dependencies (cmake)
- Complete timeout control \ref serial::Serial::setTimeout
- Check and set handshaking lines (CTS, DSR, RI, CD and RTS, DTR)
- Block for changes in handshaking lines (Linux and Windows)
- Flush I/O separately and block until all writing done
\section install Installation
\subsection Dependencies
- CMake:
- CMake is required for building the system and can be located here: http://www.cmake.org/
\subsection building Compiling
Once you have gathered the dependencies, you need to checkout the software from github.com:
<pre>
git clone git://github.com/wjwwood/serial.git
</pre>
Once you have checked out the source code from github.com you can enter the directory and build the software.
<pre>
cd serial
make
make test # (optional) builds the example and tests, and runs the tests.
make doc # (optional) builds _this_ documentation.
</pre>
\subsection installing Installing
To install simply:
<pre>
sudo make install
</pre>
*/

View File

@ -2,13 +2,13 @@
* This example expects the serial port has a loopback on it. * This example expects the serial port has a loopback on it.
* *
* Alternatively, you could use an Arduino: * Alternatively, you could use an Arduino:
* *
* <pre> * <pre>
* setup() { * void setup() {
* Serial.begin(<insert your baudrate here>); * Serial.begin(<insert your baudrate here>);
* } * }
* *
* loop() { * void loop() {
* if (Serial.available()) { * if (Serial.available()) {
* Serial.write(Serial.read()); * Serial.write(Serial.read());
* } * }
@ -21,7 +21,7 @@
#include <cstdio> #include <cstdio>
// OS Specific sleep // OS Specific sleep
#ifdef __WIN32__ #ifdef _WIN32
#include <windows.h> #include <windows.h>
#else #else
#include <unistd.h> #include <unistd.h>
@ -34,31 +34,66 @@ using std::exception;
using std::cout; using std::cout;
using std::cerr; using std::cerr;
using std::endl; using std::endl;
using std::vector;
void my_sleep(unsigned long milliseconds) { void my_sleep(unsigned long milliseconds) {
#ifdef __WIN32__ #ifdef _WIN32
Sleep(milliseconds); // 100 ms Sleep(milliseconds); // 100 ms
#else #else
usleep(milliseconds*1000); // 100 ms usleep(milliseconds*1000); // 100 ms
#endif #endif
} }
void enumerate_ports()
{
vector<serial::PortInfo> devices_found = serial::list_ports();
vector<serial::PortInfo>::iterator iter = devices_found.begin();
while( iter != devices_found.end() )
{
serial::PortInfo device = *iter++;
printf( "(%s, %s, %s)\n", device.port.c_str(), device.description.c_str(),
device.hardware_id.c_str() );
}
}
void print_usage()
{
cerr << "Usage: test_serial {-e|<serial port address>} ";
cerr << "<baudrate> [test string]" << endl;
}
int run(int argc, char **argv) int run(int argc, char **argv)
{ {
if(argc < 3) { if(argc < 2) {
cerr << "Usage: test_serial <serial port address> "; print_usage();
cerr << "<baudrate> [test string]" << endl;
return 0; return 0;
} }
// Argument 1 is the serial port
// Argument 1 is the serial port or enumerate flag
string port(argv[1]); string port(argv[1]);
if( port == "-e" ) {
enumerate_ports();
return 0;
}
else if( argc < 3 ) {
print_usage();
return 1;
}
// Argument 2 is the baudrate // Argument 2 is the baudrate
unsigned long baud = 0; unsigned long baud = 0;
#if defined(WIN32) && !defined(__MINGW32__)
sscanf_s(argv[2], "%lu", &baud);
#else
sscanf(argv[2], "%lu", &baud); sscanf(argv[2], "%lu", &baud);
#endif
// port, baudrate, timeout in milliseconds // port, baudrate, timeout in milliseconds
serial::Serial my_serial(port, baud, 1000); serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
cout << "Is the serial port open?"; cout << "Is the serial port open?";
if(my_serial.isOpen()) if(my_serial.isOpen())
@ -90,7 +125,7 @@ int run(int argc, char **argv)
} }
// Test the timeout at 250ms // Test the timeout at 250ms
my_serial.setTimeout(250); my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
count = 0; count = 0;
cout << "Timeout == 250ms, asking for 1 more byte than written." << endl; cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
while (count < 10) { while (count < 10) {

View File

@ -8,9 +8,9 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2011 William Woodall, John Harrison * Copyright (c) 2012 William Woodall, John Harrison
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"), * copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation * to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, * the rights to use, copy, modify, merge, publish, distribute, sublicense,
@ -24,8 +24,8 @@
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE. * DEALINGS IN THE SOFTWARE.
* *
* \section DESCRIPTION * \section DESCRIPTION
@ -35,6 +35,8 @@
* *
*/ */
#if !defined(_WIN32)
#ifndef SERIAL_IMPL_UNIX_H #ifndef SERIAL_IMPL_UNIX_H
#define SERIAL_IMPL_UNIX_H #define SERIAL_IMPL_UNIX_H
@ -44,17 +46,27 @@
namespace serial { namespace serial {
using std::size_t;
using std::string; using std::string;
using std::invalid_argument; using std::invalid_argument;
using serial::SerialExecption; using serial::SerialException;
using serial::IOException; using serial::IOException;
class MillisecondTimer {
public:
MillisecondTimer(const uint32_t millis);
int64_t remaining();
private:
static timespec timespec_now();
timespec expiry;
};
class serial::Serial::SerialImpl { class serial::Serial::SerialImpl {
public: public:
SerialImpl (const string &port, SerialImpl (const string &port,
unsigned long baudrate, unsigned long baudrate,
long timeout,
bytesize_t bytesize, bytesize_t bytesize,
parity_t parity, parity_t parity,
stopbits_t stopbits, stopbits_t stopbits,
@ -74,11 +86,17 @@ public:
size_t size_t
available (); available ();
size_t bool
read (unsigned char* buf, size_t size = 1); waitReadable (uint32_t timeout);
void
waitByteTimes (size_t count);
size_t size_t
write (const string &data); read (uint8_t *buf, size_t size = 1);
size_t
write (const uint8_t *data, size_t length);
void void
flush (); flush ();
@ -90,28 +108,31 @@ public:
flushOutput (); flushOutput ();
void void
sendBreak(int duration); sendBreak (int duration);
void void
setBreak(bool level); setBreak (bool level);
void void
setRTS(bool level); setRTS (bool level);
void void
setDTR(bool level); setDTR (bool level);
bool bool
getCTS(); waitForChange ();
bool bool
getDSR(); getCTS ();
bool bool
getRI(); getDSR ();
bool bool
getCD(); getRI ();
bool
getCD ();
void void
setPort (const string &port); setPort (const string &port);
@ -120,9 +141,9 @@ public:
getPort () const; getPort () const;
void void
setTimeout (long timeout); setTimeout (Timeout &timeout);
long Timeout
getTimeout () const; getTimeout () const;
void void
@ -156,16 +177,16 @@ public:
getFlowcontrol () const; getFlowcontrol () const;
void void
readLock(); readLock ();
void void
readUnlock(); readUnlock ();
void void
writeLock(); writeLock ();
void void
writeUnlock(); writeUnlock ();
protected: protected:
void reconfigurePort (); void reconfigurePort ();
@ -178,8 +199,9 @@ private:
bool xonxoff_; bool xonxoff_;
bool rtscts_; bool rtscts_;
long timeout_; // Timeout for read operations Timeout timeout_; // Timeout for read operations
unsigned long baudrate_; // Baudrate unsigned long baudrate_; // Baudrate
uint32_t byte_time_ns_; // Nanoseconds to transmit/receive a single byte
parity_t parity_; // Parity parity_t parity_; // Parity
bytesize_t bytesize_; // Size of the bytes bytesize_t bytesize_; // Size of the bytes
@ -195,3 +217,5 @@ private:
} }
#endif // SERIAL_IMPL_UNIX_H #endif // SERIAL_IMPL_UNIX_H
#endif // !defined(_WIN32)

View File

@ -8,9 +8,9 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2011 William Woodall, John Harrison * Copyright (c) 2012 William Woodall, John Harrison
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"), * copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation * to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, * the rights to use, copy, modify, merge, publish, distribute, sublicense,
@ -24,8 +24,8 @@
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE. * DEALINGS IN THE SOFTWARE.
* *
* \section DESCRIPTION * \section DESCRIPTION
@ -34,24 +34,28 @@
* *
*/ */
#if defined(_WIN32)
#ifndef SERIAL_IMPL_WINDOWS_H #ifndef SERIAL_IMPL_WINDOWS_H
#define SERIAL_IMPL_WINDOWS_H #define SERIAL_IMPL_WINDOWS_H
#include "serial/serial.h" #include "serial/serial.h"
#include "windows.h"
namespace serial { namespace serial {
using std::string; using std::string;
using std::wstring;
using std::invalid_argument; using std::invalid_argument;
using serial::SerialExecption; using serial::SerialException;
using serial::IOException; using serial::IOException;
class serial::Serial::SerialImpl { class serial::Serial::SerialImpl {
public: public:
SerialImpl (const string &port, SerialImpl (const string &port,
unsigned long baudrate, unsigned long baudrate,
long timeout,
bytesize_t bytesize, bytesize_t bytesize,
parity_t parity, parity_t parity,
stopbits_t stopbits, stopbits_t stopbits,
@ -70,12 +74,18 @@ public:
size_t size_t
available (); available ();
bool
waitReadable (uint32_t timeout);
void
waitByteTimes (size_t count);
size_t size_t
read (char* buf, size_t size = 1); read (uint8_t *buf, size_t size = 1);
size_t size_t
write (const string &data); write (const uint8_t *data, size_t length);
void void
flush (); flush ();
@ -87,50 +97,53 @@ public:
flushOutput (); flushOutput ();
void void
sendBreak(int duration); sendBreak (int duration);
void void
setBreak(bool level); setBreak (bool level);
void void
setRTS(bool level); setRTS (bool level);
void void
setDTR(bool level); setDTR (bool level);
bool bool
getCTS(); waitForChange ();
bool bool
getDSR(); getCTS ();
bool bool
getRI(); getDSR ();
bool bool
getCD(); getRI ();
bool
getCD ();
void void
setPort (const string &port); setPort (const string &port);
string string
getPort () const; getPort () const;
void void
setTimeout (long timeout); setTimeout (Timeout &timeout);
long Timeout
getTimeout () const; getTimeout () const;
void void
setBaudrate (unsigned long baudrate); setBaudrate (unsigned long baudrate);
unsigned long unsigned long
getBaudrate () const; getBaudrate () const;
void void
setBytesize (bytesize_t bytesize); setBytesize (bytesize_t bytesize);
bytesize_t bytesize_t
getBytesize () const; getBytesize () const;
@ -152,26 +165,43 @@ public:
flowcontrol_t flowcontrol_t
getFlowcontrol () const; getFlowcontrol () const;
void
readLock ();
void
readUnlock ();
void
writeLock ();
void
writeUnlock ();
protected: protected:
void reconfigurePort (); void reconfigurePort ();
private: private:
string port_; // Path to the file descriptor wstring port_; // Path to the file descriptor
int fd_; // The current file descriptor HANDLE fd_;
bool isOpen_; bool is_open_;
bool xonxoff_;
bool rtscts_;
long timeout_; // Timeout for read operations Timeout timeout_; // Timeout for read operations
unsigned long baudrate_; // Baudrate unsigned long baudrate_; // Baudrate
parity_t parity_; // Parity parity_t parity_; // Parity
bytesize_t bytesize_; // Size of the bytes bytesize_t bytesize_; // Size of the bytes
stopbits_t stopbits_; // Stop Bits stopbits_t stopbits_; // Stop Bits
flowcontrol_t flowcontrol_; // Flow Control flowcontrol_t flowcontrol_; // Flow Control
// Mutex used to lock the read functions
HANDLE read_mutex;
// Mutex used to lock the write functions
HANDLE write_mutex;
}; };
} }
#endif // SERIAL_IMPL_WINDOWS_H #endif // SERIAL_IMPL_WINDOWS_H
#endif // if defined(_WIN32)

View File

@ -8,7 +8,7 @@
* *
* The MIT License * The MIT License
* *
* Copyright (c) 2011 William Woodall * Copyright (c) 2012 William Woodall
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"), * copy of this software and associated documentation files (the "Software"),
@ -38,12 +38,15 @@
#include <limits> #include <limits>
#include <vector> #include <vector>
#include <string.h> #include <string>
#include <cstring>
#include <sstream> #include <sstream>
#include <exception> #include <exception>
#include <stdexcept> #include <stdexcept>
#include <serial/v8stdint.h>
#define THROW(exceptionClass, message) throw exceptionClass(__FILE__, __LINE__, (message) ) #define THROW(exceptionClass, message) throw exceptionClass(__FILE__, \
__LINE__, (message) )
namespace serial { namespace serial {
@ -63,7 +66,9 @@ typedef enum {
typedef enum { typedef enum {
parity_none = 0, parity_none = 0,
parity_odd = 1, parity_odd = 1,
parity_even = 2 parity_even = 2,
parity_mark = 3,
parity_space = 4
} parity_t; } parity_t;
/*! /*!
@ -71,8 +76,8 @@ typedef enum {
*/ */
typedef enum { typedef enum {
stopbits_one = 1, stopbits_one = 1,
stopbits_one_point_five, stopbits_two = 2,
stopbits_two = 2 stopbits_one_point_five
} stopbits_t; } stopbits_t;
/*! /*!
@ -80,30 +85,79 @@ typedef enum {
*/ */
typedef enum { typedef enum {
flowcontrol_none = 0, flowcontrol_none = 0,
flowcontrol_software flowcontrol_software,
flowcontrol_hardware
} flowcontrol_t; } flowcontrol_t;
/*!
* Structure for setting the timeout of the serial port, times are
* in milliseconds.
*
* In order to disable the interbyte timeout, set it to Timeout::max().
*/
struct Timeout {
#ifdef max
# undef max
#endif
static uint32_t max() {return std::numeric_limits<uint32_t>::max();}
/*!
* Convenience function to generate Timeout structs using a
* single absolute timeout.
*
* \param timeout A long that defines the time in milliseconds until a
* timeout occurs after a call to read or write is made.
*
* \return Timeout struct that represents this simple timeout provided.
*/
static Timeout simpleTimeout(uint32_t timeout) {
return Timeout(max(), timeout, 0, timeout, 0);
}
/*! Number of milliseconds between bytes received to timeout on. */
uint32_t inter_byte_timeout;
/*! A constant number of milliseconds to wait after calling read. */
uint32_t read_timeout_constant;
/*! A multiplier against the number of requested bytes to wait after
* calling read.
*/
uint32_t read_timeout_multiplier;
/*! A constant number of milliseconds to wait after calling write. */
uint32_t write_timeout_constant;
/*! A multiplier against the number of requested bytes to wait after
* calling write.
*/
uint32_t write_timeout_multiplier;
explicit Timeout (uint32_t inter_byte_timeout_=0,
uint32_t read_timeout_constant_=0,
uint32_t read_timeout_multiplier_=0,
uint32_t write_timeout_constant_=0,
uint32_t write_timeout_multiplier_=0)
: inter_byte_timeout(inter_byte_timeout_),
read_timeout_constant(read_timeout_constant_),
read_timeout_multiplier(read_timeout_multiplier_),
write_timeout_constant(write_timeout_constant_),
write_timeout_multiplier(write_timeout_multiplier_)
{}
};
/*! /*!
* Class that provides a portable serial port interface. * Class that provides a portable serial port interface.
*/ */
class Serial { class Serial {
public: public:
/*! /*!
* Constructor, creates a SerialPortBoost object and opens the port. * Creates a Serial object and opens the port if a port is specified,
* otherwise it remains closed until serial::Serial::open is called.
* *
* \param port A std::string containing the address of the serial port, * \param port A std::string containing the address of the serial port,
* which would be something like 'COM1' on Windows and '/dev/ttyS0' * which would be something like 'COM1' on Windows and '/dev/ttyS0'
* on Linux. * on Linux.
* *
* \param baudrate An integer that represents the buadrate * \param baudrate An unsigned 32-bit integer that represents the baudrate
* *
* \param timeout A long that represents the time (in milliseconds) until a * \param timeout A serial::Timeout struct that defines the timeout
* timeout on reads occur. Setting this to zero (0) will cause reading to * conditions for the serial port. \see serial::Timeout
* be non-blocking, i.e. the available data will be returned immediately,
* but it will not block to wait for more. Setting this to a number less
* than zero (-1) will result in infinite blocking behaviour, i.e. the
* serial port will block until either size bytes have been read or an
* exception has occured.
* *
* \param bytesize Size of each byte in the serial transmission of data, * \param bytesize Size of each byte in the serial transmission of data,
* default is eightbits, possible values are: fivebits, sixbits, sevenbits, * default is eightbits, possible values are: fivebits, sixbits, sevenbits,
@ -119,14 +173,13 @@ public:
* flowcontrol_none, possible values are: flowcontrol_none, * flowcontrol_none, possible values are: flowcontrol_none,
* flowcontrol_software, flowcontrol_hardware * flowcontrol_software, flowcontrol_hardware
* *
* \param buffer_size The maximum size of the internal buffer, defaults * \throw serial::PortNotOpenedException
* to 256 bytes (2^8). * \throw serial::IOException
* * \throw std::invalid_argument
* \throw PortNotOpenedException
*/ */
Serial (const std::string &port = "", Serial (const std::string &port = "",
unsigned long baudrate = 9600, uint32_t baudrate = 9600,
long timeout = 0, Timeout timeout = Timeout(),
bytesize_t bytesize = eightbits, bytesize_t bytesize = eightbits,
parity_t parity = parity_none, parity_t parity = parity_none,
stopbits_t stopbits = stopbits_one, stopbits_t stopbits = stopbits_one,
@ -136,8 +189,8 @@ public:
virtual ~Serial (); virtual ~Serial ();
/*! /*!
* Opens the serial port as long as the portname is set and the port isn't * Opens the serial port as long as the port is set and the port isn't
* alreay open. * already open.
* *
* If the port is provided to the constructor then an explicit call to open * If the port is provided to the constructor then an explicit call to open
* is not needed. * is not needed.
@ -145,7 +198,7 @@ public:
* \see Serial::Serial * \see Serial::Serial
* *
* \throw std::invalid_argument * \throw std::invalid_argument
* \throw serial::SerialExecption * \throw serial::SerialException
* \throw serial::IOException * \throw serial::IOException
*/ */
void void
@ -164,28 +217,111 @@ public:
/*! Return the number of characters in the buffer. */ /*! Return the number of characters in the buffer. */
size_t size_t
available(); available ();
/*! Read a given amount of bytes from the serial port. /*! Block until there is serial data to read or read_timeout_constant
* number of milliseconds have elapsed. The return value is true when
* the function exits with the port in a readable state, false otherwise
* (due to timeout or select interruption). */
bool
waitReadable ();
/*! Block for a period of time corresponding to the transmission time of
* count characters at present serial settings. This may be used in con-
* junction with waitReadable to read larger blocks of data from the
* port. */
void
waitByteTimes (size_t count);
/*! Read a given amount of bytes from the serial port into a given buffer.
* *
* If a timeout is set it may return less characters than requested. With * The read function will return in one of three cases:
* no timeout it will block until the requested number of bytes have been * * The number of requested bytes was read.
* read or until an exception occurs. * * In this case the number of bytes requested will match the size_t
* returned by read.
* * A timeout occurred, in this case the number of bytes read will not
* match the amount requested, but no exception will be thrown. One of
* two possible timeouts occurred:
* * The inter byte timeout expired, this means that number of
* milliseconds elapsed between receiving bytes from the serial port
* exceeded the inter byte timeout.
* * The total timeout expired, which is calculated by multiplying the
* read timeout multiplier by the number of requested bytes and then
* added to the read timeout constant. If that total number of
* milliseconds elapses after the initial call to read a timeout will
* occur.
* * An exception occurred, in this case an actual exception will be thrown.
*
* \param buffer An uint8_t array of at least the requested size.
* \param size A size_t defining how many bytes to be read.
*
* \return A size_t representing the number of bytes read as a result of the
* call to read.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
size_t
read (uint8_t *buffer, size_t size);
/*! Read a given amount of bytes from the serial port into a give buffer.
*
* \param buffer A reference to a std::vector of uint8_t.
* \param size A size_t defining how many bytes to be read.
*
* \return A size_t representing the number of bytes read as a result of the
* call to read.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
size_t
read (std::vector<uint8_t> &buffer, size_t size = 1);
/*! Read a given amount of bytes from the serial port into a give buffer.
*
* \param buffer A reference to a std::string.
* \param size A size_t defining how many bytes to be read.
*
* \return A size_t representing the number of bytes read as a result of the
* call to read.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
size_t
read (std::string &buffer, size_t size = 1);
/*! Read a given amount of bytes from the serial port and return a string
* containing the data.
* *
* \param size A size_t defining how many bytes to be read. * \param size A size_t defining how many bytes to be read.
* *
* \return A std::string containing the data read. * \return A std::string containing the data read from the port.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/ */
size_t
read (unsigned char *buffer, size_t size);
size_t
read (std::vector<unsigned char> &buffer, size_t size = 1);
size_t
read (std::string &buffer, size_t size = 1);
std::string std::string
read (size_t size = 1); read (size_t size = 1);
/*! Reads in a line or until a given delimiter has been processed /*! Reads in a line or until a given delimiter has been processed.
*
* Reads from the serial port until a single line has been read.
*
* \param buffer A std::string reference used to store the data.
* \param size A maximum length of a line, defaults to 65536 (2^16)
* \param eol A string to match against for the EOL.
*
* \return A size_t representing the number of bytes read.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/
size_t
readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
/*! Reads in a line or until a given delimiter has been processed.
* *
* Reads from the serial port until a single line has been read. * Reads from the serial port until a single line has been read.
* *
@ -193,185 +329,260 @@ public:
* \param eol A string to match against for the EOL. * \param eol A string to match against for the EOL.
* *
* \return A std::string containing the line. * \return A std::string containing the line.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/ */
size_t
readline (std::string &buffer,
size_t size = 65536,
std::string eol = "\n");
std::string std::string
readline (size_t size = 65536, readline (size_t size = 65536, std::string eol = "\n");
std::string eol = "\n");
/*! Reads in multiple lines until the serail port times out. /*! Reads in multiple lines until the serial port times out.
* *
* This requires a timeout > 0 before it can be run. It will read until a * This requires a timeout > 0 before it can be run. It will read until a
* timeout occurs and return a list of strings. * timeout occurs and return a list of strings.
* *
* \param size A maximum length of combined lines, defaults to 65536 (2^16) * \param size A maximum length of combined lines, defaults to 65536 (2^16)
* *
* \param eol A string to match against for the EOL. * \param eol A string to match against for the EOL.
* *
* \return A vector<string> containing the lines. * \return A vector<string> containing the lines.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
*/ */
std::vector<std::string> std::vector<std::string>
readlines (size_t size = 65536, std::string eol = "\n"); readlines (size_t size = 65536, std::string eol = "\n");
/*! Write a string to the serial port. /*! Write a string to the serial port.
* *
* \param data A const reference containg the data to be written * \param data A const reference containing the data to be written
* to the serial port.
*
* \param size A size_t that indicates how many bytes should be written from
* the given data buffer.
*
* \return A size_t representing the number of bytes actually written to
* the serial port.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
* \throw serial::IOException
*/
size_t
write (const uint8_t *data, size_t size);
/*! Write a string to the serial port.
*
* \param data A const reference containing the data to be written
* to the serial port. * to the serial port.
* *
* \return A size_t representing the number of bytes actually written to * \return A size_t representing the number of bytes actually written to
* the serial port. * the serial port.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
* \throw serial::IOException
*/ */
size_t size_t
write (const unsigned char *data, size_t size); write (const std::vector<uint8_t> &data);
size_t
write (const std::vector<unsigned char> &data); /*! Write a string to the serial port.
*
* \param data A const reference containing the data to be written
* to the serial port.
*
* \return A size_t representing the number of bytes actually written to
* the serial port.
*
* \throw serial::PortNotOpenedException
* \throw serial::SerialException
* \throw serial::IOException
*/
size_t size_t
write (const std::string &data); write (const std::string &data);
/*! Sets the serial port identifier. /*! Sets the serial port identifier.
* *
* \param port A const std::string reference containing the address of the * \param port A const std::string reference containing the address of the
* serial port, which would be something like 'COM1' on Windows and * serial port, which would be something like 'COM1' on Windows and
* '/dev/ttyS0' on Linux. * '/dev/ttyS0' on Linux.
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
void void
setPort (const std::string &port); setPort (const std::string &port);
/*! Gets the serial port identifier. /*! Gets the serial port identifier.
* *
* \see Serial::setPort * \see Serial::setPort
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
std::string std::string
getPort () const; getPort () const;
/*! Sets the timeout for reads in milliseconds. /*! Sets the timeout for reads and writes using the Timeout struct.
* *
* \param timeout A long that represents the time (in milliseconds) until a * There are two timeout conditions described here:
* timeout on reads occur. Setting this to zero (0) will cause reading to be * * The inter byte timeout:
* non-blocking, i.e. the available data will be returned immediately, but it * * The inter_byte_timeout component of serial::Timeout defines the
* will not block to wait for more. Setting this to a number less than * maximum amount of time, in milliseconds, between receiving bytes on
* zero (-1) will result in infinite blocking behaviour, i.e. the serial port * the serial port that can pass before a timeout occurs. Setting this
* will block until either size bytes have been read or an exception has * to zero will prevent inter byte timeouts from occurring.
* occured. * * Total time timeout:
*/ * * The constant and multiplier component of this timeout condition,
* for both read and write, are defined in serial::Timeout. This
* timeout occurs if the total time since the read or write call was
* made exceeds the specified time in milliseconds.
* * The limit is defined by multiplying the multiplier component by the
* number of requested bytes and adding that product to the constant
* component. In this way if you want a read call, for example, to
* timeout after exactly one second regardless of the number of bytes
* you asked for then set the read_timeout_constant component of
* serial::Timeout to 1000 and the read_timeout_multiplier to zero.
* This timeout condition can be used in conjunction with the inter
* byte timeout condition with out any problems, timeout will simply
* occur when one of the two timeout conditions is met. This allows
* users to have maximum control over the trade-off between
* responsiveness and efficiency.
*
* Read and write functions will return in one of three cases. When the
* reading or writing is complete, when a timeout occurs, or when an
* exception occurs.
*
* A timeout of 0 enables non-blocking mode.
*
* \param timeout A serial::Timeout struct containing the inter byte
* timeout, and the read and write timeout constants and multipliers.
*
* \see serial::Timeout
*/
void void
setTimeout (long timeout); setTimeout (Timeout &timeout);
/*! Sets the timeout for reads and writes. */
void
setTimeout (uint32_t inter_byte_timeout, uint32_t read_timeout_constant,
uint32_t read_timeout_multiplier, uint32_t write_timeout_constant,
uint32_t write_timeout_multiplier)
{
Timeout timeout(inter_byte_timeout, read_timeout_constant,
read_timeout_multiplier, write_timeout_constant,
write_timeout_multiplier);
return setTimeout(timeout);
}
/*! Gets the timeout for reads in seconds. /*! Gets the timeout for reads in seconds.
* *
* \see Serial::setTimeout * \return A Timeout struct containing the inter_byte_timeout, and read
*/ * and write timeout constants and multipliers.
long *
* \see Serial::setTimeout
*/
Timeout
getTimeout () const; getTimeout () const;
/*! Sets the baudrate for the serial port. /*! Sets the baudrate for the serial port.
* *
* Possible baudrates depends on the system but some safe baudrates include: * Possible baudrates depends on the system but some safe baudrates include:
* 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000, * 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000,
* 57600, 115200 * 57600, 115200
* Some other baudrates that are supported by some comports: * Some other baudrates that are supported by some comports:
* 128000, 153600, 230400, 256000, 460800, 921600 * 128000, 153600, 230400, 256000, 460800, 500000, 921600
* *
* \param baudrate An integer that sets the baud rate for the serial port. * \param baudrate An integer that sets the baud rate for the serial port.
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
void void
setBaudrate (unsigned long baudrate); setBaudrate (uint32_t baudrate);
/*! Gets the baudrate for the serial port. /*! Gets the baudrate for the serial port.
* *
* \return An integer that sets the baud rate for the serial port. * \return An integer that sets the baud rate for the serial port.
* *
* \see Serial::setBaudrate * \see Serial::setBaudrate
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
unsigned long uint32_t
getBaudrate () const; getBaudrate () const;
/*! Sets the bytesize for the serial port. /*! Sets the bytesize for the serial port.
* *
* \param bytesize Size of each byte in the serial transmission of data, * \param bytesize Size of each byte in the serial transmission of data,
* default is eightbits, possible values are: fivebits, sixbits, sevenbits, * default is eightbits, possible values are: fivebits, sixbits, sevenbits,
* eightbits * eightbits
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
void void
setBytesize (bytesize_t bytesize); setBytesize (bytesize_t bytesize);
/*! Gets the bytesize for the serial port. /*! Gets the bytesize for the serial port.
* *
* \see Serial::setBytesize * \see Serial::setBytesize
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
bytesize_t bytesize_t
getBytesize () const; getBytesize () const;
/*! Sets the parity for the serial port. /*! Sets the parity for the serial port.
* *
* \param parity Method of parity, default is parity_none, possible values * \param parity Method of parity, default is parity_none, possible values
* are: parity_none, parity_odd, parity_even * are: parity_none, parity_odd, parity_even
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
void void
setParity (parity_t parity); setParity (parity_t parity);
/*! Gets the parity for the serial port. /*! Gets the parity for the serial port.
* *
* \see Serial::setParity * \see Serial::setParity
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
parity_t parity_t
getParity () const; getParity () const;
/*! Sets the stopbits for the serial port. /*! Sets the stopbits for the serial port.
* *
* \param stopbits Number of stop bits used, default is stopbits_one, * \param stopbits Number of stop bits used, default is stopbits_one,
* possible values are: stopbits_one, stopbits_one_point_five, stopbits_two * possible values are: stopbits_one, stopbits_one_point_five, stopbits_two
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
void void
setStopbits (stopbits_t stopbits); setStopbits (stopbits_t stopbits);
/*! Gets the stopbits for the serial port. /*! Gets the stopbits for the serial port.
* *
* \see Serial::setStopbits * \see Serial::setStopbits
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
stopbits_t stopbits_t
getStopbits () const; getStopbits () const;
/*! Sets the flow control for the serial port. /*! Sets the flow control for the serial port.
* *
* \param flowcontrol Type of flowcontrol used, default is flowcontrol_none, * \param flowcontrol Type of flowcontrol used, default is flowcontrol_none,
* possible values are: flowcontrol_none, flowcontrol_software, * possible values are: flowcontrol_none, flowcontrol_software,
* flowcontrol_hardware * flowcontrol_hardware
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
void void
setFlowcontrol (flowcontrol_t flowcontrol); setFlowcontrol (flowcontrol_t flowcontrol);
/*! Gets the flow control for the serial port. /*! Gets the flow control for the serial port.
* *
* \see Serial::setFlowcontrol * \see Serial::setFlowcontrol
* *
* \throw InvalidConfigurationException * \throw std::invalid_argument
*/ */
flowcontrol_t flowcontrol_t
getFlowcontrol () const; getFlowcontrol () const;
@ -387,37 +598,59 @@ public:
void void
flushOutput (); flushOutput ();
/*! Sends the RS-232 break signal. See tcsendbreak(3). */
void void
sendBreak (int duration); sendBreak (int duration);
/*! Set the break condition to a given level. Defaults to true. */
void void
setBreak (bool level = true); setBreak (bool level = true);
/*! Set the RTS handshaking line to the given level. Defaults to true. */
void void
setRTS (bool level = true); setRTS (bool level = true);
/*! Set the DTR handshaking line to the given level. Defaults to true. */
void void
setDTR (bool level = true); setDTR (bool level = true);
/*!
* Blocks until CTS, DSR, RI, CD changes or something interrupts it.
*
* Can throw an exception if an error occurs while waiting.
* You can check the status of CTS, DSR, RI, and CD once this returns.
* Uses TIOCMIWAIT via ioctl if available (mostly only on Linux) with a
* resolution of less than +-1ms and as good as +-0.2ms. Otherwise a
* polling method is used which can give +-2ms.
*
* \return Returns true if one of the lines changed, false if something else
* occurred.
*
* \throw SerialException
*/
bool
waitForChange ();
/*! Returns the current status of the CTS line. */
bool bool
getCTS (); getCTS ();
/*! Returns the current status of the DSR line. */
bool bool
getDSR (); getDSR ();
/*! Returns the current status of the RI line. */
bool bool
getRI (); getRI ();
/*! Returns the current status of the CD line. */
bool bool
getCD (); getCD ();
private: private:
// Disable copy constructors // Disable copy constructors
Serial(const Serial&); Serial(const Serial&);
void operator=(const Serial&); Serial& operator=(const Serial&);
const Serial& operator=(Serial);
std::string read_cache_; //!< Cache for doing reads in chunks.
// Pimpl idiom, d_pointer // Pimpl idiom, d_pointer
class SerialImpl; class SerialImpl;
@ -429,70 +662,114 @@ private:
// Read common function // Read common function
size_t size_t
read_ (unsigned char *buffer, size_t size); read_ (uint8_t *buffer, size_t size);
// Write common function
size_t
write_ (const uint8_t *data, size_t length);
}; };
class SerialExecption : public std::exception class SerialException : public std::exception
{ {
const char* e_what_; // Disable copy constructors
SerialException& operator=(const SerialException&);
std::string e_what_;
public: public:
SerialExecption (const char *description) : e_what_ (description) {} SerialException (const char *description) {
std::stringstream ss;
virtual const char* what () const throw () ss << "SerialException " << description << " failed.";
{ e_what_ = ss.str();
std::stringstream ss; }
ss << "SerialException " << e_what_ << " failed."; SerialException (const SerialException& other) : e_what_(other.e_what_) {}
return ss.str ().c_str (); virtual ~SerialException() throw() {}
virtual const char* what () const throw () {
return e_what_.c_str();
} }
}; };
class IOException : public std::exception class IOException : public std::exception
{ {
// Disable copy constructors
IOException& operator=(const IOException&);
std::string file_; std::string file_;
int line_; int line_;
const char* e_what_; std::string e_what_;
int errno_; int errno_;
public: public:
explicit IOException (std::string file, int line, int errnum) explicit IOException (std::string file, int line, int errnum)
: file_(file), line_(line), e_what_ (strerror (errnum)), errno_(errnum) {} : file_(file), line_(line), errno_(errnum) {
std::stringstream ss;
#if defined(_WIN32) && !defined(__MINGW32__)
char error_str [1024];
strerror_s(error_str, 1024, errnum);
#else
char * error_str = strerror(errnum);
#endif
ss << "IO Exception (" << errno_ << "): " << error_str;
ss << ", file " << file_ << ", line " << line_ << ".";
e_what_ = ss.str();
}
explicit IOException (std::string file, int line, const char * description) explicit IOException (std::string file, int line, const char * description)
: file_(file), line_(line), e_what_ (description), errno_(0) {} : file_(file), line_(line), errno_(0) {
std::stringstream ss;
ss << "IO Exception: " << description;
ss << ", file " << file_ << ", line " << line_ << ".";
e_what_ = ss.str();
}
virtual ~IOException() throw() {} virtual ~IOException() throw() {}
IOException (const IOException& other) : line_(other.line_), e_what_(other.e_what_), errno_(other.errno_) {}
int getErrorNumber () { return errno_; } int getErrorNumber () const { return errno_; }
virtual const char* what () const throw () virtual const char* what () const throw () {
{ return e_what_.c_str();
std::stringstream ss;
if (errno_ == 0)
ss << "IO Exception: " << e_what_;
else
ss << "IO Exception (" << errno_ << "): " << e_what_;
ss << ", file " << file_ << ", line " << line_ << ".";
return ss.str ().c_str ();
} }
}; };
class PortNotOpenedException : public std::exception class PortNotOpenedException : public std::exception
{ {
const char * e_what_; // Disable copy constructors
const PortNotOpenedException& operator=(PortNotOpenedException);
std::string e_what_;
public: public:
PortNotOpenedException (const char * description) : e_what_ (description) {} PortNotOpenedException (const char * description) {
std::stringstream ss;
virtual const char* what () const throw () ss << "PortNotOpenedException " << description << " failed.";
{ e_what_ = ss.str();
std::stringstream ss; }
ss << e_what_ << " called before port was opened."; PortNotOpenedException (const PortNotOpenedException& other) : e_what_(other.e_what_) {}
return ss.str ().c_str (); virtual ~PortNotOpenedException() throw() {}
virtual const char* what () const throw () {
return e_what_.c_str();
} }
}; };
class SerialExceptionBase : public std::exception /*!
{ * Structure that describes a serial device.
*/
struct PortInfo {
/*! Address of the serial port (this can be passed to the constructor of Serial). */
std::string port;
/*! Human readable description of serial device if available. */
std::string description;
/*! Hardware ID (e.g. VID:PID of USB serial devices) or "n/a" if not available. */
std::string hardware_id;
}; };
/* Lists the serial ports available on the system
*
* Returns a vector of available serial ports, each represented
* by a serial::PortInfo data structure:
*
* \return vector of serial::PortInfo.
*/
std::vector<PortInfo>
list_ports();
} // namespace serial } // namespace serial
#endif #endif

57
include/serial/v8stdint.h Normal file
View File

@ -0,0 +1,57 @@
// This header is from the v8 google project:
// http://code.google.com/p/v8/source/browse/trunk/include/v8stdint.h
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Load definitions of standard types.
#ifndef V8STDINT_H_
#define V8STDINT_H_
#include <stddef.h>
#include <stdio.h>
#if defined(_WIN32) && !defined(__MINGW32__)
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t; // NOLINT
typedef unsigned short uint16_t; // NOLINT
typedef int int32_t;
typedef unsigned int uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
// intptr_t and friends are defined in crtdefs.h through stdio.h.
#else
#include <stdint.h>
#endif
#endif // V8STDINT_H_

View File

@ -1,26 +0,0 @@
/**
\mainpage
\htmlinclude manifest.html
\b serial is ...
<!--
Provide an overview of your package.
-->
\section codeapi Code API
<!--
Provide links to specific auto-generated API documentation within your
package that is of particular interest to a reader. Doxygen will
document pretty much every part of your code, so do your best here to
point the reader to the actual API.
If your codebase is fairly large or has different sets of APIs, you
should use the doxygen 'group' tag to keep these APIs together. For
example, the roscpp documentation has 'libros' group.
-->
*/

View File

@ -1,18 +0,0 @@
<package>
<description brief="serial">
serial
</description>
<author>William Woodall</author>
<license>BSD</license>
<review status="unreviewed" notes=""/>
<url>http://ros.org/wiki/serial</url>
<export>
<cpp cflags="-I${prefix}/include" lflags="-L${prefix}/lib -Wl,-rpath,${prefix}/lib -lserial"/>
</export>
</package>

24
package.xml Normal file
View File

@ -0,0 +1,24 @@
<?xml version="1.0"?>
<package>
<name>serial</name>
<version>1.2.1</version>
<description>
Serial is a cross-platform, simple to use library for using serial ports on computers.
This library provides a C++, object oriented interface for interacting with RS-232
like devices on Linux and Windows.
</description>
<maintainer email="william@osrfoundation.org">William Woodall</maintainer>
<license>MIT</license>
<url type="website">http://wjwwood.github.com/serial/</url>
<url type="repository">https://github.com/wjwwood/serial</url>
<url type="bugtracker">https://github.com/wjwwood/serial/issues</url>
<author email="wjwwood@gmail.com">William Woodall</author>
<author email="ash.gti@gmail.com">John Harrison</author>
<buildtool_depend>catkin</buildtool_depend>
</package>

View File

@ -1,150 +0,0 @@
macro(build_serial)
## Project Setup
cmake_minimum_required(VERSION 2.4.6)
if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)
project(Serial)
## Configurations
# Enable warnings
# Assuming unix means a gcc style compiler, eg. g++ or clang++.
IF(UNIX)
set(CMAKE_CXX_FLAGS "-Wall -Weffc++ -pedantic -pedantic-errors -Wextra -Wall -Waggregate-return -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment -Wconversion -Wdisabled-optimization -Wfloat-equal -Wformat -Wformat=2 -Wformat-nonliteral -Wformat-security -Wformat-y2k -Wimplicit -Wimport -Winit-self -Winline -Winvalid-pch -Wlong-long -Wmissing-braces -Wmissing-field-initializers -Wmissing-format-attribute -Wmissing-include-dirs -Wmissing-noreturn -Wpacked -Wparentheses -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wstack-protector -Wstrict-aliasing -Wstrict-aliasing=2 -Wswitch -Wswitch-default -Wswitch-enum -Wtrigraphs -Wuninitialized -Wunknown-pragmas -Wunreachable-code -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wvariadic-macros -Wvolatile-register-var -Wwrite-strings")
ELSEIF(WIN32)
# Force to always compile with W4
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
endif(UNIX)
# Use clang if available
IF(EXISTS /usr/bin/clang)
set(CMAKE_CXX_COMPILER /usr/bin/clang++)
set(CMAKE_OSX_DEPLOYMENT_TARGET "")
set(SERIAL_BUILD_WARNINGS TRUE)
set(CMAKE_BUILD_TYPE Debug)
ENDIF(EXISTS /usr/bin/clang)
option(SERIAL_BUILD_TESTS "Build all of the Serial tests." OFF)
option(SERIAL_BUILD_EXAMPLES "Build all of the Serial examples." OFF)
# Allow for building shared libs override
IF(NOT BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS OFF)
ENDIF(NOT BUILD_SHARED_LIBS)
# Threading libraries added for mutexs
FIND_PACKAGE (Threads)
# Set the default path for built executables to the "bin" directory
IF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH))
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
ENDIF(NOT DEFINED(EXECUTABLE_OUTPUT_PATH))
# set the default path for built libraries to the "lib" directory
IF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
ENDIF(NOT DEFINED(LIBRARY_OUTPUT_PATH))
## Configure the build system
# Add the include folder to the include path
include_directories(${PROJECT_SOURCE_DIR}/include)
# Add default source files
set(SERIAL_SRCS src/serial.cc)
IF(WIN32)
list(APPEND SERIAL_SRCS src/impl/windows.cc)
ELSE(WIN32)
list(APPEND SERIAL_SRCS src/impl/unix.cc)
ENDIF(WIN32)
# Add default header files
set(SERIAL_HEADERS include/serial/serial.h)
set(OTHER_LIBS "")
if(UNIX)
set(OTHER_LIBS util)
endif(UNIX)
## Build the Serial Library
# Compile the Library
add_library(serial ${SERIAL_SRCS})
target_link_libraries(serial ${CMAKE_THREAD_LIBS_INIT} ${OTHER_LIBS})
## Build Examples
# If asked to
IF(SERIAL_BUILD_EXAMPLES)
# Compile the Serial Test program
add_executable(serial_example examples/serial_example.cc)
# Link the Test program to the Serial library
target_link_libraries(serial_example serial)
ENDIF(SERIAL_BUILD_EXAMPLES)
## Build tests
# If asked to
IF(SERIAL_BUILD_TESTS)
# Find Google Test
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
# Compile the Serial Test program
add_executable(serial_tests tests/serial_tests.cc)
# Link the Test program to the serial library
target_link_libraries(serial_tests ${GTEST_BOTH_LIBRARIES}
serial)
add_test(AllTestsIntest_serial serial_tests)
ENDIF(SERIAL_BUILD_TESTS)
## Setup install and uninstall
# Unless asked not to...
IF(NOT SERIAL_DONT_CONFIGURE_INSTALL)
# Configure make install
IF(NOT CMAKE_INSTALL_PREFIX)
SET(CMAKE_INSTALL_PREFIX /usr/local)
ENDIF(NOT CMAKE_INSTALL_PREFIX)
INSTALL(TARGETS serial
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
INSTALL(FILES include/serial/serial.h
DESTINATION include/serial)
IF(NOT CMAKE_FIND_INSTALL_PATH)
set(CMAKE_FIND_INSTALL_PATH ${CMAKE_ROOT})
ENDIF(NOT CMAKE_FIND_INSTALL_PATH)
INSTALL(FILES Findserial.cmake
DESTINATION ${CMAKE_FIND_INSTALL_PATH}/Modules/)
ADD_CUSTOM_TARGET(uninstall @echo uninstall package)
IF (UNIX)
ADD_CUSTOM_COMMAND(
COMMENT "uninstall package"
COMMAND xargs ARGS rm < install_manifest.txt
TARGET uninstall
)
ELSE(UNIX)
ADD_CUSTOM_COMMAND(
COMMENT "uninstall only implemented in unix"
TARGET uninstall
)
ENDIF(UNIX)
ENDIF(NOT SERIAL_DONT_CONFIGURE_INSTALL)
endmacro(build_serial)

View File

@ -1,40 +0,0 @@
all: serial
install:
cd build && make install
uninstall:
cd build && make uninstall
serial:
@mkdir -p build
-mkdir -p bin
cd build && cmake $(CMAKE_FLAGS) ..
ifneq ($(MAKE),)
cd build && $(MAKE)
else
cd build && make
endif
clean:
-cd build && make clean
rm -rf build bin lib
.PHONY: doc
doc:
@doxygen doc/Doxyfile
ifeq ($(UNAME),Darwin)
@open doc/html/index.html
endif
.PHONY: test
test:
@mkdir -p build
@mkdir -p bin
cd build && cmake $(CMAKE_FLAGS) -DSERIAL_BUILD_TESTS=1 -DSERIAL_BUILD_EXAMPLES=1 ..
ifneq ($(MAKE),)
cd build && $(MAKE)
else
cd build && make
endif
cd bin && ./serial_tests

22
serial.sublime-project Normal file
View File

@ -0,0 +1,22 @@
{
"word_wrap": "on",
"wrap_width": 80,
"folders":
[
{
"path": "./"
}
],
"settings":
{
"sublimeclang_options":
[
"-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1",
"-I/usr/local/include",
"-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.0.0/include",
"-I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include",
"-I/usr/include",
"-I${folder:${project_path:serial.sublime-project}}/include",
]
}
}

View File

@ -1,42 +0,0 @@
macro(build_serial)
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
# Set the build type. Options are:
# Coverage : w/ debug symbols, w/o optimization, w/ code-coverage
# Debug : w/ debug symbols, w/o optimization
# Release : w/o debug symbols, w/ optimization
# RelWithDebInfo : w/ debug symbols, w/ optimization
# MinSizeRel : w/o debug symbols, w/ optimization, stripped binaries
set(ROS_BUILD_TYPE Debug)
rosbuild_init()
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
include_directories(include)
include_directories(vendor)
set(SERIAL_SRCS src/serial.cc)
if(UNIX)
list(APPEND SERIAL_SRCS src/impl/unix.cc)
else(UNIX)
list(APPEND SERIAL_SRCS src/impl/winows.cc)
endif(UNIX)
# Build the serial library
rosbuild_add_library(${PROJECT_NAME} ${SERIAL_SRCS})
# Build example
rosbuild_add_executable(serial_example examples/serial_example.cc)
target_link_libraries(serial_example ${PROJECT_NAME})
# Create unit tests
rosbuild_add_gtest(serial_tests tests/serial_tests.cc)
target_link_libraries(serial_tests ${PROJECT_NAME})
endmacro(build_serial)

View File

@ -0,0 +1,336 @@
#if defined(__linux__)
/*
* Copyright (c) 2014 Craig Lilley <cralilley@gmail.com>
* This software is made available under the terms of the MIT licence.
* A copy of the licence can be obtained from:
* http://opensource.org/licenses/MIT
*/
#include <vector>
#include <string>
#include <sstream>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdarg>
#include <cstdlib>
#include <glob.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "serial/serial.h"
using serial::PortInfo;
using std::istringstream;
using std::ifstream;
using std::getline;
using std::vector;
using std::string;
using std::cout;
using std::endl;
static vector<string> glob(const vector<string>& patterns);
static string basename(const string& path);
static string dirname(const string& path);
static bool path_exists(const string& path);
static string realpath(const string& path);
static string usb_sysfs_friendly_name(const string& sys_usb_path);
static vector<string> get_sysfs_info(const string& device_path);
static string read_line(const string& file);
static string usb_sysfs_hw_string(const string& sysfs_path);
static string format(const char* format, ...);
vector<string>
glob(const vector<string>& patterns)
{
vector<string> paths_found;
if(patterns.size() == 0)
return paths_found;
glob_t glob_results;
int glob_retval = glob(patterns[0].c_str(), 0, NULL, &glob_results);
vector<string>::const_iterator iter = patterns.begin();
while(++iter != patterns.end())
{
glob_retval = glob(iter->c_str(), GLOB_APPEND, NULL, &glob_results);
}
for(int path_index = 0; path_index < glob_results.gl_pathc; path_index++)
{
paths_found.push_back(glob_results.gl_pathv[path_index]);
}
globfree(&glob_results);
return paths_found;
}
string
basename(const string& path)
{
size_t pos = path.rfind("/");
if(pos == std::string::npos)
return path;
return string(path, pos+1, string::npos);
}
string
dirname(const string& path)
{
size_t pos = path.rfind("/");
if(pos == std::string::npos)
return path;
else if(pos == 0)
return "/";
return string(path, 0, pos);
}
bool
path_exists(const string& path)
{
struct stat sb;
if( stat(path.c_str(), &sb ) == 0 )
return true;
return false;
}
string
realpath(const string& path)
{
char* real_path = realpath(path.c_str(), NULL);
string result;
if(real_path != NULL)
{
result = real_path;
free(real_path);
}
return result;
}
string
usb_sysfs_friendly_name(const string& sys_usb_path)
{
unsigned int device_number = 0;
istringstream( read_line(sys_usb_path + "/devnum") ) >> device_number;
string manufacturer = read_line( sys_usb_path + "/manufacturer" );
string product = read_line( sys_usb_path + "/product" );
string serial = read_line( sys_usb_path + "/serial" );
if( manufacturer.empty() && product.empty() && serial.empty() )
return "";
return format("%s %s %s", manufacturer.c_str(), product.c_str(), serial.c_str() );
}
vector<string>
get_sysfs_info(const string& device_path)
{
string device_name = basename( device_path );
string friendly_name;
string hardware_id;
string sys_device_path = format( "/sys/class/tty/%s/device", device_name.c_str() );
if( device_name.compare(0,6,"ttyUSB") == 0 )
{
sys_device_path = dirname( dirname( realpath( sys_device_path ) ) );
if( path_exists( sys_device_path ) )
{
friendly_name = usb_sysfs_friendly_name( sys_device_path );
hardware_id = usb_sysfs_hw_string( sys_device_path );
}
}
else if( device_name.compare(0,6,"ttyACM") == 0 )
{
sys_device_path = dirname( realpath( sys_device_path ) );
if( path_exists( sys_device_path ) )
{
friendly_name = usb_sysfs_friendly_name( sys_device_path );
hardware_id = usb_sysfs_hw_string( sys_device_path );
}
}
else
{
// Try to read ID string of PCI device
string sys_id_path = sys_device_path + "/id";
if( path_exists( sys_id_path ) )
hardware_id = read_line( sys_id_path );
}
if( friendly_name.empty() )
friendly_name = device_name;
if( hardware_id.empty() )
hardware_id = "n/a";
vector<string> result;
result.push_back(friendly_name);
result.push_back(hardware_id);
return result;
}
string
read_line(const string& file)
{
ifstream ifs(file.c_str(), ifstream::in);
string line;
if(ifs)
{
getline(ifs, line);
}
return line;
}
string
format(const char* format, ...)
{
va_list ap;
size_t buffer_size_bytes = 256;
string result;
char* buffer = (char*)malloc(buffer_size_bytes);
if( buffer == NULL )
return result;
bool done = false;
unsigned int loop_count = 0;
while(!done)
{
va_start(ap, format);
int return_value = vsnprintf(buffer, buffer_size_bytes, format, ap);
if( return_value < 0 )
{
done = true;
}
else if( return_value >= buffer_size_bytes )
{
// Realloc and try again.
buffer_size_bytes = return_value + 1;
char* new_buffer_ptr = (char*)realloc(buffer, buffer_size_bytes);
if( new_buffer_ptr == NULL )
{
done = true;
}
else
{
buffer = new_buffer_ptr;
}
}
else
{
result = buffer;
done = true;
}
va_end(ap);
if( ++loop_count > 5 )
done = true;
}
free(buffer);
return result;
}
string
usb_sysfs_hw_string(const string& sysfs_path)
{
string serial_number = read_line( sysfs_path + "/serial" );
if( serial_number.length() > 0 )
{
serial_number = format( "SNR=%s", serial_number.c_str() );
}
string vid = read_line( sysfs_path + "/idVendor" );
string pid = read_line( sysfs_path + "/idProduct" );
return format("USB VID:PID=%s:%s %s", vid.c_str(), pid.c_str(), serial_number.c_str() );
}
vector<PortInfo>
serial::list_ports()
{
vector<PortInfo> results;
vector<string> search_globs;
search_globs.push_back("/dev/ttyACM*");
search_globs.push_back("/dev/ttyS*");
search_globs.push_back("/dev/ttyUSB*");
search_globs.push_back("/dev/tty.*");
search_globs.push_back("/dev/cu.*");
search_globs.push_back("/dev/rfcomm*");
vector<string> devices_found = glob( search_globs );
vector<string>::iterator iter = devices_found.begin();
while( iter != devices_found.end() )
{
string device = *iter++;
vector<string> sysfs_info = get_sysfs_info( device );
string friendly_name = sysfs_info[0];
string hardware_id = sysfs_info[1];
PortInfo device_entry;
device_entry.port = device;
device_entry.description = friendly_name;
device_entry.hardware_id = hardware_id;
results.push_back( device_entry );
}
return results;
}
#endif // defined(__linux__)

View File

@ -0,0 +1,286 @@
#if defined(__APPLE__)
#include <sys/param.h>
#include <stdint.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/IOBSD.h>
#include <iostream>
#include <string>
#include <vector>
#include "serial/serial.h"
using serial::PortInfo;
using std::string;
using std::vector;
#define HARDWARE_ID_STRING_LENGTH 128
string cfstring_to_string( CFStringRef cfstring );
string get_device_path( io_object_t& serial_port );
string get_class_name( io_object_t& obj );
io_registry_entry_t get_parent_iousb_device( io_object_t& serial_port );
string get_string_property( io_object_t& device, const char* property );
uint16_t get_int_property( io_object_t& device, const char* property );
string rtrim(const string& str);
string
cfstring_to_string( CFStringRef cfstring )
{
char cstring[MAXPATHLEN];
string result;
if( cfstring )
{
Boolean success = CFStringGetCString( cfstring,
cstring,
sizeof(cstring),
kCFStringEncodingASCII );
if( success )
result = cstring;
}
return result;
}
string
get_device_path( io_object_t& serial_port )
{
CFTypeRef callout_path;
string device_path;
callout_path = IORegistryEntryCreateCFProperty( serial_port,
CFSTR(kIOCalloutDeviceKey),
kCFAllocatorDefault,
0 );
if (callout_path)
{
if( CFGetTypeID(callout_path) == CFStringGetTypeID() )
device_path = cfstring_to_string( static_cast<CFStringRef>(callout_path) );
CFRelease(callout_path);
}
return device_path;
}
string
get_class_name( io_object_t& obj )
{
string result;
io_name_t class_name;
kern_return_t kern_result;
kern_result = IOObjectGetClass( obj, class_name );
if( kern_result == KERN_SUCCESS )
result = class_name;
return result;
}
io_registry_entry_t
get_parent_iousb_device( io_object_t& serial_port )
{
io_object_t device = serial_port;
io_registry_entry_t parent = 0;
io_registry_entry_t result = 0;
kern_return_t kern_result = KERN_FAILURE;
string name = get_class_name(device);
// Walk the IO Registry tree looking for this devices parent IOUSBDevice.
while( name != "IOUSBDevice" )
{
kern_result = IORegistryEntryGetParentEntry( device,
kIOServicePlane,
&parent );
if(kern_result != KERN_SUCCESS)
{
result = 0;
break;
}
device = parent;
name = get_class_name(device);
}
if(kern_result == KERN_SUCCESS)
result = device;
return result;
}
string
get_string_property( io_object_t& device, const char* property )
{
string property_name;
if( device )
{
CFStringRef property_as_cfstring = CFStringCreateWithCString (
kCFAllocatorDefault,
property,
kCFStringEncodingASCII );
CFTypeRef name_as_cfstring = IORegistryEntryCreateCFProperty(
device,
property_as_cfstring,
kCFAllocatorDefault,
0 );
if( name_as_cfstring )
{
if( CFGetTypeID(name_as_cfstring) == CFStringGetTypeID() )
property_name = cfstring_to_string( static_cast<CFStringRef>(name_as_cfstring) );
CFRelease(name_as_cfstring);
}
if(property_as_cfstring)
CFRelease(property_as_cfstring);
}
return property_name;
}
uint16_t
get_int_property( io_object_t& device, const char* property )
{
uint16_t result = 0;
if( device )
{
CFStringRef property_as_cfstring = CFStringCreateWithCString (
kCFAllocatorDefault,
property,
kCFStringEncodingASCII );
CFTypeRef number = IORegistryEntryCreateCFProperty( device,
property_as_cfstring,
kCFAllocatorDefault,
0 );
if(property_as_cfstring)
CFRelease(property_as_cfstring);
if( number )
{
if( CFGetTypeID(number) == CFNumberGetTypeID() )
{
bool success = CFNumberGetValue( static_cast<CFNumberRef>(number),
kCFNumberSInt16Type,
&result );
if( !success )
result = 0;
}
CFRelease(number);
}
}
return result;
}
string rtrim(const string& str)
{
string result = str;
string whitespace = " \t\f\v\n\r";
std::size_t found = result.find_last_not_of(whitespace);
if (found != std::string::npos)
result.erase(found+1);
else
result.clear();
return result;
}
vector<PortInfo>
serial::list_ports(void)
{
vector<PortInfo> devices_found;
CFMutableDictionaryRef classes_to_match;
io_iterator_t serial_port_iterator;
io_object_t serial_port;
mach_port_t master_port;
kern_return_t kern_result;
kern_result = IOMasterPort(MACH_PORT_NULL, &master_port);
if(kern_result != KERN_SUCCESS)
return devices_found;
classes_to_match = IOServiceMatching(kIOSerialBSDServiceValue);
if (classes_to_match == NULL)
return devices_found;
CFDictionarySetValue( classes_to_match,
CFSTR(kIOSerialBSDTypeKey),
CFSTR(kIOSerialBSDAllTypes) );
kern_result = IOServiceGetMatchingServices(master_port, classes_to_match, &serial_port_iterator);
if (KERN_SUCCESS != kern_result)
return devices_found;
while ( (serial_port = IOIteratorNext(serial_port_iterator)) )
{
string device_path = get_device_path( serial_port );
io_registry_entry_t parent = get_parent_iousb_device( serial_port );
IOObjectRelease(serial_port);
if( device_path.empty() )
continue;
PortInfo port_info;
port_info.port = device_path;
port_info.description = "n/a";
port_info.hardware_id = "n/a";
string device_name = rtrim( get_string_property( parent, "USB Product Name" ) );
string vendor_name = rtrim( get_string_property( parent, "USB Vendor Name") );
string description = rtrim( vendor_name + " " + device_name );
if( !description.empty() )
port_info.description = description;
string serial_number = rtrim(get_string_property( parent, "USB Serial Number" ) );
uint16_t vendor_id = get_int_property( parent, "idVendor" );
uint16_t product_id = get_int_property( parent, "idProduct" );
if( vendor_id && product_id )
{
char cstring[HARDWARE_ID_STRING_LENGTH];
if(serial_number.empty())
serial_number = "None";
int ret = snprintf( cstring, HARDWARE_ID_STRING_LENGTH, "USB VID:PID=%04x:%04x SNR=%s",
vendor_id,
product_id,
serial_number.c_str() );
if( (ret >= 0) && (ret < HARDWARE_ID_STRING_LENGTH) )
port_info.hardware_id = cstring;
}
devices_found.push_back(port_info);
}
IOObjectRelease(serial_port_iterator);
return devices_found;
}
#endif // defined(__APPLE__)

View File

@ -0,0 +1,152 @@
#if defined(_WIN32)
/*
* Copyright (c) 2014 Craig Lilley <cralilley@gmail.com>
* This software is made available under the terms of the MIT licence.
* A copy of the licence can be obtained from:
* http://opensource.org/licenses/MIT
*/
#include "serial/serial.h"
#include <tchar.h>
#include <windows.h>
#include <setupapi.h>
#include <initguid.h>
#include <devguid.h>
#include <cstring>
using serial::PortInfo;
using std::vector;
using std::string;
static const DWORD port_name_max_length = 256;
static const DWORD friendly_name_max_length = 256;
static const DWORD hardware_id_max_length = 256;
// Convert a wide Unicode string to an UTF8 string
std::string utf8_encode(const std::wstring &wstr)
{
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo( size_needed, 0 );
WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}
vector<PortInfo>
serial::list_ports()
{
vector<PortInfo> devices_found;
HDEVINFO device_info_set = SetupDiGetClassDevs(
(const GUID *) &GUID_DEVCLASS_PORTS,
NULL,
NULL,
DIGCF_PRESENT);
unsigned int device_info_set_index = 0;
SP_DEVINFO_DATA device_info_data;
device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);
while(SetupDiEnumDeviceInfo(device_info_set, device_info_set_index, &device_info_data))
{
device_info_set_index++;
// Get port name
HKEY hkey = SetupDiOpenDevRegKey(
device_info_set,
&device_info_data,
DICS_FLAG_GLOBAL,
0,
DIREG_DEV,
KEY_READ);
TCHAR port_name[port_name_max_length];
DWORD port_name_length = port_name_max_length;
LONG return_code = RegQueryValueEx(
hkey,
_T("PortName"),
NULL,
NULL,
(LPBYTE)port_name,
&port_name_length);
RegCloseKey(hkey);
if(return_code != EXIT_SUCCESS)
continue;
if(port_name_length > 0 && port_name_length <= port_name_max_length)
port_name[port_name_length-1] = '\0';
else
port_name[0] = '\0';
// Ignore parallel ports
if(_tcsstr(port_name, _T("LPT")) != NULL)
continue;
// Get port friendly name
TCHAR friendly_name[friendly_name_max_length];
DWORD friendly_name_actual_length = 0;
BOOL got_friendly_name = SetupDiGetDeviceRegistryProperty(
device_info_set,
&device_info_data,
SPDRP_FRIENDLYNAME,
NULL,
(PBYTE)friendly_name,
friendly_name_max_length,
&friendly_name_actual_length);
if(got_friendly_name == TRUE && friendly_name_actual_length > 0)
friendly_name[friendly_name_actual_length-1] = '\0';
else
friendly_name[0] = '\0';
// Get hardware ID
TCHAR hardware_id[hardware_id_max_length];
DWORD hardware_id_actual_length = 0;
BOOL got_hardware_id = SetupDiGetDeviceRegistryProperty(
device_info_set,
&device_info_data,
SPDRP_HARDWAREID,
NULL,
(PBYTE)hardware_id,
hardware_id_max_length,
&hardware_id_actual_length);
if(got_hardware_id == TRUE && hardware_id_actual_length > 0)
hardware_id[hardware_id_actual_length-1] = '\0';
else
hardware_id[0] = '\0';
#ifdef UNICODE
std::string portName = utf8_encode(port_name);
std::string friendlyName = utf8_encode(friendly_name);
std::string hardwareId = utf8_encode(hardware_id);
#else
std::string portName = port_name;
std::string friendlyName = friendly_name;
std::string hardwareId = hardware_id;
#endif
PortInfo port_entry;
port_entry.port = portName;
port_entry.description = friendlyName;
port_entry.hardware_id = hardwareId;
devices_found.push_back(port_entry);
}
SetupDiDestroyDeviceInfoList(device_info_set);
return devices_found;
}
#endif // #if defined(_WIN32)

905
src/impl/unix.cc Normal file → Executable file

File diff suppressed because it is too large Load Diff

646
src/impl/win.cc Normal file
View File

@ -0,0 +1,646 @@
#if defined(_WIN32)
/* Copyright 2012 William Woodall and John Harrison */
#include <sstream>
#include "serial/impl/win.h"
using std::string;
using std::wstring;
using std::stringstream;
using std::invalid_argument;
using serial::Serial;
using serial::Timeout;
using serial::bytesize_t;
using serial::parity_t;
using serial::stopbits_t;
using serial::flowcontrol_t;
using serial::SerialException;
using serial::PortNotOpenedException;
using serial::IOException;
inline wstring
_prefix_port_if_needed(const wstring &input)
{
static wstring windows_com_port_prefix = L"\\\\.\\";
if (input.compare(0, windows_com_port_prefix.size(), windows_com_port_prefix) != 0)
{
return windows_com_port_prefix + input;
}
return input;
}
Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
bytesize_t bytesize,
parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol)
: port_ (port.begin(), port.end()), fd_ (INVALID_HANDLE_VALUE), is_open_ (false),
baudrate_ (baudrate), parity_ (parity),
bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol)
{
if (port_.empty () == false)
open ();
read_mutex = CreateMutex(NULL, false, NULL);
write_mutex = CreateMutex(NULL, false, NULL);
}
Serial::SerialImpl::~SerialImpl ()
{
this->close();
CloseHandle(read_mutex);
CloseHandle(write_mutex);
}
void
Serial::SerialImpl::open ()
{
if (port_.empty ()) {
throw invalid_argument ("Empty port is invalid.");
}
if (is_open_ == true) {
throw SerialException ("Serial port already open.");
}
// See: https://github.com/wjwwood/serial/issues/84
wstring port_with_prefix = _prefix_port_if_needed(port_);
LPCWSTR lp_port = port_with_prefix.c_str();
fd_ = CreateFileW(lp_port,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (fd_ == INVALID_HANDLE_VALUE) {
DWORD create_file_err = GetLastError();
stringstream ss;
switch (create_file_err) {
case ERROR_FILE_NOT_FOUND:
// Use this->getPort to convert to a std::string
ss << "Specified port, " << this->getPort() << ", does not exist.";
THROW (IOException, ss.str().c_str());
default:
ss << "Unknown error opening the serial port: " << create_file_err;
THROW (IOException, ss.str().c_str());
}
}
reconfigurePort();
is_open_ = true;
}
void
Serial::SerialImpl::reconfigurePort ()
{
if (fd_ == INVALID_HANDLE_VALUE) {
// Can only operate on a valid file descriptor
THROW (IOException, "Invalid file descriptor, is the serial port open?");
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(fd_, &dcbSerialParams)) {
//error getting state
THROW (IOException, "Error getting the serial port state.");
}
// setup baud rate
switch (baudrate_) {
#ifdef CBR_0
case 0: dcbSerialParams.BaudRate = CBR_0; break;
#endif
#ifdef CBR_50
case 50: dcbSerialParams.BaudRate = CBR_50; break;
#endif
#ifdef CBR_75
case 75: dcbSerialParams.BaudRate = CBR_75; break;
#endif
#ifdef CBR_110
case 110: dcbSerialParams.BaudRate = CBR_110; break;
#endif
#ifdef CBR_134
case 134: dcbSerialParams.BaudRate = CBR_134; break;
#endif
#ifdef CBR_150
case 150: dcbSerialParams.BaudRate = CBR_150; break;
#endif
#ifdef CBR_200
case 200: dcbSerialParams.BaudRate = CBR_200; break;
#endif
#ifdef CBR_300
case 300: dcbSerialParams.BaudRate = CBR_300; break;
#endif
#ifdef CBR_600
case 600: dcbSerialParams.BaudRate = CBR_600; break;
#endif
#ifdef CBR_1200
case 1200: dcbSerialParams.BaudRate = CBR_1200; break;
#endif
#ifdef CBR_1800
case 1800: dcbSerialParams.BaudRate = CBR_1800; break;
#endif
#ifdef CBR_2400
case 2400: dcbSerialParams.BaudRate = CBR_2400; break;
#endif
#ifdef CBR_4800
case 4800: dcbSerialParams.BaudRate = CBR_4800; break;
#endif
#ifdef CBR_7200
case 7200: dcbSerialParams.BaudRate = CBR_7200; break;
#endif
#ifdef CBR_9600
case 9600: dcbSerialParams.BaudRate = CBR_9600; break;
#endif
#ifdef CBR_14400
case 14400: dcbSerialParams.BaudRate = CBR_14400; break;
#endif
#ifdef CBR_19200
case 19200: dcbSerialParams.BaudRate = CBR_19200; break;
#endif
#ifdef CBR_28800
case 28800: dcbSerialParams.BaudRate = CBR_28800; break;
#endif
#ifdef CBR_57600
case 57600: dcbSerialParams.BaudRate = CBR_57600; break;
#endif
#ifdef CBR_76800
case 76800: dcbSerialParams.BaudRate = CBR_76800; break;
#endif
#ifdef CBR_38400
case 38400: dcbSerialParams.BaudRate = CBR_38400; break;
#endif
#ifdef CBR_115200
case 115200: dcbSerialParams.BaudRate = CBR_115200; break;
#endif
#ifdef CBR_128000
case 128000: dcbSerialParams.BaudRate = CBR_128000; break;
#endif
#ifdef CBR_153600
case 153600: dcbSerialParams.BaudRate = CBR_153600; break;
#endif
#ifdef CBR_230400
case 230400: dcbSerialParams.BaudRate = CBR_230400; break;
#endif
#ifdef CBR_256000
case 256000: dcbSerialParams.BaudRate = CBR_256000; break;
#endif
#ifdef CBR_460800
case 460800: dcbSerialParams.BaudRate = CBR_460800; break;
#endif
#ifdef CBR_921600
case 921600: dcbSerialParams.BaudRate = CBR_921600; break;
#endif
default:
// Try to blindly assign it
dcbSerialParams.BaudRate = baudrate_;
}
// setup char len
if (bytesize_ == eightbits)
dcbSerialParams.ByteSize = 8;
else if (bytesize_ == sevenbits)
dcbSerialParams.ByteSize = 7;
else if (bytesize_ == sixbits)
dcbSerialParams.ByteSize = 6;
else if (bytesize_ == fivebits)
dcbSerialParams.ByteSize = 5;
else
throw invalid_argument ("invalid char len");
// setup stopbits
if (stopbits_ == stopbits_one)
dcbSerialParams.StopBits = ONESTOPBIT;
else if (stopbits_ == stopbits_one_point_five)
dcbSerialParams.StopBits = ONE5STOPBITS;
else if (stopbits_ == stopbits_two)
dcbSerialParams.StopBits = TWOSTOPBITS;
else
throw invalid_argument ("invalid stop bit");
// setup parity
if (parity_ == parity_none) {
dcbSerialParams.Parity = NOPARITY;
} else if (parity_ == parity_even) {
dcbSerialParams.Parity = EVENPARITY;
} else if (parity_ == parity_odd) {
dcbSerialParams.Parity = ODDPARITY;
} else if (parity_ == parity_mark) {
dcbSerialParams.Parity = MARKPARITY;
} else if (parity_ == parity_space) {
dcbSerialParams.Parity = SPACEPARITY;
} else {
throw invalid_argument ("invalid parity");
}
// setup flowcontrol
if (flowcontrol_ == flowcontrol_none) {
dcbSerialParams.fOutxCtsFlow = false;
dcbSerialParams.fRtsControl = RTS_CONTROL_DISABLE;
dcbSerialParams.fOutX = false;
dcbSerialParams.fInX = false;
}
if (flowcontrol_ == flowcontrol_software) {
dcbSerialParams.fOutxCtsFlow = false;
dcbSerialParams.fRtsControl = RTS_CONTROL_DISABLE;
dcbSerialParams.fOutX = true;
dcbSerialParams.fInX = true;
}
if (flowcontrol_ == flowcontrol_hardware) {
dcbSerialParams.fOutxCtsFlow = true;
dcbSerialParams.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcbSerialParams.fOutX = false;
dcbSerialParams.fInX = false;
}
// activate settings
if (!SetCommState(fd_, &dcbSerialParams)){
CloseHandle(fd_);
THROW (IOException, "Error setting serial port settings.");
}
// Setup timeouts
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = timeout_.inter_byte_timeout;
timeouts.ReadTotalTimeoutConstant = timeout_.read_timeout_constant;
timeouts.ReadTotalTimeoutMultiplier = timeout_.read_timeout_multiplier;
timeouts.WriteTotalTimeoutConstant = timeout_.write_timeout_constant;
timeouts.WriteTotalTimeoutMultiplier = timeout_.write_timeout_multiplier;
if (!SetCommTimeouts(fd_, &timeouts)) {
THROW (IOException, "Error setting timeouts.");
}
}
void
Serial::SerialImpl::close ()
{
if (is_open_ == true) {
if (fd_ != INVALID_HANDLE_VALUE) {
int ret;
ret = CloseHandle(fd_);
if (ret == 0) {
stringstream ss;
ss << "Error while closing serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
} else {
fd_ = INVALID_HANDLE_VALUE;
}
}
is_open_ = false;
}
}
bool
Serial::SerialImpl::isOpen () const
{
return is_open_;
}
size_t
Serial::SerialImpl::available ()
{
if (!is_open_) {
return 0;
}
COMSTAT cs;
if (!ClearCommError(fd_, NULL, &cs)) {
stringstream ss;
ss << "Error while checking status of the serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
}
return static_cast<size_t>(cs.cbInQue);
}
bool
Serial::SerialImpl::waitReadable (uint32_t /*timeout*/)
{
THROW (IOException, "waitReadable is not implemented on Windows.");
return false;
}
void
Serial::SerialImpl::waitByteTimes (size_t /*count*/)
{
THROW (IOException, "waitByteTimes is not implemented on Windows.");
}
size_t
Serial::SerialImpl::read (uint8_t *buf, size_t size)
{
if (!is_open_) {
throw PortNotOpenedException ("Serial::read");
}
DWORD bytes_read;
if (!ReadFile(fd_, buf, static_cast<DWORD>(size), &bytes_read, NULL)) {
stringstream ss;
ss << "Error while reading from the serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
}
return (size_t) (bytes_read);
}
size_t
Serial::SerialImpl::write (const uint8_t *data, size_t length)
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::write");
}
DWORD bytes_written;
if (!WriteFile(fd_, data, static_cast<DWORD>(length), &bytes_written, NULL)) {
stringstream ss;
ss << "Error while writing to the serial port: " << GetLastError();
THROW (IOException, ss.str().c_str());
}
return (size_t) (bytes_written);
}
void
Serial::SerialImpl::setPort (const string &port)
{
port_ = wstring(port.begin(), port.end());
}
string
Serial::SerialImpl::getPort () const
{
return string(port_.begin(), port_.end());
}
void
Serial::SerialImpl::setTimeout (serial::Timeout &timeout)
{
timeout_ = timeout;
if (is_open_) {
reconfigurePort ();
}
}
serial::Timeout
Serial::SerialImpl::getTimeout () const
{
return timeout_;
}
void
Serial::SerialImpl::setBaudrate (unsigned long baudrate)
{
baudrate_ = baudrate;
if (is_open_) {
reconfigurePort ();
}
}
unsigned long
Serial::SerialImpl::getBaudrate () const
{
return baudrate_;
}
void
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize)
{
bytesize_ = bytesize;
if (is_open_) {
reconfigurePort ();
}
}
serial::bytesize_t
Serial::SerialImpl::getBytesize () const
{
return bytesize_;
}
void
Serial::SerialImpl::setParity (serial::parity_t parity)
{
parity_ = parity;
if (is_open_) {
reconfigurePort ();
}
}
serial::parity_t
Serial::SerialImpl::getParity () const
{
return parity_;
}
void
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits)
{
stopbits_ = stopbits;
if (is_open_) {
reconfigurePort ();
}
}
serial::stopbits_t
Serial::SerialImpl::getStopbits () const
{
return stopbits_;
}
void
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol)
{
flowcontrol_ = flowcontrol;
if (is_open_) {
reconfigurePort ();
}
}
serial::flowcontrol_t
Serial::SerialImpl::getFlowcontrol () const
{
return flowcontrol_;
}
void
Serial::SerialImpl::flush ()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::flush");
}
FlushFileBuffers (fd_);
}
void
Serial::SerialImpl::flushInput ()
{
if (is_open_ == false) {
throw PortNotOpenedException("Serial::flushInput");
}
PurgeComm(fd_, PURGE_RXCLEAR);
}
void
Serial::SerialImpl::flushOutput ()
{
if (is_open_ == false) {
throw PortNotOpenedException("Serial::flushOutput");
}
PurgeComm(fd_, PURGE_TXCLEAR);
}
void
Serial::SerialImpl::sendBreak (int /*duration*/)
{
THROW (IOException, "sendBreak is not supported on Windows.");
}
void
Serial::SerialImpl::setBreak (bool level)
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::setBreak");
}
if (level) {
EscapeCommFunction (fd_, SETBREAK);
} else {
EscapeCommFunction (fd_, CLRBREAK);
}
}
void
Serial::SerialImpl::setRTS (bool level)
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::setRTS");
}
if (level) {
EscapeCommFunction (fd_, SETRTS);
} else {
EscapeCommFunction (fd_, CLRRTS);
}
}
void
Serial::SerialImpl::setDTR (bool level)
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::setDTR");
}
if (level) {
EscapeCommFunction (fd_, SETDTR);
} else {
EscapeCommFunction (fd_, CLRDTR);
}
}
bool
Serial::SerialImpl::waitForChange ()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::waitForChange");
}
DWORD dwCommEvent;
if (!SetCommMask(fd_, EV_CTS | EV_DSR | EV_RING | EV_RLSD)) {
// Error setting communications mask
return false;
}
if (!WaitCommEvent(fd_, &dwCommEvent, NULL)) {
// An error occurred waiting for the event.
return false;
} else {
// Event has occurred.
return true;
}
}
bool
Serial::SerialImpl::getCTS ()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::getCTS");
}
DWORD dwModemStatus;
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
THROW (IOException, "Error getting the status of the CTS line.");
}
return (MS_CTS_ON & dwModemStatus) != 0;
}
bool
Serial::SerialImpl::getDSR ()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::getDSR");
}
DWORD dwModemStatus;
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
THROW (IOException, "Error getting the status of the DSR line.");
}
return (MS_DSR_ON & dwModemStatus) != 0;
}
bool
Serial::SerialImpl::getRI()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::getRI");
}
DWORD dwModemStatus;
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
THROW (IOException, "Error getting the status of the RI line.");
}
return (MS_RING_ON & dwModemStatus) != 0;
}
bool
Serial::SerialImpl::getCD()
{
if (is_open_ == false) {
throw PortNotOpenedException ("Serial::getCD");
}
DWORD dwModemStatus;
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
// Error in GetCommModemStatus;
THROW (IOException, "Error getting the status of the CD line.");
}
return (MS_RLSD_ON & dwModemStatus) != 0;
}
void
Serial::SerialImpl::readLock()
{
if (WaitForSingleObject(read_mutex, INFINITE) != WAIT_OBJECT_0) {
THROW (IOException, "Error claiming read mutex.");
}
}
void
Serial::SerialImpl::readUnlock()
{
if (!ReleaseMutex(read_mutex)) {
THROW (IOException, "Error releasing read mutex.");
}
}
void
Serial::SerialImpl::writeLock()
{
if (WaitForSingleObject(write_mutex, INFINITE) != WAIT_OBJECT_0) {
THROW (IOException, "Error claiming write mutex.");
}
}
void
Serial::SerialImpl::writeUnlock()
{
if (!ReleaseMutex(write_mutex)) {
THROW (IOException, "Error releasing write mutex.");
}
}
#endif // #if defined(_WIN32)

View File

@ -1,670 +0,0 @@
/* Copyright 2012 William Woodall and John Harrison */
#include "serial/impl/windows.h"
using std::string;
using std::invalid_argument;
using serial::Serial;
using serial::SerialExecption;
using serial::PortNotOpenedException;
using serial::IOException;
Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
long timeout, bytesize_t bytesize,
parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol)
: port_ (port), fd_ (-1), isOpen_ (false), xonxoff_ (true), rtscts_ (false),
timeout_ (timeout), baudrate_ (baudrate), parity_ (parity), bytesize_ (bytesize),
stopbits_ (stopbits), flowcontrol_ (flowcontrol)
{
if (port_.empty () == false)
open ();
}
Serial::SerialImpl::~SerialImpl ()
{
close();
}
void
Serial::SerialImpl::open ()
{
if (port_.empty())
{
throw invalid_argument ("bad port specified");
}
if (isOpen_ == true)
{
throw SerialExecption ("port already open");
}
fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd_ == -1)
{
switch (errno)
{
case EINTR:
// Recurse because this is a recoverable error.
open ();
return;
case ENFILE:
case EMFILE:
throw IOException ("to many file handles open");
break;
default:
throw IOException (errno);
}
}
reconfigurePort();
isOpen_ = true;
}
void
Serial::SerialImpl::reconfigurePort ()
{
if (fd_ == -1)
{
// Can only operate on a valid file descriptor
throw IOException ("invalid file descriptor");
}
struct termios options; // The options for the file descriptor
if (tcgetattr(fd_, &options) == -1)
{
throw IOException ("::tcgetattr");
}
// set up raw mode / no echo / binary
options.c_cflag |= (unsigned long) (CLOCAL|CREAD);
options.c_lflag &= (unsigned long) ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|
ISIG|IEXTEN); //|ECHOPRT
options.c_oflag &= (unsigned long) ~(OPOST);
options.c_iflag &= (unsigned long) ~(INLCR|IGNCR|ICRNL|IGNBRK);
#ifdef IUCLC
options.c_iflag &= (unsigned long) ~IUCLC;
#endif
#ifdef PARMRK
options.c_iflag &= (unsigned long) ~PARMRK;
#endif
// setup baud rate
bool custom_baud = false;
speed_t baud;
switch (baudrate_)
{
#ifdef B0
case 0: baud = B0; break;
#endif
#ifdef B50
case 50: baud = B50; break;
#endif
#ifdef B75
case 75: baud = B75; break;
#endif
#ifdef B110
case 110: baud = B110; break;
#endif
#ifdef B134
case 134: baud = B134; break;
#endif
#ifdef B150
case 150: baud = B150; break;
#endif
#ifdef B200
case 200: baud = B200; break;
#endif
#ifdef B300
case 300: baud = B300; break;
#endif
#ifdef B600
case 600: baud = B600; break;
#endif
#ifdef B1200
case 1200: baud = B1200; break;
#endif
#ifdef B1800
case 1800: baud = B1800; break;
#endif
#ifdef B2400
case 2400: baud = B2400; break;
#endif
#ifdef B4800
case 4800: baud = B4800; break;
#endif
#ifdef B7200
case 7200: baud = B7200; break;
#endif
#ifdef B9600
case 9600: baud = B9600; break;
#endif
#ifdef B14400
case 14400: baud = B14400; break;
#endif
#ifdef B19200
case 19200: baud = B19200; break;
#endif
#ifdef B28800
case 28800: baud = B28800; break;
#endif
#ifdef B57600
case 57600: baud = B57600; break;
#endif
#ifdef B76800
case 76800: baud = B76800; break;
#endif
#ifdef B38400
case 38400: baud = B38400; break;
#endif
#ifdef B115200
case 115200: baud = B115200; break;
#endif
#ifdef B128000
case 128000: baud = B128000; break;
#endif
#ifdef B153600
case 153600: baud = B153600; break;
#endif
#ifdef B230400
case 230400: baud = B230400; break;
#endif
#ifdef B256000
case 256000: baud = B256000; break;
#endif
#ifdef B460800
case 460800: baud = B460800; break;
#endif
#ifdef B921600
case 921600: baud = B921600; break;
#endif
default:
custom_baud = true;
// Mac OS X 10.x Support
#if defined(__APPLE__) && defined(__MACH__)
#define IOSSIOSPEED _IOW('T', 2, speed_t)
int new_baud = static_cast<int> (baudrate_);
if (ioctl (fd_, IOSSIOSPEED, &new_baud, 1) < 0)
{
throw IOException (errno);
}
// Linux Support
#elif defined(__linux__)
struct serial_struct ser;
ioctl(fd_, TIOCGSERIAL, &ser);
// set custom divisor
ser.custom_divisor = ser.baud_base / baudrate_;
// update flags
ser.flags &= ~ASYNC_SPD_MASK;
ser.flags |= ASYNC_SPD_CUST;
if (ioctl(fd_, TIOCSSERIAL, ser) < 0)
{
throw IOException (errno);
}
#else
throw invalid_argument ("OS does not currently support custom bauds");
#endif
}
if (custom_baud == false)
{
#ifdef _BSD_SOURCE
::cfsetspeed(&options, baud);
#else
::cfsetispeed(&options, baud);
::cfsetospeed(&options, baud);
#endif
}
// setup char len
options.c_cflag &= (unsigned long) ~CSIZE;
if (bytesize_ == EIGHTBITS)
options.c_cflag |= CS8;
else if (bytesize_ == SEVENBITS)
options.c_cflag |= CS7;
else if (bytesize_ == SIXBITS)
options.c_cflag |= CS6;
else if (bytesize_ == FIVEBITS)
options.c_cflag |= CS5;
else
throw invalid_argument ("invalid char len");
// setup stopbits
if (stopbits_ == STOPBITS_ONE)
options.c_cflag &= (unsigned long) ~(CSTOPB);
else if (stopbits_ == STOPBITS_ONE_POINT_FIVE)
options.c_cflag |= (CSTOPB); // XXX same as TWO.. there is no POSIX support for 1.5
else if (stopbits_ == STOPBITS_TWO)
options.c_cflag |= (CSTOPB);
else
throw invalid_argument ("invalid stop bit");
// setup parity
options.c_iflag &= (unsigned long) ~(INPCK|ISTRIP);
if (parity_ == PARITY_NONE)
{
options.c_cflag &= (unsigned long) ~(PARENB|PARODD);
}
else if (parity_ == PARITY_EVEN)
{
options.c_cflag &= (unsigned long) ~(PARODD);
options.c_cflag |= (PARENB);
}
else if (parity_ == PARITY_ODD)
{
options.c_cflag |= (PARENB|PARODD);
}
else
{
throw invalid_argument ("invalid parity");
}
// setup flow control
// xonxoff
#ifdef IXANY
if (xonxoff_)
options.c_iflag |= (IXON|IXOFF); //|IXANY)
else
options.c_iflag &= (unsigned long) ~(IXON|IXOFF|IXANY);
#else
if (xonxoff_)
options.c_iflag |= (IXON|IXOFF);
else
options.c_iflag &= (unsigned long) ~(IXON|IXOFF);
#endif
// rtscts
#ifdef CRTSCTS
if (rtscts_)
options.c_cflag |= (CRTSCTS);
else
options.c_cflag &= (unsigned long) ~(CRTSCTS);
#elif defined CNEW_RTSCTS
if (rtscts_)
options.c_cflag |= (CNEW_RTSCTS);
else
options.c_cflag &= (unsigned long) ~(CNEW_RTSCTS);
#else
#error "OS Support seems wrong."
#endif
options.c_cc[VMIN] = 1; // Minimum of 1 character in the buffer
options.c_cc[VTIME] = 0; // timeout on waiting for new data
// activate settings
::tcsetattr (fd_, TCSANOW, &options);
}
void
Serial::SerialImpl::close ()
{
if (isOpen_ == true)
{
if (fd_ != -1)
{
::close (fd_); // Ignoring the outcome
fd_ = -1;
}
isOpen_ = false;
}
}
bool
Serial::SerialImpl::isOpen () const
{
return isOpen_;
}
size_t
Serial::SerialImpl::available ()
{
if (!isOpen_)
{
return 0;
}
int count = 0;
int result = ioctl (fd_, TIOCINQ, &count);
if (result == 0)
{
return static_cast<size_t> (count);
}
else
{
throw IOException (errno);
}
}
size_t
Serial::SerialImpl::read (char* buf, size_t size)
{
if (!isOpen_)
{
throw PortNotOpenedException ("Serial::read");
}
fd_set readfds;
ssize_t bytes_read = 0;
int count = 0;
while (true)
{
count++;
if (timeout_ != -1)
{
FD_ZERO (&readfds);
FD_SET (fd_, &readfds);
struct timeval timeout;
timeout.tv_sec = timeout_ / 1000;
timeout.tv_usec = static_cast<int> (timeout_ % 1000) * 1000;
int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
if (r == -1 && errno == EINTR)
continue;
if (r == -1)
{
throw IOException (errno);
}
}
if (timeout_ == -1 || FD_ISSET (fd_, &readfds))
{
bytes_read = ::read (fd_, buf, size);
// read should always return some data as select reported it was
// ready to read when we get to this point.
if (bytes_read < 1)
{
// Disconnected devices, at least on Linux, show the
// behavior that they are always ready to read immediately
// but reading returns nothing.
throw SerialExecption ("device reports readiness to read but "
"returned no data (device disconnected?)");
}
break;
}
else
{
break;
}
}
return static_cast<size_t> (bytes_read);
}
size_t
Serial::SerialImpl::write (const string &data)
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::write");
}
fd_set writefds;
ssize_t bytes_written = 0;
while (true)
{
if (timeout_ != -1)
{
FD_ZERO (&writefds);
FD_SET (fd_, &writefds);
struct timeval timeout;
timeout.tv_sec = timeout_ / 1000;
timeout.tv_usec = static_cast<int> (timeout_ % 1000) * 1000;
int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout);
if (r == -1 && errno == EINTR)
continue;
if (r == -1)
{
throw IOException (errno);
}
}
if (timeout_ == -1 || FD_ISSET (fd_, &writefds))
{
bytes_written = ::write (fd_, data.c_str (), data.length ());
// read should always return some data as select reported it was
// ready to read when we get to this point.
if (bytes_written < 1)
{
// Disconnected devices, at least on Linux, show the
// behavior that they are always ready to read immediately
// but reading returns nothing.
throw SerialExecption ("device reports readiness to read but "
"returned no data (device disconnected?)");
}
break;
}
else
{
break;
}
}
return static_cast<size_t> (bytes_written);
}
void
Serial::SerialImpl::setPort (const string &port)
{
port_ = port;
}
string
Serial::SerialImpl::getPort () const
{
return port_;
}
void
Serial::SerialImpl::setTimeout (long timeout)
{
timeout_ = timeout;
}
long
Serial::SerialImpl::getTimeout () const
{
return timeout_;
}
void
Serial::SerialImpl::setBaudrate (unsigned long baudrate)
{
baudrate_ = baudrate;
if (isOpen_)
reconfigurePort ();
}
unsigned long
Serial::SerialImpl::getBaudrate () const
{
return baudrate_;
}
void
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize)
{
bytesize_ = bytesize;
if (isOpen_)
reconfigurePort ();
}
serial::bytesize_t
Serial::SerialImpl::getBytesize () const
{
return bytesize_;
}
void
Serial::SerialImpl::setParity (serial::parity_t parity)
{
parity_ = parity;
if (isOpen_)
reconfigurePort ();
}
serial::parity_t
Serial::SerialImpl::getParity () const
{
return parity_;
}
void
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits)
{
stopbits_ = stopbits;
if (isOpen_)
reconfigurePort ();
}
serial::stopbits_t
Serial::SerialImpl::getStopbits () const
{
return stopbits_;
}
void
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol)
{
flowcontrol_ = flowcontrol;
if (isOpen_)
reconfigurePort ();
}
serial::flowcontrol_t
Serial::SerialImpl::getFlowcontrol () const
{
return flowcontrol_;
}
void
Serial::SerialImpl::flush ()
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::flush");
}
tcdrain (fd_);
}
void
Serial::SerialImpl::flushInput ()
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::flushInput");
}
tcflush (fd_, TCIFLUSH);
}
void
Serial::SerialImpl::flushOutput ()
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::flushOutput");
}
tcflush (fd_, TCOFLUSH);
}
void
Serial::SerialImpl::sendBreak (int duration)
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::sendBreak");
}
tcsendbreak (fd_, static_cast<int> (duration/4));
}
void
Serial::SerialImpl::setBreak (bool level)
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::setBreak");
}
if (level)
{
ioctl (fd_, TIOCSBRK);
}
else {
ioctl (fd_, TIOCCBRK);
}
}
void
Serial::SerialImpl::setRTS (bool level)
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::setRTS");
}
if (level)
{
ioctl (fd_, TIOCMBIS, TIOCM_RTS);
}
else {
ioctl (fd_, TIOCMBIC, TIOCM_RTS);
}
}
void
Serial::SerialImpl::setDTR (bool level)
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::setDTR");
}
if (level)
{
ioctl (fd_, TIOCMBIS, TIOCM_DTR);
}
else
{
ioctl (fd_, TIOCMBIC, TIOCM_DTR);
}
}
bool
Serial::SerialImpl::getCTS ()
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::getCTS");
}
int s = ioctl (fd_, TIOCMGET, 0);
return (s & TIOCM_CTS) != 0;
}
bool
Serial::SerialImpl::getDSR()
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::getDSR");
}
int s = ioctl(fd_, TIOCMGET, 0);
return (s & TIOCM_DSR) != 0;
}
bool
Serial::SerialImpl::getRI()
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::getRI");
}
int s = ioctl (fd_, TIOCMGET, 0);
return (s & TIOCM_RI) != 0;
}
bool
Serial::SerialImpl::getCD()
{
if (isOpen_ == false)
{
throw PortNotOpenedException ("Serial::getCD");
}
int s = ioctl (fd_, TIOCMGET, 0);
return (s & TIOCM_CD) != 0;
}

144
src/serial.cc Normal file → Executable file
View File

@ -1,5 +1,13 @@
/* Copyright 2012 William Woodall and John Harrison */ /* Copyright 2012 William Woodall and John Harrison */
#include <alloca.h> #include <algorithm>
#if !defined(_WIN32) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
# include <alloca.h>
#endif
#if defined (__MINGW32__)
# define alloca __builtin_alloca
#endif
#include "serial/serial.h" #include "serial/serial.h"
@ -17,7 +25,7 @@ using std::size_t;
using std::string; using std::string;
using serial::Serial; using serial::Serial;
using serial::SerialExecption; using serial::SerialException;
using serial::IOException; using serial::IOException;
using serial::bytesize_t; using serial::bytesize_t;
using serial::parity_t; using serial::parity_t;
@ -33,6 +41,10 @@ public:
this->pimpl_->readUnlock(); this->pimpl_->readUnlock();
} }
private: private:
// Disable copy constructors
ScopedReadLock(const ScopedReadLock&);
const ScopedReadLock& operator=(ScopedReadLock);
SerialImpl *pimpl_; SerialImpl *pimpl_;
}; };
@ -45,16 +57,19 @@ public:
this->pimpl_->writeUnlock(); this->pimpl_->writeUnlock();
} }
private: private:
// Disable copy constructors
ScopedWriteLock(const ScopedWriteLock&);
const ScopedWriteLock& operator=(ScopedWriteLock);
SerialImpl *pimpl_; SerialImpl *pimpl_;
}; };
Serial::Serial (const string &port, unsigned long baudrate, long timeout, Serial::Serial (const string &port, uint32_t baudrate, serial::Timeout timeout,
bytesize_t bytesize, parity_t parity, stopbits_t stopbits, bytesize_t bytesize, parity_t parity, stopbits_t stopbits,
flowcontrol_t flowcontrol) flowcontrol_t flowcontrol)
: read_cache_("") : pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
stopbits, flowcontrol))
{ {
pimpl_ = new SerialImpl (port, baudrate, timeout, bytesize, parity, pimpl_->setTimeout(timeout);
stopbits, flowcontrol);
} }
Serial::~Serial () Serial::~Serial ()
@ -86,25 +101,47 @@ Serial::available ()
return pimpl_->available (); return pimpl_->available ();
} }
bool
Serial::waitReadable ()
{
serial::Timeout timeout(pimpl_->getTimeout ());
return pimpl_->waitReadable(timeout.read_timeout_constant);
}
void
Serial::waitByteTimes (size_t count)
{
pimpl_->waitByteTimes(count);
}
size_t size_t
Serial::read_ (unsigned char *buffer, size_t size) Serial::read_ (uint8_t *buffer, size_t size)
{ {
return this->pimpl_->read (buffer, size); return this->pimpl_->read (buffer, size);
} }
size_t size_t
Serial::read (unsigned char *buffer, size_t size) Serial::read (uint8_t *buffer, size_t size)
{ {
ScopedReadLock (this->pimpl_); ScopedReadLock lock(this->pimpl_);
return this->pimpl_->read (buffer, size); return this->pimpl_->read (buffer, size);
} }
size_t size_t
Serial::read (std::vector<unsigned char> &buffer, size_t size) Serial::read (std::vector<uint8_t> &buffer, size_t size)
{ {
ScopedReadLock (this->pimpl_); ScopedReadLock lock(this->pimpl_);
unsigned char *buffer_ = new unsigned char[size]; uint8_t *buffer_ = new uint8_t[size];
size_t bytes_read = this->pimpl_->read (buffer_, size); size_t bytes_read = 0;
try {
bytes_read = this->pimpl_->read (buffer_, size);
}
catch (const std::exception &e) {
delete[] buffer_;
throw;
}
buffer.insert (buffer.end (), buffer_, buffer_+bytes_read); buffer.insert (buffer.end (), buffer_, buffer_+bytes_read);
delete[] buffer_; delete[] buffer_;
return bytes_read; return bytes_read;
@ -113,9 +150,16 @@ Serial::read (std::vector<unsigned char> &buffer, size_t size)
size_t size_t
Serial::read (std::string &buffer, size_t size) Serial::read (std::string &buffer, size_t size)
{ {
ScopedReadLock (this->pimpl_); ScopedReadLock lock(this->pimpl_);
unsigned char *buffer_ = new unsigned char[size]; uint8_t *buffer_ = new uint8_t[size];
size_t bytes_read = this->pimpl_->read (buffer_, size); size_t bytes_read = 0;
try {
bytes_read = this->pimpl_->read (buffer_, size);
}
catch (const std::exception &e) {
delete[] buffer_;
throw;
}
buffer.append (reinterpret_cast<const char*>(buffer_), bytes_read); buffer.append (reinterpret_cast<const char*>(buffer_), bytes_read);
delete[] buffer_; delete[] buffer_;
return bytes_read; return bytes_read;
@ -132,10 +176,10 @@ Serial::read (size_t size)
size_t size_t
Serial::readline (string &buffer, size_t size, string eol) Serial::readline (string &buffer, size_t size, string eol)
{ {
ScopedReadLock (this->pimpl_); ScopedReadLock lock(this->pimpl_);
size_t eol_len = eol.length (); size_t eol_len = eol.length ();
unsigned char *buffer_ = static_cast<unsigned char*> uint8_t *buffer_ = static_cast<uint8_t*>
(alloca (size * sizeof (unsigned char))); (alloca (size * sizeof (uint8_t)));
size_t read_so_far = 0; size_t read_so_far = 0;
while (true) while (true)
{ {
@ -144,6 +188,7 @@ Serial::readline (string &buffer, size_t size, string eol)
if (bytes_read == 0) { if (bytes_read == 0) {
break; // Timeout occured on reading 1 byte break; // Timeout occured on reading 1 byte
} }
if(read_so_far < eol_len) continue;
if (string (reinterpret_cast<const char*> if (string (reinterpret_cast<const char*>
(buffer_ + read_so_far - eol_len), eol_len) == eol) { (buffer_ + read_so_far - eol_len), eol_len) == eol) {
break; // EOL found break; // EOL found
@ -167,11 +212,11 @@ Serial::readline (size_t size, string eol)
vector<string> vector<string>
Serial::readlines (size_t size, string eol) Serial::readlines (size_t size, string eol)
{ {
ScopedReadLock (this->pimpl_); ScopedReadLock lock(this->pimpl_);
std::vector<std::string> lines; std::vector<std::string> lines;
size_t eol_len = eol.length (); size_t eol_len = eol.length ();
unsigned char *buffer_ = static_cast<unsigned char*> uint8_t *buffer_ = static_cast<uint8_t*>
(alloca (size * sizeof (unsigned char))); (alloca (size * sizeof (uint8_t)));
size_t read_so_far = 0; size_t read_so_far = 0;
size_t start_of_line = 0; size_t start_of_line = 0;
while (read_so_far < size) { while (read_so_far < size) {
@ -185,6 +230,7 @@ Serial::readlines (size_t size, string eol)
} }
break; // Timeout occured on reading 1 byte break; // Timeout occured on reading 1 byte
} }
if(read_so_far < eol_len) continue;
if (string (reinterpret_cast<const char*> if (string (reinterpret_cast<const char*>
(buffer_ + read_so_far - eol_len), eol_len) == eol) { (buffer_ + read_so_far - eol_len), eol_len) == eol) {
// EOL found // EOL found
@ -208,15 +254,36 @@ Serial::readlines (size_t size, string eol)
size_t size_t
Serial::write (const string &data) Serial::write (const string &data)
{ {
ScopedWriteLock(this->pimpl_); ScopedWriteLock lock(this->pimpl_);
return pimpl_->write (data); return this->write_ (reinterpret_cast<const uint8_t*>(data.c_str()),
data.length());
}
size_t
Serial::write (const std::vector<uint8_t> &data)
{
ScopedWriteLock lock(this->pimpl_);
return this->write_ (&data[0], data.size());
}
size_t
Serial::write (const uint8_t *data, size_t size)
{
ScopedWriteLock lock(this->pimpl_);
return this->write_(data, size);
}
size_t
Serial::write_ (const uint8_t *data, size_t length)
{
return pimpl_->write (data, length);
} }
void void
Serial::setPort (const string &port) Serial::setPort (const string &port)
{ {
ScopedReadLock(this->pimpl_); ScopedReadLock rlock(this->pimpl_);
ScopedWriteLock(this->pimpl_); ScopedWriteLock wlock(this->pimpl_);
bool was_open = pimpl_->isOpen (); bool was_open = pimpl_->isOpen ();
if (was_open) close(); if (was_open) close();
pimpl_->setPort (port); pimpl_->setPort (port);
@ -230,26 +297,26 @@ Serial::getPort () const
} }
void void
Serial::setTimeout (long timeout) Serial::setTimeout (serial::Timeout &timeout)
{ {
pimpl_->setTimeout (timeout); pimpl_->setTimeout (timeout);
} }
long serial::Timeout
Serial::getTimeout () const { Serial::getTimeout () const {
return pimpl_->getTimeout (); return pimpl_->getTimeout ();
} }
void void
Serial::setBaudrate (unsigned long baudrate) Serial::setBaudrate (uint32_t baudrate)
{ {
pimpl_->setBaudrate (baudrate); pimpl_->setBaudrate (baudrate);
} }
unsigned long uint32_t
Serial::getBaudrate () const Serial::getBaudrate () const
{ {
return pimpl_->getBaudrate (); return uint32_t(pimpl_->getBaudrate ());
} }
void void
@ -302,23 +369,21 @@ Serial::getFlowcontrol () const
void Serial::flush () void Serial::flush ()
{ {
ScopedReadLock(this->pimpl_); ScopedReadLock rlock(this->pimpl_);
ScopedWriteLock(this->pimpl_); ScopedWriteLock wlock(this->pimpl_);
pimpl_->flush (); pimpl_->flush ();
read_cache_.clear ();
} }
void Serial::flushInput () void Serial::flushInput ()
{ {
ScopedReadLock(this->pimpl_); ScopedReadLock lock(this->pimpl_);
pimpl_->flushInput (); pimpl_->flushInput ();
} }
void Serial::flushOutput () void Serial::flushOutput ()
{ {
ScopedWriteLock(this->pimpl_); ScopedWriteLock lock(this->pimpl_);
pimpl_->flushOutput (); pimpl_->flushOutput ();
read_cache_.clear ();
} }
void Serial::sendBreak (int duration) void Serial::sendBreak (int duration)
@ -341,6 +406,11 @@ void Serial::setDTR (bool level)
pimpl_->setDTR (level); pimpl_->setDTR (level);
} }
bool Serial::waitForChange()
{
return pimpl_->waitForChange();
}
bool Serial::getCTS () bool Serial::getCTS ()
{ {
return pimpl_->getCTS (); return pimpl_->getCTS ();

View File

@ -1,9 +0,0 @@
<stack>
<description brief="serial">serial</description>
<author>Maintained by William Woodall</author>
<license>BSD</license>
<review status="unreviewed" notes=""/>
<url>http://ros.org/wiki/serial</url>
<depend stack="ros" />
</stack>

12
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,12 @@
if(UNIX)
catkin_add_gtest(${PROJECT_NAME}-test unix_serial_tests.cc)
target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
if(NOT APPLE)
target_link_libraries(${PROJECT_NAME}-test util)
endif()
if(NOT APPLE) # these tests are unreliable on macOS
catkin_add_gtest(${PROJECT_NAME}-test-timer unit/unix_timer_tests.cc)
target_link_libraries(${PROJECT_NAME}-test-timer ${PROJECT_NAME})
endif()
endif()

View File

@ -0,0 +1,63 @@
#include "gtest/gtest.h"
#include "serial/impl/unix.h"
#include <unistd.h>
#include <stdlib.h>
using serial::MillisecondTimer;
namespace {
/**
* Do 100 trials of timing gaps between 0 and 19 milliseconds.
* Expect accuracy within one millisecond.
*/
TEST(timer_tests, short_intervals) {
for (int trial = 0; trial < 100; trial++)
{
uint32_t ms = rand() % 20;
MillisecondTimer mt(ms);
usleep(1000 * ms);
int32_t r = mt.remaining();
// 1ms slush, for the cost of calling usleep.
EXPECT_NEAR(r+1, 0, 1);
}
}
TEST(timer_tests, overlapping_long_intervals) {
MillisecondTimer* timers[10];
// Experimentally determined. Corresponds to the extra time taken by the loops,
// the big usleep, and the test infrastructure itself.
const int slush_factor = 14;
// Set up the timers to each time one second, 1ms apart.
for (int t = 0; t < 10; t++)
{
timers[t] = new MillisecondTimer(1000);
usleep(1000);
}
// Check in on them after 500ms.
usleep(500000);
for (int t = 0; t < 10; t++)
{
EXPECT_NEAR(timers[t]->remaining(), 500 - slush_factor + t, 5);
}
// Check in on them again after another 500ms and free them.
usleep(500000);
for (int t = 0; t < 10; t++)
{
EXPECT_NEAR(timers[t]->remaining(), -slush_factor + t, 5);
delete timers[t];
}
}
} // namespace
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -20,8 +20,6 @@ void loop()
#include <string> #include <string>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <boost/bind.hpp>
// Use FRIEND_TEST... its not as nasty, thats what friends are for // Use FRIEND_TEST... its not as nasty, thats what friends are for
// // OMG this is so nasty... // // OMG this is so nasty...
// #define private public // #define private public
@ -29,7 +27,7 @@ void loop()
#include "serial/serial.h" #include "serial/serial.h"
#ifdef __linux__ #if defined(__linux__)
#include <pty.h> #include <pty.h>
#else #else
#include <util.h> #include <util.h>
@ -53,7 +51,7 @@ protected:
ASSERT_TRUE(slave_fd > 0); ASSERT_TRUE(slave_fd > 0);
ASSERT_TRUE(string(name).length() > 0); ASSERT_TRUE(string(name).length() > 0);
port1 = new Serial(string(name), 115200, 250); port1 = new Serial(string(name), 115200, Timeout::simpleTimeout(250));
} }
virtual void TearDown() { virtual void TearDown() {

View File

@ -0,0 +1,147 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}</ProjectGuid>
<RootNamespace>serial</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<PlatformToolset Condition="'$(PlatformToolset)' == '' and '$(VisualStudioVersion)' == '14.0'">v140_xp</PlatformToolset>
<PlatformToolset Condition="'$(PlatformToolset)' == '' and '$(VisualStudioVersion)' == '12.0'">v120_xp</PlatformToolset>
<PlatformToolset Condition="'$(PlatformToolset)' == '' and '$(VisualStudioVersion)' == '11.0'">v110_xp</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<Lib>
<AdditionalDependencies>setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Windows</SubSystem>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<Lib>
<AdditionalDependencies>setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Windows</SubSystem>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
<Lib>
<AdditionalDependencies>setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Windows</SubSystem>
</Lib>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
<Lib>
<AdditionalDependencies>setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Windows</SubSystem>
</Lib>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\src\impl\list_ports\list_ports_win.cc" />
<ClCompile Include="..\..\src\impl\win.cc" />
<ClCompile Include="..\..\src\serial.cc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\serial\impl\win.h" />
<ClInclude Include="..\..\include\serial\serial.h" />
<ClInclude Include="..\..\include\serial\v8stdint.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\serial.cc">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\impl\win.cc">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\src\impl\list_ports\list_ports_win.cc">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\serial\serial.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\serial\v8stdint.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\serial\impl\win.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1,133 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}</ProjectGuid>
<RootNamespace>test_serial</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<PlatformToolset Condition="'$(PlatformToolset)' == '' and '$(VisualStudioVersion)' == '14.0'">v140_xp</PlatformToolset>
<PlatformToolset Condition="'$(PlatformToolset)' == '' and '$(VisualStudioVersion)' == '12.0'">v120_xp</PlatformToolset>
<PlatformToolset Condition="'$(PlatformToolset)' == '' and '$(VisualStudioVersion)' == '11.0'">v110_xp</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>$(ProjectDir)..\..\include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\examples\serial_example.cc" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\serial\serial.vcxproj">
<Project>{a8517fb2-c74e-43bd-b3c6-b05d3fc11ecd}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\examples\serial_example.cc">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "serial", "serial\serial.vcxproj", "{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_serial", "test_serial\test_serial.vcxproj", "{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}.Debug|Win32.ActiveCfg = Debug|Win32
{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}.Debug|Win32.Build.0 = Debug|Win32
{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}.Debug|x64.ActiveCfg = Debug|x64
{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}.Debug|x64.Build.0 = Debug|x64
{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}.Release|Win32.ActiveCfg = Release|Win32
{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}.Release|Win32.Build.0 = Release|Win32
{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}.Release|x64.ActiveCfg = Release|x64
{A8517FB2-C74E-43BD-B3C6-B05D3FC11ECD}.Release|x64.Build.0 = Release|x64
{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}.Debug|Win32.ActiveCfg = Debug|Win32
{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}.Debug|Win32.Build.0 = Debug|Win32
{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}.Debug|x64.ActiveCfg = Debug|x64
{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}.Debug|x64.Build.0 = Debug|x64
{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}.Release|Win32.ActiveCfg = Release|Win32
{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}.Release|Win32.Build.0 = Release|Win32
{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}.Release|x64.ActiveCfg = Release|x64
{92EE7E58-C737-41F9-B795-9B6ACF6AB0B8}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal