Writing "Hello World" to a USB to serial converter

From Rxtx

(Difference between revisions)
Jump to: navigation, search
(New page: This example shows how write a Java program to write out "Hello World" to a USB to serial converter connected to a Windwos XP computer. Firstly the USB to serial converter in installed us...)
(Installing JavaVM and RXTX)
 
(11 intermediate revisions not shown)
Line 1: Line 1:
-
This example shows how write a Java program to write out "Hello World" to a USB to serial converter connected to a Windwos XP computer.
+
== General Instructions ==
 +
This example shows how write a Java program to write out "Hello World" to a serial port or to a USB to serial converter. Before you start you will need to ensure that a suitable Java VM is installed, along with the RXTX Libraries (see platform specifc instructions below).
-
Firstly the USB to serial converter in installed using the manufacturer's instructions. Select My Computer->Properties->Hardware->Device Manager->Ports(COM&LPT)->USB - Serial Comm Port (COM4). This shows that the device was installed as a COM4 port.
+
These instructions all make use of the [http://java.sun.com/developer/releases/javacomm/SimpleWrite.java SimpleWrite.java] example file, which you will need to download. Once downloaded to a folder of your choice, from an editor:
-
Then install Sun's Java software (JDK6 Update 3).  The JDK files are placed at C:\Program Files\Java\jdk1.6.0_03\ and the runtime files are placed at C:\Program Files\Java\jre1.6.0_03\. Ensure the System Variable "path" (held in My Computer->properties->Advanced->Environment Varables->System Variables) includes ";C:\Program Files\Java\jdk1.6.0_03\bin" at the end.
+
*Change the third import statement "import javax.comm.*;" to read:
-
Install RxTxThis involves placing rxtxSerial.dll and rxtxParallell.dll in both C:\Program Files\Java\jdk1.6.0_03\jre\bin and C:\Program Files\Java\jre1.6.0_03\bin. It also involves placing RXTXcomm.jar in both C:\Program Files\Java\jdk1.6.0_03\jre\lib\ext and C:\Program Files\Java\jre1.6.0_03\lib\ext.
+
<pre> import gnu.io.*; </pre>
 +
 
 +
*Change the line "String defaultPort = "/dev/term/a";" to refer to your serial device (see platform specifc instructions below). For example on Windows:
 +
 
 +
<pre> String defaultPort = "COM4"; </pre>
-
Put the following file in a folder:
+
This Java program can be compiled and run by typing the following from the command line:
<pre>
<pre>
-
/*
+
javac SimpleWrite.java
-
* @(#)SimpleWrite.java 1.12 98/06/25 SMI
+
java SimpleWrite
-
*
+
</pre>
-
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
+
-
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
+
-
*
+
-
* Sun grants you ("Licensee") a non-exclusive, royalty free, license
+
-
* to use, modify and redistribute this software in source and binary
+
-
* code form, provided that i) this copyright notice and license appear
+
-
* on all copies of the software; and ii) Licensee does not utilize the
+
-
* software in a manner which is disparaging to Sun.
+
-
*
+
-
* This software is provided "AS IS," without a warranty of any kind.
+
-
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
+
-
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
+
-
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
+
-
* ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
+
-
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
+
-
* SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
+
-
* BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
+
-
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
+
-
* HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
+
-
* OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
+
-
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+
-
*
+
-
* This software is not designed or intended for use in on-line control
+
-
* of aircraft, air traffic, aircraft navigation or aircraft
+
-
* communications; or in the design, construction, operation or
+
-
* maintenance of any nuclear facility. Licensee represents and
+
-
* warrants that it will not use or redistribute the Software for such
+
-
* purposes.
+
-
*/
+
-
import java.io.*;
+
-
import java.util.*;
+
-
import gnu.io.*;
+
-
/**
+
"Hello World" should then appear on the device connected to the serial port, assuming that it has been set up to receive a 9600 baud rate, 8 data bits, 1 stop bit, no parity and no handshaking.
-
* Class declaration
+
-
*
+
-
*
+
-
* @author
+
-
* @version 1.10, 08/04/00
+
-
*/
+
-
public class SimpleWrite {
+
-
    static Enumeration       portList;
+
-
    static CommPortIdentifier portId;
+
-
    static String       messageString = "Hello, world!";
+
-
    static SerialPort       serialPort;
+
-
    static OutputStream      outputStream;
+
-
    static boolean       outputBufferEmptyFlag = false;
+
-
    /**
+
-
    * Method declaration
+
-
    *
+
-
    *
+
-
    * @param args
+
-
    *
+
-
    * @see
+
-
    */
+
-
    public static void main(String[] args) {
+
-
boolean portFound = false;
+
-
String  defaultPort = "COM4";
+
-
if (args.length > 0) {
 
-
    defaultPort = args[0];
 
-
}
 
-
portList = CommPortIdentifier.getPortIdentifiers();
+
== Windows XP ==
-
while (portList.hasMoreElements()) {
+
=== Installing JavaVM and RXTX ===
-
    portId = (CommPortIdentifier) portList.nextElement();
+
-
    if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
+
[http://java.sun.com/javase/downloads/index.jsp Download] and Install Sun's Java software (JDK 6 Update 3 at time of writing). The JDK files are placed at C:\Program Files\Java\jdk1.6.0_03\ and the runtime files are placed at C:\Program Files\Java\jre1.6.0_03\. Ensure the System Variable "path" (held in My Computer->properties->Advanced->Environment Varables->System Variables) includes ";C:\Program Files\Java\jdk1.6.0_03\bin" at the end.
-
if (portId.getName().equals(defaultPort)) {
+
{| style="background-color: #ffffee"
-
    System.out.println("Found port " + defaultPort);
+
| Note: JDK 1.4 will work with RXTX, but there is not harm in being up to date.
 +
|}
-
    portFound = true;
+
Install RxTx.  This involves placing rxtxSerial.dll and rxtxParallell.dll in both C:\Program Files\Java\jdk1.6.0_03\jre\bin and C:\Program Files\Java\jre1.6.0_03\bin.  It also involves placing RXTXcomm.jar in both C:\Program Files\Java\jdk1.6.0_03\jre\lib\ext and C:\Program Files\Java\jre1.6.0_03\lib\ext.
-
    try {
+
=== The Serial Port Name ===
-
serialPort =  
+
-
    (SerialPort) portId.open("SimpleWrite", 2000);
+
-
    } catch (PortInUseException e) {
+
-
System.out.println("Port in use.");
+
-
continue;
+
If the machine doesn't have a serial port, install a USB to serial converter following the manufacturer's instructions. Select My Computer->Properties->Hardware->Device Manager->Ports(COM&LPT)->USB - Serial Comm Port (COM4).  This shows a COM port to which the device was installed - in this case COM4.
-
    }
+
-
    try {
+
Serial ports on MS-Windows based systems are of the form COMn, where n is a number. For example COM4. To define the variable in the example program:
-
outputStream = serialPort.getOutputStream();
+
-
    } catch (IOException e) {}
+
-
    try {
+
<pre>
-
serialPort.setSerialPortParams(9600,
+
String  defaultPort = "COM4";
-
      SerialPort.DATABITS_8,
+
</pre>
-
      SerialPort.STOPBITS_1,
+
-
      SerialPort.PARITY_NONE);
+
-
    } catch (UnsupportedCommOperationException e) {}
+
-
+
-
    try {
+
== UBUNTU Linux ==
-
    serialPort.notifyOnOutputEmpty(true);
+
-
    } catch (Exception e) {
+
-
System.out.println("Error setting event notification");
+
-
System.out.println(e.toString());
+
-
System.exit(-1);
+
-
    }
+
-
   
+
-
   
+
-
    System.out.println(
+
-
    "Writing \""+messageString+"\" to "
+
-
+serialPort.getName());
+
-
    try {
+
=== Installing JavaVM and RXTX ===
-
outputStream.write(messageString.getBytes());
+
-
    } catch (IOException e) {}
+
-
    try {
+
Sun Java RTE is installed at /usr/lib/jvm/java-1.5.0-sun-1.5.0.06/jre and RXTXcomm.jar must be placed at /usr/lib/jvm/java-1.5.0-sun-1.5.0.06/jre/lib/ext and the librxtxSerial.so and librxtxParallel.so files are placed at /usr/lib/jvm/java-1.5.0-sun-1.5.0.06/jre/lib/i386.
-
      Thread.sleep(2000);  // Be sure data is xferred before closing
+
-
    } catch (Exception e) {}
+
-
    serialPort.close();
+
-
    System.exit(1);
+
-
}
+
-
    }
+
-
}
+
-
if (!portFound) {
+
=== The Serial Port Name ===
-
    System.out.println("port " + defaultPort + " not found.");
+
-
}
+
-
    }
+
 +
On Unix machines the serial port ID can be found by typing:
-
}
+
<pre>
 +
sudo dmesg | grep tty
</pre>
</pre>
-
Note that the third import statement has been changed to be "import gnu.io.*;"
+
This is likely to show the port to be "/dev/ttyS0".
-
rather than the "import javax.comm.*;" that appears in Sun's original version.
+
-
Note also that the defaultPort has been set to be COM4.
+
The defaultPort line in the SimpleWrite Java file (see above) must now be changed to:
-
This Java program can be compiled and run by typing the following at the Command Prompt:
 
<pre>
<pre>
-
javac SimpleWrite.java
+
String  defaultPort = "/dev/ttyS0";
-
java SimpleWrite
+
</pre>
</pre>
-
"Hello World" should then appear on the device connected to the serial port, assuming that it has been set up to receive a 9600 baud rate, 8 data bits, 1 stop bit, no parity and no handshaking.
+
 
 +
== MacOS X ==
 +
 
 +
=== Installing JavaVM and RXTX ===
 +
 
 +
MacOS X already comes with a JavaVM installed, so you will not need to install one.
 +
 
 +
The first thing to do is ensure that the java library and the native library are available to your program. If you want them to be available to all programs, then the standard locations are:
 +
* /Library/Java/Extensions - to make available to all users
 +
* ~/Library/Java/Extensions - to make available to only your user
 +
 
 +
Also, if you are using an IDE such as Eclipse, you can opt to include them in your project instead. If your project is MyHelloWorld then locations would be:
 +
* MyHelloWorld/lib/RXTXcomm.jar
 +
* MyHelloWorld/librxtxSerial.jnilib
 +
Don't forget to include them in your build path.
 +
 
 +
=== The Serial Port Name ===
 +
On MacOS X serial devices have a 'device file' defined in <tt>/dev</tt>, and are of the form tty.* or cu.*. If you don't see the one corresponding to your device then you probably don't have the appropriate driver installed. Bluetooth devices and 'USB to RS232' all appear as serial devices. If you have device called <tt>/dev/tty.mydevice</tt>, then in the SimpleWrite.java file change the value of the 'defaultPort' variable to refer to it:
 +
 
 +
<pre>
 +
String  defaultPort = "/dev/tty.mydevice";
 +
</pre>

Latest revision as of 20:03, 29 January 2010

Contents

General Instructions

This example shows how write a Java program to write out "Hello World" to a serial port or to a USB to serial converter. Before you start you will need to ensure that a suitable Java VM is installed, along with the RXTX Libraries (see platform specifc instructions below).

These instructions all make use of the SimpleWrite.java example file, which you will need to download. Once downloaded to a folder of your choice, from an editor:

  • Change the third import statement "import javax.comm.*;" to read:
 import gnu.io.*; 
  • Change the line "String defaultPort = "/dev/term/a";" to refer to your serial device (see platform specifc instructions below). For example on Windows:
 String  defaultPort = "COM4"; 

This Java program can be compiled and run by typing the following from the command line:

javac SimpleWrite.java
java SimpleWrite

"Hello World" should then appear on the device connected to the serial port, assuming that it has been set up to receive a 9600 baud rate, 8 data bits, 1 stop bit, no parity and no handshaking.


Windows XP

Installing JavaVM and RXTX

Download and Install Sun's Java software (JDK 6 Update 3 at time of writing). The JDK files are placed at C:\Program Files\Java\jdk1.6.0_03\ and the runtime files are placed at C:\Program Files\Java\jre1.6.0_03\. Ensure the System Variable "path" (held in My Computer->properties->Advanced->Environment Varables->System Variables) includes ";C:\Program Files\Java\jdk1.6.0_03\bin" at the end.

Note: JDK 1.4 will work with RXTX, but there is not harm in being up to date.

Install RxTx. This involves placing rxtxSerial.dll and rxtxParallell.dll in both C:\Program Files\Java\jdk1.6.0_03\jre\bin and C:\Program Files\Java\jre1.6.0_03\bin. It also involves placing RXTXcomm.jar in both C:\Program Files\Java\jdk1.6.0_03\jre\lib\ext and C:\Program Files\Java\jre1.6.0_03\lib\ext.

The Serial Port Name

If the machine doesn't have a serial port, install a USB to serial converter following the manufacturer's instructions. Select My Computer->Properties->Hardware->Device Manager->Ports(COM&LPT)->USB - Serial Comm Port (COM4). This shows a COM port to which the device was installed - in this case COM4.

Serial ports on MS-Windows based systems are of the form COMn, where n is a number. For example COM4. To define the variable in the example program:

String  defaultPort = "COM4";

UBUNTU Linux

Installing JavaVM and RXTX

Sun Java RTE is installed at /usr/lib/jvm/java-1.5.0-sun-1.5.0.06/jre and RXTXcomm.jar must be placed at /usr/lib/jvm/java-1.5.0-sun-1.5.0.06/jre/lib/ext and the librxtxSerial.so and librxtxParallel.so files are placed at /usr/lib/jvm/java-1.5.0-sun-1.5.0.06/jre/lib/i386.

The Serial Port Name

On Unix machines the serial port ID can be found by typing:

sudo dmesg | grep tty

This is likely to show the port to be "/dev/ttyS0".

The defaultPort line in the SimpleWrite Java file (see above) must now be changed to:

String  defaultPort = "/dev/ttyS0";


MacOS X

Installing JavaVM and RXTX

MacOS X already comes with a JavaVM installed, so you will not need to install one.

The first thing to do is ensure that the java library and the native library are available to your program. If you want them to be available to all programs, then the standard locations are:

  • /Library/Java/Extensions - to make available to all users
  • ~/Library/Java/Extensions - to make available to only your user

Also, if you are using an IDE such as Eclipse, you can opt to include them in your project instead. If your project is MyHelloWorld then locations would be:

  • MyHelloWorld/lib/RXTXcomm.jar
  • MyHelloWorld/librxtxSerial.jnilib

Don't forget to include them in your build path.

The Serial Port Name

On MacOS X serial devices have a 'device file' defined in /dev, and are of the form tty.* or cu.*. If you don't see the one corresponding to your device then you probably don't have the appropriate driver installed. Bluetooth devices and 'USB to RS232' all appear as serial devices. If you have device called /dev/tty.mydevice, then in the SimpleWrite.java file change the value of the 'defaultPort' variable to refer to it:

String  defaultPort = "/dev/tty.mydevice";
Personal tools