serial_port.h
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 //     * Redistributions of source code must retain the above copyright
00009 //       notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above copyright
00011 //       notice, this list of conditions and the following disclaimer in the
00012 //       documentation and/or other materials provided with the distribution.
00013 //     * Neither the name of Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 // ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #ifndef SERIAL_UTIL_SERIAL_PORT_H_
00031 #define SERIAL_UTIL_SERIAL_PORT_H_
00032 
00033 #include <stdint.h>
00034 
00035 #include <string>
00036 #include <vector>
00037 
00038 namespace swri_serial_util
00039 {
00043   struct SerialConfig
00044   {
00045     enum Parity
00046     {
00047       NO_PARITY,
00048       EVEN_PARITY,
00049       ODD_PARITY
00050     };
00051 
00062     SerialConfig();
00063 
00064     SerialConfig(
00065         int32_t baud,
00066         int32_t data_bits,
00067         int32_t stop_bits,
00068         Parity parity,
00069         bool flow_control,
00070         bool low_latency_mode,
00071         bool writable);
00072 
00073     int32_t baud;
00074     int32_t data_bits;
00075     int32_t stop_bits;
00076     Parity parity;
00077     bool flow_control;
00078     bool low_latency_mode;
00079     bool writable;
00080   };
00081 
00082   class SerialPort
00083   {
00084    public:
00085 
00086     enum Result
00087     {
00088       SUCCESS,
00089       TIMEOUT,
00090       INTERRUPTED,
00091       ERROR
00092     };
00093 
00094     int fd_;
00095     std::string error_msg_;
00096 
00100     SerialPort();
00101 
00107     ~SerialPort();
00108 
00123     bool Open(const std::string &device, SerialConfig config = SerialConfig());
00124 
00128     void Close();
00129 
00143     Result ReadBytes(std::vector<uint8_t>& output, size_t max_bytes, int32_t timeout);
00144 
00145     int32_t Write(const std::vector<uint8_t>& input);
00146 
00150     std::string ErrorMsg() const { return error_msg_; }
00151 
00152    private:
00156     bool SetLowLatencyMode();
00157 
00165     int32_t ParseBaudRate(int32_t baud);
00166   };
00167 }
00168 
00169 #endif  // SERIAL_UTIL_SERIAL_PORT_H_


swri_serial_util
Author(s): Marc Alban
autogenerated on Tue Oct 3 2017 03:19:43