Package de.gmxhome.conrad.JNALinux
Class JnaLinuxSerial
java.lang.Object
de.gmxhome.conrad.JNALinux.JnaLinuxSerial
- All Implemented Interfaces:
SerialIOAdapter
,AutoCloseable
Implementation of SerialIOAdapter using native Linux calls via JNA. Can be used for all similar operating systems as
long as the arguments of open, read, write, close, poll and system match the interface.
Setting baud rate, number of stop bits, data size and parity will be performed using an external command. This command will be built using property values read from de.gmxhome.conrad.JNALinux.JnaLinuxSerial.properties and is of the form
Setting baud rate, number of stop bits, data size and parity will be performed using an external command. This command will be built using property values read from de.gmxhome.conrad.JNALinux.JnaLinuxSerial.properties and is of the form
prefix baud size stop parity < port, where
- prefix will be replaced by the property value STTYPREFIX (default: "stty raw -echo"),
- baud will be replaced by one of the property values BD1200 (default: "1200"), BD2400 (default: "2400"), BD4800 (default: "4800"), BD9600 (default: "9600"), BD19200 (default: "19200"), BD38400 (default: "38400"), BD57600 (default: "57600"), BD115200 (default: "115200"), BD128000 (default: "") or BD256000 (default: ""),
- size will be replaced by one of the property values CS7 (default: "cs7") or CS8 (default: "cs8"),
- stop will be replaced by one of the property values SB1 (default: "-cstopb") or SB2 (default: "cstopb"),
- parity will be replaced by one of the property values PARNO (default: "-parenb"), PARODD (default: "parenb parodd"), PAREVEN (default: "parenb -parodd"), PARMARK (default: ""), PARSPACE (default: "") and
- port will be replaced by the the port as passed to method open.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Interface for some I/O relevant functions provided by Linux and other Unix-like operating systems.Nested classes/interfaces inherited from interface de.gmxhome.conrad.jpos.jpos_base.SerialIOAdapter
SerialIOAdapter.NotFoundException
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of received bytes that can be read without blocking.void
close()
Close the communication port.boolean
Checks whether a port still exists.void
flush()
Flushed input and output buffers.int[][]
getCommunicationConstants(int constantType)
Retrieves an array of integer pairs specifying all valid parameters for the given constant type.void
Opens a port for serial IO.byte[]
read(int count, int timeout)
Reads data from a communication port.void
setParameters(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.void
write(byte[] buffer)
Write the given byte buffer to the com port.
-
Constructor Details
-
JnaLinuxSerial
public JnaLinuxSerial()Constructor. Creates communication adapter.
-
-
Method Details
-
open
Description copied from interface:SerialIOAdapter
Opens a port for serial IO.- Specified by:
open
in 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:SerialIOAdapter
Close the communication port.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceSerialIOAdapter
- Throws:
IOException
- In case of IO error.
-
setParameters
Description copied from interface:SerialIOAdapter
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. 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:
setParameters
in 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:SerialIOAdapter
Returns the number of received bytes that can be read without blocking.- Specified by:
available
in interfaceSerialIOAdapter
- Returns:
- Number of bytes just in device input buffer.
- Throws:
IOException
- In case of an IO error
-
read
Description copied from interface:SerialIOAdapter
Reads 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:
read
in 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:SerialIOAdapter
Write the given byte buffer to the com port.- Specified by:
write
in 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:SerialIOAdapter
Flushed input and output buffers.- Specified by:
flush
in interfaceSerialIOAdapter
- Throws:
IOException
- In case of an IO error.
-
exits
Description copied from interface:SerialIOAdapter
Checks whether a port still exists. In case of serial communication via Bluetooth or USB, ports can go lost.- Specified by:
exits
in 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:SerialIOAdapter
Retrieves 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:
getCommunicationConstants
in 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.
-