SerialPort is the world's first, and still foremost Java product for serial ports. SerialPort provides highly flexible control of serial ports from your Java application.SerialPort is a high-performance class that also provides low-level serial port control. If your device plugs into a serial port—on or off the Internet—it is almost certain that you can use SerialPort to communicate with it.
Applets
To implement applet support, you must create a 'trusted' applet. If you're interested in creating trusted applets, ask about our Termlet Package which has the source code for our Termlet demo, and detailed instructions on how it was built. Note: you need Netscape Communicator 4.04 or later with the Java 1.1 patch to have full JNI support on Communicator. You use with Internet Explorer you need the MSVM with JNI support (the Java support option with IE5 provides this, there is also a VM upgrade for IE4).
Methods
Modem Classes
These classes make working with modems a snap!
Parallel Ports
SerialPort is the world's first, and still foremost Java product for serial ports. SerialPort provides highly flexible control of serial ports from your Java application. SerialPort is a high-performance class that also provides low-level serial port control. Serial Port Notifier (formerly Serial Port Monitor) sits in your notification tray and monitors the serial ports on your computer. When a port is added or removed, a notification is shown containing the port's COM port name, allowing you to easily keep track of what's been plugged in and out.
Parallel ports are not nearly as universal as serial ports. If you need to communicate to a parallel port from Java we recommend using SerialPort to talk with a parallel port by way of a serial-to-parallel adaptor; this way, the same software can be used on virtually all platforms that SerialPort supports. The same will never be true of parallel ports since many platforms do not have parallel port hardware.
USB Ports
SerialPort currently supports USB-to-Serial converters. This allows you to communicate with standard RS-232 devices using the USB port(s) of your computer.
Comm API
Sun has said that the Comm API provides a Java extension API that allows applications that use the Comm API to be certified as 100% pure. Implementations of the Comm API are not 100% pure, meaning the Comm API implementations from Sun are not 100% pure. This is an unfortunate confusion since many people tend to think if it comes from Sun it is 100% pure. In summary, applications that use implementations of the Comm API may at some future time be capable of being labeled 100% pure. As far as we know, Sun has not yet provided the specs for testing to KeyLabs (the company that does 100% Java certification) meaning that applications using the Comm API are not 100% pure.
All SerialPort packages (except Lite versions) provide support for our javax.comm.SerialPort implementation of the Java Comm API. Our implementation of the Comm API was done entirely using the SerialPort API (a testament to the power and flexibility of SerialPort). Should you use the superior SerialPort API directly, or use javax.comm.SerialPort?
Use Case Examples
SerialPort is being used worldwide to provide solutions to more interesting problems than we could ever hope to keep track of, but below is a small sampling of use cases:
- Electronic payment
- GPS interface
- Digital camera interface
- Device interface control (local and remote)
- Meter reading and control
- Barcode scanner interface
- Modem control
- File transfer
- Robot communications and control
- Telex interface
- Remote shopping
- Sensor monitoring
- Remote camera control
- Electronic banking
Package Options
SerialPort is available with many package options to suit the needs of a great variety of developers.
Need to quickly and safely transfer files over any serial port or socket? Need to exchange data with legacy systems? Our Java ZModem, Java X/YModem andJava Kermit products are just waiting to cut weeks off your development schedule.
Implementation Requirements & Limitations
- Split flow control (handshake) is not supported for SerialPort versions previous to 3.2.
- Parallel ports are not currently supported (Parallel ports don’t even exist on many of the platforms that SerialPort runs on)
- Port enumeration is not supported for SerialPort versions previous to 4.0.
- Port enumeration is currently supported on Win7/Vista/XP/2008/2003/NT/98/95, Mac OS X & OS/2, and only on JNI compliant VM’s. This feature requires jspWin.dll version 3.7 or later on Win32 and shared library version 2.8 or later on Mac, and 3.2 or later on OS/2.
- When an array of
buf[size]
is defined, and whenInputStream.read(buf)
is used, Sun's implementation will return immediately after reading the available bytes (regardless of the size). Our implementation will block until size bytes are available (this is the most logical behavior). If you want to prevent blocking userdyCnt=InputStream.available()
and pass the rdyCnt returned to theInputStream.read(buf, off, len
method wherelen=rdyCnt
.
Package | Class | Tree | Deprecated | Index | Help |
javax.comm
Class SerialPort
- public abstract class SerialPort
- extends CommPort
An RS-232 serial communications port.
SerialPort
describes the low-level interface to a serial communications port made available by the underlying system. SerialPort
defines the minimum required functionality for serial communications ports. - Author:
- Jagane Sundar
- See Also:
CommPort
,CommPortIdentifier
Field Summary | |
static int | DATABITS_5 5 data bit format. |
static int | DATABITS_6 6 data bit format. |
static int | DATABITS_7 7 data bit format. |
static int | DATABITS_8 8 data bit format. |
static int | FLOWCONTROL_NONE Flow control off. |
static int | FLOWCONTROL_RTSCTS_IN RTS/CTS flow control on input. |
static int | FLOWCONTROL_RTSCTS_OUT RTS/CTS flow control on output. |
static int | FLOWCONTROL_XONXOFF_IN XON/XOFF flow control on input. |
static int | FLOWCONTROL_XONXOFF_OUT XON/XOFF flow control on output. |
static int | PARITY_EVEN EVEN parity scheme. |
static int | PARITY_MARK MARK parity scheme. |
static int | PARITY_NONE No parity bit. |
static int | PARITY_ODD ODD parity scheme. |
static int | PARITY_SPACE SPACE parity scheme. |
static int | STOPBITS_1 Number of STOP bits - 1. |
static int | STOPBITS_1_5 Number of STOP bits - 1-1/2. |
static int | STOPBITS_2 Number of STOP bits - 2. |
Fields inherited from class javax.comm.CommPort |
name |
Constructor Summary |
SerialPort() |
Method Summary | |
abstract void | addEventListener(SerialPortEventListener lsnr) Registers a SerialPortEventListener object to listen for SerialEvent s. |
abstract int | getBaudRate() Gets the currently configured baud rate. |
abstract int | getDataBits() Gets the currently configured number of data bits. |
abstract int | getFlowControlMode() Gets the currently configured flow control mode. |
abstract int | getParity() Get the currently configured parity setting. |
abstract int | getStopBits() Gets the currently defined stop bits. |
abstract boolean | isCD() Gets the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation. |
abstract boolean | isCTS() Gets the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation. |
abstract boolean | isDSR() Gets the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation. |
abstract boolean | isDTR() Gets the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation. |
abstract boolean | isRI() Gets the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation. |
abstract boolean | isRTS() Gets the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation. |
abstract void | notifyOnBreakInterrupt(boolean enable) Expresses interest in receiving notification when there is a break interrupt on the line. |
abstract void | notifyOnCarrierDetect(boolean enable) Expresses interest in receiving notification when the CD (Carrier Detect) bit changes. |
abstract void | notifyOnCTS(boolean enable) Expresses interest in receiving notification when the CTS (Clear To Send) bit changes. |
abstract void | notifyOnDataAvailable(boolean enable) Expresses interest in receiving notification when input data is available. |
abstract void | notifyOnDSR(boolean enable) Expresses interest in receiving notification when the DSR (Data Set Ready) bit changes. |
abstract void | notifyOnFramingError(boolean enable) Expresses interest in receiving notification when there is a framing error. |
abstract void | notifyOnOutputEmpty(boolean enable) Expresses interest in receiving notification when the output buffer is empty. |
abstract void | notifyOnOverrunError(boolean enable) Expresses interest in receiving notification when there is an overrun error. |
abstract void | notifyOnParityError(boolean enable) Expresses interest in receiving notification when there is a parity error. |
abstract void | notifyOnRingIndicator(boolean enable) Expresses interest in receiving notification when the RI (Ring Indicator) bit changes. |
abstract void | removeEventListener() Deregisters event listener registered using addEventListener . |
abstract void | sendBreak(int millis) Sends a break of millis milliseconds duration. |
abstract void | setDTR(boolean dtr) Sets or clears the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation. |
abstract void | setFlowControlMode(int flowcontrol) Sets the flow control mode. |
void | setRcvFifoTrigger(int trigger) Deprecated.This was advisory only. |
abstract void | setRTS(boolean rts) Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation. |
abstract void | setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity) Sets serial port parameters. |
Methods inherited from class javax.comm.CommPort |
close, disableReceiveFraming, disableReceiveThreshold, disableReceiveTimeout, enableReceiveFraming, enableReceiveThreshold, enableReceiveTimeout, getInputBufferSize, getInputStream, getName, getOutputBufferSize, getOutputStream, getReceiveFramingByte, getReceiveThreshold, getReceiveTimeout, isReceiveFramingEnabled, isReceiveThresholdEnabled, isReceiveTimeoutEnabled, setInputBufferSize, setOutputBufferSize, toString |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
DATABITS_5
- 5 data bit format.
- See Also:
- Constant Field Values
DATABITS_6
- 6 data bit format.
- See Also:
- Constant Field Values
DATABITS_7
- 7 data bit format.
- See Also:
- Constant Field Values
DATABITS_8
- 8 data bit format.
- See Also:
- Constant Field Values
STOPBITS_1
- Number of STOP bits - 1.
- See Also:
- Constant Field Values
STOPBITS_2
- Number of STOP bits - 2.
- See Also:
- Constant Field Values
STOPBITS_1_5
- Number of STOP bits - 1-1/2. Some UARTs permit 1-1/2 STOP bits only with 5 data bit format, but permit 1 or 2 STOP bits with any format.
- See Also:
- Constant Field Values
PARITY_NONE
- No parity bit.
- See Also:
- Constant Field Values
PARITY_ODD
- ODD parity scheme. The parity bit is added so there are an odd number of TRUE bits.
- See Also:
- Constant Field Values
PARITY_EVEN
- EVEN parity scheme. The parity bit is added so there are an even number of TRUE bits.
- See Also:
- Constant Field Values
PARITY_MARK
- MARK parity scheme.
- See Also:
- Constant Field Values
PARITY_SPACE
- SPACE parity scheme.
- See Also:
- Constant Field Values
FLOWCONTROL_NONE
- Flow control off.
- See Also:
- Constant Field Values
FLOWCONTROL_RTSCTS_IN
- RTS/CTS flow control on input.
- See Also:
- Constant Field Values
FLOWCONTROL_RTSCTS_OUT
- RTS/CTS flow control on output.
- See Also:
- Constant Field Values
FLOWCONTROL_XONXOFF_IN
- XON/XOFF flow control on input.
- See Also:
- Constant Field Values
FLOWCONTROL_XONXOFF_OUT
- XON/XOFF flow control on output.
- See Also:
- Constant Field Values
Constructor Detail |
SerialPort
Method Detail |
getBaudRate
- Gets the currently configured baud rate.
- Returns:
- integer value indicating the baud rate
getDataBits
- Gets the currently configured number of data bits.
- Returns:
- integer that can be equal to DATABITS_5, DATABITS_6, DATABITS_7, or DATABITS_8
getStopBits
- Gets the currently defined stop bits.
- Returns:
- integer that can be equal to STOPBITS_1, STOPBITS_2, or STOPBITS_1_5
getParity
- Get the currently configured parity setting.
- Returns:
- integer that can be equal to PARITY_NONE, PARITY_ODD, PARITY_EVEN, PARITY_MARK or PARITY_SPACE.
sendBreak
- Sends a break of
millis
milliseconds duration. Note that it may not be possible to time the duration of the break under certain Operating Systems. Hence this parameter is advisory. - Parameters:
millis
- duration of break to send
setFlowControlMode
flowcontrol
- Can be a bitmask combination of - FLOWCONTROL_NONE: no flow control
- FLOWCONTROL_RTSCTS_IN: RTS/CTS (hardware) flow control for input
- FLOWCONTROL_RTSCTS_OUT: RTS/CTS (hardware) flow control for output
- FLOWCONTROL_XONXOFF_IN: XON/XOFF (software) flow control for input
- FLOWCONTROL_XONXOFF_OUT: XON/XOFF (software) flow control for output
- Throws:
UnsupportedCommOperationException
- if any of the flow control mode was not supported by the underline OS, or if input and output flow control are set to different values, i.e. one hardware and one software. The flow control mode will revert to the value before the call was made.
getFlowControlMode
- Gets the currently configured flow control mode.
- Returns:
- an integer bitmask of the modes FLOWCONTROL_NONE, FLOWCONTROL_RTSCTS_IN, FLOWCONTROL_RTSCTS_OUT, FLOWCONTROL_XONXOFF_IN, and FLOWCONTROL_XONXOFF_OUT.
setRcvFifoTrigger
- Deprecated.This was advisory only.
- Set the Receive Fifo trigger level If the uart has a FIFO and if it can have programmable trigger levels, then this method will cause the uart to raise an interrupt after trigger bytes have been received.
- Parameters:
trigger
- level
setSerialPortParams
baudrate
- If the baudrate passed in by the application is unsupported by the driver, the driver will throw an UnsupportedCommOperationExceptiondataBits
- - DATABITS_5: 5 bits
- DATABITS_6: 6 bits
- DATABITS_7: 7 bits
- DATABITS_8: 8 bits
stopBits
- - STOPBITS_1: 1 stop bit
- STOPBITS_2: 2 stop bits
- STOPBITS_1_5: 1.5 stop bits
parity
- - PARITY_NONE: no parity
- PARITY_ODD: odd parity
- PARITY_EVEN: even parity
- PARITY_MARK: mark parity
- PARITY_SPACE: space parity
UnsupportedCommOperationException
- if any of the above parameters are specified incorrectly. All four of the parameters will revert to the values before the call was made. DEFAULT: 9600 baud, 8 data bits, 1 stop bit, no parity
setDTR
dtr
- - true: set DTR
- false: clear DTR
isDTR
- Gets the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.
setRTS
rts
- - true: set RTS
- false: clear RTS
isRTS
- Gets the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.
isCTS
- Gets the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.
isDSR
- Gets the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.
isRI
- Gets the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.
isCD
- Gets the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.
addEventListener
Registers aSerialPortEventListener
object to listen for SerialEvent
Java Serial Port Communication
s. Interest in specific events may be expressed using thenotifyOnXXX
calls. The serialEvent
method of SerialPortEventListener
will be called with a SerialEvent
object describing the event. The current implementation only allows one listener per
SerialPort
. Once a listener is registered, subsequent call attempts to addEventListener
will throw a TooManyListenersException without effecting the listener already registered. All the events received by this listener are generated by one dedicated thread that belongs to the SerialPort object. After the port is closed, no more event will be generated. Another call to
open()
of the port's CommPortIdentifier
object will return a new CommPort
object, and the lsnr has to be added again to the new CommPort
object to receive event from this port. - Parameters:
lsnr
- TheSerialPortEventListener
object whoseserialEvent
method will be called with aSerialEvent
describing the event.- Throws:
java.util.TooManyListenersException
- If an initial attempt to attach a listener succeeds, subsequent attempts will throw TooManyListenersException without effecting the first listener.
removeEventListener
Deregisters event listener registered usingJava Serial Port Github
addEventListener
. This is done automatically at port close.
notifyOnDataAvailable
Expresses interest in receiving notification when input data is available. This may be used to drive asynchronous input. When data is available in the input buffer, this event is propagated to the listener registered usingaddEventListener
. The event will be generated once when new data arrive at the serial port. Even if the user doesn't read the data, it won't be generated again until next time new data arrive.
Java Connect To Serial Port
enable
- - true: enable notification
- false: disable notification
notifyOnOutputEmpty
Expresses interest in receiving notification when the output buffer is empty. This may be used to drive asynchronous output. When the output buffer becomes empty, this event is propagated to the listener registered usingaddEventListener
. The event will be generated after a write is completed, when the system buffer becomes empty again. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- false: disable notification
notifyOnCTS
Expresses interest in receiving notification when the CTS (Clear To Send) bit changes. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- >
notifyOnDSR
Expresses interest in receiving notification when the DSR (Data Set Ready) bit changes. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- false: disable notification
notifyOnRingIndicator
Expresses interest in receiving notification when the RI (Ring Indicator) bit changes. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- false: disable notification
notifyOnCarrierDetect
Expresses interest in receiving notification when the CD (Carrier Detect) bit changes. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- false: disable notification
notifyOnOverrunError
Expresses interest in receiving notification when there is an overrun error. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- false: disable notification
notifyOnParityError
Expresses interest in receiving notification when there is a parity error. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- false: disable notification
notifyOnFramingError
Expresses interest in receiving notification when there is a framing error. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- false: disable notification
notifyOnBreakInterrupt
Expresses interest in receiving notification when there is a break interrupt on the line. This notification is hardware dependent and may not be supported by all implementations.
enable
- - true: enable notification
- false: disable notification
Package | Class | Tree | Deprecated | Index | Help |