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.
93 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.
Scott on November 22nd, 2010, 05:47
I have such a project: to connect three devices to laptop PC, and stream data in realtime to javascript pivot table. I know java and javascript have nothing to do with each other. I dont care which language used, only if final result works well. I have all parsing information and pinout data for each device. I have $3000 usd for this project if anyone interested.
Messer on January 8th, 2011, 23:01
Thanks. It works great!
Shawn on January 11th, 2011, 10:17
Hello Sebastian:
I have two problem:
1. I download the RXTX api from "http://rxtx.qbang.org/wiki/index.php/Download". But the version is a
mismatch.===>
Native lib Version = RXTX-2.1-7pre20
Java lib Version = RXTX-2.1-7
WARNING: RXTX Version mismatch
Jar version = RXTX-2.1-7
native lib Version = RXTX-2.1-7pre20
2. I send the strings by the example and physical conecting two PC, but the reveiver(anther pc) receive the message is always lost some characters. example:
sender=>ABCDEFGHIJK1234567890
receiver=> ABCDE@IJK23456780888
Do you know of any solution?
Sebastian on January 11th, 2011, 22:36
@Shwan
Make sure you've downloaded following file: http://rxtx.qbang.org/pub/rxtx/rxtx-2.1-7-bins-r2.zip and make sure you use: RXTXcomm.jar with Windows native library from rxtx-2.1-7-bins-r2\Windows\i368-mingw32\rxtxSerial.dll directory (inside zip file).
I've checked library versions from my projects and rxtx-2.1-7-bins-r2.zip version works fine for me.
If you still have a problem, send me private message with your code or libraries. I'll check it.
Shawn on January 12th, 2011, 10:09
Sebastian:
1.I think that the version mismatch is caused by the anther softwave (Matlab7.1). The Matlab7.1 also have a rxtxSerial.dll in "C:\Program Files\MATLAB71\bin\win32". Although I set the library to the rxtxSerial.dll in the netbeans, the netbeans still use the rxtxSerial.dll in MATLAB71.
I try the solution in page "http://rxtx.qbang.org/wiki/index.php/Using_RXTX_In_NetBeans". But still failed. Do you know of any solution?
2. the problem is OK now. It is hardwave problem.
Thanks a lot!!
Alex on January 24th, 2011, 17:12
Thanks!! Useful tutorial. Good job!!!
hamed on January 28th, 2011, 20:37
Hi, thank u
Can you have Parallel tutorial ?
and is there any Parallel port emulator ?!
eljun on February 14th, 2011, 18:18
Hi Sebastian,
I really appreciate this tutorial. I now have the 14 days trial of the serial emulator you suggested.
I have some concerns as I am about to work on the receiver and sender classes, will this work on windows vista? My development environment is on vista but my production would be XP.
Please help.
P.S.
I second hamed. If you have some for Parallel tutorials using rxtx, please share it to us.
Thanks!
Eljun
Sebastian on February 14th, 2011, 22:14
@eljun
If you use x64 based system, binaries from rxtx site won't work. x64 binares are available on site: http://www.cloudhopper.com/opensource/rxtx/ but it seems to be a little outdated (2008).
Code you have written should work on Windows Vista. Virtual Serial Port Driver supports boths operating systems.
I think the only concern you may have is finding base of an architecture of your system (32 or 64). Check it before development.
Unfortunately, I do not have any tutorials for Parallel...
BR
Sebastian
eljun on February 15th, 2011, 04:04
Hi Sebastian,
Great! My desktop is running in Windows Vista x86. I am ok with that. Though my laptop is Windows 7 x64, so I have to spend more time remotely accessing my desktop for this.
Your code worked perfectly together with the Virtual Serial Port Driver (VSPD). Two thumbs up for that. It was flawless and the instructions were perfectly ordered.
I am just stuck in one problem. I need to tap through the serial port wherein I would like to have a port (COM20) with a connection to a device (COM25) and a tapper (COM26), I cannot do this with the VSPD. COM26 is restricted to connect to COM20 since COM20-COM25 connection has already been established.
Here is the scenario:
COM20: The receiver. This port is the default port within the system (a computer or workstation)
COM25: The sender. A device that is working outside the computer. It receives instructions from COM20 and sends results once the instructions have been completed.
COM26: The tapper. It listens to whatever COM25 has to send and does almost perfectly the same as COM20.
What do you think should I do?
Thanks,
Eljun
Latha Karthigaa on February 15th, 2011, 05:22
Hi Sebestian!
I am using Netbeans IDE 6.5.1
I have downloaded the zip file for rxtx from http://users.frii.com/jarvi/rxtx/download.html
I have created a project named "JavaSerial" and in the home directory of the JavaSerial project, I have created a new directory lib wherein I placed RXTXcomm.jar, rxtxSerial.dll and rxtxParallel.dll
In the libraries tab, I added RXTXcomm.jar and clicked OK
But I could not follow the instructions written under "Extend project classpath" (I could not follow the Eclipse tutorial for NetBeans). So the result is when I compile, I get the following errors...
run:
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1709)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:83)
at javaserial.Main.list(Main.java:13)
at javaserial.Main.main(Main.java:20)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Kindly tell me how to perform "Extend Project classpath" using Netbeans...
Also correct me if I made any error previously...
Appreciating your reply
Latha Karthigaa on February 15th, 2011, 05:44
Hi Sebestian!
I solved my problem by placing rxtxSerial.dll in C:\Windows\System32
Thanks for ur wonderful tutorial
Sebastian on February 16th, 2011, 02:16
@eljun
In other words, you want to distribute flow of data from COM25 device to COM20 and COM26. Am I correct?
I would simply create single pipe to the device (COM20 <=> COM25) and distribute received data to multiple listeners. But I may be wrong if I undesrtand it wrong.
eljun on February 17th, 2011, 04:08
Hello Sebastian,
You have the right idea, but I missed some information a bit.
I was not able to emphasize the scenario, that the sender (COM25) and receiver (COM20) are existing systems that should not be touched. This means that COM25, as the sender, is:
1. made to send data to its output regardless of whoever receives it.
2. not to be changed or remodeled/recoded.
Now, given the emphasis above, I am thinking of the pipe that you mentioned.
How is this implemented? Is this done programatically or with the use of additional hardware.
Thanks,
Eljun
Sebastian on February 17th, 2011, 11:55
@eljun
I think the best way you can solve your problem is to create MyProxy application which connects to the device (COM20 - COM25). Sa any flow of data is passing through your application and you can do anything you want with bytes.
MyProxy <-> COM20 -------- COM25 <-> Your Device
and COM20 ---- COM25 -> VSPD pair
Next step is to create pair of com ports (COM26, COM27) for you tapper and connect it with MyProxy application:
MyProxy <- > COM26 ------- COM27 <-> Tapper
Shorty, if you receive data from COM20 you can send it using COM26 to tapper. You create something like data tunnel.
eljun on February 18th, 2011, 02:06
Hello Sebastian,
That is great suggestion, a proxy might be my option, though I have not explored on creating a proxy, yet. I do have another situation.
- a running application (MyDesktopAppplication) in a desktop is directly linked and has ownership to port COM20 (receiver). Acting somewhat like a controller sending control signals, dos and don'ts, to an external device, that is connected to COM25 (the sender of processed information).
- COM25 is connected (externally) to a machine/anything that processes something whose results are outputted to it and received by COM20.
How do you think a proxy or a tapper is done here?
Thanks,
Eljun
eljun on February 18th, 2011, 08:49
In addition, MyDesktopAppplication is an existing system/application and is not configurable to listen to any other port other than COM20.
The application I am thinking is somewhat like an observer. It listens (observe) activities within COM20 that is owned by MyDesktopAppplication.
Sebastian on February 19th, 2011, 01:01
@Eljun
I see what you mean, this is kind of circular dependency?
COM25 <-> DEVICE <-> COM20 <-> MyDesktopApplication <-> COM25
Am I right?
If so, you need two more physical com ports (eg. COM21,COM22) to connect with COM20 using DB9-DB9 cable and then with device.
COM25 <-> DEVICE <-> COM22 <-> MyProxy <-> COM21 <==cable==> COM20 <-> MyDesktopApplication <-> COM25
As you see everything you send using COM20 is passed to COM21 connected with proxy. Proxy sends data to the device connected with COM22. Proxy could also distribute data with your expectations.
If you don't have additional com ports, use usb ports and usb-com adapters or ... create/buy simple port multiplier, for example: http://www.miille.com/din366-10p.pdf (take a look on example case in pdf document).
eljun on February 24th, 2011, 02:04
Hi Sebastian,
I was away for a short while.
It is very good to hear from you.
I still have to learn more about circular dependency. I like the illustration and I would want to do the same. please see below.
The existing system is this.
MyDesktopApplication <-> COM20 <==cable==> COM25 <-> External Device
Descriptions:
MyDesktopApplication - an application that is running and has ownership to COM20.
COM20 - COM20 is the port within the computer/desktop/laptop
- Serial port of the Computer/desktop/laptop
COM25 - (dummy port name) externally connected to
- Serial port of the external electronic device
External Device - an electronic device with a serial port (COM25) that is connected serially to a computer/desktop/laptop.
MyWantedApplication is an application that would listen to the activities (inputs/instreams) at port COM20
Thanks,
Eljun
taybo on May 13th, 2011, 11:54
very nice example, thank you ;)
Tony on May 17th, 2011, 00:57
Hello,
Thank you for the great article - Appreciate greatly.
Racking my brain on trying to get RAW serial data so that I can parse the STX and ETX codes myself. I have a box that spits out STX <Message> ETX and would like to use.
Any help or insight is greatly appreciated.
Tony
Sebastian on May 17th, 2011, 01:51
@Tony
IMHO ProtocolImpl.onReceive(byte) method should be modified to recognize 0x02 hex code for STX and 0x03 hex code for ETX. Simply you start to write in the buffer when STX code appears and invoke onMessage when ETX code shows up:
private boolean bufferEnabled = false;
public void onReceive(byte b) {
if (b == 0x02 && !bufferEnabled) {
bufferEnabled = true;
} else if (b == 0x03 && bufferEnabled) {
onMessage();
bufferEnabled = false;
} else if (bufferEnabled) {
buffer[tail] = b;
tail++;
}
}
BR
Sebastian
rayen on May 23rd, 2011, 09:00
Hi, thank you for the great work,
i want to use it to communicate with oscilloscope by sending command and request and then the oscilloscope response to me the picture of the curve, so what should I change in this code to receive picture and open it immediatly? (I know the the request that I will send to the oscilloscope)
thank you.
Sebastian on May 23rd, 2011, 09:10
@rayen
It depends on data you receive from the oscilloscope. It's likely that you'll get a raw data flow wihout any stop/interruption marks so simply take onReceive method and decode there incoming bytes and convert them to pixel dimensions.
For now it's all I can tell without seeing the documentation of your device.
BR
Sebastian
monta on June 2nd, 2011, 00:51
thank you for all,
i try to send request to oscilloscope tektronix tds210 "ID?" but i cant receive the oscilloscope response, can you hel me please?
Sebastian on June 2nd, 2011, 11:14
@monta
At first I would try to connect with an oscilloscope using any HypeTerminal application. Then see if you receive response from the device when entering commands manually.
BR
Sebastian
Saghar on August 2nd, 2011, 08:20
At first thank you so much for your sharing. It was so helpful for me. But i have got a problem. I have to write java code for prepare an interface between a temparature device and my pc in java. When i send "t" commend, it will send to my screen the tempatare of the environment. Could you help me?
Thanks Saghar.
Sebastian on August 3rd, 2011, 16:00
@Saghar
Modify ProtocolImpl to meet your requirements. If current temperature is send as byte, just change implementation of getMessage(..,..) method. It should return String representation of received temprature. To send 't' command invoke CommPortSender.send(getMessage("t"));
But if you want to make synchronous interface with method int getTemperature(), which returns temperature as int you have more to do. Look at my example:
public interface ReadCallback {
void onRead(int temperature);
}
public class TempReceiver implements ReadCallback {
private final static int TIMEOUT_MS = 30000;
private ProtocolImpl protocol;
private int lastTemperature;
public Test() {
// start protocols thread here, it listens to incoming bytes, pass to ProtocolImpl reference of ReadCallback interface - ProtocolImpl will invoke onRead() method if temperature is read
protocol = new ProtocolImpl(this);
protocol.start();
}
// implement synchronous method getTemperature, remember that it will wait max 30s on devices response, after that time the method returns last known temperature
public int getTemperature() throws InterruptedException {
synchronized(this) {
CommPortSender.send(getMessage("t"));
wait(TIMEOUT_MS);
return lastTemperature;
}
}
public void onRead(int temperature) {
synchronized(this) {
lastTemperature = temperature;
// if temperature is read by ProtocolImpl, we can return temperature immediatelly
notifyAll();
}
}
Saghar on August 4th, 2011, 11:06
Thank you so much Sebastian. Until you answer me, i did it in C but i will try your solution as soon as possible:))
Saghar on August 5th, 2011, 09:41
Hi Saghar,
After i debug your code it gives me
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com_port_example.Main.main(Main.java:34)
Java Result: 1"
error. Do you have any idea, why i get this message?
Saghar on August 5th, 2011, 10:02
(*) Hi Sebastian,
After i debug your code it gives me
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com_port_example.Main.main(Main.java:34)
Java Result: 1"
error. Do you have any idea, why i get this message?
Saghar
Sebastian Kuligowski on August 5th, 2011, 10:11
@Saghar
It's really hard to say why that error comes up. Check the RXTX library version to be sure you're using 2.1-7.
Saghar on August 5th, 2011, 10:54
Hi Sebastian,
First of all, thank you so much for your quick response. I've checked my rxtx library. It is 2.1.7. I don't want to force you more but do you have another idea:((
Thanks again, Saghar
Sebastian Kuligowski on August 5th, 2011, 10:59
@Saghar
Please send me an e-mail using Contact me form and put your code there. I'll try to set it up using my environment.
BR
Sebastian
Nikola on August 30th, 2011, 03:30
You are the man. Working 100% on my Arduino Mega 2560
Ahmad on October 13th, 2011, 03:51
I am trying to read an Arduino in Processing. I am getting:
WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2
I am using Windows XP. I downloaded “rxtx-2.1-7-bins-r2.zip” but, when I unziped this file, I have only one file (rxtx-2.1-7-bins-r2). I do not see RXTXcomm. Where can I get this file?
Sebastian on October 13th, 2011, 10:59
This file is directly in rxtx-2.1-7-bins-r2 directory:
rxtx-2.1-7-bins-r2.zip/rxtx-2.1-7-bins-r2/RXTXcomm.jar
You can download zip package from here:
http://rxtx.qbang.org/pub/rxtx/rxtx-2.1-7-bins-r2.zip
Ahmad on October 13th, 2011, 22:12
I tried again. This time, I got "folder invalid or crroupted". Would you please e-mail me these files.
Nicolas on January 9th, 2012, 02:06
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
I dont know where to put that, im very beginner...
can you tell me in what class and line plisssssss
Martin on February 5th, 2012, 19:54
Hi
I pasted the "Test your environment" and it worked without any problem.
I then tried "RS232 Application Example" but without the same luck.
I get error on: CommPortSender, CommPortSender and ProtocolImpl.
What am I missing?
I'm looking for a simple program to receive and send serial data.
Martin on February 5th, 2012, 20:03
Found the solution
Naveed Akhtar on March 7th, 2012, 13:02
Hi,
I have successfully used this code with com1 parameter and it shows Hello message.
1) can i use same code when i connect through serial cable?
2) how can i capture result from device on my output console?
3) how can i built project?
4) how can i implement this program on client machine
highly appreciate help for this topic
thanks