check(!INVALID_HANDLE_VALUE.equals(hComm), "CreateFile " + COM);
String send = "Hello World";
int tlen = send.getBytes().length;
int[] txn = { 0 };
Memory txm = new Memory(tlen + 1);
txm.clear();
txm.write(0, send.getBytes(), 0, tlen);
int[] rxn = { 0 };
Memory rxm = new Memory(tlen);
OVERLAPPED osReader = new OVERLAPPED();
osReader.writeField("hEvent", CreateEventA(null, true, false, null));
check(osReader.hEvent != null, "CreateEvent/osReader");
OVERLAPPED osWriter = new OVERLAPPED();
osWriter.writeField("hEvent", CreateEventA(null, true, false, null));
check(osWriter.hEvent != null, "CreateEvent/osWriter");
boolean first = true;
// First time through here send some stuff
first = false;
check(ResetEvent(osWriter.hEvent), "ResetEvent/osWriter.hEvent");
boolean write = WriteFile(hComm, txm, tlen, txn, osWriter);
if (!write) {
check(GetLastError() == ERROR_IO_PENDING, "WriteFile");
System.out.println("Write pending");
}
while (!write) {
System.out.println("WaitForSingleObject/write");
int dwRes = WaitForSingleObject(osWriter.hEvent, 1000);
switch (dwRes) {
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hComm, osWriter, txn, true))
check(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult/osWriter");
else
write = true;
break;
case WAIT_TIMEOUT:
System.out.println("write TIMEOT");
break;
default:
check(false, "WaitForSingleObject/write");
break;
}
}
System.out.println("Transmit: '" + txm.getString(0) + "' , len=" + txn[0]);
// First set up the read so that we actually get some overlap
check(ResetEvent(osReader.hEvent), "ResetEvent/osReader.hEvent ");
boolean read = ReadFile(hComm, rxm, tlen, rxn, osReader);
if (!read) {
check(GetLastError() == ERROR_IO_PENDING, "ReadFile");
System.out.println("Read pending");
}
while (!read) {
if (first) {
}
System.out.println("WaitForSingleObject/read");
check(ResetEvent(osReader.hEvent), "ResetEvent/osReader.hEvent");
int dwRes = WaitForSingleObject(osReader.hEvent, 1000);
switch (dwRes) {
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hComm, osReader, rxn, false))
check(GetLastError() == ERROR_IO_INCOMPLETE, "GetOverlappedResult/osReader");
else
read = true;
break;
case WAIT_TIMEOUT:
System.out.println("WAIT_TIMEOUT");
break;
default:
check(false, "WaitForSingleObject/osReader.hEvent");
break;
}
}
System.out.println("Received: '" + rxm.getString(0) + "' , len=" + rxn[0]);
check(CloseHandle(osWriter.hEvent), "CloseHandle/osWriter.hEvent");
check(CloseHandle(osReader.hEvent), "CloseHandle/osReader.hEvent");
check(CloseHandle(hComm), "CloseHandle/hComm");
System.out.println("All done");
}