Sun doesn't support serial communication extension for Windows
Official standpoint of Sun is that Windows version of Java Communication API (JavaComm) is EOLed and will not be continued. Implementations of the API are currently available only for Solaris SPARC, Solaris x86, and Linux x86.
The alternative is RXTX - Java library using a native implementation providing serial and parallel communication for Java for several operating systems (Windows included). This library is available under GPL license. Library provides the same API as Sun's Java Communication API so migration to RXTX library is very simple - you have only to change imports declarations.
Preparing Eclipse environment
Create new Java project
The first thing you need to do is creating new Java project in Eclipse (for example called JavaSerial).
Download and install RXTX library
Go to the RXTX download page and get the newest version of RXTX binaries compressed in ZIP file and destined for Windows operating system.
Go to the home directory of your recently created JavaSerial Project and create new directory called lib. Unzip downlowded RXTX package into temporary directory and copy RXTXcomm.jar, rxtxSerial.dll and rxtxParallel.dll files into lib directory. Dll files are located in Windows/i368-mingw32 directory of RXTX library.
Refresh JavaSerial project by hitting F5 key. Next click the right mouse button on the JavaSerial project and go to the Properties/Java Build Path. Select Libraries tab and click Add JARs button. Select RXTXcomm.jar and just click OK button.
Extend project classpath
If you want to run your JavaSerial project from Eclipse by clicking Run button you should enclose in classpath all copied *.dll files as a native libraries. To do this click the right mouse button on JavaSerial project and go to the Properties/Java Build Path. Select Source tab and expand JavaSerial/src tree element. Click on Native library location: (None) and click Edit button. After the Native Library Folder Configuration dialog window appears click on Workspace button and select JavaSerial/lib folder. After all start clicking OK buttons until you back to the begining.
Congartulations! Now you are ready to run and test you JavaSerial project in Eclipse.
Test your environment
It's time to make simple test if the created Eclipse environment is properly configured to run applications based on RXTX library for Windows.
Create new class named ListAvailablePorts.java and paste following code.
import gnu.io.CommPortIdentifier;
import java.util.Enumeration;
public class ListAvailablePorts {
public void list() {
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
while(ports.hasMoreElements())
System.out.println(((CommPortIdentifier)ports.nextElement()).getName());
}
public static void main(String[] args) {
new ListAvailablePorts().list();
}
}
Run your simple application by clicking Run button and if the environment is configured correctly you should see following output in console window:
Stable Library ========================================= Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 COM1 LPT1
Number of listed ports depends on number of available ports in your operating system. If you do not see presented output please repeat configuration process again.
Physical equipment
The choise of physical equipment which you need to use during development of RS232 communication application depends on your needs and capabilities of your avialable equipment. You should ask yourself following questions:
- do I have a RS232 specification of communication protocol between end-point device and application which I develop?
- is my laptop or PC equipped with serial ports (one or two?)
- do I have access to the end-point device or maybe I should write an emulator of end-point device?
Assuming that you have no access to the end-point device and you have only specification of RS232 communication protocol between device and application one of two following solutions should be considered.
Two serial ports wired together
If your laptop or PC is equipped with two serial ports you can simply buy very cheap DB9-DB9 cable and connect two serial ports together. You will be able to connect end-point device software emulator to the one of serial ports available in the operating system and the application to the second of available serial ports.
Virtual Serial Port Driver 6.0
If you don't have any serial port in your laptop or PC you should use one of available serial port emulators or simply buy USB adapter (if you have USB port available).
Virtual Serial Port Driver 6.0 is an excellent commercial serial port emulator for Windows operating system family which allows you to create pairs of serial ports. Serial ports within each pair could be connected together through the virutal RS232 wire. Then you can connect to the first emulated serial port an end-point device emulator and to the second serial port the application making a communication between these parts. It is great solution if you don't have access to the device to which you develop RS232 communication software and if you don't have any serial port on your laptop or PC - the only thing you should have is a specification of RS232 communication between device and application.
RS232 Application Example
Now we develop sample application based on RXTX library. Presented example will be very simple to show important communication issues and best practices. You can extend this sample to create more sophisticated applications.
Example communiaction main class
Following steps should be performed to establish serial port connection:
- receive CommPortIdentifier from the system with specific name (e.g. COM1) - received identifier could be in use by any other application - check it invoking portIdentifier.isCurrentlyOwned() method
- if received CommPortIdentifier is not in use by different application, you can open CommPort (which in our case is instance of SerialPort)
- the next step is to pass all setup communication parameters: baudrate, number of data bits, number of stop bits and possible parity bit
- the last step is to retrieve InputStream and OutputStream for sending and receiving raw bytes
As shown below for receiving raw bytes it is started new Thread which takes as parameter InputStream from serial port.
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
public class RS232Example {
public void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Port in use!");
} else {
// points who owns the port and connection timeout
SerialPort serialPort = (SerialPort) portIdentifier.open("RS232Example", 2000);
// setup connection parameters
serialPort.setSerialPortParams(
38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// setup serial port writer
CommPortSender.setWriterStream(serialPort.getOutputStream());
// setup serial port reader
new CommPortReceiver(serialPort.getInputStream()).start();
}
}
public static void main(String[] args) throws Exception {
// connects to the port which name (e.g. COM1) is in the first argument
new RS232Example().connect(args[0]);
// send HELO message through serial port using protocol implementation
CommPortSender.send(new ProtocolImpl().getMessage("HELO"));
}
}
Serial port data receiver
As you can see CommPortReceiver is a Thread instance with run() implemented method which is performed in inifite loop until java application is broken by the user.
While there is opened InputStream by the end-point device in.read() method blocks and waits for incoming raw bytes. Each byte is passed to the protocol manager which handles all protocol issues (for example recognizes messages from raw bytes). When end-point device breaks the opened InputStream then in.read() method starts to return -1 value without blocking. Because waiting for external device in infinite loop without in.read() blocking method could be destructive for performence of operating system sleep method is introduced to decrease number of in.read() executions.
import java.io.IOException;
import java.io.InputStream;
public class CommPortReceiver extends Thread {
InputStream in;
Protocol protocol = new ProtocolImpl();
public CommPortReceiver(InputStream in) {
this.in = in;
}
public void run() {
try {
int b;
while(true) {
// if stream is not bound in.read() method returns -1
while((b = in.read()) != -1) {
protocol.onReceive((byte) b);
}
protocol.onStreamClosed();
// wait 10ms when stream is broken and check again
sleep(10);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Protocol
Protocol is used to handle all incoming bytes, segregate them and create recognized messages. The interface of Protocol implementation allows to inform protocol manager about each incoming byte and about broken stream situation.
public interface Protocol {
// protocol manager handles each received byte
void onReceive(byte b);
// protocol manager handles broken stream
void onStreamClosed();
}
In our case protocol implementation is simple: message is always ended with new line character. Colleaction of received bytes creates String message which could be recognized.
When new message is recognized (new line character appears or stream closes) then onMessage() method is called to handle recognized message. If you develop more complicated example (and I am sure you will do) logic of triggered actions depending on recognized message should be placed in external class (some kind of interpreter).
In our case when HELO message comes then OK message should be sent as a response. If OK message is received then acknowledge should be sent as a response (OK ACK).
public class ProtocolImpl implements Protocol {
byte[] buffer = new byte[1024];
int tail = 0;
public void onReceive(byte b) {
// simple protocol: each message ends with new line
if (b=='\n') {
onMessage();
} else {
buffer[tail] = b;
tail++;
}
}
public void onStreamClosed() {
onMessage();
}
/*
* When message is recognized onMessage is invoked
*/
private void onMessage() {
if (tail!=0) {
// constructing message
String message = getMessage(buffer, tail);
System.out.println("RECEIVED MESSAGE: " + message);
// this logic should be placed in some kind of
// message interpreter class not here
if ("HELO".equals(message)) {
CommPortSender.send(getMessage("OK"));
} else if ("OK".equals(message)) {
CommPortSender.send(getMessage("OK ACK"));
}
tail = 0;
}
}
// helper methods
public byte[] getMessage(String message) {
return (message+"\n").getBytes();
}
public String getMessage(byte[] buffer, int len) {
return new String(buffer, 0, tail);
}
}
Serial port data sender
import java.io.IOException;
import java.io.OutputStream;
public class CommPortSender {
static OutputStream out;
public static void setWriterStream(OutputStream out) {
CommPortSender.out = out;
}
public static void send(byte[] bytes) {
try {
System.out.println("SENDING: " + new String(bytes, 0, bytes.length));
// sending through serial port is simply writing into OutputStream
out.write(bytes);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Running RS232 example
In order to run serial port communication example you need to run two instances of developed application:
- the first as an end-point device which is connected to the port COM1
- the second as the application connected to the port COM2
Moreover you have to have connected COM1 and COM2 port together (using DB9-DB9 cable or using virtual wire from emulating serial port software).
Follow steps below to run two instance of application in Eclipse environment with different command line parameters:
- Click right mouse button on RS232Example class and select Run As/Open Run Dialog
- Choose Arguments tab and type in Program arguments text area the first parameter COM1
- Click Apply button and then Run button
You should see following output in output console which means that the first instance has been strated and sent HELO command to the not bound serial port.
Stable Library ========================================= Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 SENDING: HELO
Next repeat above steps but type COM2 in Program arguments text area. Switch between output consoles using button with monitor icon and compare results. You should have two output consolse with results as presented below.
The first console (COM1 - 1 instance)
After the second instance had been started HELO message was received. Then OK message was sent as a response. Then acknowledge of OK message was received.
Stable Library ========================================= Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 SENDING: HELO RECEIVED MESSAGE: HELO SENDING: OK RECEIVED MESSAGE: OK ACK
The second console (COM2 - 2 instance)
The second instance sent HELO message receiving OK message. Then acknowledge of OK message was sent to the first instance.
Stable Library ========================================= Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 SENDING: HELO RECEIVED MESSAGE: OK SENDING: OK ACK
Conclusions
When you receive information about that Sun doesn't support serial communication for Windows system environment you starts desperately searching alternative. In consequence there is very good free alternative which is used in several projects (and works fine) - RXTX library. Moreover you can in simple way prepare development environment to work on your project effectively not having end-point device or even serial ports installed in your laptop or PC.
50 comments to "RS232 in Java for Windows"
Mark Dorff on March 6th, 2009, 18:56
Great tutorial! Thanks a lot! You saved 2 days from my life :)
tony gair on March 23rd, 2009, 21:38
Cheers you saved me a lot of time and head scratching too. Now I just got to reverse engineer some messages :)
Have you got any more java examples?
peter on April 8th, 2009, 00:22
Fantastic I have implement part of this using Sun's Example code and it was very confusing to say the least! I was about to embark on the second part when I found this wonderful example that sets it out clearly
Regards Peter
Todd on May 2nd, 2009, 14:25
Good work, cleared up a lot of confusing I have had the past 3 days with Java and Serial comm. Thanks again.
Myro on May 6th, 2009, 17:48
Cheers man. Good job!
Out of topic: You could work on your English a bit ... more :-)
Pedro Quina on May 10th, 2009, 03:54
Tks ...... I was trying it while two days.
300OSV on May 17th, 2009, 13:11
Thanks a lot! Very helpfull ....
sharkscream on May 21st, 2009, 11:47
Thanks alot! Very clear and easy to understand. But one more stupid question... How can I close the connection afterwards?
Sebastian on May 21st, 2009, 14:20
There is a couple things to do:
1) Close all streams invoking out.close() on OutputStream and in.close() on InputStream
2) Invoke commPort.close() on CommPortIdentifier
Utsav on June 2nd, 2009, 13:52
Hi,
I have tried your code but it does not seem to work on my computer. I have downloaded all the files and packages mentioned here but it does not give me an output on running ListAvailablePorts.java..
I understand to have setup the environment exactly as mentioned here...
Please help....
Thanks...
Utsav
Sebastian on June 2nd, 2009, 20:44
Hi,
Please paste the result of ListAvailablePorts.java execution. Is there any exception thrown? Do you have available COM or LPT ports - you might check it in BIOS options...
Utsav on June 3rd, 2009, 07:29
Hi Sebastian,
Thank you for your immediate response.
The output is as follows :
C:\jdk1.5.0_04\bin\java.exe -classpath "C:\Introduction\R&D\lib\build;C:\Introduction\R&D\lib\core;C:\Introduction\R&D\lib\optional;C:\Introduction\R&D\lib\activation.jar;C:\Introduction\R&D\lib\classes111.zip;C:\Introduction\R&D\lib\classes12.jar;C:\Introduction\R&D\lib\classes12.zip;C:\Introduction\R&D\lib\commons-el.jar;C:\Introduction\R&D\lib\commons-fileupload-1.0.jar;C:\Introduction\R&D\lib\commons-logging.jar;C:\Introduction\R&D\lib\iText.jar;C:\Introduction\R&D\lib\jasper-compiler.jar;C:\Introduction\R&D\lib\jasper-compiler.jar_Old;C:\Introduction\R&D\lib\jasper-runtime.jar;C:\Introduction\R&D\lib\jCharts-0.7.5.jar;C:\Introduction\R&D\lib\jcommon-1.0.0-rc1.jar;C:\Introduction\R&D\lib\jfreechart-1.0.0-rc1.jar;C:\Introduction\R&D\lib\jfreereport-0.8.4_10-all.jar;C:\Introduction\R&D\lib\jxl.jar;C:\Introduction\R&D\lib\log4j.jar;C:\Introduction\R&D\lib\mail.jar;C:\Introduction\R&D\lib\msbase.jar;C:\Introduction\R&D\lib\mssqlserver.jar;C:\Introduction\R&D\lib\msutil.jar;C:\Introduction\R&D\lib\ojdbc14.jar;C:\Introduction\R&D\lib\ojsp.jar;C:\Introduction\R&D\lib\ojsp.jarNotUsed;C:\Introduction\R&D\lib\ojsputil.jar;C:\Introduction\R&D\lib\ojsputil.jarNotUsed;C:\Introduction\R&D\lib\oracle.jar;C:\Introduction\R&D\lib\quartz-1.6.0.jar;C:\Introduction\R&D\lib\quartz-all-1.6.0.jar;C:\Introduction\R&D\lib\readme.txt;C:\Introduction\R&D\lib\servlet.jar;C:\Introduction\R&D\lib\VERSIONS.txt;C:\Introduction\R&D\lib\activation.jar;C:\Introduction\R&D\lib\classes12.jar;C:\Introduction\R&D\lib\comm.jar;C:\Introduction\R&D\lib\commons-el.jar;C:\Introduction\R&D\lib\commons-fileupload-1.0.jar;C:\Introduction\R&D\lib\commons-logging.jar;C:\Introduction\R&D\lib\iText.jar;C:\Introduction\R&D\lib\jasper-compiler.jar;C:\Introduction\R&D\lib\jasper-runtime.jar;C:\Introduction\R&D\lib\jCharts-0.7.5.jar;C:\Introduction\R&D\lib\jcommon-1.0.0-rc1.jar;C:\Introduction\R&D\lib\jfreechart-1.0.0-rc1.jar;C:\Introduction\R&D\lib\jfreereport-0.8.4_10-all.jar;C:\Introduction\R&D\lib\jxl.jar;C:\Introduction\R&D\lib\log4j.jar;C:\Introduction\R&D\lib\mail.jar;C:\Introduction\R&D\lib\msbase.jar;C:\Introduction\R&D\lib\mssqlserver.jar;C:\Introduction\R&D\lib\msutil.jar;C:\Introduction\R&D\lib\ojdbc14.jar;C:\Introduction\R&D\lib\ojsp.jar;C:\Introduction\R&D\lib\ojsputil.jar;C:\Introduction\R&D\lib\oracle.jar;C:\Introduction\R&D\lib\quartz-1.6.0.jar;C:\Introduction\R&D\lib\quartz-all-1.6.0.jar;C:\Introduction\R&D\lib\servlet.jar;C:\Introduction\Simple_RTJ\simplertj-1.4.2\lib\ClassLinker.jar;C:\Introduction\Simple_RTJ\simplertj-1.4.2\lib\java.jar;C:\Introduction\Simple_RTJ\simplertj-1.4.2\lib\javax.jar;C:\Introduction\Simple_RTJ\simplertj-1.4.2\lib;C:\jdk1.5.0_04\lib\comm.jar;C:\jdk1.5.0_04\lib\dt.jar;C:\jdk1.5.0_04\lib\htmlconverter.jar;C:\jdk1.5.0_04\lib\jconsole.jar;C:\jdk1.5.0_04\lib\tools.jar;C:\jdk1.5.0_04\jre\lib\rt.jar;C:\jdk1.5.0_04\jre\lib\deploy.jar;C:\jdk1.5.0_04\jre\lib\javaws.jar;C:\jdk1.5.0_04\jre\lib\jce.jar;C:\jdk1.5.0_04\jre\lib\jsse.jar;C:\jdk1.5.0_04\jre\lib\plugin.jar;C:\jdk1.5.0_04\jre\lib\charsets.jar;C:\jdk1.5.0_04\lib;C:\jdk1.5.0_04\jre\lib\ext\dnsns.jar;C:\jdk1.5.0_04\jre\lib\ext\localedata.jar;C:\jdk1.5.0_04\jre\lib\ext\sunjce_provider.jar;C:\jdk1.5.0_04\jre\lib\ext\sunpkcs11.jar;C:\jdk1.5.0_04\jre\lib\im\indicim.jar;C:\jdk1.5.0_04\jre\lib\im\thaiim.jar;C:\jdk1.5.0_04\jre\lib\security\US_export_policy.jar;C:\jdk1.5.0_04\jre\lib\security\local_policy.jar;C:\jdk1.5.0_04\jre\lib\rt.jar;C:\jdk1.5.0_04\jre\lib\deploy.jar;C:\jdk1.5.0_04\jre\lib\javaws.jar;C:\jdk1.5.0_04\jre\lib\jce.jar;C:\jdk1.5.0_04\jre\lib\jsse.jar;C:\jdk1.5.0_04\jre\lib\plugin.jar;C:\jdk1.5.0_04\jre\lib\charsets.jar;C:\Program Files\JControl\lib\xpp3.jar;C:\Program Files\JControl\lib\jibx-run.jar;C:\Program Files\JControl\lib\qdox-1.6.3.jar;C:\Program Files\JControl\lib\commons-logging.jar;C:\Program Files\JControl\jre\win32\lib\rt.jar;C:\Program Files\JControl\jre\win32\lib\javaws.jar;C:\Program Files\JControl\jre\win32\lib\jce.jar;C:\Program Files\JControl\jre\win32\lib\jsse.jar;C:\Program Files\JControl\jre\win32\lib\management-agent.jar;C:\Program Files\JControl\jre\win32\lib\plugin.jar;C:\Program Files\JControl\jre\win32\lib\resources.jar;C:\Program Files\JControl\jre\win32\lib\deploy.jar;C:\Program Files\JControl\lib\shared\comm.jar;C:\Program Files\JControl\lib\shared\ftd2xxj.jar;C:\Program Files\JControl\lib\shared\jd2xx.jar;C:\Program Files\JControl\lib\shared\junit-4.1.jar;C:\Program Files\JControl\lib\shared\log4j.jar;C:\Program Files\JControl\lib\shared\org.eclipse.core.boot_3.1.0.jar;C:\Program Files\JControl\lib\shared\org.eclipse.core.resources_3.1.0.jar;C:\Program Files\JControl\lib\shared\org.eclipse.core.runtime_3.1.1.jar;C:\Program Files\JControl\lib\shared\org.eclipse.jdt.core_3.2.0.v_671.jar;C:\Program Files\JControl\lib\shared\org.eclipse.jface_3.1.1.jar;C:\Program Files\JControl\lib\shared\org.eclipse.ui.editors_3.1.0.jar;C:\Program Files\JControl\lib\shared\org.eclipse.ui.forms_3.1.0.jar;C:\Program Files\JControl\lib\shared\org.eclipse.ui.views_3.2.101.M20080207-0800.jar;C:\Program Files\JControl\lib\shared\org.eclipse.ui.workbench_3.1.1.jar;C:\Program Files\JControl\lib\shared\RXTXcomm.jar;C:\Program Files\JControl\lib\win32\swt.jar;C:\Introduction\jars\activation.jar;C:\Introduction\jars\ant.jar;C:\Introduction\jars\ant-junit.jar;C:\Introduction\jars\ant-launcher.jar;C:\Introduction\jars\antlr-2.7.6.jar;C:\Introduction\jars\ant-trax.jar;C:\Introduction\jars\aopalliance.jar;C:\Introduction\jars\asm-2.2.3.jar;C:\Introduction\jars\asm-commons-2.2.3.jar;C:\Introduction\jars\asm-util-2.2.3.jar;C:\Introduction\jars\aspectjrt.jar;C:\Introduction\jars\aspectjweaver.jar;C:\Introduction\jars\axis.jar;C:\Introduction\jars\bcprov-jdk14-128.jar;C:\Introduction\jars\BlackBox.jar;C:\Introduction\jars\bootstrap.jar;C:\Introduction\jars\bsh-2.0b4.jar;C:\Introduction\jars\c3p0-0.9.1.1.jar;C:\Introduction\jars\catalina.jar;C:\Introduction\jars\catalina-ant.jar;C:\Introduction\jars\catalina-cluster.jar;C:\Introduction\jars\catalina-host-manager.jar;C:\Introduction\jars\catalina-manager.jar;C:\Introduction\jars\catalina-optional.jar;C:\Introduction\jars\catalina-storeconfig.jar;C:\Introduction\jars\cglib-nodep-2.1_3.jar;C:\Introduction\jars\charsets.jar;C:\Introduction\jars\classes12.jar;C:\Introduction\jars\ClassLinker.jar;C:\Introduction\jars\comm.jar;C:\Introduction\jars\commons-beanutils.jar;C:\Introduction\jars\commons-collections.jar;C:\Introduction\jars\commons-collections-3.1.jar;C:\Introduction\jars\commons-daemon.jar;C:\Introduction\jars\commons-dbcp-1.2.1.jar;C:\Introduction\jars\commons-digester.jar;C:\Introduction\jars\commons-digester-1.7.jar;C:\Introduction\jars\commons-el.jar;C:\Introduction\jars\commons-fileupload-1.0.jar;C:\Introduction\jars\commons-logging.jar;C:\Introduction\jars\commons-logging-api.jar;C:\Introduction\jars\commons-modeler.jar;C:\Introduction\jars\commons-modeler-1.1.jar;C:\Introduction\jars\commons-pool-1.2.jar;C:\Introduction\jars\commons-validator-1.1.4.jar;C:\Introduction\jars\countries.jar;C:\Introduction\jars\deploy.jar;C:\Introduction\jars\dnsns.jar;C:\Introduction\jars\dt.jar;C:\Introduction\jars\ejb.jar;C:\Introduction\jars\hessian-3.0.20.jar;C:\Introduction\jars\htmlconverter.jar;C:\Introduction\jars\HTMLWriter.jar;C:\Introduction\jars\iText.jar;C:\Introduction\jars\itext-1.4.8.jar;C:\Introduction\jars\jasper-compiler.jar;C:\Introduction\jars\jasper-compiler-jdt.jar;C:\Introduction\jars\jasper-runtime.jar;C:\Introduction\jars\java.jar;C:\Introduction\jars\javaws.jar;C:\Introduction\jars\javax.jar;C:\Introduction\jars\javax.jms.jar;C:\Introduction\jars\jce.jar;C:\Introduction\jars\jCharts-0.7.5.jar;C:\Introduction\jars\jcommon-1.0.0-rc1.jar;C:\Introduction\jars\jcommon-1.0.13.jar;C:\Introduction\jars\jconsole.jar;C:\Introduction\jars\jdbc2_0-stdext.jar;C:\Introduction\jars\jfreechart-1.0.0-rc1.jar;C:\Introduction\jars\jfreereport-0.8.4_10-all.jar;C:\Introduction\jars\jmx.jar;C:\Introduction\jars\JSON.jar;C:\Introduction\jars\jsp-api.jar;C:\Introduction\jars\jsr173_1.0_api.jar;C:\Introduction\jars\jsse.jar;C:\Introduction\jars\jstl.jar;C:\Introduction\jars\jta.jar;C:\Introduction\jars\junit.jar;C:\Introduction\jars\jxl.jar;C:\Introduction\jars\local_policy.jar;C:\Introduction\jars\localedata.jar;C:\Introduction\jars\log4j.jar;C:\Introduction\jars\log4j-1.2.11.jar;C:\Introduction\jars\log4j-1.2.9.jar;C:\Introduction\jars\mail.jar;C:\Introduction\jars\management-agent.jar;C:\Introduction\jars\msbase.jar;C:\Introduction\jars\mssqlserver.jar;C:\Introduction\jars\msutil.jar;C:\Introduction\jars\naming-factory.jar;C:\Introduction\jars\naming-factory-dbcp.jar;C:\Introduction\jars\naming-resources.jar;C:\Introduction\jars\ojdbc14.jar;C:\Introduction\jars\ojsp.jar;C:\Introduction\jars\ojsputil.jar;C:\Introduction\jars\optional.jar;C:\Introduction\jars\oracle.jar;C:\Introduction\jars\plugin.jar;C:\Introduction\jars\poi-2.5.1.jar;C:\Introduction\jars\quartz-1.6.0.jar;C:\Introduction\jars\quartz-all-1.6.0.jar;C:\Introduction\jars\resources.jar;C:\Introduction\jars\rssutils.jar;C:\Introduction\jars\rt.jar;C:\Introduction\jars\saaj.jar;C:\Introduction\jars\servlet.jar;C:\Introduction\jars\servlet-api.jar;C:\Introduction\jars\servlets-default.jar;C:\Introduction\jars\servlets-invoker.jar;C:\Introduction\jars\servlets-webdav.jar;C:\Introduction\jars\SMSSender.jar;C:\Introduction\jars\spring.jar;C:\Introduction\jars\spring-agent.jar;C:\Introduction\jars\spring-aop.jar;C:\Introduction\jars\spring-aspects.jar;C:\Introduction\jars\spring-beans.jar;C:\Introduction\jars\spring-context.jar;C:\Introduction\jars\spring-core.jar;C:\Introduction\jars\spring-dao.jar;C:\Introduction\jars\spring-hibernate2.jar;C:\Introduction\jars\spring-hibernate3.jar;C:\Introduction\jars\spring-ibatis.jar;C:\Introduction\jars\spring-jca.jar;C:\Introduction\jars\spring-jdbc.jar;C:\Introduction\jars\spring-jdo.jar;C:\Introduction\jars\spring-jms.jar;C:\Introduction\jars\spring-jmx.jar;C:\Introduction\jars\spring-jpa.jar;C:\Introduction\jars\spring-mock.jar;C:\Introduction\jars\spring-portlet.jar;C:\Introduction\jars\spring-remoting.jar;C:\Introduction\jars\spring-struts.jar;C:\Introduction\jars\spring-support.jar;C:\Introduction\jars\spring-tomcat-weaver.jar;C:\Introduction\jars\spring-toplink.jar;C:\Introduction\jars\spring-web.jar;C:\Introduction\jars\spring-webmvc.jar;C:\Introduction\jars\SprMVC.jar;C:\Introduction\jars\standard.jar;C:\Introduction\jars\struts.jar;C:\Introduction\jars\sunjce_provider.jar;C:\Introduction\jars\sunmscapi.jar;C:\Introduction\jars\sunpkcs11.jar;C:\Introduction\jars\tomcat-ajp.jar;C:\Introduction\jars\tomcat-coyote.jar;C:\Introduction\jars\tomcat-http.jar;C:\Introduction\jars\tomcat-i18n-en.jar;C:\Introduction\jars\tomcat-i18n-es.jar;C:\Introduction\jars\tomcat-i18n-fr.jar;C:\Introduction\jars\tomcat-i18n-ja.jar;C:\Introduction\jars\tomcat-juli.jar;C:\Introduction\jars\tomcat-util.jar;C:\Introduction\jars\tools.jar;C:\Introduction\jars\US_export_policy.jar;C:\Introduction\jars\wsdl4j.jar;C:\Introduction\jars\xercesImpl.jar;C:\Introduction\jars\xml-apis.jar;C:\Introduction\jars;C:\Introduction\SMSrarextract\trying\lib\ext\RXTXcomm.jar;C:\Introduction\SMSrarextract\trying\lib\ext;C:\Introduction\SMSrarextract\trying" ListAvailablePorts
Finished executing
The thing is that the condition " while(ports.hasMoreElements())"
gets a false value due to which the code seems to be failing...
I have checked it in the BIOS and it shows that the COM and LPT ports are active.
It does not throw any exception...
By the way, could you please mail me the entire "GNU" package that you have imported....You can send that to my mail id...if possible..
I guess, the package can also be a reason for the failure....
Awaiting your reply.
In anticipation of your reply.
Thank You.
Utsav
Utsav on June 3rd, 2009, 14:18
Hello Sebastian,
Sorry to write in again..
Does this code require any specific system configuration for the ports to be detected?
The scenario has turned like this...
The code works fine on another laptop which is a Pentium 4 while my machine is a Pentium 3. Are there any values that i need to set for the ports in order for them to be detected?Or are the default values fine?
Another thing is that I have a serial mouse connected to my Pentium3 machine and it is working fine. So that implies that the COM port is active. but it is still not getting detected using the code.
Awaiting your reply to both my posts.
Thanks again...
Utsav.
Sebastian on June 3rd, 2009, 20:08
Hello,
I think you could try to use Serial Virtual Port Driver to see if emulated serial ports are recognized by rxtx library.
Did you link native rxtx libraries to you app (dll file)?
I will send you libs that I use.
Greets
Sebastian
Sid on June 4th, 2009, 06:52
Hey Thanks for the turorial.
I am running this on NetBeans 6.5. I put the dll and the jar in the JDK 1.5 package. I have created virtual ports COM1 and COM2 that were detected using the ListAvailable ports.
I have created all the specidfied java files. I clean build it and its successfull. But when I run it i get the following error.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Exception in thread "main" gnu.io.PortInUseException: Unknown Application
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)
at RS232Example.connect(RS232Example.java:13)
at RS232Example.main(RS232Example.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
All help is much appreciated.
Thanks
Sid
Sid on June 4th, 2009, 06:54
Rs232Example line 13
SerialPort serialPort = (SerialPort) portIdentifier.open("RS232Example", 2000);
RS232Exmple line 30
new RS232Example().connect(args[0]);
Utsav on June 4th, 2009, 07:52
Hello Sebastian,
I have linked the Serial Port and the Parallel Port Dll's.
Your email is awaited.
Thanks again...
Utsav.
Utsav on June 4th, 2009, 13:12
Hi Sebastian,
I downloaded the Serial Ports emulators and they work fine on the laptop. But again, the result is same on my machine, the Pentium 3. Also, when i tested the code on a Pentium 4 desktop, it did not work.
I am really confused with the configurations here...I have tried and checked every possible value that i could check regarding the COM ports on approximately 5 different machines with two of them being Pentium4 desktops, and 3 being Pentium3 desktops. All of them match with the laptop. but the only place where they differ is the successful output from the code.
I am awaiting your email containing all the lib files and the gnu package as well...
by the way, is it possible for you to come online, or give me your personal email id so that we both can have a chat, so that this issue can be sorted out.....
awaiting the email and all the details required in the email.
kindly reply asap so that i can successfully execute the code...
in anticipation of your reply.....
Thank You.....
Utsav
:(
Sid on June 4th, 2009, 19:04
What if I want to send a Byte array of 6 bytes and recieve 3 bytes. No strings involved?
Sebastian on June 4th, 2009, 20:41
@Sid
Modify ProtocolImpl by adding byte counter. If you are sure that application receives constant number of bytes, you should invoke onMessage() method when the counter equlas 3. Sending 6 bytes is simpler. You simply pass 6-elements byte array to send method.
@Ustav
I sent you e-mail with libraries I had used before.
Utsav on June 5th, 2009, 11:04
the code now works man....
i guess, the jar file that i had downloaded did not meet the requirement.
Thank you for all your support....
Utsav
Sid on June 5th, 2009, 14:41
Thanks a lot Sebastian this tutorial saved my job!
Any pointers on how to disconnect a port....(disabling further sending and recieving.....)?
Sebastian on June 5th, 2009, 14:59
There is a couple things to do:
1) Close all streams invoking out.close() on OutputStream and in.close() on InputStream
2) Invoke commPort.close() on CommPortIdentifier
Deon on June 9th, 2009, 11:41
Thanks for your tutorial. You saved me a stack of heart ache regarding serial ports. Thanks again!
BTW any pointers as to how I can periodically confirm if the device
(A fire control panel in my case) is still alive on the com port ?
If it had a IP address "pinging" would be sufficient but a device com port ?
Regards
Deon Botha
South Africa
Tor-Erik on June 22nd, 2009, 13:38
Can anybody please tell me how to read the documentation for the code? e.g Javadoc. Also how can i "add" the javadoc to eclipse, so that I can just press f2 to read the documentation.
Regards
Tor-Erik
Norway
Tim on June 29th, 2009, 12:09
Hi Sebastian.
Thanks for a clear tutorial, this was very helpful. Could you give me some pointers on how to open multiple serial ports using the above examples.
Thanks for your time
Tim
Sebastian on June 29th, 2009, 21:39
Hi Tim,
The way how to open multiple serial ports in your application depends on your communication model. For example you can use multiple serial ports for sending bytes and receiving bytes simultaneously - then you should consider multithreaded architecture - one thread per writer and reader. If you send bytes to all devices sequently, then you should consider opening one thread for sending and multiple threads for receiving bytes.
Please give me more details... then maybe I could help...
Regards
Sebastian
Fikri on July 22nd, 2009, 12:43
Thanks a lot.. It is much more easier to use compared to the javax.comm package. Your code works fine in both eclipse and Netbean IDE. Just a couple of questions for you. Is the receiving of the data is in polling mode or event driven? From what i see (might be wrong) it is working base on polling mechanism. Is it possible to use event-driven? Thanks a lot.
Sebastian on July 23rd, 2009, 09:17
Hi Fikri,
Presented solution works in event-driven mode. Note that CommPortReceiver works as thread and it is listening to the incomming bytes from connected device (so it is consuming events). Every byte is passed to higher abstraction: implementation of the protocol. You can observe that ProtocolImpl contains two significiant methods: onReceive and onMessage. These methods corresponds to event consumers which revceive events.
BR
Sebastian
Dave Pfaltzgraff on August 19th, 2009, 00:22
Would it be possible for you to post the corresponding procedure for installing this under NetBeans? I tried and failed miserably with everything related to the gnu.io module.
I'm sure it's something simple, but right now I new to everything!
Thanks,
Dave
Bob on August 19th, 2009, 15:34
http://java.sun.com/developer/Books/javaprogramming/cookbook/11.pdf
Piyush on August 24th, 2009, 13:50
Wow.... this solution worked.
Amazing.
thanks a lot for this help
Piyush on August 24th, 2009, 17:01
Hi Sebastian,
Your tutorial helped me a lot for configuring the eclipse environment. While sending the message to the other device, I have to send data a mixture of hex numbers and bytes. Please explain me, in which section shall I make the changes.
regards
Piyush
Sebastian on August 25th, 2009, 01:34
Hi Piyush,
You can simply send mixture of hex and byte values using this way:
CommPortSender.send(new byte[] {'A', 'C', 'K', 0x20, 0x20, 0x0F, 0xFF});
BR
Sebastian
pohcb_sonic on August 26th, 2009, 03:04
Hi, I'm new to Java and I have the following errors in Eclipse 3.2 when I run the program:
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Exception in thread "main" gnu.io.PortInUseException: Unknown Application
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)
at RS232Example.connect(RS232Example.java:13)
at RS232Example.main(RS232Example.java:30)
Rs232Example line 13
-------------------------
SerialPort serialPort = (SerialPort) portIdentifier.open("RS232Example", 2000);
RS232Exmple line 30
---------------------------
new RS232Example().connect(args[0]);
Can kindly assist?
Sebastian on August 26th, 2009, 08:54
Hi,
I assume you have downloaded wrong libraries. Please send me a private email using contact form and I send you libraries you should use.
BR
Sebastian
Varghese on September 3rd, 2009, 19:45
Hi
I am planning to start a RS232 comms project in windows os and I did downloaded the RXTX zip file from the web site.But it won't unzip,shows some error.Will you please email a small RS232 program with all relevant documents(where to download the.jar file and othe things) to my personal email please
Varghese
Tzu on October 1st, 2009, 11:24
that\\\'s awesome !! thank you ..
João Pereira on October 22nd, 2009, 00:56
Hello there,
I am a post graduated student from FEUP, Portugal. This mail is just to let you know that I used your RS232 interface driver (after some modifications) in my thesis' project. So, thank you very much! It spared me a lot of time during the project development. As a rewarding action i mentioned your work in my thesis acknowledgements (http://feupload.fe.up.pt/get/qkD0RsH5xekWzDp) . Keep up the good and helpful work!
Cheers!
João Pereira
Billikin on November 17th, 2009, 16:13
Hi,
Receiving is working fine, but I still can't cope with closing serial port.
Any detailed ideas??
Sebastian on November 17th, 2009, 20:31
You should:
1) Close all streams by invoking out.close() on OutputStream and in.close() on InputStream
2) Invoke commPort.close() on CommPortIdentifier
I've used this method in several projects and I think it works fine.
Billikin on November 18th, 2009, 12:04
P.S. "Contact me" section is not working properly on Your website
Sebastian on November 19th, 2009, 11:01
@Billikin
Thank you very much for pointing me the bug. Now contact form should work properly. There was somthing wrong with firewall.
Emre on April 20th, 2010, 11:26
Hi Sebasti.
After running ListAvailablePorts, I got this output below.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
ports.hasMoreElements() methot returns 0, namely I cannot see the port names that your output gives or nothing else ports.
COM1
LPT1
I have lots of usb, com and a port in my computer, i can use them properly.
What are you thinking about?
Thanks in advance.
Sebastian on April 21st, 2010, 00:32
@Emre
First of all, be sure you have your COM ports enabled in BIOS.
If you want to use USB port, you should buy USB-RS232 adapter which converts USB signals to RS232 specification.
Emre on April 23rd, 2010, 19:49
Finally, I got this output with win xp.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
COM3
But with 64 bit vista I get the same output.
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Anyway, I work with veteran xp.
I soppose COM3 is modem port, but I use a rs232 to usb converter that you said.
By the way,
I want to comminicate with a point of sale that is verifone nurit 8400 http://www.verifone.com/PDF/NURIT_8400_Acq_Sheet.pdf.
Now, this pos is used like;
Somebody has developed a software on NURIT_8400. It gets some inputs from the pin pad which is stored in date base that is in a simple laptop pc, and gives some outputs to its screen. (Pin pad is used by a human so the aim is passing on this job to a machine)
Later, it is going to be used like;
NURIT_8400 provides a rs232 port.
I think the simplest way to comminicate with this pos is rs232.
I found a manuel that tells about the frame formats of NURIT_8400. I focused this reference these days.
Can you give me some hints, clues about these things that I mentioned? Am I on the right way?
Anything is appreciated.
Sebastian on April 27th, 2010, 21:56
In your place I would go on this site: http://www.verifonedevnet.com/im2/verifone/devnet/dnProgramView.do?pageTypeId=109140&programId=149039
and find out is there any api (eg for java language) available for your unit. Crating native communication is tricky and generates errors.
Finally if you decide to create RS232 application, try to find emulator for nurit 8400 (on the site above, there are tools for this unit types, maybe there is a tool which simulates rs232 protocol).
Make sure, your device sends something to you: launch terminal and verify connection with device.
Finally write tests covering all needed operations and implement device protocol.
If you want, I could send you some examples for different devices. Rules are same.
Harry on May 18th, 2010, 00:29
Hi Sabastian,
Apparently I set up the environment ok, however the dll's don't work with Windows 7 64-bit:
java.lang.UnsatisfiedLinkError: C:\Users\BigBoss\Workspace\JavaSerial\lib\rxtxSerial.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\BigBoss\Workspace\JavaSerial\lib\rxtxSerial.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform
Do you know of any solution?
Harry on May 18th, 2010, 10:55
Hi Sebastian,
I didnot care to look in the prerelease because it was sad to be not stable. But there is the Windows 64-bit package.
My output acknowledges the TODO remark:
WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2
COM1
etc.
At least I set the first steps.. Ready to proceed!
Many thanx for helping me start with Eclipse/Jdk/RS232,
Cheers
POTTERShelly on September 1st, 2010, 22:16
Following my own monitoring, millions of persons all over the world receive the <a href="http://bestfinance-blog.com/topics/credit-loans">credit loans</a> from good creditors. So, there is good chances to receive a auto loan in all countries.
Add comment