Package totalcross.io.device
Class PortConnector
- java.lang.Object
-
- totalcross.io.Stream
-
- totalcross.io.device.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 intBLUETOOTHBluetooth: 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 intDEFAULTDefault Port (cradle)static intIRCOMMIrCOMM Port (Serial Connection on top of IrDA Stack)static intPARITY_EVENUsed in the constructor to define the paritystatic intPARITY_NONEUsed in the constructor to define the paritystatic intPARITY_ODDUsed in the constructor to define the parityintreadTimeoutDefines the read timeout, in milliseconds.static intreadTriesSet this to a value that will be used to keep trying to read further while something is available.static intSIRSIR Port (Physical Layer of IrDA Stack).booleanstopWriteCheckOnTimeoutSet to false to not let the timeout error stop the write check loop.static intUSBUSB Endpoint 2 PortintwriteTimeoutDefines the write timeout, in milliseconds.-
Fields inherited from class totalcross.io.Stream
skipBuffer
-
-
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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the port.protected voidfinalize()intreadBytes(byte[] buf, int start, int count)Reads bytes from the stream.intreadCheck()Returns the number of bytes currently available to be read from the port's queue.voidsetFlowControl(boolean on)Turns RTS/CTS flow control (hardware flow control) on or off.intwriteBytes(byte[] buf, int start, int count)Writes to the port.-
Methods inherited from class totalcross.io.Stream
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
-
-
-
-
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.
-
DEFAULT
public static final int DEFAULT
Default Port (cradle)- See Also:
- Constant Field Values
-
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
-
USB
public static final int USB
USB Endpoint 2 Port- 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, boolean parity, int stopBits) throws IllegalArgumentIOException, IOExceptionOpens a port.- Parameters:
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 bits- Throws:
IllegalArgumentIOExceptionIOException- See Also:
setFlowControl(boolean),PortConnector(int, int, int, int, int)
-
PortConnector
public PortConnector(int number, int baudRate, int bits, int parity, int stopBits) throws IllegalArgumentIOException, IOExceptionOpens 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 useConvert.chars2int("rfcm")or in Windows CE useConvert.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 bits- Throws:
IllegalArgumentIOExceptionIOException- Since:
- SuperWaba 4.5
- See Also:
setFlowControl(boolean)
-
PortConnector
public PortConnector(int number, int baudRate) throws IllegalArgumentIOException, IOExceptionOpen a port with settings of 8 bits, no parity and 1 stop bit. These are the most commonly used port settings.- Parameters:
number- port number. On Windows, this is the number of the COM port.baudRate- baud rate- Throws:
IllegalArgumentIOExceptionIOException- See Also:
PortConnector(int, int, int, int, int)
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses the port.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
IOException- If an I/O error occurs.
-
setFlowControl
public final void setFlowControl(boolean on) throws IOExceptionTurns 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 IOExceptionDescription copied from class:StreamReads 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:
readBytesin classStream- Parameters:
buf- the byte array to read data intostart- the start position in the arraycount- the number of bytes to read- Throws:
IOException
-
readCheck
public final int readCheck() throws IOExceptionReturns 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 IOExceptionWrites to the port. Returns the number of bytes written or throws anIOExceptionif 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:
writeBytesin classStream- Parameters:
buf- the byte array to write data fromstart- the start position in the byte arraycount- the number of bytes to write- Returns:
- the number of bytes actually written
- Throws:
IOException
-
finalize
protected void finalize()
- Overrides:
finalizein classjava.lang.Object
-
-