mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-22 19:54:57 +08:00
Compare commits
No commits in common. "main" and "1.1.2" have entirely different histories.
7
.gitignore
vendored
7
.gitignore
vendored
@ -25,10 +25,3 @@ msg_gen
|
|||||||
srv_gen
|
srv_gen
|
||||||
doc/html
|
doc/html
|
||||||
*sublime-workspace
|
*sublime-workspace
|
||||||
*.user
|
|
||||||
*.suo
|
|
||||||
*.sdf
|
|
||||||
*.opensdf
|
|
||||||
ipch
|
|
||||||
Debug
|
|
||||||
Release
|
|
||||||
|
|||||||
16
.travis.yml
16
.travis.yml
@ -1,10 +1,12 @@
|
|||||||
os:
|
|
||||||
- linux
|
|
||||||
- osx
|
|
||||||
language: cpp
|
language: cpp
|
||||||
|
compiler:
|
||||||
|
- gcc
|
||||||
|
- clang
|
||||||
install:
|
install:
|
||||||
- make install_deps
|
- sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu precise main" > /etc/apt/sources.list.d/ros-latest.list'
|
||||||
- source setup.bash
|
- wget http://packages.ros.org/ros.key -O - | sudo apt-key add -
|
||||||
|
- sudo apt-get update
|
||||||
|
- sudo apt-get install ros-groovy-catkin
|
||||||
|
- source /opt/ros/groovy/setup.bash
|
||||||
script:
|
script:
|
||||||
- mkdir build && cd build && cmake .. -DPYTHON_EXECUTABLE=$(which python2) && make && make tests && make run_tests
|
- make
|
||||||
- catkin_test_results .
|
|
||||||
|
|||||||
@ -1,85 +0,0 @@
|
|||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
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.
|
|
||||||
@ -4,15 +4,8 @@ project(serial)
|
|||||||
# Find catkin
|
# Find catkin
|
||||||
find_package(catkin REQUIRED)
|
find_package(catkin REQUIRED)
|
||||||
|
|
||||||
if(APPLE)
|
|
||||||
find_library(IOKIT_LIBRARY IOKit)
|
|
||||||
find_library(FOUNDATION_LIBRARY Foundation)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(UNIX AND NOT APPLE)
|
if(UNIX AND NOT APPLE)
|
||||||
# If Linux, add rt and pthread
|
# If Linux, add rt and pthread
|
||||||
set(rt_LIBRARIES rt)
|
|
||||||
set(pthread_LIBRARIES pthread)
|
|
||||||
catkin_package(
|
catkin_package(
|
||||||
LIBRARIES ${PROJECT_NAME}
|
LIBRARIES ${PROJECT_NAME}
|
||||||
INCLUDE_DIRS include
|
INCLUDE_DIRS include
|
||||||
@ -32,28 +25,18 @@ set(serial_SRCS
|
|||||||
include/serial/serial.h
|
include/serial/serial.h
|
||||||
include/serial/v8stdint.h
|
include/serial/v8stdint.h
|
||||||
)
|
)
|
||||||
if(APPLE)
|
if(UNIX)
|
||||||
# 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
|
# If unix
|
||||||
list(APPEND serial_SRCS src/impl/unix.cc)
|
list(APPEND serial_SRCS src/impl/unix.cc)
|
||||||
list(APPEND serial_SRCS src/impl/list_ports/list_ports_linux.cc)
|
|
||||||
else()
|
else()
|
||||||
# If windows
|
# If windows
|
||||||
list(APPEND serial_SRCS src/impl/win.cc)
|
list(APPEND serial_SRCS src/impl/win.cc)
|
||||||
list(APPEND serial_SRCS src/impl/list_ports/list_ports_win.cc)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
## Add serial library
|
## Add serial library
|
||||||
add_library(${PROJECT_NAME} ${serial_SRCS})
|
add_library(${PROJECT_NAME} ${serial_SRCS})
|
||||||
if(APPLE)
|
if(UNIX AND NOT APPLE)
|
||||||
target_link_libraries(${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
|
target_link_libraries(${PROJECT_NAME} rt)
|
||||||
elseif(UNIX)
|
|
||||||
target_link_libraries(${PROJECT_NAME} rt pthread)
|
|
||||||
else()
|
|
||||||
target_link_libraries(${PROJECT_NAME} setupapi)
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
## Uncomment for example
|
## Uncomment for example
|
||||||
@ -65,17 +48,12 @@ target_link_libraries(serial_example ${PROJECT_NAME})
|
|||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|
||||||
## Install executable
|
## Install executable
|
||||||
install(TARGETS ${PROJECT_NAME}
|
install(
|
||||||
|
TARGETS ${PROJECT_NAME}
|
||||||
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
|
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
|
||||||
LIBRARY 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
|
## Tests
|
||||||
if(CATKIN_ENABLE_TESTING)
|
catkin_add_gtest(${PROJECT_NAME}-test tests/serial_tests.cc)
|
||||||
add_subdirectory(tests)
|
target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
|
||||||
endif()
|
|
||||||
|
|||||||
7
LICENSE
7
LICENSE
@ -1,7 +0,0 @@
|
|||||||
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.
|
|
||||||
29
Makefile
29
Makefile
@ -1,36 +1,13 @@
|
|||||||
all: serial
|
all: serial
|
||||||
|
|
||||||
CMAKE_FLAGS := -DCMAKE_INSTALL_PREFIX=/tmp/usr/local
|
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
|
|
||||||
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:
|
install:
|
||||||
cd build && make install
|
cd build && make install
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
cd build && make uninstall
|
||||||
|
|
||||||
serial:
|
serial:
|
||||||
@mkdir -p build
|
@mkdir -p build
|
||||||
cd build && cmake $(CMAKE_FLAGS) ..
|
cd build && cmake $(CMAKE_FLAGS) ..
|
||||||
|
|||||||
37
README.md
37
README.md
@ -1,7 +1,5 @@
|
|||||||
# Serial Communication Library
|
# Serial Communication Library
|
||||||
|
|
||||||
[](https://travis-ci.org/wjwwood/serial)*(Linux and OS X)* [](https://ci.appveyor.com/project/wjwwood/serial)*(Windows)*
|
|
||||||
|
|
||||||
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++.
|
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++.
|
||||||
|
|
||||||
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.
|
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.
|
||||||
@ -10,22 +8,17 @@ Serial is a class that provides the basic interface common to serial libraries (
|
|||||||
|
|
||||||
### Documentation
|
### Documentation
|
||||||
|
|
||||||
Website: http://wjwwood.github.io/serial/
|
Website: http://wjwwood.github.com/serial/
|
||||||
|
|
||||||
API Documentation: http://wjwwood.github.io/serial/doc/1.1.0/index.html
|
API Documentation: http://wjwwood.github.com/serial/doc/1.1.0/index.html
|
||||||
|
|
||||||
### Dependencies
|
### Dependencies
|
||||||
|
|
||||||
Required:
|
|
||||||
* [catkin](http://www.ros.org/wiki/catkin) - cmake and Python based buildsystem
|
* [catkin](http://www.ros.org/wiki/catkin) - cmake and Python based buildsystem
|
||||||
* [cmake](http://www.cmake.org) - buildsystem
|
* [cmake](http://www.cmake.org) - buildsystem
|
||||||
* [Python](http://www.python.org) - scripting language
|
* [Python](http://www.python.org) - scripting language
|
||||||
* [empy](http://www.alcyone.com/pyos/empy/) - Python templating library
|
* [empy](http://www.alcyone.com/pyos/empy/) - Python templating library
|
||||||
* [catkin_pkg](http://pypi.python.org/pypi/catkin_pkg/) - Runtime Python library for catkin
|
* [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
|
### Install
|
||||||
|
|
||||||
@ -43,15 +36,27 @@ Build and run the tests:
|
|||||||
|
|
||||||
Build the documentation:
|
Build the documentation:
|
||||||
|
|
||||||
make doc
|
make docs
|
||||||
|
|
||||||
Install:
|
Install:
|
||||||
|
|
||||||
make install
|
make install
|
||||||
|
|
||||||
|
Uninstall:
|
||||||
|
|
||||||
|
make uninstall
|
||||||
|
|
||||||
### License
|
### License
|
||||||
|
|
||||||
[The MIT License](LICENSE)
|
The BSD License
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
### Authors
|
### Authors
|
||||||
|
|
||||||
@ -60,4 +65,4 @@ John Harrison <ash.gti@gmail.com>
|
|||||||
|
|
||||||
### Contact
|
### Contact
|
||||||
|
|
||||||
William Woodall <william@osrfoundation.org>
|
William Woodall <wwoodall@willowgarage.com>
|
||||||
|
|||||||
2504
doc/Doxyfile
2504
doc/Doxyfile
File diff suppressed because it is too large
Load Diff
@ -55,4 +55,10 @@ To install simply:
|
|||||||
sudo make install
|
sudo make install
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
To uninstall simply:
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
sudo make uninstall
|
||||||
|
</pre>
|
||||||
|
|
||||||
*/
|
*/
|
||||||
@ -2,12 +2,12 @@
|
|||||||
* 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>
|
||||||
* void setup() {
|
* void setup() {
|
||||||
* Serial.begin(<insert your baudrate here>);
|
* Serial.begin(<insert your baudrate here>);
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* void loop() {
|
* void loop() {
|
||||||
* if (Serial.available()) {
|
* if (Serial.available()) {
|
||||||
* Serial.write(Serial.read());
|
* Serial.write(Serial.read());
|
||||||
@ -34,7 +34,6 @@ 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
|
||||||
@ -44,53 +43,19 @@ void my_sleep(unsigned long milliseconds) {
|
|||||||
#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 < 2) {
|
if(argc < 3) {
|
||||||
print_usage();
|
cerr << "Usage: test_serial <serial port address> ";
|
||||||
|
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, serial::Timeout::simpleTimeout(1000));
|
serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
|
||||||
|
|||||||
@ -35,8 +35,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if !defined(_WIN32)
|
|
||||||
|
|
||||||
#ifndef SERIAL_IMPL_UNIX_H
|
#ifndef SERIAL_IMPL_UNIX_H
|
||||||
#define SERIAL_IMPL_UNIX_H
|
#define SERIAL_IMPL_UNIX_H
|
||||||
|
|
||||||
@ -46,23 +44,12 @@
|
|||||||
|
|
||||||
namespace serial {
|
namespace serial {
|
||||||
|
|
||||||
using std::size_t;
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
|
|
||||||
using serial::SerialException;
|
using serial::SerialExecption;
|
||||||
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,
|
||||||
@ -86,12 +73,6 @@ public:
|
|||||||
size_t
|
size_t
|
||||||
available ();
|
available ();
|
||||||
|
|
||||||
bool
|
|
||||||
waitReadable (uint32_t timeout);
|
|
||||||
|
|
||||||
void
|
|
||||||
waitByteTimes (size_t count);
|
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
read (uint8_t *buf, size_t size = 1);
|
read (uint8_t *buf, size_t size = 1);
|
||||||
|
|
||||||
@ -199,9 +180,8 @@ private:
|
|||||||
bool xonxoff_;
|
bool xonxoff_;
|
||||||
bool rtscts_;
|
bool rtscts_;
|
||||||
|
|
||||||
Timeout 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
|
||||||
@ -217,5 +197,3 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif // SERIAL_IMPL_UNIX_H
|
#endif // SERIAL_IMPL_UNIX_H
|
||||||
|
|
||||||
#endif // !defined(_WIN32)
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2012 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,8 +34,6 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if defined(_WIN32)
|
|
||||||
|
|
||||||
#ifndef SERIAL_IMPL_WINDOWS_H
|
#ifndef SERIAL_IMPL_WINDOWS_H
|
||||||
#define SERIAL_IMPL_WINDOWS_H
|
#define SERIAL_IMPL_WINDOWS_H
|
||||||
|
|
||||||
@ -46,10 +44,9 @@
|
|||||||
namespace serial {
|
namespace serial {
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::wstring;
|
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
|
|
||||||
using serial::SerialException;
|
using serial::SerialExecption;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
|
|
||||||
class serial::Serial::SerialImpl {
|
class serial::Serial::SerialImpl {
|
||||||
@ -74,12 +71,6 @@ public:
|
|||||||
|
|
||||||
size_t
|
size_t
|
||||||
available ();
|
available ();
|
||||||
|
|
||||||
bool
|
|
||||||
waitReadable (uint32_t timeout);
|
|
||||||
|
|
||||||
void
|
|
||||||
waitByteTimes (size_t count);
|
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
read (uint8_t *buf, size_t size = 1);
|
read (uint8_t *buf, size_t size = 1);
|
||||||
@ -181,7 +172,7 @@ protected:
|
|||||||
void reconfigurePort ();
|
void reconfigurePort ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wstring port_; // Path to the file descriptor
|
string port_; // Path to the file descriptor
|
||||||
HANDLE fd_;
|
HANDLE fd_;
|
||||||
|
|
||||||
bool is_open_;
|
bool is_open_;
|
||||||
@ -203,5 +194,3 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif // SERIAL_IMPL_WINDOWS_H
|
#endif // SERIAL_IMPL_WINDOWS_H
|
||||||
|
|
||||||
#endif // if defined(_WIN32)
|
|
||||||
|
|||||||
@ -66,9 +66,7 @@ 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;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -76,8 +74,8 @@ typedef enum {
|
|||||||
*/
|
*/
|
||||||
typedef enum {
|
typedef enum {
|
||||||
stopbits_one = 1,
|
stopbits_one = 1,
|
||||||
stopbits_two = 2,
|
stopbits_one_point_five,
|
||||||
stopbits_one_point_five
|
stopbits_two = 2
|
||||||
} stopbits_t;
|
} stopbits_t;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -90,9 +88,9 @@ typedef enum {
|
|||||||
} flowcontrol_t;
|
} flowcontrol_t;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Structure for setting the timeout of the serial port, times are
|
* Structure for setting the timeout of the serial port, times are
|
||||||
* in milliseconds.
|
* in milliseconds.
|
||||||
*
|
*
|
||||||
* In order to disable the interbyte timeout, set it to Timeout::max().
|
* In order to disable the interbyte timeout, set it to Timeout::max().
|
||||||
*/
|
*/
|
||||||
struct Timeout {
|
struct Timeout {
|
||||||
@ -104,7 +102,7 @@ struct Timeout {
|
|||||||
* Convenience function to generate Timeout structs using a
|
* Convenience function to generate Timeout structs using a
|
||||||
* single absolute timeout.
|
* single absolute timeout.
|
||||||
*
|
*
|
||||||
* \param timeout A long that defines the time in milliseconds until a
|
* \param timeout A long that defines the time in milliseconds until a
|
||||||
* timeout occurs after a call to read or write is made.
|
* timeout occurs after a call to read or write is made.
|
||||||
*
|
*
|
||||||
* \return Timeout struct that represents this simple timeout provided.
|
* \return Timeout struct that represents this simple timeout provided.
|
||||||
@ -117,13 +115,13 @@ struct Timeout {
|
|||||||
uint32_t inter_byte_timeout;
|
uint32_t inter_byte_timeout;
|
||||||
/*! A constant number of milliseconds to wait after calling read. */
|
/*! A constant number of milliseconds to wait after calling read. */
|
||||||
uint32_t read_timeout_constant;
|
uint32_t read_timeout_constant;
|
||||||
/*! A multiplier against the number of requested bytes to wait after
|
/*! A multiplier against the number of requested bytes to wait after
|
||||||
* calling read.
|
* calling read.
|
||||||
*/
|
*/
|
||||||
uint32_t read_timeout_multiplier;
|
uint32_t read_timeout_multiplier;
|
||||||
/*! A constant number of milliseconds to wait after calling write. */
|
/*! A constant number of milliseconds to wait after calling write. */
|
||||||
uint32_t write_timeout_constant;
|
uint32_t write_timeout_constant;
|
||||||
/*! A multiplier against the number of requested bytes to wait after
|
/*! A multiplier against the number of requested bytes to wait after
|
||||||
* calling write.
|
* calling write.
|
||||||
*/
|
*/
|
||||||
uint32_t write_timeout_multiplier;
|
uint32_t write_timeout_multiplier;
|
||||||
@ -147,7 +145,7 @@ struct Timeout {
|
|||||||
class Serial {
|
class Serial {
|
||||||
public:
|
public:
|
||||||
/*!
|
/*!
|
||||||
* Creates a Serial object and opens the port if a port is specified,
|
* Creates a Serial object and opens the port if a port is specified,
|
||||||
* otherwise it remains closed until serial::Serial::open is called.
|
* 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,
|
||||||
@ -156,7 +154,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \param baudrate An unsigned 32-bit integer that represents the baudrate
|
* \param baudrate An unsigned 32-bit integer that represents the baudrate
|
||||||
*
|
*
|
||||||
* \param timeout A serial::Timeout struct that defines the timeout
|
* \param timeout A serial::Timeout struct that defines the timeout
|
||||||
* conditions for the serial port. \see serial::Timeout
|
* conditions for the serial port. \see serial::Timeout
|
||||||
*
|
*
|
||||||
* \param bytesize Size of each byte in the serial transmission of data,
|
* \param bytesize Size of each byte in the serial transmission of data,
|
||||||
@ -173,9 +171,7 @@ public:
|
|||||||
* flowcontrol_none, possible values are: flowcontrol_none,
|
* flowcontrol_none, possible values are: flowcontrol_none,
|
||||||
* flowcontrol_software, flowcontrol_hardware
|
* flowcontrol_software, flowcontrol_hardware
|
||||||
*
|
*
|
||||||
* \throw serial::PortNotOpenedException
|
* \throw PortNotOpenedException
|
||||||
* \throw serial::IOException
|
|
||||||
* \throw std::invalid_argument
|
|
||||||
*/
|
*/
|
||||||
Serial (const std::string &port = "",
|
Serial (const std::string &port = "",
|
||||||
uint32_t baudrate = 9600,
|
uint32_t baudrate = 9600,
|
||||||
@ -198,7 +194,7 @@ public:
|
|||||||
* \see Serial::Serial
|
* \see Serial::Serial
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw std::invalid_argument
|
||||||
* \throw serial::SerialException
|
* \throw serial::SerialExecption
|
||||||
* \throw serial::IOException
|
* \throw serial::IOException
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
@ -219,88 +215,62 @@ public:
|
|||||||
size_t
|
size_t
|
||||||
available ();
|
available ();
|
||||||
|
|
||||||
/*! 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.
|
/*! Read a given amount of bytes from the serial port into a given buffer.
|
||||||
*
|
*
|
||||||
* The read function will return in one of three cases:
|
* The read function will return in one of three cases:
|
||||||
* * The number of requested bytes was read.
|
* * The number of requested bytes was read.
|
||||||
* * In this case the number of bytes requested will match the size_t
|
* * In this case the number of bytes requested will match the size_t
|
||||||
* returned by read.
|
* returned by read.
|
||||||
* * A timeout occurred, in this case the number of bytes read will not
|
* * 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
|
* match the amount requested, but no exception will be thrown. One of
|
||||||
* two possible timeouts occurred:
|
* two possible timeouts occurred:
|
||||||
* * The inter byte timeout expired, this means that number of
|
* * The inter byte timeout expired, this means that number of
|
||||||
* milliseconds elapsed between receiving bytes from the serial port
|
* milliseconds elapsed between receiving bytes from the serial port
|
||||||
* exceeded the inter byte timeout.
|
* exceeded the inter byte timeout.
|
||||||
* * The total timeout expired, which is calculated by multiplying the
|
* * The total timeout expired, which is calculated by multiplying the
|
||||||
* read timeout multiplier by the number of requested bytes and then
|
* read timeout multiplier by the number of requested bytes and then
|
||||||
* added to the read timeout constant. If that total number of
|
* added to the read timeout constant. If that total number of
|
||||||
* milliseconds elapses after the initial call to read a timeout will
|
* milliseconds elapses after the initial call to read a timeout will
|
||||||
* occur.
|
* occur.
|
||||||
* * An exception occurred, in this case an actual exception will be thrown.
|
* * 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 buffer An uint8_t array of at least the requested size.
|
||||||
* \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 size_t representing the number of bytes read as a result of the
|
* \return A size_t representing the number of bytes read as a result of the
|
||||||
* call to read.
|
* call to read.
|
||||||
*
|
|
||||||
* \throw serial::PortNotOpenedException
|
|
||||||
* \throw serial::SerialException
|
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
read (uint8_t *buffer, size_t size);
|
read (uint8_t *buffer, size_t size);
|
||||||
|
|
||||||
/*! Read a given amount of bytes from the serial port into a give buffer.
|
/*! 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 buffer A reference to a std::vector of uint8_t.
|
||||||
* \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 size_t representing the number of bytes read as a result of the
|
* \return A size_t representing the number of bytes read as a result of the
|
||||||
* call to read.
|
* call to read.
|
||||||
*
|
|
||||||
* \throw serial::PortNotOpenedException
|
|
||||||
* \throw serial::SerialException
|
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
read (std::vector<uint8_t> &buffer, size_t size = 1);
|
read (std::vector<uint8_t> &buffer, size_t size = 1);
|
||||||
|
|
||||||
/*! Read a given amount of bytes from the serial port into a give buffer.
|
/*! Read a given amount of bytes from the serial port into a give buffer.
|
||||||
*
|
*
|
||||||
* \param buffer A reference to a std::string.
|
* \param buffer A reference to a std::string.
|
||||||
* \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 size_t representing the number of bytes read as a result of the
|
* \return A size_t representing the number of bytes read as a result of the
|
||||||
* call to read.
|
* call to read.
|
||||||
*
|
|
||||||
* \throw serial::PortNotOpenedException
|
|
||||||
* \throw serial::SerialException
|
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
read (std::string &buffer, size_t size = 1);
|
read (std::string &buffer, size_t size = 1);
|
||||||
|
|
||||||
/*! Read a given amount of bytes from the serial port and return a string
|
/*! Read a given amount of bytes from the serial port and return a string
|
||||||
* containing the data.
|
* 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 from the port.
|
* \return A std::string containing the data read from the port.
|
||||||
*
|
|
||||||
* \throw serial::PortNotOpenedException
|
|
||||||
* \throw serial::SerialException
|
|
||||||
*/
|
*/
|
||||||
std::string
|
std::string
|
||||||
read (size_t size = 1);
|
read (size_t size = 1);
|
||||||
@ -314,9 +284,6 @@ public:
|
|||||||
* \param eol A string to match against for the EOL.
|
* \param eol A string to match against for the EOL.
|
||||||
*
|
*
|
||||||
* \return A size_t representing the number of bytes read.
|
* \return A size_t representing the number of bytes read.
|
||||||
*
|
|
||||||
* \throw serial::PortNotOpenedException
|
|
||||||
* \throw serial::SerialException
|
|
||||||
*/
|
*/
|
||||||
size_t
|
size_t
|
||||||
readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
|
readline (std::string &buffer, size_t size = 65536, std::string eol = "\n");
|
||||||
@ -329,9 +296,6 @@ 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
|
|
||||||
*/
|
*/
|
||||||
std::string
|
std::string
|
||||||
readline (size_t size = 65536, std::string eol = "\n");
|
readline (size_t size = 65536, std::string eol = "\n");
|
||||||
@ -342,13 +306,10 @@ public:
|
|||||||
* 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");
|
||||||
@ -358,15 +319,11 @@ public:
|
|||||||
* \param data A const reference containing the data to be written
|
* \param data A const reference containing the data to be written
|
||||||
* to the serial port.
|
* to the serial port.
|
||||||
*
|
*
|
||||||
* \param size A size_t that indicates how many bytes should be written from
|
* \param size A size_t that indicates how many bytes should be written from
|
||||||
* the given data buffer.
|
* the given data buffer.
|
||||||
*
|
*
|
||||||
* \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 uint8_t *data, size_t size);
|
write (const uint8_t *data, size_t size);
|
||||||
@ -378,10 +335,6 @@ public:
|
|||||||
*
|
*
|
||||||
* \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 std::vector<uint8_t> &data);
|
write (const std::vector<uint8_t> &data);
|
||||||
@ -393,10 +346,6 @@ public:
|
|||||||
*
|
*
|
||||||
* \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 std::string &data);
|
write (const std::string &data);
|
||||||
@ -407,7 +356,7 @@ public:
|
|||||||
* 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 std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
setPort (const std::string &port);
|
setPort (const std::string &port);
|
||||||
@ -416,7 +365,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \see Serial::setPort
|
* \see Serial::setPort
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
std::string
|
std::string
|
||||||
getPort () const;
|
getPort () const;
|
||||||
@ -425,36 +374,34 @@ public:
|
|||||||
*
|
*
|
||||||
* There are two timeout conditions described here:
|
* There are two timeout conditions described here:
|
||||||
* * The inter byte timeout:
|
* * The inter byte timeout:
|
||||||
* * The inter_byte_timeout component of serial::Timeout defines the
|
* * The inter_byte_timeout component of serial::Timeout defines the
|
||||||
* maximum amount of time, in milliseconds, between receiving bytes on
|
* maximum amount of time, in milliseconds, between receiving bytes on
|
||||||
* the serial port that can pass before a timeout occurs. Setting this
|
* the serial port that can pass before a timeout occurs. Setting this
|
||||||
* to zero will prevent inter byte timeouts from occurring.
|
* to zero will prevent inter byte timeouts from occurring.
|
||||||
* * Total time timeout:
|
* * Total time timeout:
|
||||||
* * The constant and multiplier component of this timeout condition,
|
* * The the constant and multiplier component of this timeout condition,
|
||||||
* for both read and write, are defined in serial::Timeout. This
|
* for both read and write, are defined in serial::Timeout. This
|
||||||
* timeout occurs if the total time since the read or write call was
|
* timeout occurs if the total time since the read or write call was
|
||||||
* made exceeds the specified time in milliseconds.
|
* made exceeds the specified time in milliseconds.
|
||||||
* * The limit is defined by multiplying the multiplier component by the
|
* * The limit is defined by multiplying the multiplier component by the
|
||||||
* number of requested bytes and adding that product to the constant
|
* number of requested bytes and adding that product to the constant
|
||||||
* component. In this way if you want a read call, for example, to
|
* component. In this way if you want a read call, for example, to
|
||||||
* timeout after exactly one second regardless of the number of bytes
|
* timeout after exactly one second regardless of the number of bytes
|
||||||
* you asked for then set the read_timeout_constant component of
|
* you asked for then set the read_timeout_constant component of
|
||||||
* serial::Timeout to 1000 and the read_timeout_multiplier to zero.
|
* serial::Timeout to 1000 and the read_timeout_multiplier to zero.
|
||||||
* This timeout condition can be used in conjunction with the inter
|
* This timeout condition can be used in conjunction with the inter
|
||||||
* byte timeout condition with out any problems, timeout will simply
|
* byte timeout condition with out any problems, timeout will simply
|
||||||
* occur when one of the two timeout conditions is met. This allows
|
* occur when one of the two timeout conditions is met. This allows
|
||||||
* users to have maximum control over the trade-off between
|
* users to have maximum control over the trade-off between
|
||||||
* responsiveness and efficiency.
|
* responsiveness and efficiency.
|
||||||
*
|
*
|
||||||
* Read and write functions will return in one of three cases. When the
|
* 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
|
* reading or writing is complete, when a timeout occurs, or when an
|
||||||
* exception occurs.
|
* exception occurs.
|
||||||
*
|
*
|
||||||
* A timeout of 0 enables non-blocking mode.
|
* \param timeout A serial::Timeout struct containing the inter byte
|
||||||
*
|
|
||||||
* \param timeout A serial::Timeout struct containing the inter byte
|
|
||||||
* timeout, and the read and write timeout constants and multipliers.
|
* timeout, and the read and write timeout constants and multipliers.
|
||||||
*
|
*
|
||||||
* \see serial::Timeout
|
* \see serial::Timeout
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
@ -474,7 +421,7 @@ public:
|
|||||||
|
|
||||||
/*! Gets the timeout for reads in seconds.
|
/*! Gets the timeout for reads in seconds.
|
||||||
*
|
*
|
||||||
* \return A Timeout struct containing the inter_byte_timeout, and read
|
* \return A Timeout struct containing the inter_byte_timeout, and read
|
||||||
* and write timeout constants and multipliers.
|
* and write timeout constants and multipliers.
|
||||||
*
|
*
|
||||||
* \see Serial::setTimeout
|
* \see Serial::setTimeout
|
||||||
@ -488,11 +435,11 @@ public:
|
|||||||
* 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, 500000, 921600
|
* 128000, 153600, 230400, 256000, 460800, 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 std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
setBaudrate (uint32_t baudrate);
|
setBaudrate (uint32_t baudrate);
|
||||||
@ -503,7 +450,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \see Serial::setBaudrate
|
* \see Serial::setBaudrate
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
uint32_t
|
uint32_t
|
||||||
getBaudrate () const;
|
getBaudrate () const;
|
||||||
@ -514,7 +461,7 @@ public:
|
|||||||
* default is eightbits, possible values are: fivebits, sixbits, sevenbits,
|
* default is eightbits, possible values are: fivebits, sixbits, sevenbits,
|
||||||
* eightbits
|
* eightbits
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
setBytesize (bytesize_t bytesize);
|
setBytesize (bytesize_t bytesize);
|
||||||
@ -523,7 +470,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \see Serial::setBytesize
|
* \see Serial::setBytesize
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
bytesize_t
|
bytesize_t
|
||||||
getBytesize () const;
|
getBytesize () const;
|
||||||
@ -533,7 +480,7 @@ public:
|
|||||||
* \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 std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
setParity (parity_t parity);
|
setParity (parity_t parity);
|
||||||
@ -542,7 +489,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \see Serial::setParity
|
* \see Serial::setParity
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
parity_t
|
parity_t
|
||||||
getParity () const;
|
getParity () const;
|
||||||
@ -552,7 +499,7 @@ public:
|
|||||||
* \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 std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
setStopbits (stopbits_t stopbits);
|
setStopbits (stopbits_t stopbits);
|
||||||
@ -561,7 +508,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \see Serial::setStopbits
|
* \see Serial::setStopbits
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
stopbits_t
|
stopbits_t
|
||||||
getStopbits () const;
|
getStopbits () const;
|
||||||
@ -572,7 +519,7 @@ public:
|
|||||||
* possible values are: flowcontrol_none, flowcontrol_software,
|
* possible values are: flowcontrol_none, flowcontrol_software,
|
||||||
* flowcontrol_hardware
|
* flowcontrol_hardware
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
setFlowcontrol (flowcontrol_t flowcontrol);
|
setFlowcontrol (flowcontrol_t flowcontrol);
|
||||||
@ -581,7 +528,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \see Serial::setFlowcontrol
|
* \see Serial::setFlowcontrol
|
||||||
*
|
*
|
||||||
* \throw std::invalid_argument
|
* \throw InvalidConfigurationException
|
||||||
*/
|
*/
|
||||||
flowcontrol_t
|
flowcontrol_t
|
||||||
getFlowcontrol () const;
|
getFlowcontrol () const;
|
||||||
@ -616,14 +563,14 @@ public:
|
|||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Blocks until CTS, DSR, RI, CD changes or something interrupts it.
|
* Blocks until CTS, DSR, RI, CD changes or something interrupts it.
|
||||||
*
|
*
|
||||||
* Can throw an exception if an error occurs while waiting.
|
* Can throw an exception if an error occurs while waiting.
|
||||||
* You can check the status of CTS, DSR, RI, and CD once this returns.
|
* 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
|
* 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
|
* resolution of less than +-1ms and as good as +-0.2ms. Otherwise a
|
||||||
* polling method is used which can give +-2ms.
|
* polling method is used which can give +-2ms.
|
||||||
*
|
*
|
||||||
* \return Returns true if one of the lines changed, false if something else
|
* \return Returns true if one of the lines changed, false if something else
|
||||||
* occurred.
|
* occurred.
|
||||||
*
|
*
|
||||||
* \throw SerialException
|
* \throw SerialException
|
||||||
@ -650,7 +597,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
// Disable copy constructors
|
// Disable copy constructors
|
||||||
Serial(const Serial&);
|
Serial(const Serial&);
|
||||||
Serial& operator=(const Serial&);
|
void 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;
|
||||||
@ -669,107 +619,79 @@ private:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SerialException : public std::exception
|
class SerialExecption : public std::exception
|
||||||
{
|
{
|
||||||
// Disable copy constructors
|
// Disable copy constructors
|
||||||
SerialException& operator=(const SerialException&);
|
void operator=(const SerialExecption&);
|
||||||
std::string e_what_;
|
const SerialExecption& operator=(SerialExecption);
|
||||||
|
const char* e_what_;
|
||||||
public:
|
public:
|
||||||
SerialException (const char *description) {
|
SerialExecption (const char *description) : e_what_ (description) {}
|
||||||
std::stringstream ss;
|
SerialExecption (const SerialExecption& other) {
|
||||||
ss << "SerialException " << description << " failed.";
|
e_what_ = other.e_what_;
|
||||||
e_what_ = ss.str();
|
|
||||||
}
|
}
|
||||||
SerialException (const SerialException& other) : e_what_(other.e_what_) {}
|
|
||||||
virtual ~SerialException() throw() {}
|
virtual const char* what () const throw ()
|
||||||
virtual const char* what () const throw () {
|
{
|
||||||
return e_what_.c_str();
|
std::stringstream ss;
|
||||||
|
ss << "SerialException " << e_what_ << " failed.";
|
||||||
|
return ss.str ().c_str ();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class IOException : public std::exception
|
class IOException : public std::exception
|
||||||
{
|
{
|
||||||
// Disable copy constructors
|
// Disable copy constructors
|
||||||
IOException& operator=(const IOException&);
|
void operator=(const IOException&);
|
||||||
|
const IOException& operator=(IOException);
|
||||||
std::string file_;
|
std::string file_;
|
||||||
int line_;
|
int line_;
|
||||||
std::string e_what_;
|
const char* 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), errno_(errnum) {
|
: file_(file), line_(line), e_what_ (strerror (errnum)), 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), errno_(0) {
|
: file_(file), line_(line), e_what_ (description), 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_) {}
|
IOException (const IOException& other) {
|
||||||
|
e_what_ = other.e_what_;
|
||||||
|
}
|
||||||
|
|
||||||
int getErrorNumber () const { return errno_; }
|
int getErrorNumber () { 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
|
||||||
{
|
{
|
||||||
// Disable copy constructors
|
// Disable copy constructors
|
||||||
|
void operator=(const PortNotOpenedException&);
|
||||||
const PortNotOpenedException& operator=(PortNotOpenedException);
|
const PortNotOpenedException& operator=(PortNotOpenedException);
|
||||||
std::string e_what_;
|
const char * e_what_;
|
||||||
public:
|
public:
|
||||||
PortNotOpenedException (const char * description) {
|
PortNotOpenedException (const char * description) : e_what_ (description) {}
|
||||||
std::stringstream ss;
|
PortNotOpenedException (const PortNotOpenedException& other) {
|
||||||
ss << "PortNotOpenedException " << description << " failed.";
|
e_what_ = other.e_what_;
|
||||||
e_what_ = ss.str();
|
|
||||||
}
|
}
|
||||||
PortNotOpenedException (const PortNotOpenedException& other) : e_what_(other.e_what_) {}
|
|
||||||
virtual ~PortNotOpenedException() throw() {}
|
virtual const char* what () const throw ()
|
||||||
virtual const char* what () const throw () {
|
{
|
||||||
return e_what_.c_str();
|
std::stringstream ss;
|
||||||
|
ss << e_what_ << " called before port was opened.";
|
||||||
|
return ss.str ().c_str ();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
|
||||||
* 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
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
// This header is from the v8 google project:
|
// This header is from the v8 google project:
|
||||||
// http://code.google.com/p/v8/source/browse/trunk/include/v8stdint.h
|
// http://code.google.com/p/v8/source/browse/trunk/include/v8stdint.h
|
||||||
|
|
||||||
// Copyright 2012 the V8 project authors. All rights reserved.
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
||||||
|
|||||||
10
package.xml
10
package.xml
@ -1,16 +1,14 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<package>
|
<package>
|
||||||
<name>serial</name>
|
<name>serial</name>
|
||||||
<version>1.2.1</version>
|
<version>1.1.2</version>
|
||||||
<description>
|
<description>
|
||||||
Serial is a cross-platform, simple to use library for using serial ports on computers.
|
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.
|
||||||
This library provides a C++, object oriented interface for interacting with RS-232
|
|
||||||
like devices on Linux and Windows.
|
|
||||||
</description>
|
</description>
|
||||||
|
|
||||||
<maintainer email="william@osrfoundation.org">William Woodall</maintainer>
|
<maintainer email="wwoodall@willowgarage.com">William Woodall</maintainer>
|
||||||
|
|
||||||
<license>MIT</license>
|
<license>BSD</license>
|
||||||
|
|
||||||
<url type="website">http://wjwwood.github.com/serial/</url>
|
<url type="website">http://wjwwood.github.com/serial/</url>
|
||||||
<url type="repository">https://github.com/wjwwood/serial</url>
|
<url type="repository">https://github.com/wjwwood/serial</url>
|
||||||
|
|||||||
@ -11,12 +11,9 @@
|
|||||||
{
|
{
|
||||||
"sublimeclang_options":
|
"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/usr/include",
|
||||||
"-I${folder:${project_path:serial.sublime-project}}/include",
|
"-I/usr/local/include",
|
||||||
|
"-I${folder:${project_path:serial.sublime-project}}/include"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,336 +0,0 @@
|
|||||||
#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__)
|
|
||||||
@ -1,286 +0,0 @@
|
|||||||
#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__)
|
|
||||||
@ -1,152 +0,0 @@
|
|||||||
#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)
|
|
||||||
591
src/impl/unix.cc
591
src/impl/unix.cc
@ -1,9 +1,4 @@
|
|||||||
/* Copyright 2012 William Woodall and John Harrison
|
/* Copyright 2012 William Woodall and John Harrison */
|
||||||
*
|
|
||||||
* Additional Contributors: Christopher Baker @bakercp
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(_WIN32)
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -27,7 +22,6 @@
|
|||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#ifdef __MACH__
|
#ifdef __MACH__
|
||||||
#include <AvailabilityMacros.h>
|
|
||||||
#include <mach/clock.h>
|
#include <mach/clock.h>
|
||||||
#include <mach/mach.h>
|
#include <mach/mach.h>
|
||||||
#endif
|
#endif
|
||||||
@ -42,69 +36,15 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3)
|
|
||||||
#include <IOKit/serial/ioss.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::stringstream;
|
using std::stringstream;
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
using serial::MillisecondTimer;
|
|
||||||
using serial::Serial;
|
using serial::Serial;
|
||||||
using serial::SerialException;
|
using serial::SerialExecption;
|
||||||
using serial::PortNotOpenedException;
|
using serial::PortNotOpenedException;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
|
|
||||||
|
|
||||||
MillisecondTimer::MillisecondTimer (const uint32_t millis)
|
|
||||||
: expiry(timespec_now())
|
|
||||||
{
|
|
||||||
int64_t tv_nsec = expiry.tv_nsec + (millis * 1e6);
|
|
||||||
if (tv_nsec >= 1e9) {
|
|
||||||
int64_t sec_diff = tv_nsec / static_cast<int> (1e9);
|
|
||||||
expiry.tv_nsec = tv_nsec % static_cast<int>(1e9);
|
|
||||||
expiry.tv_sec += sec_diff;
|
|
||||||
} else {
|
|
||||||
expiry.tv_nsec = tv_nsec;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t
|
|
||||||
MillisecondTimer::remaining ()
|
|
||||||
{
|
|
||||||
timespec now(timespec_now());
|
|
||||||
int64_t millis = (expiry.tv_sec - now.tv_sec) * 1e3;
|
|
||||||
millis += (expiry.tv_nsec - now.tv_nsec) / 1e6;
|
|
||||||
return millis;
|
|
||||||
}
|
|
||||||
|
|
||||||
timespec
|
|
||||||
MillisecondTimer::timespec_now ()
|
|
||||||
{
|
|
||||||
timespec time;
|
|
||||||
# ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
|
|
||||||
clock_serv_t cclock;
|
|
||||||
mach_timespec_t mts;
|
|
||||||
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
|
|
||||||
clock_get_time(cclock, &mts);
|
|
||||||
mach_port_deallocate(mach_task_self(), cclock);
|
|
||||||
time.tv_sec = mts.tv_sec;
|
|
||||||
time.tv_nsec = mts.tv_nsec;
|
|
||||||
# else
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &time);
|
|
||||||
# endif
|
|
||||||
return time;
|
|
||||||
}
|
|
||||||
|
|
||||||
timespec
|
|
||||||
timespec_from_ms (const uint32_t millis)
|
|
||||||
{
|
|
||||||
timespec time;
|
|
||||||
time.tv_sec = millis / 1e3;
|
|
||||||
time.tv_nsec = (millis - (time.tv_sec * 1e3)) * 1e6;
|
|
||||||
return time;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
|
Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
|
||||||
bytesize_t bytesize,
|
bytesize_t bytesize,
|
||||||
parity_t parity, stopbits_t stopbits,
|
parity_t parity, stopbits_t stopbits,
|
||||||
@ -133,7 +73,7 @@ Serial::SerialImpl::open ()
|
|||||||
throw invalid_argument ("Empty port is invalid.");
|
throw invalid_argument ("Empty port is invalid.");
|
||||||
}
|
}
|
||||||
if (is_open_ == true) {
|
if (is_open_ == true) {
|
||||||
throw SerialException ("Serial port already open.");
|
throw SerialExecption ("Serial port already open.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
|
fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||||
@ -269,12 +209,6 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
#ifdef B460800
|
#ifdef B460800
|
||||||
case 460800: baud = B460800; break;
|
case 460800: baud = B460800; break;
|
||||||
#endif
|
#endif
|
||||||
#ifdef B500000
|
|
||||||
case 500000: baud = B500000; break;
|
|
||||||
#endif
|
|
||||||
#ifdef B576000
|
|
||||||
case 576000: baud = B576000; break;
|
|
||||||
#endif
|
|
||||||
#ifdef B921600
|
#ifdef B921600
|
||||||
case 921600: baud = B921600; break;
|
case 921600: baud = B921600; break;
|
||||||
#endif
|
#endif
|
||||||
@ -304,6 +238,29 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
#endif
|
#endif
|
||||||
default:
|
default:
|
||||||
custom_baud = true;
|
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__) && defined (TIOCSSERIAL)
|
||||||
|
struct serial_struct ser;
|
||||||
|
ioctl (fd_, TIOCGSERIAL, &ser);
|
||||||
|
// set custom divisor
|
||||||
|
ser.custom_divisor = ser.baud_base / (int) 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) {
|
if (custom_baud == false) {
|
||||||
#ifdef _BSD_SOURCE
|
#ifdef _BSD_SOURCE
|
||||||
@ -345,22 +302,7 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
options.c_cflag |= (PARENB);
|
options.c_cflag |= (PARENB);
|
||||||
} else if (parity_ == parity_odd) {
|
} else if (parity_ == parity_odd) {
|
||||||
options.c_cflag |= (PARENB | PARODD);
|
options.c_cflag |= (PARENB | PARODD);
|
||||||
}
|
} else {
|
||||||
#ifdef CMSPAR
|
|
||||||
else if (parity_ == parity_mark) {
|
|
||||||
options.c_cflag |= (PARENB | CMSPAR | PARODD);
|
|
||||||
}
|
|
||||||
else if (parity_ == parity_space) {
|
|
||||||
options.c_cflag |= (PARENB | CMSPAR);
|
|
||||||
options.c_cflag &= (tcflag_t) ~(PARODD);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
// CMSPAR is not defined on OSX. So do not support mark or space parity.
|
|
||||||
else if (parity_ == parity_mark || parity_ == parity_space) {
|
|
||||||
throw invalid_argument ("OS does not support mark or space parity");
|
|
||||||
}
|
|
||||||
#endif // ifdef CMSPAR
|
|
||||||
else {
|
|
||||||
throw invalid_argument ("invalid parity");
|
throw invalid_argument ("invalid parity");
|
||||||
}
|
}
|
||||||
// setup flow control
|
// setup flow control
|
||||||
@ -412,51 +354,6 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
|
|
||||||
// activate settings
|
// activate settings
|
||||||
::tcsetattr (fd_, TCSANOW, &options);
|
::tcsetattr (fd_, TCSANOW, &options);
|
||||||
|
|
||||||
// apply custom baud rate, if any
|
|
||||||
if (custom_baud == true) {
|
|
||||||
// OS X support
|
|
||||||
#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
|
|
||||||
// Starting with Tiger, the IOSSIOSPEED ioctl can be used to set arbitrary baud rates
|
|
||||||
// other than those specified by POSIX. The driver for the underlying serial hardware
|
|
||||||
// ultimately determines which baud rates can be used. This ioctl sets both the input
|
|
||||||
// and output speed.
|
|
||||||
speed_t new_baud = static_cast<speed_t> (baudrate_);
|
|
||||||
// PySerial uses IOSSIOSPEED=0x80045402
|
|
||||||
if (-1 == ioctl (fd_, IOSSIOSPEED, &new_baud, 1)) {
|
|
||||||
THROW (IOException, errno);
|
|
||||||
}
|
|
||||||
// Linux Support
|
|
||||||
#elif defined(__linux__) && defined (TIOCSSERIAL)
|
|
||||||
struct serial_struct ser;
|
|
||||||
|
|
||||||
if (-1 == ioctl (fd_, TIOCGSERIAL, &ser)) {
|
|
||||||
THROW (IOException, errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
// set custom divisor
|
|
||||||
ser.custom_divisor = ser.baud_base / static_cast<int> (baudrate_);
|
|
||||||
// update flags
|
|
||||||
ser.flags &= ~ASYNC_SPD_MASK;
|
|
||||||
ser.flags |= ASYNC_SPD_CUST;
|
|
||||||
|
|
||||||
if (-1 == ioctl (fd_, TIOCSSERIAL, &ser)) {
|
|
||||||
THROW (IOException, errno);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
throw invalid_argument ("OS does not currently support custom bauds");
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update byte_time_ based on the new settings.
|
|
||||||
uint32_t bit_time_ns = 1e9 / baudrate_;
|
|
||||||
byte_time_ns_ = bit_time_ns * (1 + bytesize_ + parity_ + stopbits_);
|
|
||||||
|
|
||||||
// Compensate for the stopbits_one_point_five enum being equal to int 3,
|
|
||||||
// and not 1.5.
|
|
||||||
if (stopbits_ == stopbits_one_point_five) {
|
|
||||||
byte_time_ns_ += ((1.5 - stopbits_one_point_five) * bit_time_ns);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -464,13 +361,8 @@ Serial::SerialImpl::close ()
|
|||||||
{
|
{
|
||||||
if (is_open_ == true) {
|
if (is_open_ == true) {
|
||||||
if (fd_ != -1) {
|
if (fd_ != -1) {
|
||||||
int ret;
|
::close (fd_); // Ignoring the outcome
|
||||||
ret = ::close (fd_);
|
fd_ = -1;
|
||||||
if (ret == 0) {
|
|
||||||
fd_ = -1;
|
|
||||||
} else {
|
|
||||||
THROW (IOException, errno);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
is_open_ = false;
|
is_open_ = false;
|
||||||
}
|
}
|
||||||
@ -489,49 +381,27 @@ Serial::SerialImpl::available ()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int count = 0;
|
int count = 0;
|
||||||
if (-1 == ioctl (fd_, TIOCINQ, &count)) {
|
int result = ioctl (fd_, TIOCINQ, &count);
|
||||||
THROW (IOException, errno);
|
if (result == 0) {
|
||||||
|
return static_cast<size_t> (count);
|
||||||
} else {
|
} else {
|
||||||
return static_cast<size_t> (count);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
|
||||||
Serial::SerialImpl::waitReadable (uint32_t timeout)
|
|
||||||
{
|
|
||||||
// Setup a select call to block for serial data or a timeout
|
|
||||||
fd_set readfds;
|
|
||||||
FD_ZERO (&readfds);
|
|
||||||
FD_SET (fd_, &readfds);
|
|
||||||
timespec timeout_ts (timespec_from_ms (timeout));
|
|
||||||
int r = pselect (fd_ + 1, &readfds, NULL, NULL, &timeout_ts, NULL);
|
|
||||||
|
|
||||||
if (r < 0) {
|
|
||||||
// Select was interrupted
|
|
||||||
if (errno == EINTR) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Otherwise there was some error
|
|
||||||
THROW (IOException, errno);
|
THROW (IOException, errno);
|
||||||
}
|
}
|
||||||
// Timeout occurred
|
|
||||||
if (r == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// This shouldn't happen, if r > 0 our fd has to be in the list!
|
|
||||||
if (!FD_ISSET (fd_, &readfds)) {
|
|
||||||
THROW (IOException, "select reports ready to read, but our fd isn't"
|
|
||||||
" in the list, this shouldn't happen!");
|
|
||||||
}
|
|
||||||
// Data available to read.
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
inline void get_time_now(struct timespec &time)
|
||||||
Serial::SerialImpl::waitByteTimes (size_t count)
|
|
||||||
{
|
{
|
||||||
timespec wait_time = { 0, static_cast<long>(byte_time_ns_ * count)};
|
# ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
|
||||||
pselect (0, NULL, NULL, NULL, &wait_time, NULL);
|
clock_serv_t cclock;
|
||||||
|
mach_timespec_t mts;
|
||||||
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
||||||
|
clock_get_time(cclock, &mts);
|
||||||
|
mach_port_deallocate(mach_task_self(), cclock);
|
||||||
|
time.tv_sec = mts.tv_sec;
|
||||||
|
time.tv_nsec = mts.tv_nsec;
|
||||||
|
# else
|
||||||
|
clock_gettime(CLOCK_REALTIME, &time);
|
||||||
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
@ -541,71 +411,113 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
|
|||||||
if (!is_open_) {
|
if (!is_open_) {
|
||||||
throw PortNotOpenedException ("Serial::read");
|
throw PortNotOpenedException ("Serial::read");
|
||||||
}
|
}
|
||||||
|
fd_set readfds;
|
||||||
size_t bytes_read = 0;
|
size_t bytes_read = 0;
|
||||||
|
// Setup the total_timeout timeval
|
||||||
|
// This timeout is maximum time before a timeout after read is called
|
||||||
|
struct timeval total_timeout;
|
||||||
// Calculate total timeout in milliseconds t_c + (t_m * N)
|
// Calculate total timeout in milliseconds t_c + (t_m * N)
|
||||||
long total_timeout_ms = timeout_.read_timeout_constant;
|
long total_timeout_ms = timeout_.read_timeout_constant;
|
||||||
total_timeout_ms += timeout_.read_timeout_multiplier * static_cast<long> (size);
|
total_timeout_ms += timeout_.read_timeout_multiplier*static_cast<long>(size);
|
||||||
MillisecondTimer total_timeout(total_timeout_ms);
|
total_timeout.tv_sec = total_timeout_ms / 1000;
|
||||||
|
total_timeout.tv_usec = static_cast<int>(total_timeout_ms % 1000);
|
||||||
// Pre-fill buffer with available bytes
|
total_timeout.tv_usec *= 1000; // To convert to micro seconds
|
||||||
{
|
// Setup the inter byte timeout
|
||||||
ssize_t bytes_read_now = ::read (fd_, buf, size);
|
struct timeval inter_byte_timeout;
|
||||||
if (bytes_read_now > 0) {
|
inter_byte_timeout.tv_sec = timeout_.inter_byte_timeout / 1000;
|
||||||
bytes_read = bytes_read_now;
|
inter_byte_timeout.tv_usec =
|
||||||
}
|
static_cast<int> (timeout_.inter_byte_timeout % 1000);
|
||||||
}
|
inter_byte_timeout.tv_usec *= 1000; // To convert to micro seconds
|
||||||
|
|
||||||
while (bytes_read < size) {
|
while (bytes_read < size) {
|
||||||
int64_t timeout_remaining_ms = total_timeout.remaining();
|
// Setup the select timeout timeval
|
||||||
if (timeout_remaining_ms <= 0) {
|
struct timeval timeout;
|
||||||
// Timed out
|
// If the total_timeout is less than the inter_byte_timeout
|
||||||
break;
|
if (total_timeout.tv_sec < inter_byte_timeout.tv_sec
|
||||||
|
|| (total_timeout.tv_sec == inter_byte_timeout.tv_sec
|
||||||
|
&& total_timeout.tv_usec < inter_byte_timeout.tv_sec))
|
||||||
|
{
|
||||||
|
// Then set the select timeout to use the total time
|
||||||
|
timeout = total_timeout;
|
||||||
|
} else {
|
||||||
|
// Else set the select timeout to use the inter byte time
|
||||||
|
timeout = inter_byte_timeout;
|
||||||
}
|
}
|
||||||
// Timeout for the next select is whichever is less of the remaining
|
FD_ZERO (&readfds);
|
||||||
// total read timeout and the inter-byte timeout.
|
FD_SET (fd_, &readfds);
|
||||||
uint32_t timeout = std::min(static_cast<uint32_t> (timeout_remaining_ms),
|
// Begin timing select
|
||||||
timeout_.inter_byte_timeout);
|
struct timespec start, end;
|
||||||
// Wait for the device to be readable, and then attempt to read.
|
get_time_now (start);
|
||||||
if (waitReadable(timeout)) {
|
// Call select to block for serial data or a timeout
|
||||||
// If it's a fixed-length multi-byte read, insert a wait here so that
|
int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout);
|
||||||
// we can attempt to grab the whole thing in a single IO call. Skip
|
// Calculate difference and update the structure
|
||||||
// this wait if a non-max inter_byte_timeout is specified.
|
get_time_now (end);
|
||||||
if (size > 1 && timeout_.inter_byte_timeout == Timeout::max()) {
|
// Calculate the time select took
|
||||||
size_t bytes_available = available();
|
struct timeval diff;
|
||||||
if (bytes_available + bytes_read < size) {
|
diff.tv_sec = end.tv_sec - start.tv_sec;
|
||||||
waitByteTimes(size - (bytes_available + bytes_read));
|
diff.tv_usec = static_cast<int> ((end.tv_nsec - start.tv_nsec) / 1000);
|
||||||
}
|
// Update the timeout
|
||||||
}
|
if (total_timeout.tv_sec <= diff.tv_sec) {
|
||||||
// This should be non-blocking returning only what is available now
|
total_timeout.tv_sec = 0;
|
||||||
// Then returning so that select can block again.
|
} else {
|
||||||
ssize_t bytes_read_now =
|
total_timeout.tv_sec -= diff.tv_sec;
|
||||||
::read (fd_, buf + bytes_read, size - bytes_read);
|
}
|
||||||
// read should always return some data as select reported it was
|
if (total_timeout.tv_usec <= diff.tv_usec) {
|
||||||
// ready to read when we get to this point.
|
total_timeout.tv_usec = 0;
|
||||||
if (bytes_read_now < 1) {
|
} else {
|
||||||
// Disconnected devices, at least on Linux, show the
|
total_timeout.tv_usec -= diff.tv_usec;
|
||||||
// behavior that they are always ready to read immediately
|
}
|
||||||
// but reading returns nothing.
|
|
||||||
throw SerialException ("device reports readiness to read but "
|
// Figure out what happened by looking at select's response 'r'
|
||||||
"returned no data (device disconnected?)");
|
/** Error **/
|
||||||
}
|
if (r < 0) {
|
||||||
// Update bytes_read
|
// Select was interrupted, try again
|
||||||
bytes_read += static_cast<size_t> (bytes_read_now);
|
if (errno == EINTR) {
|
||||||
// If bytes_read == size then we have read everything we need
|
|
||||||
if (bytes_read == size) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// If bytes_read < size then we have more to read
|
|
||||||
if (bytes_read < size) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// If bytes_read > size then we have over read, which shouldn't happen
|
// Otherwise there was some error
|
||||||
if (bytes_read > size) {
|
THROW (IOException, errno);
|
||||||
throw SerialException ("read over read, too many bytes where "
|
}
|
||||||
"read, this shouldn't happen, might be "
|
/** Timeout **/
|
||||||
"a logical error!");
|
if (r == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/** Something ready to read **/
|
||||||
|
if (r > 0) {
|
||||||
|
// Make sure our file descriptor is in the ready to read list
|
||||||
|
if (FD_ISSET (fd_, &readfds)) {
|
||||||
|
// This should be non-blocking returning only what is available now
|
||||||
|
// Then returning so that select can block again.
|
||||||
|
ssize_t bytes_read_now =
|
||||||
|
::read (fd_, buf + bytes_read, size - bytes_read);
|
||||||
|
// read should always return some data as select reported it was
|
||||||
|
// ready to read when we get to this point.
|
||||||
|
if (bytes_read_now < 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?)");
|
||||||
|
}
|
||||||
|
// Update bytes_read
|
||||||
|
bytes_read += static_cast<size_t> (bytes_read_now);
|
||||||
|
// If bytes_read == size then we have read everything we need
|
||||||
|
if (bytes_read == size) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// If bytes_read < size then we have more to read
|
||||||
|
if (bytes_read < size) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// If bytes_read > size then we have over read, which shouldn't happen
|
||||||
|
if (bytes_read > size) {
|
||||||
|
throw SerialExecption ("read over read, too many bytes where "
|
||||||
|
"read, this shouldn't happen, might be "
|
||||||
|
"a logical error!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// This shouldn't happen, if r > 0 our fd has to be in the list!
|
||||||
|
THROW (IOException, "select reports ready to read, but our fd isn't"
|
||||||
|
" in the list, this shouldn't happen!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bytes_read;
|
return bytes_read;
|
||||||
@ -619,30 +531,42 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
|||||||
}
|
}
|
||||||
fd_set writefds;
|
fd_set writefds;
|
||||||
size_t bytes_written = 0;
|
size_t bytes_written = 0;
|
||||||
|
struct timeval timeout;
|
||||||
// Calculate total timeout in milliseconds t_c + (t_m * N)
|
timeout.tv_sec = timeout_.write_timeout_constant / 1000;
|
||||||
long total_timeout_ms = timeout_.write_timeout_constant;
|
timeout.tv_usec = static_cast<int> (timeout_.write_timeout_multiplier % 1000);
|
||||||
total_timeout_ms += timeout_.write_timeout_multiplier * static_cast<long> (length);
|
timeout.tv_usec *= 1000; // To convert to micro seconds
|
||||||
MillisecondTimer total_timeout(total_timeout_ms);
|
|
||||||
|
|
||||||
bool first_iteration = true;
|
|
||||||
while (bytes_written < length) {
|
while (bytes_written < length) {
|
||||||
int64_t timeout_remaining_ms = total_timeout.remaining();
|
|
||||||
// Only consider the timeout if it's not the first iteration of the loop
|
|
||||||
// otherwise a timeout of 0 won't be allowed through
|
|
||||||
if (!first_iteration && (timeout_remaining_ms <= 0)) {
|
|
||||||
// Timed out
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
first_iteration = false;
|
|
||||||
|
|
||||||
timespec timeout(timespec_from_ms(timeout_remaining_ms));
|
|
||||||
|
|
||||||
FD_ZERO (&writefds);
|
FD_ZERO (&writefds);
|
||||||
FD_SET (fd_, &writefds);
|
FD_SET (fd_, &writefds);
|
||||||
|
// On Linux the timeout struct is updated by select to contain the time
|
||||||
|
// left on the timeout to make looping easier, but on other platforms this
|
||||||
|
// does not occur.
|
||||||
|
#if !defined(__linux__)
|
||||||
|
// Begin timing select
|
||||||
|
struct timespec start, end;
|
||||||
|
get_time_now(start);
|
||||||
|
#endif
|
||||||
// Do the select
|
// Do the select
|
||||||
int r = pselect (fd_ + 1, NULL, &writefds, NULL, &timeout, NULL);
|
int r = select (fd_ + 1, NULL, &writefds, NULL, &timeout);
|
||||||
|
#if !defined(__linux__)
|
||||||
|
// Calculate difference and update the structure
|
||||||
|
get_time_now(end);
|
||||||
|
// Calculate the time select took
|
||||||
|
struct timeval diff;
|
||||||
|
diff.tv_sec = end.tv_sec - start.tv_sec;
|
||||||
|
diff.tv_usec = static_cast<int> ((end.tv_nsec - start.tv_nsec) / 1000);
|
||||||
|
// Update the timeout
|
||||||
|
if (timeout.tv_sec <= diff.tv_sec) {
|
||||||
|
timeout.tv_sec = 0;
|
||||||
|
} else {
|
||||||
|
timeout.tv_sec -= diff.tv_sec;
|
||||||
|
}
|
||||||
|
if (timeout.tv_usec <= diff.tv_usec) {
|
||||||
|
timeout.tv_usec = 0;
|
||||||
|
} else {
|
||||||
|
timeout.tv_usec -= diff.tv_usec;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Figure out what happened by looking at select's response 'r'
|
// Figure out what happened by looking at select's response 'r'
|
||||||
/** Error **/
|
/** Error **/
|
||||||
@ -665,27 +589,14 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
|||||||
// This will write some
|
// This will write some
|
||||||
ssize_t bytes_written_now =
|
ssize_t bytes_written_now =
|
||||||
::write (fd_, data + bytes_written, length - bytes_written);
|
::write (fd_, data + bytes_written, length - bytes_written);
|
||||||
|
|
||||||
// even though pselect returned readiness the call might still be
|
|
||||||
// interrupted. In that case simply retry.
|
|
||||||
if (bytes_written_now == -1 && errno == EINTR) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// write should always return some data as select reported it was
|
// write should always return some data as select reported it was
|
||||||
// ready to write when we get to this point.
|
// ready to write when we get to this point.
|
||||||
if (bytes_written_now < 1) {
|
if (bytes_written_now < 1) {
|
||||||
// Disconnected devices, at least on Linux, show the
|
// Disconnected devices, at least on Linux, show the
|
||||||
// behavior that they are always ready to write immediately
|
// behavior that they are always ready to write immediately
|
||||||
// but writing returns nothing.
|
// but writing returns nothing.
|
||||||
std::stringstream strs;
|
throw SerialExecption ("device reports readiness to write but "
|
||||||
strs << "device reports readiness to write but "
|
"returned no data (device disconnected?)");
|
||||||
"returned no data (device disconnected?)";
|
|
||||||
strs << " errno=" << errno;
|
|
||||||
strs << " bytes_written_now= " << bytes_written_now;
|
|
||||||
strs << " bytes_written=" << bytes_written;
|
|
||||||
strs << " length=" << length;
|
|
||||||
throw SerialException(strs.str().c_str());
|
|
||||||
}
|
}
|
||||||
// Update bytes_written
|
// Update bytes_written
|
||||||
bytes_written += static_cast<size_t> (bytes_written_now);
|
bytes_written += static_cast<size_t> (bytes_written_now);
|
||||||
@ -699,7 +610,7 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
|||||||
}
|
}
|
||||||
// If bytes_written > size then we have over written, which shouldn't happen
|
// If bytes_written > size then we have over written, which shouldn't happen
|
||||||
if (bytes_written > length) {
|
if (bytes_written > length) {
|
||||||
throw SerialException ("write over wrote, too many bytes where "
|
throw SerialExecption ("write over wrote, too many bytes where "
|
||||||
"written, this shouldn't happen, might be "
|
"written, this shouldn't happen, might be "
|
||||||
"a logical error!");
|
"a logical error!");
|
||||||
}
|
}
|
||||||
@ -848,21 +759,10 @@ Serial::SerialImpl::setBreak (bool level)
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::setBreak");
|
throw PortNotOpenedException ("Serial::setBreak");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (level) {
|
if (level) {
|
||||||
if (-1 == ioctl (fd_, TIOCSBRK))
|
ioctl (fd_, TIOCSBRK);
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "setBreak failed on a call to ioctl(TIOCSBRK): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (-1 == ioctl (fd_, TIOCCBRK))
|
ioctl (fd_, TIOCCBRK);
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "setBreak failed on a call to ioctl(TIOCCBRK): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -872,23 +772,10 @@ Serial::SerialImpl::setRTS (bool level)
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::setRTS");
|
throw PortNotOpenedException ("Serial::setRTS");
|
||||||
}
|
}
|
||||||
|
|
||||||
int command = TIOCM_RTS;
|
|
||||||
|
|
||||||
if (level) {
|
if (level) {
|
||||||
if (-1 == ioctl (fd_, TIOCMBIS, &command))
|
ioctl (fd_, TIOCMBIS, TIOCM_RTS);
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "setRTS failed on a call to ioctl(TIOCMBIS): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (-1 == ioctl (fd_, TIOCMBIC, &command))
|
ioctl (fd_, TIOCMBIC, TIOCM_RTS);
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "setRTS failed on a call to ioctl(TIOCMBIC): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -898,23 +785,10 @@ Serial::SerialImpl::setDTR (bool level)
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::setDTR");
|
throw PortNotOpenedException ("Serial::setDTR");
|
||||||
}
|
}
|
||||||
|
|
||||||
int command = TIOCM_DTR;
|
|
||||||
|
|
||||||
if (level) {
|
if (level) {
|
||||||
if (-1 == ioctl (fd_, TIOCMBIS, &command))
|
ioctl (fd_, TIOCMBIS, TIOCM_DTR);
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "setDTR failed on a call to ioctl(TIOCMBIS): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (-1 == ioctl (fd_, TIOCMBIC, &command))
|
ioctl (fd_, TIOCMBIC, TIOCM_DTR);
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "setDTR failed on a call to ioctl(TIOCMBIC): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -922,40 +796,21 @@ bool
|
|||||||
Serial::SerialImpl::waitForChange ()
|
Serial::SerialImpl::waitForChange ()
|
||||||
{
|
{
|
||||||
#ifndef TIOCMIWAIT
|
#ifndef TIOCMIWAIT
|
||||||
|
while (is_open_ == true) {
|
||||||
while (is_open_ == true) {
|
int s = ioctl (fd_, TIOCMGET, 0);
|
||||||
|
if ((s & TIOCM_CTS) != 0) return true;
|
||||||
int status;
|
if ((s & TIOCM_DSR) != 0) return true;
|
||||||
|
if ((s & TIOCM_RI) != 0) return true;
|
||||||
if (-1 == ioctl (fd_, TIOCMGET, &status))
|
if ((s & TIOCM_CD) != 0) return true;
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "waitForChange failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (0 != (status & TIOCM_CTS)
|
|
||||||
|| 0 != (status & TIOCM_DSR)
|
|
||||||
|| 0 != (status & TIOCM_RI)
|
|
||||||
|| 0 != (status & TIOCM_CD))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
usleep(1000);
|
usleep(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
#else
|
#else
|
||||||
int command = (TIOCM_CD|TIOCM_DSR|TIOCM_RI|TIOCM_CTS);
|
if (ioctl(fd_, TIOCMIWAIT, (TIOCM_CD|TIOCM_DSR|TIOCM_RI|TIOCM_CTS)) != 0) {
|
||||||
|
|
||||||
if (-1 == ioctl (fd_, TIOCMIWAIT, &command)) {
|
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << "waitForDSR failed on a call to ioctl(TIOCMIWAIT): "
|
ss << "waitForDSR failed on a call to ioctl(TIOCMIWAIT): "
|
||||||
<< errno << " " << strerror(errno);
|
<< errno << " " << strerror(errno);
|
||||||
throw(SerialException(ss.str().c_str()));
|
throw(SerialExecption(ss.str().c_str()));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
#endif
|
#endif
|
||||||
@ -967,19 +822,8 @@ Serial::SerialImpl::getCTS ()
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::getCTS");
|
throw PortNotOpenedException ("Serial::getCTS");
|
||||||
}
|
}
|
||||||
|
int s = ioctl (fd_, TIOCMGET, 0);
|
||||||
int status;
|
return (s & TIOCM_CTS) != 0;
|
||||||
|
|
||||||
if (-1 == ioctl (fd_, TIOCMGET, &status))
|
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "getCTS failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 0 != (status & TIOCM_CTS);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -988,19 +832,8 @@ Serial::SerialImpl::getDSR ()
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::getDSR");
|
throw PortNotOpenedException ("Serial::getDSR");
|
||||||
}
|
}
|
||||||
|
int s = ioctl (fd_, TIOCMGET, 0);
|
||||||
int status;
|
return (s & TIOCM_DSR) != 0;
|
||||||
|
|
||||||
if (-1 == ioctl (fd_, TIOCMGET, &status))
|
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "getDSR failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 0 != (status & TIOCM_DSR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -1009,19 +842,8 @@ Serial::SerialImpl::getRI ()
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::getRI");
|
throw PortNotOpenedException ("Serial::getRI");
|
||||||
}
|
}
|
||||||
|
int s = ioctl (fd_, TIOCMGET, 0);
|
||||||
int status;
|
return (s & TIOCM_RI) != 0;
|
||||||
|
|
||||||
if (-1 == ioctl (fd_, TIOCMGET, &status))
|
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "getRI failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 0 != (status & TIOCM_RI);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -1030,19 +852,8 @@ Serial::SerialImpl::getCD ()
|
|||||||
if (is_open_ == false) {
|
if (is_open_ == false) {
|
||||||
throw PortNotOpenedException ("Serial::getCD");
|
throw PortNotOpenedException ("Serial::getCD");
|
||||||
}
|
}
|
||||||
|
int s = ioctl (fd_, TIOCMGET, 0);
|
||||||
int status;
|
return (s & TIOCM_CD) != 0;
|
||||||
|
|
||||||
if (-1 == ioctl (fd_, TIOCMGET, &status))
|
|
||||||
{
|
|
||||||
stringstream ss;
|
|
||||||
ss << "getCD failed on a call to ioctl(TIOCMGET): " << errno << " " << strerror(errno);
|
|
||||||
throw(SerialException(ss.str().c_str()));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 0 != (status & TIOCM_CD);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -1080,5 +891,3 @@ Serial::SerialImpl::writeUnlock ()
|
|||||||
THROW (IOException, result);
|
THROW (IOException, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // !defined(_WIN32)
|
|
||||||
|
|||||||
185
src/impl/win.cc
185
src/impl/win.cc
@ -1,13 +1,8 @@
|
|||||||
#if defined(_WIN32)
|
|
||||||
|
|
||||||
/* Copyright 2012 William Woodall and John Harrison */
|
/* Copyright 2012 William Woodall and John Harrison */
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "serial/impl/win.h"
|
#include "serial/impl/win.h"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::wstring;
|
|
||||||
using std::stringstream;
|
using std::stringstream;
|
||||||
using std::invalid_argument;
|
using std::invalid_argument;
|
||||||
using serial::Serial;
|
using serial::Serial;
|
||||||
@ -16,33 +11,23 @@ using serial::bytesize_t;
|
|||||||
using serial::parity_t;
|
using serial::parity_t;
|
||||||
using serial::stopbits_t;
|
using serial::stopbits_t;
|
||||||
using serial::flowcontrol_t;
|
using serial::flowcontrol_t;
|
||||||
using serial::SerialException;
|
using serial::SerialExecption;
|
||||||
using serial::PortNotOpenedException;
|
using serial::PortNotOpenedException;
|
||||||
using serial::IOException;
|
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,
|
Serial::SerialImpl::SerialImpl (const string &port, unsigned long baudrate,
|
||||||
bytesize_t bytesize,
|
bytesize_t bytesize,
|
||||||
parity_t parity, stopbits_t stopbits,
|
parity_t parity, stopbits_t stopbits,
|
||||||
flowcontrol_t flowcontrol)
|
flowcontrol_t flowcontrol)
|
||||||
: port_ (port.begin(), port.end()), fd_ (INVALID_HANDLE_VALUE), is_open_ (false),
|
: port_ (port), fd_ (INVALID_HANDLE_VALUE), is_open_ (false),
|
||||||
baudrate_ (baudrate), parity_ (parity),
|
baudrate_ (baudrate), parity_ (parity),
|
||||||
bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol)
|
bytesize_ (bytesize), stopbits_ (stopbits), flowcontrol_ (flowcontrol)
|
||||||
{
|
{
|
||||||
if (port_.empty () == false)
|
|
||||||
open ();
|
|
||||||
read_mutex = CreateMutex(NULL, false, NULL);
|
read_mutex = CreateMutex(NULL, false, NULL);
|
||||||
write_mutex = CreateMutex(NULL, false, NULL);
|
write_mutex = CreateMutex(NULL, false, NULL);
|
||||||
|
if (port_.empty () == false)
|
||||||
|
open ();
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial::SerialImpl::~SerialImpl ()
|
Serial::SerialImpl::~SerialImpl ()
|
||||||
@ -59,30 +44,26 @@ Serial::SerialImpl::open ()
|
|||||||
throw invalid_argument ("Empty port is invalid.");
|
throw invalid_argument ("Empty port is invalid.");
|
||||||
}
|
}
|
||||||
if (is_open_ == true) {
|
if (is_open_ == true) {
|
||||||
throw SerialException ("Serial port already open.");
|
throw SerialExecption ("Serial port already open.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// See: https://github.com/wjwwood/serial/issues/84
|
fd_ = CreateFile(port_.c_str(),
|
||||||
wstring port_with_prefix = _prefix_port_if_needed(port_);
|
GENERIC_READ | GENERIC_WRITE,
|
||||||
LPCWSTR lp_port = port_with_prefix.c_str();
|
0,
|
||||||
fd_ = CreateFileW(lp_port,
|
0,
|
||||||
GENERIC_READ | GENERIC_WRITE,
|
OPEN_EXISTING,
|
||||||
0,
|
FILE_ATTRIBUTE_NORMAL,
|
||||||
0,
|
0);
|
||||||
OPEN_EXISTING,
|
|
||||||
FILE_ATTRIBUTE_NORMAL,
|
|
||||||
0);
|
|
||||||
|
|
||||||
if (fd_ == INVALID_HANDLE_VALUE) {
|
if (fd_ == INVALID_HANDLE_VALUE) {
|
||||||
DWORD create_file_err = GetLastError();
|
DWORD errno_ = GetLastError();
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
switch (create_file_err) {
|
switch (errno_) {
|
||||||
case ERROR_FILE_NOT_FOUND:
|
case ERROR_FILE_NOT_FOUND:
|
||||||
// Use this->getPort to convert to a std::string
|
ss << "Specified port, " << port_ << ", does not exist.";
|
||||||
ss << "Specified port, " << this->getPort() << ", does not exist.";
|
|
||||||
THROW (IOException, ss.str().c_str());
|
THROW (IOException, ss.str().c_str());
|
||||||
default:
|
default:
|
||||||
ss << "Unknown error opening the serial port: " << create_file_err;
|
ss << "Unknown error opening the serial port: " << errno;
|
||||||
THROW (IOException, ss.str().c_str());
|
THROW (IOException, ss.str().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -228,10 +209,6 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
dcbSerialParams.Parity = EVENPARITY;
|
dcbSerialParams.Parity = EVENPARITY;
|
||||||
} else if (parity_ == parity_odd) {
|
} else if (parity_ == parity_odd) {
|
||||||
dcbSerialParams.Parity = ODDPARITY;
|
dcbSerialParams.Parity = ODDPARITY;
|
||||||
} else if (parity_ == parity_mark) {
|
|
||||||
dcbSerialParams.Parity = MARKPARITY;
|
|
||||||
} else if (parity_ == parity_space) {
|
|
||||||
dcbSerialParams.Parity = SPACEPARITY;
|
|
||||||
} else {
|
} else {
|
||||||
throw invalid_argument ("invalid parity");
|
throw invalid_argument ("invalid parity");
|
||||||
}
|
}
|
||||||
@ -239,39 +216,27 @@ Serial::SerialImpl::reconfigurePort ()
|
|||||||
// setup flowcontrol
|
// setup flowcontrol
|
||||||
if (flowcontrol_ == flowcontrol_none) {
|
if (flowcontrol_ == flowcontrol_none) {
|
||||||
dcbSerialParams.fOutxCtsFlow = false;
|
dcbSerialParams.fOutxCtsFlow = false;
|
||||||
dcbSerialParams.fRtsControl = RTS_CONTROL_DISABLE;
|
dcbSerialParams.fRtsControl = 0x00;
|
||||||
dcbSerialParams.fOutX = false;
|
dcbSerialParams.fOutX = false;
|
||||||
dcbSerialParams.fInX = false;
|
dcbSerialParams.fInX = false;
|
||||||
}
|
}
|
||||||
if (flowcontrol_ == flowcontrol_software) {
|
if (flowcontrol_ == flowcontrol_software) {
|
||||||
dcbSerialParams.fOutxCtsFlow = false;
|
dcbSerialParams.fOutxCtsFlow = false;
|
||||||
dcbSerialParams.fRtsControl = RTS_CONTROL_DISABLE;
|
dcbSerialParams.fRtsControl = 0x00;
|
||||||
dcbSerialParams.fOutX = true;
|
dcbSerialParams.fOutX = true;
|
||||||
dcbSerialParams.fInX = true;
|
dcbSerialParams.fInX = true;
|
||||||
}
|
}
|
||||||
if (flowcontrol_ == flowcontrol_hardware) {
|
if (flowcontrol_ == flowcontrol_hardware) {
|
||||||
dcbSerialParams.fOutxCtsFlow = true;
|
dcbSerialParams.fOutxCtsFlow = true;
|
||||||
dcbSerialParams.fRtsControl = RTS_CONTROL_HANDSHAKE;
|
dcbSerialParams.fRtsControl = 0x03;
|
||||||
dcbSerialParams.fOutX = false;
|
dcbSerialParams.fOutX = false;
|
||||||
dcbSerialParams.fInX = false;
|
dcbSerialParams.fInX = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// activate settings
|
// activate settings
|
||||||
if (!SetCommState(fd_, &dcbSerialParams)){
|
if(!SetCommState(fd_, &dcbSerialParams)){
|
||||||
CloseHandle(fd_);
|
|
||||||
THROW (IOException, "Error setting serial port settings.");
|
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
|
void
|
||||||
@ -279,15 +244,8 @@ Serial::SerialImpl::close ()
|
|||||||
{
|
{
|
||||||
if (is_open_ == true) {
|
if (is_open_ == true) {
|
||||||
if (fd_ != INVALID_HANDLE_VALUE) {
|
if (fd_ != INVALID_HANDLE_VALUE) {
|
||||||
int ret;
|
CloseHandle(fd_);
|
||||||
ret = CloseHandle(fd_);
|
fd_ = INVALID_HANDLE_VALUE;
|
||||||
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;
|
is_open_ = false;
|
||||||
}
|
}
|
||||||
@ -302,29 +260,7 @@ Serial::SerialImpl::isOpen () const
|
|||||||
size_t
|
size_t
|
||||||
Serial::SerialImpl::available ()
|
Serial::SerialImpl::available ()
|
||||||
{
|
{
|
||||||
if (!is_open_) {
|
THROW (IOException, "available is not implemented on Windows.");
|
||||||
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
|
size_t
|
||||||
@ -334,7 +270,7 @@ Serial::SerialImpl::read (uint8_t *buf, size_t size)
|
|||||||
throw PortNotOpenedException ("Serial::read");
|
throw PortNotOpenedException ("Serial::read");
|
||||||
}
|
}
|
||||||
DWORD bytes_read;
|
DWORD bytes_read;
|
||||||
if (!ReadFile(fd_, buf, static_cast<DWORD>(size), &bytes_read, NULL)) {
|
if (!ReadFile(fd_, buf, size, &bytes_read, NULL)) {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << "Error while reading from the serial port: " << GetLastError();
|
ss << "Error while reading from the serial port: " << GetLastError();
|
||||||
THROW (IOException, ss.str().c_str());
|
THROW (IOException, ss.str().c_str());
|
||||||
@ -349,7 +285,7 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
|||||||
throw PortNotOpenedException ("Serial::write");
|
throw PortNotOpenedException ("Serial::write");
|
||||||
}
|
}
|
||||||
DWORD bytes_written;
|
DWORD bytes_written;
|
||||||
if (!WriteFile(fd_, data, static_cast<DWORD>(length), &bytes_written, NULL)) {
|
if (!WriteFile(fd_, data, length, &bytes_written, NULL)) {
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
ss << "Error while writing to the serial port: " << GetLastError();
|
ss << "Error while writing to the serial port: " << GetLastError();
|
||||||
THROW (IOException, ss.str().c_str());
|
THROW (IOException, ss.str().c_str());
|
||||||
@ -360,21 +296,27 @@ Serial::SerialImpl::write (const uint8_t *data, size_t length)
|
|||||||
void
|
void
|
||||||
Serial::SerialImpl::setPort (const string &port)
|
Serial::SerialImpl::setPort (const string &port)
|
||||||
{
|
{
|
||||||
port_ = wstring(port.begin(), port.end());
|
port_ = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
string
|
string
|
||||||
Serial::SerialImpl::getPort () const
|
Serial::SerialImpl::getPort () const
|
||||||
{
|
{
|
||||||
return string(port_.begin(), port_.end());
|
return port_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Serial::SerialImpl::setTimeout (serial::Timeout &timeout)
|
Serial::SerialImpl::setTimeout (serial::Timeout &timeout)
|
||||||
{
|
{
|
||||||
timeout_ = timeout;
|
timeout_ = timeout;
|
||||||
if (is_open_) {
|
COMMTIMEOUTS timeouts = {0};
|
||||||
reconfigurePort ();
|
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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,9 +330,8 @@ void
|
|||||||
Serial::SerialImpl::setBaudrate (unsigned long baudrate)
|
Serial::SerialImpl::setBaudrate (unsigned long baudrate)
|
||||||
{
|
{
|
||||||
baudrate_ = baudrate;
|
baudrate_ = baudrate;
|
||||||
if (is_open_) {
|
if (is_open_)
|
||||||
reconfigurePort ();
|
reconfigurePort ();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long
|
unsigned long
|
||||||
@ -403,9 +344,8 @@ void
|
|||||||
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize)
|
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize)
|
||||||
{
|
{
|
||||||
bytesize_ = bytesize;
|
bytesize_ = bytesize;
|
||||||
if (is_open_) {
|
if (is_open_)
|
||||||
reconfigurePort ();
|
reconfigurePort ();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
serial::bytesize_t
|
serial::bytesize_t
|
||||||
@ -418,9 +358,8 @@ void
|
|||||||
Serial::SerialImpl::setParity (serial::parity_t parity)
|
Serial::SerialImpl::setParity (serial::parity_t parity)
|
||||||
{
|
{
|
||||||
parity_ = parity;
|
parity_ = parity;
|
||||||
if (is_open_) {
|
if (is_open_)
|
||||||
reconfigurePort ();
|
reconfigurePort ();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
serial::parity_t
|
serial::parity_t
|
||||||
@ -433,9 +372,8 @@ void
|
|||||||
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits)
|
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits)
|
||||||
{
|
{
|
||||||
stopbits_ = stopbits;
|
stopbits_ = stopbits;
|
||||||
if (is_open_) {
|
if (is_open_)
|
||||||
reconfigurePort ();
|
reconfigurePort ();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
serial::stopbits_t
|
serial::stopbits_t
|
||||||
@ -448,9 +386,8 @@ void
|
|||||||
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol)
|
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol)
|
||||||
{
|
{
|
||||||
flowcontrol_ = flowcontrol;
|
flowcontrol_ = flowcontrol;
|
||||||
if (is_open_) {
|
if (is_open_)
|
||||||
reconfigurePort ();
|
reconfigurePort ();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
serial::flowcontrol_t
|
serial::flowcontrol_t
|
||||||
@ -471,23 +408,17 @@ Serial::SerialImpl::flush ()
|
|||||||
void
|
void
|
||||||
Serial::SerialImpl::flushInput ()
|
Serial::SerialImpl::flushInput ()
|
||||||
{
|
{
|
||||||
if (is_open_ == false) {
|
THROW (IOException, "flushInput is not supported on Windows.");
|
||||||
throw PortNotOpenedException("Serial::flushInput");
|
|
||||||
}
|
|
||||||
PurgeComm(fd_, PURGE_RXCLEAR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Serial::SerialImpl::flushOutput ()
|
Serial::SerialImpl::flushOutput ()
|
||||||
{
|
{
|
||||||
if (is_open_ == false) {
|
THROW (IOException, "flushOutput is not supported on Windows.");
|
||||||
throw PortNotOpenedException("Serial::flushOutput");
|
|
||||||
}
|
|
||||||
PurgeComm(fd_, PURGE_TXCLEAR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Serial::SerialImpl::sendBreak (int /*duration*/)
|
Serial::SerialImpl::sendBreak (int duration)
|
||||||
{
|
{
|
||||||
THROW (IOException, "sendBreak is not supported on Windows.");
|
THROW (IOException, "sendBreak is not supported on Windows.");
|
||||||
}
|
}
|
||||||
@ -560,11 +491,11 @@ Serial::SerialImpl::getCTS ()
|
|||||||
throw PortNotOpenedException ("Serial::getCTS");
|
throw PortNotOpenedException ("Serial::getCTS");
|
||||||
}
|
}
|
||||||
DWORD dwModemStatus;
|
DWORD dwModemStatus;
|
||||||
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
|
if (!GetCommModemStatus(fd_, &dwModemStatus))
|
||||||
|
// Error in GetCommModemStatus;
|
||||||
THROW (IOException, "Error getting the status of the CTS line.");
|
THROW (IOException, "Error getting the status of the CTS line.");
|
||||||
}
|
|
||||||
|
|
||||||
return (MS_CTS_ON & dwModemStatus) != 0;
|
return (bool) (MS_CTS_ON & dwModemStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -574,11 +505,11 @@ Serial::SerialImpl::getDSR ()
|
|||||||
throw PortNotOpenedException ("Serial::getDSR");
|
throw PortNotOpenedException ("Serial::getDSR");
|
||||||
}
|
}
|
||||||
DWORD dwModemStatus;
|
DWORD dwModemStatus;
|
||||||
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
|
if (!GetCommModemStatus(fd_, &dwModemStatus))
|
||||||
|
// Error in GetCommModemStatus;
|
||||||
THROW (IOException, "Error getting the status of the DSR line.");
|
THROW (IOException, "Error getting the status of the DSR line.");
|
||||||
}
|
|
||||||
|
|
||||||
return (MS_DSR_ON & dwModemStatus) != 0;
|
return (bool) (MS_DSR_ON & dwModemStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -588,11 +519,11 @@ Serial::SerialImpl::getRI()
|
|||||||
throw PortNotOpenedException ("Serial::getRI");
|
throw PortNotOpenedException ("Serial::getRI");
|
||||||
}
|
}
|
||||||
DWORD dwModemStatus;
|
DWORD dwModemStatus;
|
||||||
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
|
if (!GetCommModemStatus(fd_, &dwModemStatus))
|
||||||
THROW (IOException, "Error getting the status of the RI line.");
|
// Error in GetCommModemStatus;
|
||||||
}
|
THROW (IOException, "Error getting the status of the DSR line.");
|
||||||
|
|
||||||
return (MS_RING_ON & dwModemStatus) != 0;
|
return (bool) (MS_RING_ON & dwModemStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -602,12 +533,11 @@ Serial::SerialImpl::getCD()
|
|||||||
throw PortNotOpenedException ("Serial::getCD");
|
throw PortNotOpenedException ("Serial::getCD");
|
||||||
}
|
}
|
||||||
DWORD dwModemStatus;
|
DWORD dwModemStatus;
|
||||||
if (!GetCommModemStatus(fd_, &dwModemStatus)) {
|
if (!GetCommModemStatus(fd_, &dwModemStatus))
|
||||||
// Error in GetCommModemStatus;
|
// Error in GetCommModemStatus;
|
||||||
THROW (IOException, "Error getting the status of the CD line.");
|
THROW (IOException, "Error getting the status of the DSR line.");
|
||||||
}
|
|
||||||
|
|
||||||
return (MS_RLSD_ON & dwModemStatus) != 0;
|
return (bool) (MS_RLSD_ON & dwModemStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -641,6 +571,3 @@ Serial::SerialImpl::writeUnlock()
|
|||||||
THROW (IOException, "Error releasing write mutex.");
|
THROW (IOException, "Error releasing write mutex.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // #if defined(_WIN32)
|
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,8 @@
|
|||||||
/* Copyright 2012 William Woodall and John Harrison */
|
/* Copyright 2012 William Woodall and John Harrison */
|
||||||
#include <algorithm>
|
#ifndef _WIN32
|
||||||
|
|
||||||
#if !defined(_WIN32) && !defined(__OpenBSD__) && !defined(__FreeBSD__)
|
|
||||||
# include <alloca.h>
|
# include <alloca.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined (__MINGW32__)
|
|
||||||
# define alloca __builtin_alloca
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "serial/serial.h"
|
#include "serial/serial.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -25,7 +19,7 @@ using std::size_t;
|
|||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
using serial::Serial;
|
using serial::Serial;
|
||||||
using serial::SerialException;
|
using serial::SerialExecption;
|
||||||
using serial::IOException;
|
using serial::IOException;
|
||||||
using serial::bytesize_t;
|
using serial::bytesize_t;
|
||||||
using serial::parity_t;
|
using serial::parity_t;
|
||||||
@ -43,6 +37,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
// Disable copy constructors
|
// Disable copy constructors
|
||||||
ScopedReadLock(const ScopedReadLock&);
|
ScopedReadLock(const ScopedReadLock&);
|
||||||
|
void operator=(const ScopedReadLock&);
|
||||||
const ScopedReadLock& operator=(ScopedReadLock);
|
const ScopedReadLock& operator=(ScopedReadLock);
|
||||||
|
|
||||||
SerialImpl *pimpl_;
|
SerialImpl *pimpl_;
|
||||||
@ -59,6 +54,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
// Disable copy constructors
|
// Disable copy constructors
|
||||||
ScopedWriteLock(const ScopedWriteLock&);
|
ScopedWriteLock(const ScopedWriteLock&);
|
||||||
|
void operator=(const ScopedWriteLock&);
|
||||||
const ScopedWriteLock& operator=(ScopedWriteLock);
|
const ScopedWriteLock& operator=(ScopedWriteLock);
|
||||||
SerialImpl *pimpl_;
|
SerialImpl *pimpl_;
|
||||||
};
|
};
|
||||||
@ -66,7 +62,7 @@ private:
|
|||||||
Serial::Serial (const string &port, uint32_t baudrate, serial::Timeout 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)
|
||||||
: pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
|
: read_cache_(""), pimpl_(new SerialImpl (port, baudrate, bytesize, parity,
|
||||||
stopbits, flowcontrol))
|
stopbits, flowcontrol))
|
||||||
{
|
{
|
||||||
pimpl_->setTimeout(timeout);
|
pimpl_->setTimeout(timeout);
|
||||||
@ -101,19 +97,6 @@ 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_ (uint8_t *buffer, size_t size)
|
Serial::read_ (uint8_t *buffer, size_t size)
|
||||||
{
|
{
|
||||||
@ -123,25 +106,16 @@ Serial::read_ (uint8_t *buffer, size_t size)
|
|||||||
size_t
|
size_t
|
||||||
Serial::read (uint8_t *buffer, size_t size)
|
Serial::read (uint8_t *buffer, size_t size)
|
||||||
{
|
{
|
||||||
ScopedReadLock lock(this->pimpl_);
|
ScopedReadLock (this->pimpl_);
|
||||||
return this->pimpl_->read (buffer, size);
|
return this->pimpl_->read (buffer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
Serial::read (std::vector<uint8_t> &buffer, size_t size)
|
Serial::read (std::vector<uint8_t> &buffer, size_t size)
|
||||||
{
|
{
|
||||||
ScopedReadLock lock(this->pimpl_);
|
ScopedReadLock (this->pimpl_);
|
||||||
uint8_t *buffer_ = new uint8_t[size];
|
uint8_t *buffer_ = new uint8_t[size];
|
||||||
size_t bytes_read = 0;
|
size_t bytes_read = this->pimpl_->read (buffer_, size);
|
||||||
|
|
||||||
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;
|
||||||
@ -150,16 +124,9 @@ Serial::read (std::vector<uint8_t> &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 lock(this->pimpl_);
|
ScopedReadLock (this->pimpl_);
|
||||||
uint8_t *buffer_ = new uint8_t[size];
|
uint8_t *buffer_ = new uint8_t[size];
|
||||||
size_t bytes_read = 0;
|
size_t bytes_read = this->pimpl_->read (buffer_, size);
|
||||||
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;
|
||||||
@ -176,7 +143,7 @@ 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 lock(this->pimpl_);
|
ScopedReadLock (this->pimpl_);
|
||||||
size_t eol_len = eol.length ();
|
size_t eol_len = eol.length ();
|
||||||
uint8_t *buffer_ = static_cast<uint8_t*>
|
uint8_t *buffer_ = static_cast<uint8_t*>
|
||||||
(alloca (size * sizeof (uint8_t)));
|
(alloca (size * sizeof (uint8_t)));
|
||||||
@ -188,7 +155,6 @@ 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
|
||||||
@ -212,7 +178,7 @@ 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 lock(this->pimpl_);
|
ScopedReadLock (this->pimpl_);
|
||||||
std::vector<std::string> lines;
|
std::vector<std::string> lines;
|
||||||
size_t eol_len = eol.length ();
|
size_t eol_len = eol.length ();
|
||||||
uint8_t *buffer_ = static_cast<uint8_t*>
|
uint8_t *buffer_ = static_cast<uint8_t*>
|
||||||
@ -230,7 +196,6 @@ 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
|
||||||
@ -254,7 +219,7 @@ Serial::readlines (size_t size, string eol)
|
|||||||
size_t
|
size_t
|
||||||
Serial::write (const string &data)
|
Serial::write (const string &data)
|
||||||
{
|
{
|
||||||
ScopedWriteLock lock(this->pimpl_);
|
ScopedWriteLock(this->pimpl_);
|
||||||
return this->write_ (reinterpret_cast<const uint8_t*>(data.c_str()),
|
return this->write_ (reinterpret_cast<const uint8_t*>(data.c_str()),
|
||||||
data.length());
|
data.length());
|
||||||
}
|
}
|
||||||
@ -262,14 +227,14 @@ Serial::write (const string &data)
|
|||||||
size_t
|
size_t
|
||||||
Serial::write (const std::vector<uint8_t> &data)
|
Serial::write (const std::vector<uint8_t> &data)
|
||||||
{
|
{
|
||||||
ScopedWriteLock lock(this->pimpl_);
|
ScopedWriteLock(this->pimpl_);
|
||||||
return this->write_ (&data[0], data.size());
|
return this->write_ (&data[0], data.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
Serial::write (const uint8_t *data, size_t size)
|
Serial::write (const uint8_t *data, size_t size)
|
||||||
{
|
{
|
||||||
ScopedWriteLock lock(this->pimpl_);
|
ScopedWriteLock(this->pimpl_);
|
||||||
return this->write_(data, size);
|
return this->write_(data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -282,8 +247,8 @@ Serial::write_ (const uint8_t *data, size_t length)
|
|||||||
void
|
void
|
||||||
Serial::setPort (const string &port)
|
Serial::setPort (const string &port)
|
||||||
{
|
{
|
||||||
ScopedReadLock rlock(this->pimpl_);
|
ScopedReadLock(this->pimpl_);
|
||||||
ScopedWriteLock wlock(this->pimpl_);
|
ScopedWriteLock(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);
|
||||||
@ -369,21 +334,23 @@ Serial::getFlowcontrol () const
|
|||||||
|
|
||||||
void Serial::flush ()
|
void Serial::flush ()
|
||||||
{
|
{
|
||||||
ScopedReadLock rlock(this->pimpl_);
|
ScopedReadLock(this->pimpl_);
|
||||||
ScopedWriteLock wlock(this->pimpl_);
|
ScopedWriteLock(this->pimpl_);
|
||||||
pimpl_->flush ();
|
pimpl_->flush ();
|
||||||
|
read_cache_.clear ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial::flushInput ()
|
void Serial::flushInput ()
|
||||||
{
|
{
|
||||||
ScopedReadLock lock(this->pimpl_);
|
ScopedReadLock(this->pimpl_);
|
||||||
pimpl_->flushInput ();
|
pimpl_->flushInput ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial::flushOutput ()
|
void Serial::flushOutput ()
|
||||||
{
|
{
|
||||||
ScopedWriteLock lock(this->pimpl_);
|
ScopedWriteLock(this->pimpl_);
|
||||||
pimpl_->flushOutput ();
|
pimpl_->flushOutput ();
|
||||||
|
read_cache_.clear ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Serial::sendBreak (int duration)
|
void Serial::sendBreak (int duration)
|
||||||
|
|||||||
@ -1,12 +0,0 @@
|
|||||||
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()
|
|
||||||
@ -20,6 +20,8 @@ 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
|
||||||
@ -27,7 +29,7 @@ void loop()
|
|||||||
|
|
||||||
#include "serial/serial.h"
|
#include "serial/serial.h"
|
||||||
|
|
||||||
#if defined(__linux__)
|
#ifdef __linux__
|
||||||
#include <pty.h>
|
#include <pty.h>
|
||||||
#else
|
#else
|
||||||
#include <util.h>
|
#include <util.h>
|
||||||
@ -1,63 +0,0 @@
|
|||||||
#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();
|
|
||||||
}
|
|
||||||
@ -1,147 +0,0 @@
|
|||||||
<?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>
|
|
||||||
@ -1,39 +0,0 @@
|
|||||||
<?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>
|
|
||||||
@ -1,133 +0,0 @@
|
|||||||
<?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>
|
|
||||||
@ -1,22 +0,0 @@
|
|||||||
<?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>
|
|
||||||
@ -1,36 +0,0 @@
|
|||||||
|
|
||||||
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
|
|
||||||
Loading…
x
Reference in New Issue
Block a user