waba.io
Class SerialPort

java.lang.Object
  |
  +--waba.io.Stream
        |
        +--waba.io.SerialPort

public class SerialPort
extends Stream

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

SerialPort

public SerialPort(int number,
                  int baudRate,
                  int bits,
                  boolean parity,
                  int stopBits)
Opens a serial port. The number passed is the number of the serial port for devices with multiple serial ports. Port number 0 defines the "default port" of a given type. For Windows CE and PalmPilot devices, you should pass 0 as the port number to access the device's single serial port.

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.
Parameters:
number - port number
baudRate - baud rate
bits - bits per char [5 to 8]
parity - true for even parity, false for no parity
stopBits - number of stop bits
See Also:
setFlowControl(boolean)

SerialPort

public SerialPort(int number,
                  int baudRate)
Open a serial port with settings of 8 bits, no parity and 1 stop bit. These are the most commonly used serial port settings.
Method Detail

close

public boolean close()
Closes the port. Returns true if the operation is successful and false otherwise.
Overrides:
close in class Stream

isOpen

public boolean isOpen()
Returns true if the port is open and false otherwise. This can be used to check if opening the serial port was successful.

setFlowControl

public boolean setFlowControl(boolean on)
Turns RTS/CTS flow control (hardware flow control) on or off.
Parameters:
on - pass true to set flow control on and false to set it off

setReadTimeout

public boolean setReadTimeout(int millis)
Sets the timeout value for read operations. The value specifies the number of milliseconds to wait from the time of last activity before timing out a read operation. Passing a value of 0 sets no timeout causing any read operation to return immediately with or without data. The default timeout is 100 milliseconds. This method returns true if successful and false if the value passed is negative or the port is not open.
Parameters:
millis - timeout in milliseconds

readBytes

public int readBytes(byte[] buf,
                     int start,
                     int count)
Reads bytes from the port into a byte array. Returns the number of bytes actually read or -1 if an error prevented the read operation from occurring. The read will timeout if no activity takes place within the timeout value for the port.
Overrides:
readBytes in class Stream
Parameters:
buf - the byte array to read data into
start - the start position in the byte array
count - the number of bytes to read
See Also:
setReadTimeout(int)

readCheck

public int readCheck()
Returns the number of bytes currently available to be read from the serial port's queue. This method only works under PalmOS and not WinCE due to limitations in the Win32 CE API. Under Win32 and Java, this method will always return -1.

writeBytes

public int writeBytes(byte[] buf,
                      int start,
                      int count)
Writes to the port. Returns the number of bytes written or -1 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.
Overrides:
writeBytes in class Stream
Parameters:
buf - the byte array to write data from
start - the start position in the byte array
count - the number of bytes to write