From ba0c648119c78f282f23b96a4d31184e4351f6a0 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Mon, 7 May 2012 13:04:34 -0500 Subject: [PATCH 1/5] Fixes #16. --- include/serial/serial.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/serial/serial.h b/include/serial/serial.h index 3f2c60c..bb310b4 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include From 3039acbab9b3f93bb7f43ef73076147e691a2a49 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Mon, 7 May 2012 13:06:28 -0500 Subject: [PATCH 2/5] Adding a convenience function for generating simple Timeouts based purely on the total time until a timeout occurs with no regard to inter byte timeout or timeout multipliers. --- include/serial/serial.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/serial/serial.h b/include/serial/serial.h index bb310b4..8d40342 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -99,6 +99,20 @@ struct Timeout { write_timeout_constant(write_timeout_constant_), write_timeout_multiplier(write_timeout_multiplier_) {} + + /*! + * Convenience function to generate Timeout structs using a + * single absolute timeout. + * + * \param timeout A long that defines the time in milliseconds until a + * timeout occurs after a call to read or write is made. + * + * \return Timeout struct that represents this simple timeout provided. + */ + static Timeout simpleTimeout(long timeout) { + return Timeout(0, timeout, 0, timeout, 0); + } + /*! Number of milliseconds between bytes received to timeout on. */ long inter_byte_timeout; /*! A constant number of milliseconds to wait after calling read. */ From 937e83469836dda5f7a8cd9d1173820ebc06512e Mon Sep 17 00:00:00 2001 From: William Woodall Date: Wed, 9 May 2012 19:51:56 -0500 Subject: [PATCH 3/5] Updating the unix implementation to support the more sophisticated timeout system. --- CMakeLists.txt | 2 +- serial.sublime-workspace | 128 +++++---------------------------------- src/impl/unix.cc | 58 +++++++++++------- 3 files changed, 51 insertions(+), 137 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dfffc80..d6f9e6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.4.6) set(ROS_ROOT $ENV{ROS_ROOT}) -option(SERIAL_BUILD_WIHOUT_ROS "Build without ROS?" OFF) +option(SERIAL_BUILD_WIHOUT_ROS "Build without ROS?" ON) if(DEFINED ROS_ROOT AND NOT SERIAL_BUILD_WIHOUT_ROS) # Build with ROS diff --git a/serial.sublime-workspace b/serial.sublime-workspace index 486d099..053d890 100644 --- a/serial.sublime-workspace +++ b/serial.sublime-workspace @@ -275,32 +275,8 @@ }, "buffers": [ - { - "file": "include/serial/serial.h", - "settings": - { - "buffer_size": 17876, - "line_ending": "Unix" - } - }, - { - "file": "src/serial.cc", - "settings": - { - "buffer_size": 7015, - "line_ending": "Unix" - } - }, - { - "file": "src/impl/unix.cc", - "settings": - { - "buffer_size": 16696, - "line_ending": "Unix" - } - } ], - "build_system": "Packages/Python/Python.sublime-build", + "build_system": "Packages/Makefile/Make.sublime-build", "command_palette": { "height": 47.0, @@ -332,6 +308,14 @@ }, "file_history": [ + "/Users/william/devel/serial/src/serial.cc", + "/Users/william/devel/serial/src/impl/unix.cc", + "/Users/william/devel/serial/include/serial/serial.h", + "/Users/william/devel/serial/CMakeLists.txt", + "/Users/william/devel/serial/serial.sublime-project", + "/Users/william/Library/Application Support/Sublime Text 2/Packages/SublimeClang/SublimeClang.sublime-settings", + "/Users/william/Library/Application Support/Sublime Text 2/Packages/SublimeClang/Default.sublime-keymap", + "/Users/william/Library/Application Support/Sublime Text 2/Packages/User/SublimeClang.sublime-settings", "/Users/william/devel/automow_ws/au_automow_common/automow_planning/tests/test_path_planner.py", "/Users/william/devel/automow_ws/au_automow_common/automow_planning/tests/test_coverage_planner.py", "/Users/william/devel/automow_ws/au_automow_common/automow_planning/tests/test_costmap.py", @@ -343,7 +327,6 @@ "/Users/william/devel/xbow400/src/xbow_400.cpp", "/Users/william/devel/xbow400/README", "/Users/william/devel/xbow400/xbow400.sublime-project", - "/Users/william/Library/Application Support/Sublime Text 2/Packages/SublimeClang/SublimeClang.sublime-settings", "/Users/william/devel/comp6400/homework6/homework6.sublime-project", "/Users/william/devel/comp6400/homework6/src/main.cc", "/Users/william/devel/comp6400/homework6/models/student.obj", @@ -462,95 +445,8 @@ "groups": [ { - "selected": 2, "sheets": [ - { - "buffer": 0, - "file": "include/serial/serial.h", - "settings": - { - "buffer_size": 17876, - "regions": - { - }, - "selection": - [ - [ - 11407, - 11407 - ] - ], - "settings": - { - "syntax": "Packages/C++/C++.tmLanguage", - "tab_size": 4, - "translate_tabs_to_spaces": true, - "word_wrap": true, - "wrap_width": 80 - }, - "translation.x": 0.0, - "translation.y": 6934.0, - "zoom_level": 1.0 - }, - "type": "text" - }, - { - "buffer": 1, - "file": "src/serial.cc", - "settings": - { - "buffer_size": 7015, - "regions": - { - }, - "selection": - [ - [ - 0, - 0 - ] - ], - "settings": - { - "syntax": "Packages/C++/C++.tmLanguage", - "tab_size": 2, - "translate_tabs_to_spaces": true - }, - "translation.x": 0.0, - "translation.y": 479.0, - "zoom_level": 1.0 - }, - "type": "text" - }, - { - "buffer": 2, - "file": "src/impl/unix.cc", - "settings": - { - "buffer_size": 16696, - "regions": - { - }, - "selection": - [ - [ - 15521, - 15521 - ] - ], - "settings": - { - "syntax": "Packages/C++/C++.tmLanguage", - "tab_size": 2, - "translate_tabs_to_spaces": true - }, - "translation.x": 0.0, - "translation.y": 10013.0, - "zoom_level": 1.0 - }, - "type": "text" - } ] }, { @@ -587,7 +483,7 @@ "cols": [ 0.0, - 0.61784814072, + 0.766633361467, 1.0 ], "rows": @@ -601,6 +497,10 @@ { "height": 100.0 }, + "output.exec": + { + "height": 229.0 + }, "replace": { "height": 64.0 diff --git a/src/impl/unix.cc b/src/impl/unix.cc index d34d7aa..10af480 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -371,47 +371,61 @@ inline void get_time_now(struct timespec &time) size_t Serial::SerialImpl::read (unsigned char *buf, size_t size) { + // If the port is not open, throw if (!is_open_) { throw PortNotOpenedException ("Serial::read"); } fd_set readfds; size_t bytes_read = 0; - struct timeval timeout; - timeout.tv_sec = timeout_.read_timeout_constant / 1000; - timeout.tv_usec = static_cast (timeout_.read_timeout_constant % 1000); - timeout.tv_usec *= 1000; // To convert to micro seconds + // 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) + long total_timeout_ms = timeout_.read_timeout_constant; + total_timeout_ms += timeout_.read_timeout_multiplier*static_cast(size); + total_timeout.tv_sec = total_timeout_ms / 1000; + total_timeout.tv_usec = static_cast(total_timeout_ms % 1000); + total_timeout.tv_usec *= 1000; // To convert to micro seconds while (bytes_read < size) { + // Setup the inter_byte timeout timeval + struct timeval timeout; // Inter-byte timeout + timeout.tv_sec = timeout_.inter_byte_timeout / 1000; + timeout.tv_usec = static_cast (timeout_.inter_byte_timeout % 1000); + timeout.tv_usec *= 1000; // To convert to micro seconds FD_ZERO (&readfds); FD_SET (fd_, &readfds); - // 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 - int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout); -#if !defined(__linux__) + get_time_now (start); + int r; + // If the total_timeout is less than the inter_byte_timeout + if (total_timeout.tv_sec < timeout.tv_sec + || (total_timeout.tv_sec == timeout.tv_sec + && total_timeout.tv_usec < timeout.tv_sec)) + { + // Use the total_timeout + r = select (fd_ + 1, &readfds, NULL, NULL, &total_timeout); + } else { + // Else use the inter_byte_timeout + r = select (fd_ + 1, &readfds, NULL, NULL, &timeout); + } // Calculate difference and update the structure - get_time_now(end); + 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 ((end.tv_nsec - start.tv_nsec) / 1000); // Update the timeout - if (timeout.tv_sec <= diff.tv_sec) { - timeout.tv_sec = 0; + if (total_timeout.tv_sec <= diff.tv_sec) { + total_timeout.tv_sec = 0; } else { - timeout.tv_sec -= diff.tv_sec; + total_timeout.tv_sec -= diff.tv_sec; } - if (timeout.tv_usec <= diff.tv_usec) { - timeout.tv_usec = 0; + if (total_timeout.tv_usec <= diff.tv_usec) { + total_timeout.tv_usec = 0; } else { - timeout.tv_usec -= diff.tv_usec; + total_timeout.tv_usec -= diff.tv_usec; } -#endif // Figure out what happened by looking at select's response 'r' /** Error **/ @@ -431,7 +445,7 @@ Serial::SerialImpl::read (unsigned char *buf, size_t size) 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 avaialble now + // 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); From 15f01a53a12bcb9d5266dcf3feed360204954628 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Thu, 10 May 2012 11:43:17 -0500 Subject: [PATCH 4/5] Adjustments to the read timeouts --- .gitignore | 1 + serial.sublime-project | 2 + serial.sublime-workspace | 531 --------------------------------------- src/impl/unix.cc | 38 +-- 4 files changed, 24 insertions(+), 548 deletions(-) delete mode 100644 serial.sublime-workspace diff --git a/.gitignore b/.gitignore index 00f5a05..2fa13ef 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ patched wiped msg_gen srv_gen +*sublime-workspace diff --git a/serial.sublime-project b/serial.sublime-project index 1746fa9..c6270a1 100644 --- a/serial.sublime-project +++ b/serial.sublime-project @@ -1,4 +1,6 @@ { + "word_wrap": "on", + "wrap_width": 80, "folders": [ { diff --git a/serial.sublime-workspace b/serial.sublime-workspace deleted file mode 100644 index 053d890..0000000 --- a/serial.sublime-workspace +++ /dev/null @@ -1,531 +0,0 @@ -{ - "auto_complete": - { - "selected_items": - [ - [ - "tour", - "toupper(int _c) int" - ], - [ - "rotat", - "SceneObject::rotateTo(GLfloat roll, GLfloat pitch, GLfloat yaw) void" - ], - [ - "tou", - "touring_index size_t" - ], - [ - "person", - "person_mbb Model::CollisionBox" - ], - [ - "SE", - "SE Model::CollisionBox" - ], - [ - "m", - "mbb Model::CollisionBox" - ], - [ - "glVer", - "glVertex2fv(const GLfloat *v) void" - ], - [ - "di", - "distance GLfloat" - ], - [ - "the", - "their_position GLfloat *" - ], - [ - "their", - "their_obj_it vector::iterator" - ], - [ - "my", - "my_obj_it vector::iterator" - ], - [ - "my_mod", - "my_model_it vector::iterator" - ], - [ - "thei", - "their_model_it vector::iterator" - ], - [ - "iterator", - "assign(_InputIterator __first, _InputIterator __last) void" - ], - [ - "get", - "getPosition() GLfloat *" - ], - [ - "lh", - "lhs_mbb Model::CollisionBox" - ], - [ - "cal", - "calculateMBB(Model::Object &obj) Model::CollisionBox" - ], - [ - "Col", - "CollisionBox " - ], - [ - "spo", - "spotlight LightingStyle" - ], - [ - "main", - "main_camera Camera" - ], - [ - "pers", - "person Person *" - ], - [ - "glro", - "glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) void" - ], - [ - "glTran", - "glTranslatef(GLfloat x, GLfloat y, GLfloat z) void" - ], - [ - "she", - "shelby_model string" - ], - [ - "loa", - "loaded_ bool" - ], - [ - "ena", - "enabled_ bool" - ], - [ - "sp", - "specular(GLfloat r, GLfloat g, GLfloat b, GLfloat a) void" - ], - [ - "en", - "direction(GLfloat x, GLfloat y, GLfloat z) void" - ], - [ - "spot", - "spot_light Light" - ], - [ - "GL_NOR", - "GL_NORMALIZE " - ], - [ - "att", - "attenuation_q_ GLfloat" - ], - [ - "atte", - "attenuation_c_ GLfloat" - ], - [ - "glPop", - "glPopMatrix() void" - ], - [ - "appl", - "apply_() void" - ], - [ - "no_l", - "no_lights_left const size_t" - ], - [ - "ligh", - "lights_left size_t" - ], - [ - "do", - "do_normals bool" - ], - [ - "back_in", - "back_inserter(_Container &__x) back_insert_iterator<_Container>" - ], - [ - "istr", - "istream_iterator " - ], - [ - "is", - "istringstream " - ], - [ - "math", - "matches size_t" - ], - [ - "curr", - "current_object Model::Object" - ], - [ - "matc", - "matches2 size_t" - ], - [ - "QwtPlot", - "QwtPlotZoomer" - ], - [ - "__init", - "__initZooming" - ], - [ - "plot_coi", - "plot_coil2" - ], - [ - "plo", - "plot_coil1" - ], - [ - "plot_co", - "plot_coil2" - ], - [ - "QwtPl", - "QwtPlotCurve" - ], - [ - "trail", - "trailer_plotcurve" - ], - [ - "pl", - "plot_coil1" - ], - [ - "plot", - "plot_coil1" - ], - [ - "open", - "open_button" - ], - [ - "plotcoi", - "plot_coil1" - ], - [ - "EM", - "EM61MK2Log" - ], - [ - "pa", - "path_file_name" - ], - [ - "head", - "headings" - ], - [ - "eas", - "eastings" - ], - [ - "north", - "northings" - ], - [ - "las", - "last_east" - ], - [ - "odom", - "odom" - ], - [ - "logs", - "logs_data" - ], - [ - "plot_", - "plot_em61mk2logs" - ], - [ - "Em61", - "EM61MK2Log" - ], - [ - "sib", - "sibling_file_name" - ], - [ - "si", - "sibling_file_name" - ], - [ - "fil", - "file_name" - ] - ] - }, - "buffers": - [ - ], - "build_system": "Packages/Makefile/Make.sublime-build", - "command_palette": - { - "height": 47.0, - "selected_items": - [ - [ - "install", - "Package Control: Install Package" - ], - [ - "late", - "LaTeXTools: View PDF" - ] - ], - "width": 449.0 - }, - "console": - { - "height": 0.0 - }, - "distraction_free": - { - "menu_visible": true, - "show_minimap": false, - "show_open_files": false, - "show_tabs": false, - "side_bar_visible": false, - "status_bar_visible": false - }, - "file_history": - [ - "/Users/william/devel/serial/src/serial.cc", - "/Users/william/devel/serial/src/impl/unix.cc", - "/Users/william/devel/serial/include/serial/serial.h", - "/Users/william/devel/serial/CMakeLists.txt", - "/Users/william/devel/serial/serial.sublime-project", - "/Users/william/Library/Application Support/Sublime Text 2/Packages/SublimeClang/SublimeClang.sublime-settings", - "/Users/william/Library/Application Support/Sublime Text 2/Packages/SublimeClang/Default.sublime-keymap", - "/Users/william/Library/Application Support/Sublime Text 2/Packages/User/SublimeClang.sublime-settings", - "/Users/william/devel/automow_ws/au_automow_common/automow_planning/tests/test_path_planner.py", - "/Users/william/devel/automow_ws/au_automow_common/automow_planning/tests/test_coverage_planner.py", - "/Users/william/devel/automow_ws/au_automow_common/automow_planning/tests/test_costmap.py", - "/Users/william/devel/automow_ws/au_automow_common/automow_planning/tests/survey.csv", - "/Users/william/devel/automow_ws/au_automow_common/automow_planning/tests/image_generator.py", - "/Users/william/.ros/rosdep.yaml", - "/Users/william/.bashrc", - "/Users/william/devel/xbow400/include/xbow_400.h", - "/Users/william/devel/xbow400/src/xbow_400.cpp", - "/Users/william/devel/xbow400/README", - "/Users/william/devel/xbow400/xbow400.sublime-project", - "/Users/william/devel/comp6400/homework6/homework6.sublime-project", - "/Users/william/devel/comp6400/homework6/src/main.cc", - "/Users/william/devel/comp6400/homework6/models/student.obj", - "/Users/william/devel/comp6400/homework6/models/ground.obj", - "/Users/william/devel/comp6400/homework6/models/shelby.obj", - "/Users/william/devel/comp6400/homework6/models/ground.mtl", - "/Users/william/devel/comp6400/homework6/models/shelby.mtl", - "/Users/william/devel/comp6400/homework6/CMakeLists.txt", - "/Users/william/devel/comp6400/homework6/models/shelby.blend", - "/Users/william/devel/comp6400/homework6/Makefile", - "/usr/include/c++/4.2.1/tr1/cstdio", - "/Users/william/devel/comp6400/homework6/src/michaels.cc", - "/Users/william/devel/auxos_orig/scripts/plot_em.py", - "/Users/william/devel/auxos_orig/scripts/logtool.py", - "/Users/william/devel/auxos_orig/scripts/em61mk2.py", - "/Users/william/devel/auxos_orig/scripts/lib/__init__.py", - "/usr/local/Library/Formula/pyqt.rb", - "/usr/local/Library/Formula/pyqwt.rb", - "/Users/william/devel/3dteleop_ws/3d_teleop/redo_slam_offline/launch/play_and_remap_tf.launch", - "/Users/william/devel/3dteleop_ws/3d_teleop/misc_scripts/launch/automow_stuff.launch", - "/Users/william/devel/auxos_orig/scripts/coords2path.py", - "/Users/william/Downloads/sup1_waypts_utm_path.txt", - "/Users/william/Downloads/example.txt", - "/Users/william/devel/auxos_orig/scripts/sup1_waypts_utm_path.txt", - "/Users/william/devel/ion_plans_paper/citations.bib", - "/Users/william/devel/ion_plans_paper/ion_plans.tex", - "/Users/william/Desktop/em61mk2.py", - "/Users/william/devel/ion_plans_paper/ion_plans.log", - "/Users/william/devel/ion_plans_paper/ion_plans.pdf", - "/Users/william/devel/3dteleop_ws/octomap/octomap/include/octomap/OcTree.h", - "/Users/william/devel/3dteleop_ws/octomap/octomap/include/octomap/OccupancyOcTreeBase.h", - "/Users/william/devel/3dteleop_ws/octomap/octomap/include/octomap/OccupancyOcTreeBase.hxx", - "/Users/william/devel/3dteleop_ws/octomap/octomap/src/OcTree.cpp", - "/Users/william/devel/3dteleop_ws/octomap/octomap/include/octomap/octomap_deprecated.h", - "/Users/william/devel/3dteleop_ws/octomap/CMakeLists.txt", - "/Users/william/devel/comp6400/homework3/src/main.cc", - "/Users/william/Desktop/untitled.obj", - "/Users/william/devel/cs373-tests/util.py", - "/Users/william/devel/cs373-tests/2.6/code.py", - "/Users/william/devel/cs373-tests/2.6/template.py", - "/Users/william/devel/3dteleop_ws/3d_teleop/misc_scripts/manifest.xml", - "/Users/william/devel/3dteleop_ws/3d_teleop/misc_scripts/nodes/odom2tf.py", - "/Users/william/devel/3dteleop_ws/3d_teleop/misc_scripts/nodes/automow_laser_filter.py", - "/Users/william/devel/3dteleop_ws/3d_teleop/misc_scripts/nodes/rename_tf_frame.py", - "/Users/william/devel/3dteleop_ws/3d_teleop/misc_scripts/nodes/remap_tf.py", - "/Users/william/Downloads/orocos_kinematics_dynamics-ffdeaad-install_name_tool.patch", - "/Users/william/devel/automow_ws/au_automow_common/automow_bringup/launch/minimal.launch", - "/Users/william/devel/vis_ws/visualization/rviz/manifest.xml", - "/Library/Python/2.7/site-packages/pkg_resources.py", - "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", - "/Volumes/Storage/Data/Camp Sibert/Feb 15 - Feb 20/plot_em61mk2.py", - "/Volumes/Storage/Data/Camp Sibert/Feb 15 - Feb 20/em61mk2.py", - "/Volumes/Storage/Data/Camp Sibert 2-15to2-20/em61mk2.py", - "/Volumes/Storage/Data/Camp Sibert 2-15to2-20/plot_em61mk2.py" - ], - "find": - { - "height": 35.0 - }, - "find_in_files": - { - "height": 0.0, - "where_history": - [ - "" - ] - }, - "find_state": - { - "case_sensitive": false, - "find_history": - [ - "cts", - " ", - "o ", - "Cube_Cube.001", - "min[2]", - "min[1]", - "max[2]", - "max[1]", - "max[0]", - "min[0]", - "window_width", - "800", - "•", - "Cube", - "expected 1.", - "counter", - "go", - "setZoomerMousePattern", - "updateNode", - "compute", - "insertS" - ], - "highlight": true, - "in_selection": false, - "preserve_case": false, - "regex": false, - "replace_history": - [ - ", ", - "min.z", - "min.y", - "max.z", - "max.y", - "max.x", - "min.x", - "*" - ], - "reverse": false, - "show_context": true, - "use_buffer2": true, - "whole_word": false, - "wrap": true - }, - "groups": - [ - { - "sheets": - [ - ] - }, - { - "sheets": - [ - ] - } - ], - "incremental_find": - { - "height": 0.0 - }, - "input": - { - "height": 31.0 - }, - "layout": - { - "cells": - [ - [ - 0, - 0, - 1, - 1 - ], - [ - 1, - 0, - 2, - 1 - ] - ], - "cols": - [ - 0.0, - 0.766633361467, - 1.0 - ], - "rows": - [ - 0.0, - 1.0 - ] - }, - "menu_visible": true, - "output.clang": - { - "height": 100.0 - }, - "output.exec": - { - "height": 229.0 - }, - "replace": - { - "height": 64.0 - }, - "save_all_on_build": true, - "select_file": - { - "height": 0.0, - "selected_items": - [ - ], - "width": 0.0 - }, - "select_project": - { - "height": 0.0, - "selected_items": - [ - ], - "width": 0.0 - }, - "show_minimap": false, - "show_open_files": false, - "show_tabs": true, - "side_bar_visible": true, - "side_bar_width": 283.0, - "status_bar_visible": true -} diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 10af480..9064ef2 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -386,29 +386,33 @@ Serial::SerialImpl::read (unsigned char *buf, size_t size) total_timeout.tv_sec = total_timeout_ms / 1000; total_timeout.tv_usec = static_cast(total_timeout_ms % 1000); total_timeout.tv_usec *= 1000; // To convert to micro seconds + // Setup the inter byte timeout + struct timeval inter_byte_timeout; + inter_byte_timeout.tv_sec = timeout_.inter_byte_timeout / 1000; + inter_byte_timeout.tv_usec = + static_cast (timeout_.inter_byte_timeout % 1000); + inter_byte_timeout.tv_usec *= 1000; // To convert to micro seconds while (bytes_read < size) { - // Setup the inter_byte timeout timeval - struct timeval timeout; // Inter-byte timeout - timeout.tv_sec = timeout_.inter_byte_timeout / 1000; - timeout.tv_usec = static_cast (timeout_.inter_byte_timeout % 1000); - timeout.tv_usec *= 1000; // To convert to micro seconds + // Setup the select timeout timeval + struct timeval timeout; + // If the total_timeout is less than the inter_byte_timeout + 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; + } FD_ZERO (&readfds); FD_SET (fd_, &readfds); // Begin timing select struct timespec start, end; get_time_now (start); - int r; - // If the total_timeout is less than the inter_byte_timeout - if (total_timeout.tv_sec < timeout.tv_sec - || (total_timeout.tv_sec == timeout.tv_sec - && total_timeout.tv_usec < timeout.tv_sec)) - { - // Use the total_timeout - r = select (fd_ + 1, &readfds, NULL, NULL, &total_timeout); - } else { - // Else use the inter_byte_timeout - r = select (fd_ + 1, &readfds, NULL, NULL, &timeout); - } + // Call select to block for serial data or a timeout + int r = select (fd_ + 1, &readfds, NULL, NULL, &timeout); // Calculate difference and update the structure get_time_now (end); // Calculate the time select took From dcc73db6a82d90cfc886c99ca0d82086081460a8 Mon Sep 17 00:00:00 2001 From: William Woodall Date: Fri, 11 May 2012 15:28:23 -0500 Subject: [PATCH 5/5] Syntax fix --- src/impl/unix.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 9064ef2..6705bf3 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -711,7 +711,7 @@ Serial::SerialImpl::getDSR () } bool -Serial::SerialImpl::getRI() +Serial::SerialImpl::getRI () { if (is_open_ == false) { throw PortNotOpenedException ("Serial::getRI"); @@ -721,7 +721,7 @@ Serial::SerialImpl::getRI() } bool -Serial::SerialImpl::getCD() +Serial::SerialImpl::getCD () { if (is_open_ == false) { throw PortNotOpenedException ("Serial::getCD"); @@ -731,7 +731,7 @@ Serial::SerialImpl::getCD() } void -Serial::SerialImpl::readLock() +Serial::SerialImpl::readLock () { int result = pthread_mutex_lock(&this->read_mutex); if (result) { @@ -740,7 +740,7 @@ Serial::SerialImpl::readLock() } void -Serial::SerialImpl::readUnlock() +Serial::SerialImpl::readUnlock () { int result = pthread_mutex_unlock(&this->read_mutex); if (result) { @@ -749,7 +749,7 @@ Serial::SerialImpl::readUnlock() } void -Serial::SerialImpl::writeLock() +Serial::SerialImpl::writeLock () { int result = pthread_mutex_lock(&this->write_mutex); if (result) { @@ -758,7 +758,7 @@ Serial::SerialImpl::writeLock() } void -Serial::SerialImpl::writeUnlock() +Serial::SerialImpl::writeUnlock () { int result = pthread_mutex_unlock(&this->write_mutex); if (result) {