----------------------------------------------------------------------------- Initialize the serial port -----------------------------------------------------------------------------
| 181 | // Initialize the serial port |
| 182 | //----------------------------------------------------------------------------- |
| 183 | bool SerialControllerImpl::Init |
| 184 | ( |
| 185 | uint32 const _attempts |
| 186 | ) |
| 187 | { |
| 188 | |
| 189 | string device = m_owner->m_serialControllerName; |
| 190 | |
| 191 | Log::Write( LogLevel_Info, "Trying to open serial port %s (attempt %d)", device.c_str(), _attempts ); |
| 192 | |
| 193 | #ifdef __NetBSD__ |
| 194 | m_hSerialController = open( device.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 195 | #else |
| 196 | m_hSerialController = open( device.c_str(), O_RDWR | O_NOCTTY, 0 ); |
| 197 | #endif |
| 198 | |
| 199 | if( -1 == m_hSerialController ) |
| 200 | { |
| 201 | //Error |
| 202 | Log::Write( LogLevel_Error, "ERROR: Cannot open serial port %s. Error code %d", device.c_str(), errno ); |
| 203 | goto SerialOpenFailure; |
| 204 | } |
| 205 | |
| 206 | if( flock( m_hSerialController, LOCK_EX | LOCK_NB) == -1 ) |
| 207 | { |
| 208 | Log::Write( LogLevel_Error, "ERROR: Cannot get exclusive lock for serial port %s. Error code %d", device.c_str(), errno ); |
| 209 | } |
| 210 | |
| 211 | int bits; |
| 212 | bits = 0; |
| 213 | ioctl( m_hSerialController, TIOCMSET, &bits ); |
| 214 | |
| 215 | // Configure the serial device parameters |
| 216 | // Build on the current configuration |
| 217 | struct termios tios; |
| 218 | |
| 219 | bzero( &tios, sizeof(tios) ); |
| 220 | tcgetattr( m_hSerialController, &tios ); |
| 221 | switch( m_owner->m_parity ) |
| 222 | { |
| 223 | case SerialController::Parity_None: |
| 224 | tios.c_iflag = IGNPAR; |
| 225 | break; |
| 226 | case SerialController::Parity_Odd: |
| 227 | tios.c_iflag = INPCK; |
| 228 | tios.c_cflag = PARENB | PARODD; |
| 229 | break; |
| 230 | default: |
| 231 | Log::Write( LogLevel_Error, "ERROR: Parity not supported" ); |
| 232 | goto SerialOpenFailure; |
| 233 | } |
| 234 | switch( m_owner->m_stopBits ) |
| 235 | { |
| 236 | case SerialController::StopBits_One: |
| 237 | break; // default |
| 238 | case SerialController::StopBits_Two: |
| 239 | tios.c_cflag |= CSTOPB; |
| 240 | break; |
nothing calls this directly
no test coverage detected