| 175 | } |
| 176 | |
| 177 | int waitForEvent(void *portHandle) |
| 178 | { |
| 179 | // Create an asynchronous event structure |
| 180 | OVERLAPPED overlappedStruct; |
| 181 | memset(&overlappedStruct, 0, sizeof(OVERLAPPED)); |
| 182 | overlappedStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 183 | int event = com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_TIMED_OUT; |
| 184 | if (!overlappedStruct.hEvent) |
| 185 | { |
| 186 | printf("Error Line = %d, Code = %d\n", __LINE__ - 3, GetLastError()); |
| 187 | return event; |
| 188 | } |
| 189 | |
| 190 | // Wait for a serial port event |
| 191 | DWORD eventMask = 0, errorMask = 0, waitValue, numBytesTransferred; |
| 192 | if (!WaitCommEvent(portHandle, &eventMask, &overlappedStruct)) |
| 193 | { |
| 194 | if ((GetLastError() == ERROR_IO_PENDING) || (GetLastError() == ERROR_INVALID_PARAMETER)) |
| 195 | { |
| 196 | do { waitValue = WaitForSingleObject(overlappedStruct.hEvent, 500); } |
| 197 | while (waitValue == WAIT_TIMEOUT); |
| 198 | if ((waitValue != WAIT_OBJECT_0) || !GetOverlappedResult(portHandle, &overlappedStruct, &numBytesTransferred, FALSE)) |
| 199 | { |
| 200 | event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_PORT_DISCONNECTED; |
| 201 | printf("Error Line = %d, Code = %d, Wait Value = %d\n", __LINE__ - 3, GetLastError(), waitValue); |
| 202 | CloseHandle(overlappedStruct.hEvent); |
| 203 | return event; |
| 204 | } |
| 205 | } |
| 206 | else // Problem occurred |
| 207 | { |
| 208 | event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_PORT_DISCONNECTED; |
| 209 | printf("Error Line = %d, Code = %d\n", __LINE__ - 17, GetLastError()); |
| 210 | CloseHandle(overlappedStruct.hEvent); |
| 211 | return event; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Retrieve and clear any serial port errors |
| 216 | COMSTAT commInfo; |
| 217 | if (ClearCommError(portHandle, &errorMask, &commInfo)) |
| 218 | { |
| 219 | if (errorMask & CE_BREAK) |
| 220 | event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_BREAK_INTERRUPT; |
| 221 | if (errorMask & CE_FRAME) |
| 222 | event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_FRAMING_ERROR; |
| 223 | if (errorMask & CE_OVERRUN) |
| 224 | event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_FIRMWARE_OVERRUN_ERROR; |
| 225 | if (errorMask & CE_RXOVER) |
| 226 | event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_SOFTWARE_OVERRUN_ERROR; |
| 227 | if (errorMask & CE_RXPARITY) |
| 228 | event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_PARITY_ERROR; |
| 229 | } |
| 230 | |
| 231 | // Parse any received serial port events |
| 232 | if (eventMask & EV_BREAK) |
| 233 | event |= com_fazecast_jSerialComm_SerialPort_LISTENING_EVENT_BREAK_INTERRUPT; |
| 234 | if (eventMask & EV_TXEMPTY) |