|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--waba.io.Stream | +--waba.io.SerialPort
SerialPort accesses a device's serial port.
This works under the devices and under JDK (in this case, it uses javax.comm)
When a serial port is created, an attempt is made to open the port. If the open attempt is successful, a call to isOpen() will return true and 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:
SerialPort port = new SerialPort(0, 9600); if (!port.isOpen()) return; byte buf[] = new byte[10]; buf[0] = 3; buf[1] = 7; port.writeBytes(buf, 0, 2); int count = port.readBytes(buf, 0, 10); if (count == 10) ... port.close();
Note on HandSpring users: the Keyboard thread is disabled after open and enabled again after close.
Field Summary | |
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) |
int |
lastError
Returns the last error when calling a method. |
static int |
SIR
SIR Port (Physical Layer of IrDA Stack). |
static int |
USB
USB Endpoint 2 Port |
Constructor Summary | |
SerialPort(int number,
int baudRate)
Open a serial port with settings of 8 bits, no parity and 1 stop bit. |
|
SerialPort(int number,
int baudRate,
int bits,
boolean parity,
int stopBits)
Opens a serial port. |
Method Summary | |
boolean |
close()
Closes the port. |
boolean |
isOpen()
Returns true if the port is open and false otherwise. |
int |
readBytes(byte[] buf,
int start,
int count)
Reads bytes from the port into a byte array. |
int |
readCheck()
Returns the number of bytes currently available to be read from the serial port's queue. |
boolean |
setFlowControl(boolean on)
Turns RTS/CTS flow control (hardware flow control) on or off. |
boolean |
setReadTimeout(int millis)
Sets the timeout value for read operations. |
int |
writeBytes(byte[] buf,
int start,
int count)
Writes to the port. |
Methods inherited from class java.lang.Object |
equals,
getClass,
hashCode,
notify,
toString,
wait,
wait |
Field Detail |
public int lastError
SW Custom codes (See nmpalm_c.c for details): 65525 (-11): NO_SERIAL_LIBRARY_AVAILABLE 65522 (-14): ARRAY_RANGE_CHECKWindowsCE error codes: see winerror.h in the Windows SDK. Palm OS error codes.
serErrBadParam | 769 |
BadPort | 770 |
NoMem | 771 |
BadConnID | 772 |
TimeOut | 773 |
LineErr | 774 |
AlreadyOpen | 775 |
StillOpen | 776 |
NotOpen | 777 |
NotSupported | 778 |
NoDevicesAvail | 779 |
USBConfigurationFailed | 780 |
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
Constructor Detail |
public SerialPort(int number, int baudRate, int bits, boolean parity, int stopBits)
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 PalmPilot device at 9600 baud with settings of 8 bits, no partity and one stop bit (8/N/1):
SerialPort port = new SerialPort(0, 9600, 8, false, 1);Here is an example of opening serial port COM2: on a Windows device:
SerialPort port = new SerialPort(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 is a boolean. If false, no parity is used. If true, "even" parity is used.
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 bitssetFlowControl(boolean)
public SerialPort(int number, int baudRate)
number
- port number. In Windows, this is the number of the COM port.baudRate
- baud rateMethod Detail |
public boolean close()
public boolean isOpen()
public boolean setFlowControl(boolean on)
on
- pass true to set flow control on and false to set it offpublic boolean setReadTimeout(int millis)
millis
- timeout in millisecondspublic int readBytes(byte[] buf, int start, int count)
buf
- the byte array to read data intostart
- the start position in the byte arraycount
- the number of bytes to readsetReadTimeout(int)
public int readCheck()
public int writeBytes(byte[] buf, int start, int count)
buf
- the byte array to write data fromstart
- the start position in the byte arraycount
- the number of bytes to write
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |