----------------------------------------------------------------------------- Initialize the serial port -----------------------------------------------------------------------------
| 173 | // Initialize the serial port |
| 174 | //----------------------------------------------------------------------------- |
| 175 | bool SerialControllerImpl::Init |
| 176 | ( |
| 177 | uint32 const _attempts |
| 178 | ) |
| 179 | { |
| 180 | Log::Write( LogLevel_Info, " Trying to open serial port %s (Attempt %d)", m_owner->m_serialControllerName.c_str(), _attempts ); |
| 181 | |
| 182 | m_hSerialController = CreateFileA( m_owner->m_serialControllerName.c_str(), |
| 183 | GENERIC_READ | GENERIC_WRITE, |
| 184 | 0, |
| 185 | NULL, |
| 186 | OPEN_EXISTING, |
| 187 | FILE_FLAG_OVERLAPPED, |
| 188 | NULL ); |
| 189 | |
| 190 | if( INVALID_HANDLE_VALUE == m_hSerialController ) |
| 191 | { |
| 192 | //Error |
| 193 | Log::Write( LogLevel_Error, "ERROR: Cannot open serial port %s. Error code %d\n", m_owner->m_serialControllerName.c_str(), GetLastError() ); |
| 194 | goto SerialOpenFailure; |
| 195 | } |
| 196 | |
| 197 | // Configure the serial device parameters |
| 198 | // Build on the current configuration |
| 199 | DCB dcb; |
| 200 | if( !GetCommState( m_hSerialController, &dcb ) ) |
| 201 | { |
| 202 | //Error. Clean up and exit |
| 203 | Log::Write( LogLevel_Error, "ERROR: Failed to read serial port state" ); |
| 204 | goto SerialOpenFailure; |
| 205 | } |
| 206 | |
| 207 | // Fill in the Device Control Block |
| 208 | dcb.BaudRate = (DWORD)m_owner->m_baud; |
| 209 | dcb.ByteSize = 8; |
| 210 | dcb.Parity = (BYTE)m_owner->m_parity; |
| 211 | dcb.StopBits = (BYTE)m_owner->m_stopBits; |
| 212 | |
| 213 | if( !SetCommState( m_hSerialController, &dcb) ) |
| 214 | { |
| 215 | //Error. Clean up and exit |
| 216 | Log::Write( LogLevel_Error, "ERROR: Failed to set serial port state" ); |
| 217 | goto SerialOpenFailure; |
| 218 | } |
| 219 | |
| 220 | // Set the timeouts for the serial port |
| 221 | COMMTIMEOUTS commTimeouts; |
| 222 | commTimeouts.ReadIntervalTimeout = MAXDWORD; |
| 223 | commTimeouts.ReadTotalTimeoutConstant = 0; |
| 224 | commTimeouts.ReadTotalTimeoutMultiplier = 0; |
| 225 | commTimeouts.WriteTotalTimeoutConstant = 0; |
| 226 | commTimeouts.WriteTotalTimeoutMultiplier = 0; |
| 227 | if( !SetCommTimeouts( m_hSerialController, &commTimeouts ) ) |
| 228 | { |
| 229 | // Error. Clean up and exit |
| 230 | Log::Write( LogLevel_Error, "ERROR: Failed to set serial port timeouts" ); |
| 231 | goto SerialOpenFailure; |
| 232 | } |