Gets the native file descriptor used by this port.
The file descriptor can be used in calls to JTermios functions. This
maybe useful in extreme cases where performance is more important than
convenience, for example using JTermios.read(...)
instead of
SerialPort.getInputStream().read(...)
.
Note that mixing direct JTermios read/write calls with SerialPort stream
read/write calls is at best fragile and likely to fail, which also
implies that when using JTermios directly then configuring the port,
especially termios.cc[VMIN] and termios.cc[VTIME] is the users
responsibility.
Below is a sketch of minimum necessary to perform a read using raw
JTermios functionality.
// import the JTermios functionality like this
import jtermios.*;
import static jtermios.JTermios.*;
SerialPort port = ...;
// cast the port to PureJavaSerialPort to get access to getNativeFileDescriptor
int FD = ((PureJavaSerialPort) port).getNativeFileDescriptor();
// timeout and threshold values
int messageLength = 25; // bytes
int timeout = 200; // msec
// to initialize timeout and threshold first read current termios
Termios termios = new Termios();
if (0 != tcgetattr(FD, termios))
errorHandling();
// then set VTIME and VMIN, note VTIME in 1/10th of sec and both max 255
termios.c_cc[VTIME] = (byte) ((timeout+99) / 100);
termios.c_cc[VMIN] = (byte) messageLength;
// update termios
if (0 != tcsetattr(FD, TCSANOW, termios))
errorHandling();
...
// allocate read buffer
byte[] readBuffer = new byte[messageLength];
...
// then perform raw read, not this may block indefinitely
int n = read(FD, readBuffer, messageLength);
if (n < 0)
errorHandling();