| 73 | } |
| 74 | |
| 75 | Serial::Impl::Impl( const Serial::Device &device, int baudRate ) |
| 76 | { |
| 77 | #if defined( CINDER_MAC ) || defined( CINDER_LINUX ) |
| 78 | mFd = open( ( "/dev/" + device.getName() ).c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK ); |
| 79 | if( mFd == -1 ) { |
| 80 | throw SerialExcOpenFailed(); |
| 81 | } |
| 82 | |
| 83 | termios options; |
| 84 | tcgetattr( mFd, &mSavedOptions ); |
| 85 | options = mSavedOptions; |
| 86 | map<int,int> baudToConstant; |
| 87 | baudToConstant[300] = B300; |
| 88 | baudToConstant[1200] = B1200; |
| 89 | baudToConstant[2400] = B2400; |
| 90 | baudToConstant[4800] = B4800; |
| 91 | baudToConstant[9600] = B9600; |
| 92 | baudToConstant[19200] = B19200; |
| 93 | #if !defined( CINDER_LINUX ) |
| 94 | baudToConstant[28800] = B28800; |
| 95 | #endif |
| 96 | baudToConstant[38400] = B38400; |
| 97 | baudToConstant[57600] = B57600; |
| 98 | baudToConstant[115200] = B115200; |
| 99 | baudToConstant[230400] = B230400; |
| 100 | |
| 101 | int rateConstant = B9600; |
| 102 | if( baudToConstant.find( baudRate ) != baudToConstant.end() ) |
| 103 | rateConstant = baudToConstant[baudRate]; |
| 104 | |
| 105 | ::cfsetispeed( &options, rateConstant ); |
| 106 | ::cfsetospeed( &options, rateConstant ); |
| 107 | |
| 108 | options.c_cflag |= (CLOCAL | CREAD); |
| 109 | options.c_cflag &= ~PARENB; |
| 110 | options.c_cflag &= ~CSTOPB; |
| 111 | options.c_cflag &= ~CSIZE; |
| 112 | options.c_cflag |= CS8; |
| 113 | ::tcsetattr( mFd, TCSANOW, &options ); |
| 114 | #elif defined( CINDER_MSW ) |
| 115 | mDeviceHandle = ::CreateFileA( device.getPath().c_str(), GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 ); |
| 116 | if( mDeviceHandle == INVALID_HANDLE_VALUE ) { |
| 117 | throw SerialExcOpenFailed(); |
| 118 | } |
| 119 | |
| 120 | ::COMMCONFIG config; |
| 121 | ::DWORD configSize = sizeof( ::COMMCONFIG ); |
| 122 | ::GetCommConfig( mDeviceHandle, &config, &configSize ); |
| 123 | |
| 124 | string settingsStr = "baud=" + toString( baudRate ) + " parity=N data=8 stop=1"; |
| 125 | if( ! ::BuildCommDCBA( settingsStr.c_str(), &config.dcb ) ) { |
| 126 | throw SerialExcOpenFailed(); |
| 127 | } |
| 128 | |
| 129 | if( ! ::SetCommState( mDeviceHandle, &config.dcb ) ) { |
| 130 | throw SerialExcOpenFailed(); |
| 131 | } |
| 132 | |