Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
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_