Methods Summary |
---|
public void | connect(java.lang.String name, int mode, boolean timeouts)Open a serial port connection.
Note: DTR line is always on.
Hint: On Solaris opening by port number or /dev/term/* will block
until the Data Set Ready line is On. To work around this open
by device name using /dev/cua/* as root.
int portNumber = 0;
String deviceName = null;
int start = 0;
int pos = 0;
verifyPermissionCheck();
if (name.length() == 0) {
throw new IllegalArgumentException("Missing port ID");
}
if (Character.isDigit(name.charAt(0))) {
portNumber = Integer.parseInt(name.substring(0, 1));
pos++;
} else {
pos = name.indexOf(";");
if (pos < 0) {
deviceName = name;
pos = name.length();
} else {
deviceName = name.substring(0, pos);
}
}
while (name.length() > pos) {
if (name.charAt(pos) != ';") {
throw new IllegalArgumentException(
"missing parameter delimiter");
}
pos++;
start = pos;
while (true) {
if (pos == name.length()) {
parseParameter(name, start, pos);
break;
}
if (name.charAt(pos) == ';") {
parseParameter(name, start, pos);
break;
}
pos++;
}
}
// blocking is handled at the Java layer so other Java threads can run
if (deviceName != null) {
handle = native_openByName(deviceName, baud,
bbc|stop|parity|rts|cts);
} else {
handle = native_openByNumber(portNumber, baud,
bbc|stop|parity|rts|cts);
}
registerCleanup();
|
protected void | disconnect()Close the native serial port.
try {
native_close(handle);
} finally {
/* Reset handle to prevent resgistered cleanup close. */
handle = -1;
}
|
private native void | finalize()Native finalizer.
|
public int | getBaudRate()Gets the baudrate for the serial port connection.
return baud;
|
private static native void | native_close(int hPort)Close a serial port.
|
private static native void | native_configurePort(int port, int baud, int flags)Configure a serial port optional parameters.
|
private static native int | native_openByName(java.lang.String name, int baud, int flags)Open a serial port by system dependent device name.
|
private static native int | native_openByNumber(int port, int baud, int flags)Open a serial port by logical number.
|
private static native int | native_readBytes(int hPort, byte[] b, int off, int len)Read from a serial port without blocking.
|
private static native int | native_writeBytes(int hPort, byte[] b, int off, int len)Write to a serial port without blocking.
|
protected int | nonBufferedRead(byte[] b, int off, int len)Reads up to len bytes of data from the input stream into
an array of bytes, blocks until at least one byte is available,
if blocking is turned on.
Sets the eof field of the connection when the native read
returns -1.
Polling the native code is done here to avoid the need for
asynchronous native methods to be written. Not all implementations
work this way (they block in the native code) but the same
Java code works for both.
int bytesRead;
for (;;) {
try {
bytesRead = native_readBytes(handle, b, off, len);
} finally {
if (iStreams == 0) {
throw new InterruptedIOException("Stream closed");
}
}
if (bytesRead == -1) {
eof = true;
return -1;
}
if (bytesRead != 0 || !blocking) {
return bytesRead;
}
/* Wait a while for I/O to become ready */
GeneralBase.iowait();
}
|
private void | parseParameter(java.lang.String parm, int start, int end)Parse the next parameter out of a string.
parm = parm.substring(start, end);
if (parm.equals("baudrate=110")) {
baud = 110;
} else if (parm.equals("baudrate=300")) {
baud = 300;
} else if (parm.equals("baudrate=600")) {
baud = 600;
} else if (parm.equals("baudrate=1200")) {
baud = 1200;
} else if (parm.equals("baudrate=2400")) {
baud = 2400;
} else if (parm.equals("baudrate=4800")) {
baud = 4800;
} else if (parm.equals("baudrate=9600")) {
baud = 9600;
} else if (parm.equals("baudrate=14400")) {
baud = 14400;
} else if (parm.equals("baudrate=19200")) {
baud = 19200;
} else if (parm.equals("baudrate=38400")) {
baud = 38400;
} else if (parm.equals("baudrate=56000")) {
baud = 56000;
} else if (parm.equals("baudrate=57600")) {
baud = 57600;
} else if (parm.equals("baudrate=115200")) {
baud = 115200;
} else if (parm.equals("baudrate=128000")) {
baud = 128000;
} else if (parm.equals("baudrate=256000")) {
baud = 256000;
} else if (parm.equals("bitsperchar=7")) {
bbc = serSettingsFlagBitsPerChar7;
} else if (parm.equals("bitsperchar=8")) {
bbc = serSettingsFlagBitsPerChar8;
} else if (parm.equals("stopbits=1")) {
stop = serSettingsFlagStopBits1;
} else if (parm.equals("stopbits=2")) {
stop = serSettingsFlagStopBits2;
} else if (parm.equals("parity=none")) {
parity = 0;
} else if (parm.equals("parity=odd")) {
parity = serSettingsFlagParityOddM;
} else if (parm.equals("parity=even")) {
parity = serSettingsFlagParityEvenM;
} else if (parm.equals("autorts=off")) {
rts = 0;
} else if (parm.equals("autorts=on")) {
rts = serSettingsFlagRTSAutoM;
} else if (parm.equals("autocts=off")) {
cts = 0;
} else if (parm.equals("autocts=on")) {
cts = serSettingsFlagCTSAutoM;
} else if (parm.equals("blocking=off")) {
blocking = false;
} else if (parm.equals("blocking=on")) {
blocking = true;
} else {
throw new IllegalArgumentException("Bad parameter");
}
|
protected int | readBytesNonBlocking(byte[] b, int off, int len)Reads up to len bytes of data from the input stream into
an array of bytes, but does not block if no bytes available.
Sets the eof flag if the end of stream is reached.
This is implemented so the available method of
BufferedConnectionBaseAdapter will return more than
zero if the buffer is empty.
int bytesRead;
try {
// the native read does not block
bytesRead = native_readBytes(handle, b, off, len);
} finally {
if (iStreams == 0) {
throw new InterruptedIOException("Stream closed");
}
}
if (bytesRead == -1) {
eof = true;
}
return bytesRead;
|
private native void | registerCleanup()Register this object's native cleanup function.
|
public int | setBaudRate(int baudrate)Sets the baudrate for the serial port connection.
If the requested baudrate is not supported
on the platform, then the system MAY use an alternate valid setting.
The alternate value can be accessed using the
getBaudRate method.
int temp = baud;
/*
* If the baudrate is not supported, select one
* that is allowed.
*/
if (baudrate < 299) {
baudrate = 110;
} else if (baudrate < 599) {
baudrate = 300;
} else if (baudrate < 1199) {
baudrate = 600;
} else if (baudrate < 2399) {
baudrate = 1200;
} else if (baudrate < 4799) {
baudrate = 2400;
} else if (baudrate < 9599) {
baudrate = 4800;
} else if (baudrate < 14399) {
baudrate = 9600;
} else if (baudrate < 19199) {
baudrate = 14400;
} else if (baudrate < 38399) {
baudrate = 19200;
} else if (baudrate < 55999) {
baudrate = 38400;
} else if (baudrate < 57599) {
baudrate = 56000;
} else if (baudrate < 115199) {
baudrate = 57600;
} else if (baudrate < 127999) {
baudrate = 115200;
} else if (baudrate < 255999) {
baudrate = 128000;
} else {
baudrate = 256000;
}
try {
/* Set the new baudrate. */
native_configurePort(handle, baudrate,
bbc|stop|parity|rts|cts);
/* If successful, update the local baud variable. */
baud = baudrate;
} catch (IOException ioe) {
// NYI - could not set baudrate as requested.
}
return temp;
|
public int | writeBytes(byte[] b, int off, int len)Writes len bytes from the specified byte array
starting at offset off to this output stream.
Polling the native code is in the stream object handed out by
our parent helper class. This done to avoid the need for
asynchronous native methods to be written. Not all implementations
work this way (they block in the native code) but the same
Java code works for both.
return native_writeBytes(handle, b, off, len);
|