This WinAPI class implements a simple wrapper API to access the Windows COM ports from Java. The purpose is to follow reasonably closely the WIN32 API so that COM port related C-code can be ported to Java almost as-is with little changes when this class is statically imported.
This is a pure lightweight wrapper around WIN32 API calls with no added syntactic sugar, functionality or niceties.
Here is a rude example:
import static jtermios.windows.WinAPI.*; ... byte[] buffer = "Hello World".getBytes(); HANDLE hcomm = CreateFileA( "COM5:", GENERIC_READ |GENERIC_WRITE, 0, null, 0, 0, null ); int[] wrtn = {0}; WriteFile(hcomm, buffer, buffer.length, wrtn); CloseHandle(hcomm);
Can't get much closer to C-code, what!
In addition to the basic open/close/read/write and setup operations this class also makes available enough of the WIN32 Event API to make it possible to use overlapped (asynchronous) I/O on COM ports.
Note that overlapped IO API is full of fine print. Especially worth mentioning is that the OVERLAPPED structure cannot use autosync as it is modified (by Windows) outside the function calls that use it. OVERLAPPED takes care of not autosyncing but it is best to us the writeField() methods to set fields of OVERLAPPED.
OVERLAPPED ovl = new OVERLAPPED(); ovl.writeField("hEvent",CreateEvent(null, true, false, null)); ... WriteFile(hComm, txm, txb.length, txn, ovl); ... GetOverlappedResult(hComm, ovl, txn, true);
@author Kustaa Nyholm