public class PortConnector extends Stream
This works on the devices and on JDK (in this case, it uses javax.comm, which must be installed separately)
When a serial port is created, an attempt is made to open the port. If the open attempt is successful, the port will remain open until close() is called. If close() is never called, the port will be closed when the object is garbage collected.
Here is an example showing data being written and read from a serial port:
PortConnector port = new PortConnector(0, 9600); DataStream ds = new DataStream(port); ds.writeCString("Hello...\r\n"); port.close();On JDK, you can define a file called serial.properties with the number and ports to be used:
Modifier and Type | Field and Description |
---|---|
static int |
BLUETOOTH
Bluetooth: open the built-in Bluetooth discovery dialog,
then establish a Serial Port Profile (just serial emulation across
Bluetooth, using the RFComm BT layer) virtual serial port connection.
|
static int |
DEFAULT
Default Port (cradle)
|
static int |
IRCOMM
IrCOMM Port (Serial Connection on top of IrDA Stack)
|
static int |
PARITY_EVEN
Used in the constructor to define the parity
|
static int |
PARITY_NONE
Used in the constructor to define the parity
|
static int |
PARITY_ODD
Used in the constructor to define the parity
|
int |
readTimeout
Defines the read timeout, in milliseconds.
|
static int |
readTries
Set this to a value that will be used to keep trying to read further while something is available.
|
static int |
SIR
SIR Port (Physical Layer of IrDA Stack).
|
boolean |
stopWriteCheckOnTimeout
Set to false to not let the timeout error stop the write check loop.
|
static int |
USB
USB Endpoint 2 Port
|
int |
writeTimeout
Defines the write timeout, in milliseconds.
|
skipBuffer
Constructor and Description |
---|
PortConnector(int number,
int baudRate)
Open a port with settings of 8 bits, no parity and 1 stop bit.
|
PortConnector(int number,
int baudRate,
int bits,
boolean parity,
int stopBits)
Opens a port.
|
PortConnector(int number,
int baudRate,
int bits,
int parity,
int stopBits)
Opens a port.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the port.
|
protected void |
finalize() |
int |
readBytes(byte[] buf,
int start,
int count)
Reads bytes from the stream.
|
int |
readCheck()
Returns the number of bytes currently available to be read from the
port's queue.
|
void |
setFlowControl(boolean on)
Turns RTS/CTS flow control (hardware flow control) on or off.
|
int |
writeBytes(byte[] buf,
int start,
int count)
Writes to the port.
|
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
public int writeTimeout
public int readTimeout
public boolean stopWriteCheckOnTimeout
public static final int DEFAULT
public static final int IRCOMM
public static final int SIR
public static final int USB
public static final int BLUETOOTH
public static final int PARITY_NONE
public static final int PARITY_EVEN
public static final int PARITY_ODD
public static int readTries
public PortConnector(int number, int baudRate, int bits, boolean parity, int stopBits) throws IllegalArgumentIOException, IOException
number
- port number. In Windows, this is the number of the COM port.baudRate
- baud ratebits
- bits per char [5 to 8]parity
- true for even parity, false for no paritystopBits
- number of stop bitsIllegalArgumentIOException
IOException
setFlowControl(boolean)
,
PortConnector(int, int, int, int, int)
public PortConnector(int number, int baudRate, int bits, int parity, int stopBits) throws IllegalArgumentIOException, IOException
On Windows devices, port numbers map to COM port numbers. For example, serial port 2 maps to "COM2:".
Here is an example showing how to open the serial port of a Palm OS device at 9600 baud with settings of 8 bits, no partity and one stop bit (8/N/1):
PortConnector port = new PortConnector(0, 9600, 8, false, 1);Here is an example of opening serial port COM2: on a Windows device:
PortConnector port = new PortConnector(2, 57600, 8, false, 1);No serial XON/XOFF flow control (commonly called software flow control) is used and RTS/CTS flow control (commonly called hardware flow control) is turn on by default on all platforms but Windows CE. The parity setting must be one of the constants PARITY_NONE, PARITY_EVEN, PARITY_ODD. For Palm OS, PARITY_ODD turns on XON/XOFF mode.
number
- port number. In Windows, this is the number of the COM port. On Windows and Palm OS, you can pass
a 4-letter that indicates the connection that will be used. For example, on Palm OS you could use Convert.chars2int("rfcm")
or in Windows CE use Convert.chars2int("COM4")
.baudRate
- baud ratebits
- bits per char [5 to 8]parity
- of the constants PARITY_NONE, PARITY_EVEN, PARITY_ODD. For Palm OS, PARITY_ODD turns on XonXoff mode.stopBits
- number of stop bitsIllegalArgumentIOException
IOException
setFlowControl(boolean)
public PortConnector(int number, int baudRate) throws IllegalArgumentIOException, IOException
number
- port number. On Windows, this is the number of the COM port.baudRate
- baud rateIllegalArgumentIOException
IOException
PortConnector(int, int, int, int, int)
public void close() throws IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
IOException
- If an I/O error occurs.public final void setFlowControl(boolean on) throws IOException
on
- pass true to set flow control on and false to set it offIOException
public int readBytes(byte[] buf, int start, int count) throws IOException
Stream
readBytes
in class Stream
buf
- the byte array to read data intostart
- the start position in the arraycount
- the number of bytes to readIOException
public final int readCheck() throws IOException
IOException
public int writeBytes(byte[] buf, int start, int count) throws IOException
IOException
if an error prevented the write operation from occurring. If data
can't be written to the port and flow control is on, the write
operation will time out and fail after approximately 2 seconds.writeBytes
in class Stream
buf
- the byte array to write data fromstart
- the start position in the byte arraycount
- the number of bytes to writeIOException
protected void finalize()
finalize
in class java.lang.Object