Writing "Hello World" to a USB to serial converter

From Rxtx

Jump to: navigation, search


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";


Installing JavaVM and RXTX

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

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";


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