It is not a tutorial; readers should know the basics of serial and parallel port programming. Here's how to test the SimpleRead.java example above on Solaris. If nothing is plugged in, you can't really read anything. You may be able to set your port to local loopback but it's been a long time since I've worked with serial ports and I've never used RXTX to do it. How To Ask Questions On JavaRanch.
27 Jul 2014CPOL
How to send data to device and receive data from device through COM-port
Introduction
Program gets list of available serial ports on the PC and prints it to console.
If there are no available serial ports, it prints an error message.
Else, it requests an input from user. After user typed a serial port which he wanted to use, program sends string 'Hurrah!' to this serial port.
Then, program waits for response from serial port and prints it to console.
This program must properly work in Win32/64 (98..8.1), Linux (x86, ARM, x86-64), Solaris (x86, x86-64) and MacOS 10.5+ (x86, x86-64, PPC, PPC64).
Code snippets
At first, import jssc package to code:
How to get a list of available serial ports via
SerialPortList
class:How to send data through COM-port via
SerialPort
class and receive data through COM-port via SerialPortEventListener
class:How to test it
You may use VSPE emulator to emulate COM-ports and Advanced Serial Port Monitor to listen data sent through COM-port ('Hurrah!') and send it to the COM-port.
If you would like to use the jSerialComm library in a completely asynchronous fashion, you can enable event-based callbacks via the addDataListener() method. This method takes your own custom callback object as a parameter which must implement the SerialPortDataListener interface. In any of the event-driven callback modes, you can register your own custom callback to be triggered upon certain serial I/O events.
The events you can listen for are:
- Data Available for Reading (LISTENING_EVENT_DATA_AVAILABLE)
- Data Writing Successful (LISTENING_EVENT_DATA_WRITTEN)
- Data Bytes Received (LISTENING_EVENT_DATA_RECEIVED)
- Fixed-Length Data Packet Received
- Byte- or Multibyte-Delimited Message Received
Data Available for Reading Example
In this case, your callback will be triggered whenever there is any data available to be read over the serial port. Once your callback is triggered, you can optionally call bytesAvailable() to determine how much data is available to read, and you must actually read the data using any of the
read()
or readBytes()
methods.The following example shows one way to use this event trigger:
Data Writing Successful Example
In this case, your callback will be triggered whenever all data you have written using any of the
write()
or writeBytes()
methods has actually been transmitted.Note that this mode of operation is only functional on Windows operating systems due to limitations in Linux's handling of serial writes.
The following example shows one way to use this event trigger:
Data Bytes Received Example
In this case, your callback will be triggered whenever some amount of data has actually been read from the serial port. This raw data will be returned to you within your own callback, so there is no further need to read directly from the serial port.
The following example shows one way to use this event trigger:
Fixed-Length Data Packet Received
In this case, your callback will be triggered whenever a set fixed amount of data has been read from the serial port. This raw data will be returned to you within your own callback, so there is no further need to read directly from the serial port.
Note that in this case, you must implement the SerialPortPacketListener which is itself a sub-class of the SerialPortDataListener.
The following example shows one way to use this event trigger. Notice that in this example, we actually define a qualified listener class instead of passing in an anonymous one as in the previous examples. Either method is fine, and both are shown simply for illustrative purposes:
Byte- or Multibyte-Delimited Message Received
In this case, your callback will be triggered whenever a message has been received based on a custom delimiter that you specify. This delimiter can contain one or more consecutive bytes and can indicate either the beginning or the end of a data packet. The raw data will be returned to you within your own callback, so there is no further need to read directly from the serial port.
Note that in this case, you must implement the SerialPortMessageListener which is itself a sub-class of the SerialPortDataListener.
![Connect Connect](/uploads/1/2/5/8/125872241/717902339.png)
The following example shows one way to use this event trigger. Notice that in this example, we actually define a qualified listener class instead of passing in an anonymous one as in the previous examples. Either method is fine, and both are shown simply for illustrative purposes: