Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 }