Java Serial Communication

Posted on by

Many of the games require no downloading or installing and can be played in the browser just like Flash but using Java while others require a free and safe plugin to be installed. And also have 100s of hacked roms, all playable on the site hassle free. El teke teke RGR offers games of every genre including RPG, Platform, Arcade, Sports, Puzzle, Strategy, Simulation, Board, Card, Multiplayer, etc. Whether you're going to video game design colleges for a video game design degree and need some inspiration, you're interested in reliving your childhood or you want to simply experience some of the greatest games ever made, Vizzed RGR is for everyone!

Communicate with Arduino through Java Arduino can send message to the serial port, use Java to get the serial messages. Code on Arduino: void setup. Download Java serial communication library. For 32-bit machines (Windows, Mac OX, and Linux), download from here. Feb 13, 2015  A easy way to communicate between your Arduino and PC. Use the built-in serial port of an Arduino to communicate with a Java program. In this demo, a. Aug 02, 2014  Today I'm going to show you how you can use the Java RxTx library for Serial Communication with Arduino. This small java program is just to identify from which port your Arduino is connected to the PC. So this will be a good start if you are going to use RxTx library with Arduino. Serial communication over UART is still a widely used technology. The Raspberry Pi is a nice example of the typical use case for this kind of communication where a PC exchanges data with an embedded system for debugging purposes. The RXTX library enables access to serial interfaces in Java applications. 1) for netbeans code click on link 2) for arduino code click on below link 3) for RXTX comm driver. The official API for serial communication in Java is the JavaComm API. This API is not part of the standard Java 2 version. Instead, an implementation of the API has to be downloaded separately. Unfortunately, JavaComm has not received much attention. The Java Communications API (also known as javax.comm) provides applications access to RS-232 hardware (serial ports) and limited access to IEEE-1284 (parallel ports), SPP mode. Implementations of the API are currently available for Solaris SPARC, Solaris x86, and Linux x86.

Active3 years, 4 months ago

I've been looking around for a Java API that can communicate with serial devices on Windows/Win32 but many of the APIs I've checked out are either for Linux, too outdated, or just had bad critics.

Can someone recommend one to me that they've tried or knows about that is easy to implement on Windows XP?

dsolimano
7,6923 gold badges41 silver badges57 bronze badges
SteveSteve
8057 gold badges22 silver badges30 bronze badges

5 Answers

I started looking for the same thing couple weeks ago, and I've been very happy with the multi-platform RXTX library so far. Works with any Windows, Linux and OS X. Has a very clean, easy to understand API.

edit: RXTX is also open source.

Murat AyferSerialMurat Ayfer
2,0946 gold badges23 silver badges26 bronze badges

Without reservation, I recommend Java Serial Port from serialio.com; I had significant stability problems with the Sun, IBM and RxTx serial package. SerialPort has been rock solid in production 24/7 for over 5 years.

They support the standard Java serial API, as well as their own alternative proprietary one. I would stick with the standard API though, unless you really need something theirs has that the standard one doesn't, just to keep your options open.

Lawrence DolLawrence Dol
48.5k23 gold badges126 silver badges178 bronze badges

I've been using PureJavaComm for the last five years or so. It's actively maintained, and is a pure Java (via JNA) implementation. RXTX has let me down because of a lack of maintenance and numerous subtle problems.

Jason SJason S
112k140 gold badges506 silver badges844 bronze badges

Java is notorious for its flaky serial I/O support. At a previous job, we tried both RXTX and SerialIO for an application that streamed data at 56kbps from a Teknic servo controller, and found them to gobble up the CPU quite a bit. Perhaps for apps that don't require continuous streaming from a serial port, both of these libraries are good, but we didn't feel that streaming I/O from a serial port should be eating a sustained 15-30% of the CPU on the machine when it is much needed for other threads in the JVM that need to be responsive.

Instead, we created a server in C++ that would read the stream of data from the serial port on the servo, transform/packetize it and send it to our Java app in XML over a socket connection. The CPU load on the serial I/O server in C++? Barely creeping into 1% at its worst.

There are certain things Java does well - serial I/O, in my opinion, isn't one of them, depending on the type of application..

Ultimately, you should take even what I said with a grain of salt, and try both, RXTX and SerialIO (which is dirt cheap, like $50 or so for the java version) and if they meet your needs, go with it. Personally, I'd stick with SerialIO because it is supported and actively worked on. RXTX, not so much.

Martin NaskovskiMartin Naskovski

I've written an open-source Java library because none of the existing ones fit my needs (outdated, closed-source, hard to modify, un-maintained, ..).

It's called JSerial, it's MIT-licensed, and you can learn more here: https://github.com/thibautd/JSerial !

Currently only supports Windows, but I have plans for supporting Linux. You can easily modify the native part with the latest Visual Studio if you need.

Thibaut D.Thibaut D.
9863 gold badges12 silver badges27 bronze badges

protected by CommunityJun 1 '15 at 19:16

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged javawindowsserial-port or ask your own question.

PackageClassTreeDeprecatedIndexHelp
FRAMESNO FRAMESAll Classes SUMMARY: NESTED FIELD CONSTR METHOD DETAIL: FIELD CONSTR METHOD

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.

Stress Crack – A crack that occurs without anything hitting the windshield, typically due to a large variation in the temperature. One situation that might cause stress cracks is when a car becomes overheated from sitting in the sun, and then the air conditioner is turned on high or the car is washed. Crack across windshield.

Author:
Jagane Sundar
See Also:
CommPort, CommPortIdentifier
Field Summary
static intDATABITS_5
5 data bit format.
static intDATABITS_6
6 data bit format.
static intDATABITS_7
7 data bit format.
static intDATABITS_8
8 data bit format.
static intFLOWCONTROL_NONE
Flow control off.
static intFLOWCONTROL_RTSCTS_IN
RTS/CTS flow control on input.
static intFLOWCONTROL_RTSCTS_OUT
RTS/CTS flow control on output.
static intFLOWCONTROL_XONXOFF_IN
XON/XOFF flow control on input.
static intFLOWCONTROL_XONXOFF_OUT
XON/XOFF flow control on output.
static intPARITY_EVEN
EVEN parity scheme.
static intPARITY_MARK
MARK parity scheme.
static intPARITY_NONE
No parity bit.
static intPARITY_ODD
ODD parity scheme.
static intPARITY_SPACE
SPACE parity scheme.
static intSTOPBITS_1
Number of STOP bits - 1.
static intSTOPBITS_1_5
Number of STOP bits - 1-1/2.
static intSTOPBITS_2
Number of STOP bits - 2.
Fields inherited from class javax.comm.CommPort
name
Constructor Summary
SerialPort()
Method Summary
abstract voidaddEventListener(SerialPortEventListener lsnr)
Registers a SerialPortEventListener object to listen for SerialEvents.
abstract intgetBaudRate()
Gets the currently configured baud rate.
abstract intgetDataBits()
Gets the currently configured number of data bits.
abstract intgetFlowControlMode()
Gets the currently configured flow control mode.
abstract intgetParity()
Get the currently configured parity setting.
abstract intgetStopBits()
Gets the currently defined stop bits.
abstract booleanisCD()
Gets the state of the CD (Carrier Detect) bit in the UART, if supported by the underlying implementation.
abstract booleanisCTS()
Gets the state of the CTS (Clear To Send) bit in the UART, if supported by the underlying implementation.
abstract booleanisDSR()
Gets the state of the DSR (Data Set Ready) bit in the UART, if supported by the underlying implementation.
abstract booleanisDTR()
Gets the state of the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.
abstract booleanisRI()
Gets the state of the RI (Ring Indicator) bit in the UART, if supported by the underlying implementation.
abstract booleanisRTS()
Gets the state of the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.
abstract voidnotifyOnBreakInterrupt(boolean enable)
Expresses interest in receiving notification when there is a break interrupt on the line.
abstract voidnotifyOnCarrierDetect(boolean enable)
Expresses interest in receiving notification when the CD (Carrier Detect) bit changes.
abstract voidnotifyOnCTS(boolean enable)
Expresses interest in receiving notification when the CTS (Clear To Send) bit changes.
abstract voidnotifyOnDataAvailable(boolean enable)
Expresses interest in receiving notification when input data is available.
abstract voidnotifyOnDSR(boolean enable)
Expresses interest in receiving notification when the DSR (Data Set Ready) bit changes.
abstract voidnotifyOnFramingError(boolean enable)
Expresses interest in receiving notification when there is a framing error.
abstract voidnotifyOnOutputEmpty(boolean enable)
Expresses interest in receiving notification when the output buffer is empty.
abstract voidnotifyOnOverrunError(boolean enable)
Expresses interest in receiving notification when there is an overrun error.
abstract voidnotifyOnParityError(boolean enable)
Expresses interest in receiving notification when there is a parity error.
abstract voidnotifyOnRingIndicator(boolean enable)
Expresses interest in receiving notification when the RI (Ring Indicator) bit changes.
abstract voidremoveEventListener()
Deregisters event listener registered using addEventListener.
abstract voidsendBreak(int millis)
Sends a break of millis milliseconds duration.
abstract voidsetDTR(boolean dtr)
Sets or clears the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.
abstract voidsetFlowControlMode(int flowcontrol)
Sets the flow control mode.
voidsetRcvFifoTrigger(int trigger)
Deprecated.This was advisory only.
abstract voidsetRTS(boolean rts)
Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.
abstract voidsetSerialPortParams(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

Sets the flow control mode.
Parameters:
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

Sets serial port parameters.
Parameters:
baudrate - If the baudrate passed in by the application is unsupported by the driver, the driver will throw an UnsupportedCommOperationException
dataBits -
  • 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
Throws:
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

Sets or clears the DTR (Data Terminal Ready) bit in the UART, if supported by the underlying implementation.
Parameters:
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

Sets or clears the RTS (Request To Send) bit in the UART, if supported by the underlying implementation.
Parameters:
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 a SerialPortEventListener object to listen for SerialEvents. Interest in specific events may be expressed using the notifyOnXXX 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 - The SerialPortEventListener object whose serialEvent method will be called with a SerialEvent 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 using 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 using addEventListener.

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.

Parameters:
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 using addEventListener. 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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Parameters:
enable -
  • true: enable notification
  • false: disable notification

Java Serial Port Example

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.

Parameters:
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.

Parameters:
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.

Parameters:
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.

Java Serial Communication Windows

Parameters:
enable -
  • true: enable notification
  • false: disable notification
PackageClassTreeDeprecatedIndexHelp
FRAMESNO FRAMESAll Classes SUMMARY: NESTED FIELD CONSTR METHOD DETAIL: FIELD CONSTR METHOD

Java Serial Communication Slow