Package jtermios.windows
Class WinAPI
- java.lang.Object
-
- jtermios.windows.WinAPI
-
public class WinAPI extends Object
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:
Can't get much closer to C-code, what!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);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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classWinAPI.COMMTIMEOUTSstatic classWinAPI.COMSTATstatic classWinAPI.DCBstatic classWinAPI.HANDLEstatic classWinAPI.OVERLAPPEDRepresent the Windows API struct OVERLAPPED.static classWinAPI.SECURITY_ATTRIBUTESstatic classWinAPI.ULONG_PTRstatic interfaceWinAPI.WaitMultiplestatic interfaceWinAPI.Windows_kernel32_libstatic classWinAPI.Windows_kernel32_lib_Direct
-
Field Summary
-
Constructor Summary
Constructors Constructor Description WinAPI()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanCancelIo(WinAPI.HANDLE hFile)static booleanClearCommBreak(WinAPI.HANDLE hFile)static booleanClearCommError(WinAPI.HANDLE hFile, int[] n, WinAPI.COMSTAT s)static booleanCloseHandle(WinAPI.HANDLE hFile)static WinAPI.HANDLECreateEvent(WinAPI.SECURITY_ATTRIBUTES security, boolean manual, boolean initial, String name)static WinAPI.HANDLECreateFile(String name, int access, int sharing, WinAPI.SECURITY_ATTRIBUTES security, int create, int attribs, com.sun.jna.Pointer template)static booleanEscapeCommFunction(WinAPI.HANDLE hFile, int func)static booleanFlushFileBuffers(WinAPI.HANDLE hFile)static intFormatMessageW(int flags, com.sun.jna.Pointer src, int msgId, int langId, com.sun.jna.Pointer dst, int sze, com.sun.jna.Pointer va_list)static booleanGetCommMask(WinAPI.HANDLE hFile, int[] mask)static booleanGetCommModemStatus(WinAPI.HANDLE hFile, int[] stat)static booleanGetCommState(WinAPI.HANDLE hFile, WinAPI.DCB dcb)static intGetLastError()static booleanGetOverlappedResult(WinAPI.HANDLE hFile, WinAPI.OVERLAPPED ovl, int[] ntfrd, boolean wait)static intMAKELANGID(int p, int s)static booleanPurgeComm(WinAPI.HANDLE hFile, int qmask)static intQueryDosDevice(String name, byte[] buffer, int bsize)static booleanReadFile(WinAPI.HANDLE hFile, byte[] buf, int rdn, int[] nrd)static booleanReadFile(WinAPI.HANDLE hFile, com.sun.jna.Pointer buf, int rdn, int[] nrd, WinAPI.OVERLAPPED ovrlp)static booleanResetEvent(WinAPI.HANDLE hEvent)static booleanSetCommBreak(WinAPI.HANDLE hFile)static booleanSetCommMask(WinAPI.HANDLE hFile, int mask)static booleanSetCommState(WinAPI.HANDLE hFile, WinAPI.DCB dcb)static booleanSetCommTimeouts(WinAPI.HANDLE hFile, WinAPI.COMMTIMEOUTS touts)static booleanSetEvent(WinAPI.HANDLE hEvent)static booleanSetupComm(WinAPI.HANDLE hFile, int inQueueSz, int outQueueSz)static booleanWaitCommEvent(WinAPI.HANDLE hFile, int[] lpEvtMask)static booleanWaitCommEvent(WinAPI.HANDLE hFile, com.sun.jna.ptr.IntByReference lpEvtMask, WinAPI.OVERLAPPED ovl)static intWaitForMultipleObjects(int nCount, WinAPI.HANDLE[] lpHandles, boolean bWaitAll, int dwMilliseconds)static intWaitForSingleObject(WinAPI.HANDLE hHandle, int dwMilliseconds)static booleanWriteFile(WinAPI.HANDLE hFile, byte[] buf, int wrn, int[] nwrtn)static booleanWriteFile(WinAPI.HANDLE hFile, com.sun.jna.Pointer buf, int wrn, int[] nwrtn, WinAPI.OVERLAPPED ovrlp)
-
-
-
Field Detail
-
INVALID_HANDLE_VALUE
public static WinAPI.HANDLE INVALID_HANDLE_VALUE
-
NULL
public static WinAPI.HANDLE NULL
-
ERROR_INSUFFICIENT_BUFFER
public static final int ERROR_INSUFFICIENT_BUFFER
- See Also:
- Constant Field Values
-
MAXDWORD
public static final int MAXDWORD
- See Also:
- Constant Field Values
-
STATUS_WAIT_0
public static final int STATUS_WAIT_0
- See Also:
- Constant Field Values
-
STATUS_ABANDONED_WAIT_0
public static final int STATUS_ABANDONED_WAIT_0
- See Also:
- Constant Field Values
-
WAIT_ABANDONED
public static final int WAIT_ABANDONED
- See Also:
- Constant Field Values
-
WAIT_ABANDONED_0
public static final int WAIT_ABANDONED_0
- See Also:
- Constant Field Values
-
WAIT_OBJECT_0
public static final int WAIT_OBJECT_0
- See Also:
- Constant Field Values
-
WAIT_FAILED
public static final int WAIT_FAILED
- See Also:
- Constant Field Values
-
INFINITE
public static final int INFINITE
- See Also:
- Constant Field Values
-
WAIT_TIMEOUT
public static final int WAIT_TIMEOUT
- See Also:
- Constant Field Values
-
GENERIC_READ
public static final int GENERIC_READ
- See Also:
- Constant Field Values
-
GENERIC_WRITE
public static final int GENERIC_WRITE
- See Also:
- Constant Field Values
-
GENERIC_EXECUTE
public static final int GENERIC_EXECUTE
- See Also:
- Constant Field Values
-
GENERIC_ALL
public static final int GENERIC_ALL
- See Also:
- Constant Field Values
-
CREATE_NEW
public static final int CREATE_NEW
- See Also:
- Constant Field Values
-
CREATE_ALWAYS
public static final int CREATE_ALWAYS
- See Also:
- Constant Field Values
-
OPEN_EXISTING
public static final int OPEN_EXISTING
- See Also:
- Constant Field Values
-
OPEN_ALWAYS
public static final int OPEN_ALWAYS
- See Also:
- Constant Field Values
-
TRUNCATE_EXISTING
public static final int TRUNCATE_EXISTING
- See Also:
- Constant Field Values
-
PURGE_TXABORT
public static final int PURGE_TXABORT
- See Also:
- Constant Field Values
-
PURGE_RXABORT
public static final int PURGE_RXABORT
- See Also:
- Constant Field Values
-
PURGE_TXCLEAR
public static final int PURGE_TXCLEAR
- See Also:
- Constant Field Values
-
PURGE_RXCLEAR
public static final int PURGE_RXCLEAR
- See Also:
- Constant Field Values
-
MS_CTS_ON
public static final int MS_CTS_ON
- See Also:
- Constant Field Values
-
MS_DSR_ON
public static final int MS_DSR_ON
- See Also:
- Constant Field Values
-
MS_RING_ON
public static final int MS_RING_ON
- See Also:
- Constant Field Values
-
MS_RLSD_ON
public static final int MS_RLSD_ON
- See Also:
- Constant Field Values
-
SETXOFF
public static final int SETXOFF
- See Also:
- Constant Field Values
-
SETXON
public static final int SETXON
- See Also:
- Constant Field Values
-
SETRTS
public static final int SETRTS
- See Also:
- Constant Field Values
-
CLRRTS
public static final int CLRRTS
- See Also:
- Constant Field Values
-
SETDTR
public static final int SETDTR
- See Also:
- Constant Field Values
-
CLRDTR
public static final int CLRDTR
- See Also:
- Constant Field Values
-
RESETDEV
public static final int RESETDEV
- See Also:
- Constant Field Values
-
SETBREAK
public static final int SETBREAK
- See Also:
- Constant Field Values
-
CLRBREAK
public static final int CLRBREAK
- See Also:
- Constant Field Values
-
FILE_ATTRIBUTE_NORMAL
public static final int FILE_ATTRIBUTE_NORMAL
- See Also:
- Constant Field Values
-
FILE_FLAG_WRITE_THROUGH
public static final int FILE_FLAG_WRITE_THROUGH
- See Also:
- Constant Field Values
-
FILE_FLAG_OVERLAPPED
public static final int FILE_FLAG_OVERLAPPED
- See Also:
- Constant Field Values
-
FILE_FLAG_NO_BUFFERING
public static final int FILE_FLAG_NO_BUFFERING
- See Also:
- Constant Field Values
-
FILE_FLAG_RANDOM_ACCESS
public static final int FILE_FLAG_RANDOM_ACCESS
- See Also:
- Constant Field Values
-
FILE_FLAG_SEQUENTIAL_SCAN
public static final int FILE_FLAG_SEQUENTIAL_SCAN
- See Also:
- Constant Field Values
-
FILE_FLAG_DELETE_ON_CLOSE
public static final int FILE_FLAG_DELETE_ON_CLOSE
- See Also:
- Constant Field Values
-
FILE_FLAG_BACKUP_SEMANTICS
public static final int FILE_FLAG_BACKUP_SEMANTICS
- See Also:
- Constant Field Values
-
FILE_FLAG_POSIX_SEMANTICS
public static final int FILE_FLAG_POSIX_SEMANTICS
- See Also:
- Constant Field Values
-
FILE_FLAG_OPEN_REPARSE_POINT
public static final int FILE_FLAG_OPEN_REPARSE_POINT
- See Also:
- Constant Field Values
-
FILE_FLAG_OPEN_NO_RECALL
public static final int FILE_FLAG_OPEN_NO_RECALL
- See Also:
- Constant Field Values
-
FILE_FLAG_FIRST_PIPE_INSTANCE
public static final int FILE_FLAG_FIRST_PIPE_INSTANCE
- See Also:
- Constant Field Values
-
ERROR_OPERATION_ABORTED
public static final int ERROR_OPERATION_ABORTED
- See Also:
- Constant Field Values
-
ERROR_IO_INCOMPLETE
public static final int ERROR_IO_INCOMPLETE
- See Also:
- Constant Field Values
-
ERROR_IO_PENDING
public static final int ERROR_IO_PENDING
- See Also:
- Constant Field Values
-
ERROR_INVALID_PARAMETER
public static final int ERROR_INVALID_PARAMETER
- See Also:
- Constant Field Values
-
ERROR_BROKEN_PIPE
public static final int ERROR_BROKEN_PIPE
- See Also:
- Constant Field Values
-
ERROR_MORE_DATA
public static final int ERROR_MORE_DATA
- See Also:
- Constant Field Values
-
ERROR_FILE_NOT_FOUND
public static final int ERROR_FILE_NOT_FOUND
- See Also:
- Constant Field Values
-
NOPARITY
public static final byte NOPARITY
- See Also:
- Constant Field Values
-
ODDPARITY
public static final byte ODDPARITY
- See Also:
- Constant Field Values
-
EVENPARITY
public static final byte EVENPARITY
- See Also:
- Constant Field Values
-
MARKPARITY
public static final byte MARKPARITY
- See Also:
- Constant Field Values
-
SPACEPARITY
public static final byte SPACEPARITY
- See Also:
- Constant Field Values
-
ONESTOPBIT
public static final byte ONESTOPBIT
- See Also:
- Constant Field Values
-
ONE5STOPBITS
public static final byte ONE5STOPBITS
- See Also:
- Constant Field Values
-
TWOSTOPBITS
public static final byte TWOSTOPBITS
- See Also:
- Constant Field Values
-
CBR_110
public static final int CBR_110
- See Also:
- Constant Field Values
-
CBR_300
public static final int CBR_300
- See Also:
- Constant Field Values
-
CBR_600
public static final int CBR_600
- See Also:
- Constant Field Values
-
CBR_1200
public static final int CBR_1200
- See Also:
- Constant Field Values
-
CBR_2400
public static final int CBR_2400
- See Also:
- Constant Field Values
-
CBR_4800
public static final int CBR_4800
- See Also:
- Constant Field Values
-
CBR_9600
public static final int CBR_9600
- See Also:
- Constant Field Values
-
CBR_14400
public static final int CBR_14400
- See Also:
- Constant Field Values
-
CBR_19200
public static final int CBR_19200
- See Also:
- Constant Field Values
-
CBR_38400
public static final int CBR_38400
- See Also:
- Constant Field Values
-
CBR_56000
public static final int CBR_56000
- See Also:
- Constant Field Values
-
CBR_57600
public static final int CBR_57600
- See Also:
- Constant Field Values
-
CBR_115200
public static final int CBR_115200
- See Also:
- Constant Field Values
-
CBR_128000
public static final int CBR_128000
- See Also:
- Constant Field Values
-
CBR_256000
public static final int CBR_256000
- See Also:
- Constant Field Values
-
CE_RXOVER
public static final int CE_RXOVER
- See Also:
- Constant Field Values
-
CE_OVERRUN
public static final int CE_OVERRUN
- See Also:
- Constant Field Values
-
CE_RXPARITY
public static final int CE_RXPARITY
- See Also:
- Constant Field Values
-
CE_FRAME
public static final int CE_FRAME
- See Also:
- Constant Field Values
-
CE_BREAK
public static final int CE_BREAK
- See Also:
- Constant Field Values
-
CE_TXFULL
public static final int CE_TXFULL
- See Also:
- Constant Field Values
-
CE_PTO
public static final int CE_PTO
- See Also:
- Constant Field Values
-
CE_IOE
public static final int CE_IOE
- See Also:
- Constant Field Values
-
CE_DNS
public static final int CE_DNS
- See Also:
- Constant Field Values
-
CE_OOP
public static final int CE_OOP
- See Also:
- Constant Field Values
-
CE_MODE
public static final int CE_MODE
- See Also:
- Constant Field Values
-
IE_BADID
public static final int IE_BADID
- See Also:
- Constant Field Values
-
IE_OPEN
public static final int IE_OPEN
- See Also:
- Constant Field Values
-
IE_NOPEN
public static final int IE_NOPEN
- See Also:
- Constant Field Values
-
IE_MEMORY
public static final int IE_MEMORY
- See Also:
- Constant Field Values
-
IE_DEFAULT
public static final int IE_DEFAULT
- See Also:
- Constant Field Values
-
IE_HARDWARE
public static final int IE_HARDWARE
- See Also:
- Constant Field Values
-
IE_BYTESIZE
public static final int IE_BYTESIZE
- See Also:
- Constant Field Values
-
IE_BAUDRATE
public static final int IE_BAUDRATE
- See Also:
- Constant Field Values
-
EV_RXCHAR
public static final int EV_RXCHAR
- See Also:
- Constant Field Values
-
EV_RXFLAG
public static final int EV_RXFLAG
- See Also:
- Constant Field Values
-
EV_TXEMPTY
public static final int EV_TXEMPTY
- See Also:
- Constant Field Values
-
EV_CTS
public static final int EV_CTS
- See Also:
- Constant Field Values
-
EV_DSR
public static final int EV_DSR
- See Also:
- Constant Field Values
-
EV_RLSD
public static final int EV_RLSD
- See Also:
- Constant Field Values
-
EV_BREAK
public static final int EV_BREAK
- See Also:
- Constant Field Values
-
EV_ERR
public static final int EV_ERR
- See Also:
- Constant Field Values
-
EV_RING
public static final int EV_RING
- See Also:
- Constant Field Values
-
EV_PERR
public static final int EV_PERR
- See Also:
- Constant Field Values
-
EV_RX80FULL
public static final int EV_RX80FULL
- See Also:
- Constant Field Values
-
EV_EVENT1
public static final int EV_EVENT1
- See Also:
- Constant Field Values
-
EV_EVENT2
public static final int EV_EVENT2
- See Also:
- Constant Field Values
-
FORMAT_MESSAGE_ALLOCATE_BUFFER
public static final int FORMAT_MESSAGE_ALLOCATE_BUFFER
- See Also:
- Constant Field Values
-
FORMAT_MESSAGE_IGNORE_INSERTS
public static final int FORMAT_MESSAGE_IGNORE_INSERTS
- See Also:
- Constant Field Values
-
FORMAT_MESSAGE_FROM_STRING
public static final int FORMAT_MESSAGE_FROM_STRING
- See Also:
- Constant Field Values
-
FORMAT_MESSAGE_FROM_HMODULE
public static final int FORMAT_MESSAGE_FROM_HMODULE
- See Also:
- Constant Field Values
-
FORMAT_MESSAGE_FROM_SYSTEM
public static final int FORMAT_MESSAGE_FROM_SYSTEM
- See Also:
- Constant Field Values
-
FORMAT_MESSAGE_ARGUMENT_ARRAY
public static final int FORMAT_MESSAGE_ARGUMENT_ARRAY
- See Also:
- Constant Field Values
-
FORMAT_MESSAGE_MAX_WIDTH_MASK
public static final int FORMAT_MESSAGE_MAX_WIDTH_MASK
- See Also:
- Constant Field Values
-
LANG_NEUTRAL
public static final int LANG_NEUTRAL
- See Also:
- Constant Field Values
-
SUBLANG_DEFAULT
public static final int SUBLANG_DEFAULT
- See Also:
- Constant Field Values
-
-
Method Detail
-
MAKELANGID
public static int MAKELANGID(int p, int s)
-
CreateFile
public static WinAPI.HANDLE CreateFile(String name, int access, int sharing, WinAPI.SECURITY_ATTRIBUTES security, int create, int attribs, com.sun.jna.Pointer template)
-
WriteFile
public static boolean WriteFile(WinAPI.HANDLE hFile, byte[] buf, int wrn, int[] nwrtn)
-
WriteFile
public static boolean WriteFile(WinAPI.HANDLE hFile, com.sun.jna.Pointer buf, int wrn, int[] nwrtn, WinAPI.OVERLAPPED ovrlp)
-
ReadFile
public static boolean ReadFile(WinAPI.HANDLE hFile, byte[] buf, int rdn, int[] nrd)
-
ReadFile
public static boolean ReadFile(WinAPI.HANDLE hFile, com.sun.jna.Pointer buf, int rdn, int[] nrd, WinAPI.OVERLAPPED ovrlp)
-
FlushFileBuffers
public static boolean FlushFileBuffers(WinAPI.HANDLE hFile)
-
PurgeComm
public static boolean PurgeComm(WinAPI.HANDLE hFile, int qmask)
-
CancelIo
public static boolean CancelIo(WinAPI.HANDLE hFile)
-
CloseHandle
public static boolean CloseHandle(WinAPI.HANDLE hFile)
-
ClearCommError
public static boolean ClearCommError(WinAPI.HANDLE hFile, int[] n, WinAPI.COMSTAT s)
-
SetCommMask
public static boolean SetCommMask(WinAPI.HANDLE hFile, int mask)
-
GetCommMask
public static boolean GetCommMask(WinAPI.HANDLE hFile, int[] mask)
-
GetCommState
public static boolean GetCommState(WinAPI.HANDLE hFile, WinAPI.DCB dcb)
-
SetCommState
public static boolean SetCommState(WinAPI.HANDLE hFile, WinAPI.DCB dcb)
-
SetCommTimeouts
public static boolean SetCommTimeouts(WinAPI.HANDLE hFile, WinAPI.COMMTIMEOUTS touts)
-
SetupComm
public static boolean SetupComm(WinAPI.HANDLE hFile, int inQueueSz, int outQueueSz)
-
SetCommBreak
public static boolean SetCommBreak(WinAPI.HANDLE hFile)
-
ClearCommBreak
public static boolean ClearCommBreak(WinAPI.HANDLE hFile)
-
GetCommModemStatus
public static boolean GetCommModemStatus(WinAPI.HANDLE hFile, int[] stat)
-
EscapeCommFunction
public static boolean EscapeCommFunction(WinAPI.HANDLE hFile, int func)
-
CreateEvent
public static WinAPI.HANDLE CreateEvent(WinAPI.SECURITY_ATTRIBUTES security, boolean manual, boolean initial, String name)
-
SetEvent
public static boolean SetEvent(WinAPI.HANDLE hEvent)
-
ResetEvent
public static boolean ResetEvent(WinAPI.HANDLE hEvent)
-
WaitCommEvent
public static boolean WaitCommEvent(WinAPI.HANDLE hFile, com.sun.jna.ptr.IntByReference lpEvtMask, WinAPI.OVERLAPPED ovl)
-
WaitCommEvent
public static boolean WaitCommEvent(WinAPI.HANDLE hFile, int[] lpEvtMask)
-
WaitForSingleObject
public static int WaitForSingleObject(WinAPI.HANDLE hHandle, int dwMilliseconds)
-
WaitForMultipleObjects
public static int WaitForMultipleObjects(int nCount, WinAPI.HANDLE[] lpHandles, boolean bWaitAll, int dwMilliseconds)
-
GetOverlappedResult
public static boolean GetOverlappedResult(WinAPI.HANDLE hFile, WinAPI.OVERLAPPED ovl, int[] ntfrd, boolean wait)
-
GetLastError
public static int GetLastError()
-
FormatMessageW
public static int FormatMessageW(int flags, com.sun.jna.Pointer src, int msgId, int langId, com.sun.jna.Pointer dst, int sze, com.sun.jna.Pointer va_list)
-
QueryDosDevice
public static int QueryDosDevice(String name, byte[] buffer, int bsize)
-
-