|
|||||||||
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.
Serial port access is only available when running under a native WabaVM, it is not supported when running under Java.
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();
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 |
hashCode,
toString |
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 numberbaudRate
- 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)
Method 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 |