Package de.gmxhome.conrad.JNAWindows
Class JnaSerial
java.lang.Object
de.gmxhome.conrad.JNAWindows.JnaSerial
- All Implemented Interfaces:
SerialIOAdapter,AutoCloseable
Implementation of SerialIOAdapter using native Win32 OS calls via JNA.
-
Nested Class Summary
Nested classes/interfaces inherited from interface de.gmxhome.conrad.jpos.jpos_base.SerialIOAdapter
SerialIOAdapter.NotFoundException -
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of received bytes that can be read without blocking.voidclose()Close the communication port.booleanChecks whether a port still exists.voidflush()Flushed input and output buffers.int[][]getCommunicationConstants(int constantType)Retrieves an array of integer pairs specifying all valid parameters for the given constant type.voidOpens a port for serial IO.byte[]read(int count, int timeout)Reads data from a communication port.voidsetParameters(int baudrate, int databits, int stopbits, int parity)Set communication parameters for speed, data size and frame structure.
baudrate specifies the baud rate, databits the number of data bits, stopbits the number of stop bits per data unit and parity the kind of parity bit, if parity shall be used.voidwrite(byte[] buffer)Write the given byte buffer to the com port.
-
Constructor Details
-
JnaSerial
public JnaSerial()Constructor. Creates communication adapter.
-
-
Method Details
-
open
Description copied from interface:SerialIOAdapterOpens a port for serial IO.- Specified by:
openin interfaceSerialIOAdapter- Parameters:
port- Port. e.g. COM2- Throws:
IOException- If the port does not exist or in case of IO errors.
-
close
Description copied from interface:SerialIOAdapterClose the communication port.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceSerialIOAdapter- Throws:
IOException- In case of IO error.
-
setParameters
Description copied from interface:SerialIOAdapterSet communication parameters for speed, data size and frame structure.
baudrate specifies the baud rate, databits the number of data bits, stopbits the number of stop bits per data unit and parity the kind of parity bit, if parity shall be used. Keep in mind that these constants can vary between adapter implementations. The application should use method getCommunicationConstants to retrieve the valid constant values and the corresponding adapter independent values.- Specified by:
setParametersin interfaceSerialIOAdapter- Parameters:
baudrate- Baud rate.databits- Bits per date unit.stopbits- Number of stop bits to be used.parity- Parity constant.- Throws:
IOException- If an IO error occurs of if a parameter is invalid.
-
available
Description copied from interface:SerialIOAdapterReturns the number of received bytes that can be read without blocking.- Specified by:
availablein interfaceSerialIOAdapter- Returns:
- Number of bytes just in device input buffer.
- Throws:
IOException- In case of an IO error
-
read
Description copied from interface:SerialIOAdapterReads data from a communication port. If no data are available, read blocks until data are available or the specified timeout has been reached.- Specified by:
readin interfaceSerialIOAdapter- Parameters:
count- Maximum number of bytes to be read.timeout- Maximum time to wait for data in milliseconds.- Returns:
- byte array holding all available data bytes
- Throws:
IOException- In case of an IO error.
-
write
Description copied from interface:SerialIOAdapterWrite the given byte buffer to the com port.- Specified by:
writein interfaceSerialIOAdapter- Parameters:
buffer- Buffer to be written- Throws:
IOException- If the port has not been opened or in case of an IO error.
-
flush
Description copied from interface:SerialIOAdapterFlushed input and output buffers.- Specified by:
flushin interfaceSerialIOAdapter- Throws:
IOException- In case of an IO error.
-
exits
Description copied from interface:SerialIOAdapterChecks whether a port still exists. In case of serial communication via Bluetooth or USB, ports can go lost.- Specified by:
exitsin interfaceSerialIOAdapter- Parameters:
port- Port to be checked.- Returns:
- true if the port still exists, false otherwise-
-
getCommunicationConstants
public int[][] getCommunicationConstants(int constantType)Description copied from interface:SerialIOAdapterRetrieves an array of integer pairs specifying all valid parameters for the given constant type. constantType can be one of T_BAUD, T_DATA, T_STOP or T_PARITY. The method returns an array of integer pairs where the first element specifies the constant value to be passed to the setParameters method of the SerialIOProcessor and the second element specifies the corresponding value to be passed to the setParameters method of the specific SerialIOAdapter implementation.- Specified by:
getCommunicationConstantsin interfaceSerialIOAdapter- Parameters:
constantType- Type of constants to be requested.- Returns:
- Array of integer pairs containing the adapter independent and adapter specific constants of the requested type. null if constantType is invalid.
-