JeVois  1.20
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 #include <future>
27 
28 namespace jevois
29 {
30  namespace serial
31  {
32  static ParameterCategory const ParamCateg("Serial Port Options");
33 
34  //! Parameter \relates jevois::Serial
35  JEVOIS_DECLARE_PARAMETER(devname, std::string, "Device file name",
36  "", ParamCateg);
37 
38  //! Parameter \relates jevois::Serial
39  JEVOIS_DECLARE_PARAMETER(baudrate, unsigned int, "Baudrate",
40  115200, { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200,
41  230400, 460800, 921600, 1000000, 1152000, 1500000, 2000000,
42  2500000, 3000000, 3500000, 4000000 }, ParamCateg);
43 
44  //! Parameter \relates jevois::Serial
45  JEVOIS_DECLARE_PARAMETER(format, std::string, "Data format",
46  "8N1", boost::regex("^[5-8][NEO][12]$"), ParamCateg);
47 
48  //! Parameter \relates jevois::Serial
49  JEVOIS_DECLARE_PARAMETER(flowsoft, bool, "Use soft (XON/XOFF) flow control",
50  false, ParamCateg);
51 
52  //! Parameter \relates jevois::Serial
53  JEVOIS_DECLARE_PARAMETER(flowhard, bool, "Use hard (RTS/CTS) flow control",
54  false, ParamCateg);
55 
56  //! Parameter \relates jevois::Serial
57  JEVOIS_DECLARE_PARAMETER(drop, bool, "Silently drop write data when write buffer is full. Useful to "
58  "avoid blocking when writing messages to serial-over-USB port and the host is "
59  "not listening to it. Note that even when drop is false, we will still drop "
60  "data after repeated attempts to send it, and will report an error (as opposed to "
61  "silently dropping when drop is true).",
62  true, ParamCateg);
63 
64  //! Enum for Parameter \relates jevois::Serial
65  JEVOIS_DEFINE_ENUM_CLASS(LineStyle, (LF) (CR) (CRLF) (Zero) (Sloppy) );
66 
67  //! Parameter \relates jevois::Serial
68  JEVOIS_DECLARE_PARAMETER(linestyle, LineStyle, "End of line style: LF is for 0x0a [\\n]; CR is for 0x0d [\\r]; "
69  "CRLF is for 0x0d 0x0a [\\r\\n]; Zero is for 0x00 [\\0]; Sloppy accepts any of "
70  "CR, LF, CRLF, 0xd0 (issued by some keyboards instead of Return), and Zero as input "
71  "and issues CRLF in outputs.",
72  LineStyle::Sloppy, LineStyle_Values, ParamCateg);
73 
74  //! Enum for Parameter \relates jevois::Serial
75  JEVOIS_DEFINE_ENUM_CLASS(TerminalMode, (Plain) (VT100) );
76 
77  //! Parameter \relates jevois::Serial
78  JEVOIS_DECLARE_PARAMETER(mode, TerminalMode, "Terminal emulation mode for input",
79  TerminalMode::Plain, TerminalMode_Values, ParamCateg);
80  } // namespace serial
81 
82  //! Interface to a serial port
83  /*! This class is thread-safe. Concurrent read and write (which do not seem to be supported by the O.S. or hardware)
84  are serialized through the use of a mutex in the Serial class. \ingroup core */
85  class Serial : public UserInterface,
86  public Parameter<serial::devname, serial::baudrate, serial::format, serial::flowsoft,
87  serial::flowhard, serial::drop, serial::linestyle, serial::mode>
88  {
89  public:
90  //! Constructor
91  Serial(std::string const & instance, UserInterface::Type type);
92 
93  //! destructor
94  virtual ~Serial();
95 
96  //! Set the access to blocking or not
97  /*! Default is non-blocking. If blocking, can also specify a timeout (rounded to tenth of seconds) */
98  void setBlocking(bool blocking, std::chrono::milliseconds const & timeout);
99 
100  //! Set the DTR mode off momentarily.
101  void toggleDTR(std::chrono::milliseconds const & dur);
102 
103  //! transmit continuous stream of zero-valued bits for specific duration.
104  void sendBreak(void);
105 
106  //! Read some bytes if available, and return true and a string when one is complete
107  /*! This would usually only make sense to use in non-blocking mode. */
108  bool readSome(std::string & str) override;
109 
110  //! Write a string, using the line termination convention of serial::linestyle
111  /*! No line terminator should be included in the string, writeString() will add one. */
112  void writeString(std::string const & str) override;
113 
114  //! Send a file from the local microSD to the host computer
115  /*! abspath should be the full absolute path of the file. The port will be locked during the entire
116  transaction. */
117  void fileGet(std::string const & abspath);
118 
119  //! Receive a file from the host and write it to the local microSD
120  /*! abspath should be the full absolute path of the file. The port will be locked during the entire
121  transaction. */
122  void filePut(std::string const & abspath);
123 
124  //! Flush all inputs
125  void flush(void);
126 
127  //! Return our port type, here Hard or USB
128  UserInterface::Type type() const override;
129 
130  protected:
131  void postInit() override;
132  void postUninit() override;
133 
134  private:
135  void tryReconnect();
136  void openPort(); // must be locked
137  void writeInternal(void const * buffer, const int nbytes, bool nodrop = false);
138  int itsDev; // descriptor associated with the device file
139  termios itsSavedState; // saved state to restore in the destructor
140  std::string itsPartialString;
141  std::mutex itsMtx;
142  int itsWriteOverflowCounter; // counter so we do not send too many write overflow errors
144  std::atomic<int> itsErrno;
145  std::future<void> itsOpenFut;
146  };
147 } // namespace jevois
jevois::Serial::setBlocking
void setBlocking(bool blocking, std::chrono::milliseconds const &timeout)
Set the access to blocking or not.
Definition: Serial.C:213
jevois::Serial
Interface to a serial port.
Definition: Serial.H:85
jevois::Serial::sendBreak
void sendBreak(void)
transmit continuous stream of zero-valued bits for specific duration.
Definition: Serial.C:253
jevois::imu::mode
Magnetometer sampling or Off to disable or Once to only get one measurement You can repeatedly set this parameter to Once to obtain repeated measurements at your own pace In JeVois you need to alternate between Off and Once In FIFO mode
Definition: ICM20948.H:115
JEVOIS_DECLARE_PARAMETER
JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg)
jevois::Serial::Serial
Serial(std::string const &instance, UserInterface::Type type)
Constructor.
Definition: Serial.C:61
jevois::Serial::readSome
bool readSome(std::string &str) override
Read some bytes if available, and return true and a string when one is complete.
Definition: Serial.C:262
jevois::UserInterface
Abstract base class for a string-based user interface.
Definition: UserInterface.H:32
jevois::Serial::postInit
void postInit() override
Called after all sub-Components are init()ed.
Definition: Serial.C:66
UserInterface.H
jevois::ParameterCategory
A category to which multiple ParameterDef definitions can belong.
Definition: ParameterDef.H:33
jevois::Serial::filePut
void filePut(std::string const &abspath)
Receive a file from the host and write it to the local microSD.
Definition: Serial.C:443
jevois::Serial::toggleDTR
void toggleDTR(std::chrono::milliseconds const &dur)
Set the DTR mode off momentarily.
Definition: Serial.C:234
jevois
Definition: Concepts.dox:1
jevois::Serial::writeString
void writeString(std::string const &str) override
Write a string, using the line termination convention of serial::linestyle.
Definition: Serial.C:316
jevois::Serial::flush
void flush(void)
Flush all inputs.
Definition: Serial.C:401
jevois::Serial::~Serial
virtual ~Serial()
destructor
Definition: Serial.C:411
jevois::UserInterface::Type
Type
Enum for the interface type.
Definition: UserInterface.H:59
jevois::Serial::postUninit
void postUninit() override
Called after all sub-Components are uninit()ed.
Definition: Serial.C:200
jevois::JEVOIS_DEFINE_ENUM_CLASS
JEVOIS_DEFINE_ENUM_CLASS(CameraSensor,(any)(imx290)(os08a10)(ar0234))
Enum for different sensor models.
jevois::Serial::type
UserInterface::Type type() const override
Return our port type, here Hard or USB.
Definition: Serial.C:415
jevois::Serial::fileGet
void fileGet(std::string const &abspath)
Send a file from the local microSD to the host computer.
Definition: Serial.C:419
Enum.H