Class PortConnector

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class PortConnector
    extends Stream
    PortConnector accesses the device ports.

    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:
    DEFAULT=a
    IRCOMM=b
    SIR=c
    USB=d
    If the file is not found, it will use COM1, COM2, COM3, and COM4, respectively.
    • Field Summary

      Fields 
      Modifier and Type Field 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.
    • Constructor Summary

      Constructors 
      Constructor 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.
    • Field Detail

      • writeTimeout

        public int writeTimeout
        Defines the write timeout, in milliseconds. The default is 6 seconds.
      • readTimeout

        public int readTimeout
        Defines the read timeout, in milliseconds. The default is 6 seconds.
      • stopWriteCheckOnTimeout

        public boolean stopWriteCheckOnTimeout
        Set to false to not let the timeout error stop the write check loop.
      • IRCOMM

        public static final int IRCOMM
        IrCOMM Port (Serial Connection on top of IrDA Stack)
        See Also:
        Constant Field Values
      • SIR

        public static final int SIR
        SIR Port (Physical Layer of IrDA Stack). Important! In order to access the SIR port on the Pocket PCs the following has to be UNCHECKED: Settings/Connections/Beam/Receive all incoming beams and select discoverable mode. Note that Palm OS OMAP-based devices do NOT support SIR (aka raw IR); see this: http://www.alanjmcf.me.uk/comms/infrared/IrDA%20FAQ.html and http://news.palmos.com/read/messages?id=146145#146145. Note also that there's no handshaking support on SIR, and that not all devices work with it. Use IRCOMM instead when possible.
        See Also:
        Constant Field Values
      • BLUETOOTH

        public static final 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. Note: on WinCE, this maps to the Serial (port 0). You must explicitly map your BlueTooth outside the program.
        See Also:
        Constant Field Values
      • PARITY_NONE

        public static final int PARITY_NONE
        Used in the constructor to define the parity
        See Also:
        Constant Field Values
      • PARITY_EVEN

        public static final int PARITY_EVEN
        Used in the constructor to define the parity
        See Also:
        Constant Field Values
      • PARITY_ODD

        public static final int PARITY_ODD
        Used in the constructor to define the parity
        See Also:
        Constant Field Values
      • readTries

        public static int readTries
        Set this to a value that will be used to keep trying to read further while something is available. If a series of bytes is returned in very few steps, this will make the read stop after readTries has reached zero.
        Since:
        SuperWaba 5.72
    • Constructor Detail

      • PortConnector

        public PortConnector​(int number,
                             int baudRate,
                             int bits,
                             int parity,
                             int stopBits)
                      throws IllegalArgumentIOException,
                             IOException
        Opens a port. The number passed is the number of the port for devices with multiple ports. Port number 0 defines the "default port" of a given type. For Windows CE and Palm OS devices, you should pass 0 as the port number to access the device's single serial or usb 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 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.
        Parameters:
        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 rate
        bits - 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 bits
        Throws:
        IllegalArgumentIOException
        IOException
        Since:
        SuperWaba 4.5
        See Also:
        setFlowControl(boolean)
    • Method Detail

      • close

        public void close()
                   throws IOException
        Closes the port.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        IOException - If an I/O error occurs.
      • setFlowControl

        public final void setFlowControl​(boolean on)
                                  throws IOException
        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
        Throws:
        IOException
      • readBytes

        public int readBytes​(byte[] buf,
                             int start,
                             int count)
                      throws IOException
        Description copied from class: Stream
        Reads bytes from the stream. Returns the number of bytes actually read or -1 if the end of the stream was reached. (if applicable to the stream)
        Specified by:
        readBytes in class Stream
        Parameters:
        buf - the byte array to read data into
        start - the start position in the array
        count - the number of bytes to read
        Throws:
        IOException
      • readCheck

        public final int readCheck()
                            throws IOException
        Returns the number of bytes currently available to be read from the port's queue.
        Throws:
        IOException
      • writeBytes

        public int writeBytes​(byte[] buf,
                              int start,
                              int count)
                       throws IOException
        Writes to the port. Returns the number of bytes written or throws an 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.
        Specified by:
        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
        Returns:
        the number of bytes actually written
        Throws:
        IOException
      • finalize

        protected void finalize()
        Overrides:
        finalize in class java.lang.Object