JeVois  1.8
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Serial.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
21 #include <jevois/Types/Enum.H>
22 #include <chrono>
23 #include <termios.h>
24 #include <unistd.h>
25 #include <mutex>
26 
27 namespace jevois
28 {
29  namespace serial
30  {
31  static ParameterCategory const ParamCateg("Serial Port Options");
32 
33  //! Parameter \relates jevois::Serial
34  JEVOIS_DECLARE_PARAMETER(devname, std::string, "Device file name",
35  "", ParamCateg);
36 
37  //! Parameter \relates jevois::Serial
38  JEVOIS_DECLARE_PARAMETER(baudrate, unsigned int, "Baudrate",
39  115200, { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200,
40  230400, 460800, 921600, 1000000, 1152000, 1500000, 2000000,
41  2500000, 3000000, 3500000, 4000000 }, ParamCateg);
42 
43  //! Parameter \relates jevois::Serial
44  JEVOIS_DECLARE_PARAMETER(format, std::string, "Data format",
45  "8N1", boost::regex("^[5-8][NEO][12]$"), ParamCateg);
46 
47  //! Parameter \relates jevois::Serial
48  JEVOIS_DECLARE_PARAMETER(flowsoft, bool, "Use soft (XON/XOFF) flow control",
49  false, ParamCateg);
50 
51  //! Parameter \relates jevois::Serial
52  JEVOIS_DECLARE_PARAMETER(flowhard, bool, "Use hard (RTS/CTS) flow control",
53  false, ParamCateg);
54 
55  //! Parameter \relates jevois::Serial
56  JEVOIS_DECLARE_PARAMETER(drop, bool, "Silently drop write data when write buffer is full. Useful to "
57  "avoid blocking when writing messages to serial-over-USB port and the host is "
58  "not listening to it",
59  true, ParamCateg);
60 
61  //! Enum for Parameter \relates jevois::Serial
62  JEVOIS_DEFINE_ENUM_CLASS(LineStyle, (LF) (CR) (CRLF) (Zero) (Sloppy) );
63 
64  //! Parameter \relates jevois::Serial
65  JEVOIS_DECLARE_PARAMETER(linestyle, LineStyle, "End of line style: LF is for 0x0a [\\n]; CR is for 0x0d [\\r]; "
66  "CRLF is for 0x0d 0x0a [\\r\\n]; Zero is for 0x00 [\\0]; Sloppy accepts any of "
67  "CR, LF, CRLF, 0xd0 (issued by some keyboards instead of Return), and Zero as input "
68  "and issues CRLF in outputs.",
69  LineStyle::Sloppy, LineStyle_Values, ParamCateg);
70 
71  //! Enum for Parameter \relates jevois::Serial
72  JEVOIS_DEFINE_ENUM_CLASS(TerminalMode, (Plain) (VT100) );
73 
74  //! Parameter \relates jevois::Serial
75  JEVOIS_DECLARE_PARAMETER(mode, TerminalMode, "Terminal emulation mode for input",
76  TerminalMode::Plain, TerminalMode_Values, ParamCateg);
77  } // namespace serial
78 
79  //! Interface to a serial port
80  /*! This class is thread-safe. Concurrent read and write (which do not seem to be supported by the O.S. or hardware)
81  are serialized through the use of a mutex in the Serial class. \ingroup core */
82  class Serial : public UserInterface,
83  public Parameter<serial::devname, serial::baudrate, serial::format, serial::flowsoft,
84  serial::flowhard, serial::drop, serial::linestyle, serial::mode>
85  {
86  public:
87  //! Constructor
88  Serial(std::string const & instance, UserInterface::Type type);
89 
90  //! destructor
91  virtual ~Serial();
92 
93  //! Set the access to blocking or not
94  /*! Default is non-blocking. If blocking, can also specify a timeout (rounded to tenth of seconds) */
95  void setBlocking(bool blocking, std::chrono::milliseconds const & timeout);
96 
97  //! Set the DTR mode off momentarily.
98  void toggleDTR(std::chrono::milliseconds const & dur);
99 
100  //! transmit continuous stream of zero-valued bits for specific duration.
101  void sendBreak(void);
102 
103  //! Read some bytes if available, and return true and a string when one is complete
104  /*! This would usually only make sense to use in non-blocking mode. */
105  bool readSome(std::string & str) override;
106 
107  //! Read a string, using the line termination convention of serial::linestyle
108  /*! No line terminator is included in the read string that is returned. This would normally only make sense to use
109  when the port is in blocking mode, as it throws if trying to chars until the end of line marker but none is
110  available. */
111  std::string readString();
112 
113  //! Write a string, using the line termination convention of serial::linestyle
114  /*! No line terminator should be included in the string, writeString() will add one. */
115  void writeString(std::string const & str) override;
116 
117  //! Attempt to read up to nbytes from serial port into the buffer
118  /*! @param buffer holds bytes after read
119  @param nbytes number of bytes to attempt to read
120  @return 0 on a timeout, -1 on error, number of bytes actually read on a success */
121  int read(void * buffer, const int nbytes);
122 
123  //! Attempt to read up to nbytes from serial port into the buffer
124  /*! @param buffer holds bytes after read
125  @param nbytes number of bytes to attempt to read
126  @return 0 on a timeout, 0 on no new characters, -1 on error, number of bytes actually read on a success */
127  int read2(void * buffer, const int nbytes);
128 
129  //! Write bytes to the port
130  /*! @param buffer begin writing from the location buffer.
131  @param nbytes number of bytes to write */
132  void write(void const * buffer, const int nbytes);
133 
134  //! Write bytes to the port - does not wait for buffer to be emptied
135  /*! @param buffer begin writing from the location buffer.
136  @param nbytes number of bytes to write */
137  void writeNoCheck(void const * buffer, const int nbytes);
138 
139  //! Flush all inputs
140  void flush(void);
141 
142  //! Return our port type, here Hard or USB
143  UserInterface::Type type() const override;
144 
145  protected:
146  void postInit() override;
147  void postUninit() override;
148 
149  private:
150  int itsDev; // descriptor associated with the device file
151  termios itsSavedState; // saved state to restore in the destructor
152  std::string itsPartialString;
153  std::mutex itsMtx;
154  int itsWriteOverflowCounter; // counter so we do not send too many write overflow errors, or -1 to drop
156  };
157 } // namespace jevois
Type
Enum for the interface type.
Definition: UserInterface.H:53
Generic variadic template class template definition for Component Parameters.
A category to which multiple ParameterDef definitions can belong.
Definition: ParameterDef.H:33
Interface to a serial port.
Definition: Serial.H:82
JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg)
Abstract base class for a string-based user interface.
Definition: UserInterface.H:32