Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Serial.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2009, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Exception.h"
27 
28 #include <string>
29 #include <vector>
30 
31 #if defined( CINDER_MAC )
32  #include <termios.h>
33 #elif defined( CINDER_MSW )
34  #include <windows.h>
35  #undef min
36  #undef max
37 #endif
38 
39 namespace cinder {
40 
41 class Serial {
42  public:
43  class Device {
44  public:
45  Device() {}
46  Device( const std::string &nameAndPath ) : mName( nameAndPath ), mPath( nameAndPath ) {}
47  Device( const std::string &name, const std::string &path ) : mName( name ), mPath( path ) {}
48 
49  const std::string& getName() const { return mName; }
50  const std::string& getPath() const { return mPath; }
51 
52  private:
53  std::string mName;
54  std::string mPath;
55  };
56 
58  static const std::vector<Serial::Device>& getDevices( bool forceRefresh = false );
60  static Serial::Device findDeviceByName( const std::string &name, bool forceRefresh = false );
62  static Serial::Device findDeviceByNameContains( const std::string &searchString, bool forceRefresh = false );
63 
64 
65  Serial() {}
66  Serial( const Serial::Device &device, int baudRate );
67 
69  const Device& getDevice() const;
70 
72  void readBytes( void *data, size_t numBytes );
74  size_t readAvailableBytes( void *data, size_t maximumBytes );
76  void writeBytes( const void *data, size_t numBytes );
78  void writeByte( uint8_t data );
80  uint8_t readByte();
82  char readChar() { return static_cast<char>( readByte() ); }
83 
85  std::string readStringUntil( char token, size_t maxLength = 0, double timeoutSeconds = -1.0 );
87  void writeString( const std::string &str );
88 
90  void flush( bool input = true, bool output = true );
92  size_t getNumBytesAvailable() const;
93 
94  protected:
95  struct Obj {
96  Obj();
97  Obj( const Serial::Device &device, int baudRate );
98  ~Obj();
99 
101 
102 #ifdef CINDER_MSW
103  ::HANDLE mDeviceHandle;
104  ::COMMTIMEOUTS mSavedTimeouts;
105 #else
106  int mFd;
107  ::termios mSavedOptions;
108 #endif
109  };
110 
111  std::shared_ptr<Obj> mObj;
112 
113  private:
114 
115  static bool sDevicesInited;
116  static std::vector<Serial::Device> sDevices;
117 };
118 
119 class SerialExc : public Exception {
120 };
121 
123 };
124 
126 };
127 
129 };
130 
132 };
133 
134 class SerialTimeoutExc : public SerialExc {
135 };
136 
137 } // namespace cinder
GLenum GLenum GLenum input
Definition: GLee.h:8931
void writeByte(uint8_t data)
Writes a single byte data to the serial port.
Definition: Serial.cpp:286
static const std::vector< Serial::Device > & getDevices(bool forceRefresh=false)
Returns a vector of all serial devices available on the machine. Uses a cached list unless forceRefre...
Definition: Serial.cpp:145
GLsizei const GLchar ** string
Definition: GLee.h:2427
Definition: Serial.h:134
uint8_t readByte()
Returns a single byte read from the serial port.
Definition: Serial.cpp:291
void writeBytes(const void *data, size_t numBytes)
Writes numBytes bytes of data to the serial port from data.
Definition: Serial.cpp:230
int mFd
Definition: Serial.h:106
Device mDevice
Definition: Serial.h:100
void readBytes(void *data, size_t numBytes)
Reads numBytes bytes of data from the serial port to data.
Definition: Serial.cpp:249
std::shared_ptr< Obj > mObj
Definition: Serial.h:111
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: GLee.h:1011
::termios mSavedOptions
Definition: Serial.h:107
Device()
Definition: Serial.h:45
void flush(bool input=true, bool output=true)
Forces the device to flush any buffered input and/or output bytes.
Definition: Serial.cpp:361
Device(const std::string &name, const std::string &path)
Definition: Serial.h:47
size_t getNumBytesAvailable() const
Returns the number of bytes available for reading from the device.
Definition: Serial.cpp:343
const std::string & getPath() const
Definition: Serial.h:50
Definition: Serial.h:122
~Obj()
Definition: Serial.cpp:213
Definition: Serial.h:43
Definition: Serial.h:128
Device(const std::string &nameAndPath)
Definition: Serial.h:46
static Serial::Device findDeviceByName(const std::string &name, bool forceRefresh=false)
Returns the first Serial::Device whose name is name. Returns a null Serial::Device if none are found...
Definition: Serial.cpp:123
Serial()
Definition: Serial.h:65
void writeString(const std::string &str)
Writes a string str to the serial port, excluding the null terminator.
Definition: Serial.cpp:337
static Serial::Device findDeviceByNameContains(const std::string &searchString, bool forceRefresh=false)
Returns the first Serial::Device whose name contains the string searchString. Returns a null Serial::...
Definition: Serial.cpp:134
size_t readAvailableBytes(void *data, size_t maximumBytes)
Reads up to maximumBytes bytes of data from the serial port to data. Returns the number of bytes read...
Definition: Serial.cpp:270
const std::string & getName() const
Definition: Serial.h:49
GLuint const GLchar * name
Definition: GLee.h:2259
Definition: Serial.h:41
Definition: Serial.h:131
Definition: Serial.h:95
std::string readStringUntil(char token, size_t maxLength=0, double timeoutSeconds=-1.0)
Returns a string composed of bytes read until a character token is found, or up to maxLength bytes ha...
Definition: Serial.cpp:298
Definition: Exception.h:32
Definition: Serial.h:119
char readChar()
Returns a single character read from the serial port.
Definition: Serial.h:82
const Device & getDevice() const
Returns the Device associated with this Serial port.
Definition: Serial.cpp:225
GLsizei maxLength
Definition: GLee.h:4974