JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative()

in src/main/c/Windows/SerialPort_Windows.c [1212:1239]


JNIEXPORT jlong JNICALL Java_com_fazecast_jSerialComm_SerialPort_closePortNative(JNIEnv *env, jobject obj, jlong serialPortPointer)
{
	// Force the port to enter non-blocking mode to ensure that any current reads return
	COMMTIMEOUTS timeouts;
	memset(&timeouts, 0, sizeof(COMMTIMEOUTS));
	serialPort *port = (serialPort*)(intptr_t)serialPortPointer;
	timeouts.WriteTotalTimeoutMultiplier = 0;
	timeouts.ReadIntervalTimeout = MAXDWORD;
	timeouts.ReadTotalTimeoutMultiplier = 0;
	timeouts.ReadTotalTimeoutConstant = 0;
	timeouts.WriteTotalTimeoutConstant = 0;
	SetCommTimeouts(port->handle, &timeouts);

	// Purge any outstanding port operations
	PurgeComm(port->handle, PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR);
	if (CancelIoEx)
		CancelIoEx(port->handle, NULL);
	SetCommMask(port->handle, 0);

	// Close the port
	port->eventListenerRunning = 0;
	port->errorLineNumber = lastErrorLineNumber = __LINE__ + 1;
	port->errorNumber = lastErrorNumber = (!CloseHandle(port->handle) ? GetLastError() : 0);
	EnterCriticalSection(&criticalSection);
	port->handle = INVALID_HANDLE_VALUE;
	LeaveCriticalSection(&criticalSection);
	return 0;
}