00001 /* 00002 Copyright (c) 2009, The Barbarian Group 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that 00006 the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright notice, this list of conditions and 00009 the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 00011 the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00014 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00015 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 00016 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00017 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00018 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00019 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00020 POSSIBILITY OF SUCH DAMAGE. 00021 */ 00022 00023 00024 #include "cinder/Cinder.h" 00025 #include "cinder/Exception.h" 00026 00027 #include <string> 00028 #include <vector> 00029 00030 #if defined( CINDER_MAC ) 00031 #include <termios.h> 00032 #elif defined( CINDER_MSW ) 00033 #include <windows.h> 00034 #undef min 00035 #undef max 00036 #endif 00037 00038 namespace cinder { 00039 00040 class Serial { 00041 public: 00042 class Device { 00043 public: 00044 Device() {} 00045 Device( const std::string &nameAndPath ) : mName( nameAndPath ), mPath( nameAndPath ) {} 00046 Device( const std::string &name, const std::string &path ) : mName( name ), mPath( path ) {} 00047 00048 const std::string& getName() const { return mName; } 00049 const std::string& getPath() const { return mPath; } 00050 00051 private: 00052 std::string mName; 00053 std::string mPath; 00054 }; 00055 00057 static const std::vector<Serial::Device>& getDevices( bool forceRefresh = false ); 00059 static Serial::Device findDeviceByName( const std::string &name, bool forceRefresh = false ); 00061 static Serial::Device findDeviceByNameContains( const std::string &searchString, bool forceRefresh = false ); 00062 00063 00064 Serial() {} 00065 Serial( const Serial::Device &device, int baudRate ); 00066 00068 const Device& getDevice() const; 00069 00071 void readBytes( void *data, size_t numBytes ); 00073 size_t readAvailableBytes( void *data, size_t maximumBytes ); 00075 void writeBytes( const void *data, size_t numBytes ); 00077 void writeByte( uint8_t data ); 00079 uint8_t readByte(); 00081 char readChar() { return static_cast<char>( readByte() ); } 00082 00084 std::string readStringUntil( char token, size_t maxLength = 0, double timeoutSeconds = -1.0 ); 00086 void writeString( const std::string &str ); 00087 00089 void flush( bool input = true, bool output = true ); 00091 size_t getNumBytesAvailable() const; 00092 00093 protected: 00094 struct Obj { 00095 Obj(); 00096 Obj( const Serial::Device &device, int baudRate ); 00097 ~Obj(); 00098 00099 Device mDevice; 00100 00101 #ifdef CINDER_MSW 00102 ::HANDLE mDeviceHandle; 00103 ::COMMTIMEOUTS mSavedTimeouts; 00104 #else 00105 int mFd; 00106 ::termios mSavedOptions; 00107 #endif 00108 }; 00109 00110 std::shared_ptr<Obj> mObj; 00111 00112 private: 00113 00114 static bool sDevicesInited; 00115 static std::vector<Serial::Device> sDevices; 00116 }; 00117 00118 class SerialExc : public Exception { 00119 }; 00120 00121 class SerialExcOpenFailed : public SerialExc { 00122 }; 00123 00124 class SerialExcDeviceEnumerationFailed : public SerialExc { 00125 }; 00126 00127 class SerialExcReadFailure : public SerialExc { 00128 }; 00129 00130 class SerialExcWriteFailure : public SerialExc { 00131 }; 00132 00133 class SerialTimeoutExc : public SerialExc { 00134 }; 00135 00136 } // namespace cinder