JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
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
28namespace 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 bool readSome(std::string & str) override;
108
109 //! Write a string, using the line termination convention of serial::linestyle
110 /*! No line terminator should be included in the string, writeString() will add one. */
111 void writeString(std::string const & str) override;
112
113 //! Send a file from the local microSD to the host computer
114 /*! abspath should be the full absolute path of the file. The port will be locked during the entire
115 transaction. */
116 void fileGet(std::string const & abspath);
117
118 //! Receive a file from the host and write it to the local microSD
119 /*! abspath should be the full absolute path of the file. The port will be locked during the entire
120 transaction. */
121 void filePut(std::string const & abspath);
122
123 //! Flush all inputs
124 void flush(void);
125
126 //! Return our port type, here Hard or USB
127 UserInterface::Type type() const override;
128
129 protected:
130 void postInit() override;
131 void postUninit() override;
132
133 private:
134 void tryReconnect();
135 void openPort(); // must be locked
136 void writeInternal(void const * buffer, const int nbytes, bool nodrop = false);
137 int itsDev; // descriptor associated with the device file
138 termios itsSavedState; // saved state to restore in the destructor
139 std::string itsPartialString;
140 std::mutex itsMtx;
141 int itsWriteOverflowCounter; // counter so we do not send too many write overflow errors
143 std::atomic<int> itsErrno;
144 std::future<void> itsOpenFut;
145 };
146} // namespace jevois
Interface to a serial port.
Definition Serial.H:88
void writeString(std::string const &str) override
Write a string, using the line termination convention of serial::linestyle.
Definition Serial.C:316
JEVOIS_DECLARE_PARAMETER(format, std::string, "Data format", "8N1", boost::regex("^[5-8][NEO][12]$"), ParamCateg)
Parameter.
void postInit() override
Called after all sub-Components are init()ed.
Definition Serial.C:66
void setBlocking(bool blocking, std::chrono::milliseconds const &timeout)
Set the access to blocking or not.
Definition Serial.C:213
JEVOIS_DECLARE_PARAMETER(mode, TerminalMode, "Terminal emulation mode for input", TerminalMode::Plain, TerminalMode_Values, ParamCateg)
Parameter.
void flush(void)
Flush all inputs.
Definition Serial.C:401
JEVOIS_DECLARE_PARAMETER(linestyle, LineStyle, "End of line style: LF is for 0x0a [\\n] CR is for 0x0d [\\r] " "CRLF is for 0x0d 0x0a [\\r\\n] Zero is for 0x00 [\\0] Sloppy accepts any of " "CR, LF, CRLF, 0xd0 (issued by some keyboards instead of Return), and Zero as input " "and issues CRLF in outputs.", LineStyle::Sloppy, LineStyle_Values, ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(flowsoft, bool, "Use soft (XON/XOFF) flow control", false, ParamCateg)
Parameter.
void sendBreak(void)
transmit continuous stream of zero-valued bits for specific duration.
Definition Serial.C:253
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
void toggleDTR(std::chrono::milliseconds const &dur)
Set the DTR mode off momentarily.
Definition Serial.C:234
void filePut(std::string const &abspath)
Receive a file from the host and write it to the local microSD.
Definition Serial.C:443
JEVOIS_DEFINE_ENUM_CLASS(TerminalMode,(Plain)(VT100))
Enum for Parameter.
void fileGet(std::string const &abspath)
Send a file from the local microSD to the host computer.
Definition Serial.C:419
JEVOIS_DECLARE_PARAMETER(baudrate, unsigned int, "Baudrate", 115200, { 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000, 4000000 }, ParamCateg)
Parameter.
virtual ~Serial()
destructor
Definition Serial.C:411
JEVOIS_DECLARE_PARAMETER(devname, std::string, "Device file name", "", ParamCateg)
Parameter.
JEVOIS_DECLARE_PARAMETER(drop, bool, "Silently drop write data when write buffer is full. Useful to " "avoid blocking when writing messages to serial-over-USB port and the host is " "not listening to it. Note that even when drop is false, we will still drop " "data after repeated attempts to send it, and will report an error (as opposed to " "silently dropping when drop is true).", true, ParamCateg)
Parameter.
JEVOIS_DEFINE_ENUM_CLASS(LineStyle,(LF)(CR)(CRLF)(Zero)(Sloppy))
Enum for Parameter.
UserInterface::Type type() const override
Return our port type, here Hard or USB.
Definition Serial.C:415
JEVOIS_DECLARE_PARAMETER(flowhard, bool, "Use hard (RTS/CTS) flow control", false, ParamCateg)
Parameter.
void postUninit() override
Called after all sub-Components are uninit()ed.
Definition Serial.C:200
Abstract base class for a string-based user interface.
Type
Enum for the interface type.
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2