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 #pragma once 00024 00025 #include "cinder/Cinder.h" 00026 #include "cinder/Exception.h" 00027 00028 #include <string> 00029 #include <vector> 00030 00031 #if defined( CINDER_MAC ) 00032 #include <termios.h> 00033 #elif defined( CINDER_MSW ) 00034 #include <windows.h> 00035 #undef min 00036 #undef max 00037 #endif 00038 00039 namespace cinder { 00040 00041 class Serial { 00042 public: 00043 class Device { 00044 public: 00045 Device() {} 00046 Device( const std::string &nameAndPath ) : mName( nameAndPath ), mPath( nameAndPath ) {} 00047 Device( const std::string &name, const std::string &path ) : mName( name ), mPath( path ) {} 00048 00049 const std::string& getName() const { return mName; } 00050 const std::string& getPath() const { return mPath; } 00051 00052 private: 00053 std::string mName; 00054 std::string mPath; 00055 }; 00056 00058 static const std::vector<Serial::Device>& getDevices( bool forceRefresh = false ); 00060 static Serial::Device findDeviceByName( const std::string &name, bool forceRefresh = false ); 00062 static Serial::Device findDeviceByNameContains( const std::string &searchString, bool forceRefresh = false ); 00063 00064 00065 Serial() {} 00066 Serial( const Serial::Device &device, int baudRate ); 00067 00069 const Device& getDevice() const; 00070 00072 void readBytes( void *data, size_t numBytes ); 00074 size_t readAvailableBytes( void *data, size_t maximumBytes ); 00076 void writeBytes( const void *data, size_t numBytes ); 00078 void writeByte( uint8_t data ); 00080 uint8_t readByte(); 00082 char readChar() { return static_cast<char>( readByte() ); } 00083 00085 std::string readStringUntil( char token, size_t maxLength = 0, double timeoutSeconds = -1.0 ); 00087 void writeString( const std::string &str ); 00088 00090 void flush( bool input = true, bool output = true ); 00092 size_t getNumBytesAvailable() const; 00093 00094 protected: 00095 struct Obj { 00096 Obj(); 00097 Obj( const Serial::Device &device, int baudRate ); 00098 ~Obj(); 00099 00100 Device mDevice; 00101 00102 #ifdef CINDER_MSW 00103 ::HANDLE mDeviceHandle; 00104 ::COMMTIMEOUTS mSavedTimeouts; 00105 #else 00106 int mFd; 00107 ::termios mSavedOptions; 00108 #endif 00109 }; 00110 00111 std::shared_ptr<Obj> mObj; 00112 00113 private: 00114 00115 static bool sDevicesInited; 00116 static std::vector<Serial::Device> sDevices; 00117 }; 00118 00119 class SerialExc : public Exception { 00120 }; 00121 00122 class SerialExcOpenFailed : public SerialExc { 00123 }; 00124 00125 class SerialExcDeviceEnumerationFailed : public SerialExc { 00126 }; 00127 00128 class SerialExcReadFailure : public SerialExc { 00129 }; 00130 00131 class SerialExcWriteFailure : public SerialExc { 00132 }; 00133 00134 class SerialTimeoutExc : public SerialExc { 00135 }; 00136 00137 } // namespace cinder