Wednesday, January 7, 2009

Java - Serial port communication

1. Java Development Environment set up under winxp.

a. download Eclipse IDE (Eclipse IDE for Java Developers (85 MB)) from
http://www.eclipse.org/downloads/
b. download JDK (Java SE Development Kit (JDK) 6 Update 11 72.9MB) from
http://java.sun.com/javase/downloads/?intcmp=1281

Installing JavaVM (JDK,JRE), and IDE.
The JDK files are placed at C:\Program Files\Java\jdk1.6.0_11\ and the runtime files are placed at C:\Program Files\Java\jdk1.6.0_11\. Ensure the System Variable "path" (held in My Computer->properties->Advanced->Environment Varables->System Variables) includes ";C:\Program Files\Java\jdk1.6.0_11\bin" at the end.

"C:\Program Files\Java\jre6" have JRE installed.

2. Download 'RXTX-2.1-7r2(stable) Binary rxtx-2.1-7-bins-r2.zip ' zip file from
http://www.rxtx.org/ wiki

a. Install RXTX
This involves placing rxtxSerial.dll and rxtxParallell.dll in both C:\Program Files\Java\jdk1.6.0_11\jre\bin and C:\Program Files\Java\jre6\bin. It also involves placing RXTXcomm.jar in both C:\Program Files\Java\jdk1.6.0_11\jre\lib\ext and C:\Program Files\Java\jre6\lib\ext.

b. Example is in link path: usage(Using RXTX)->Code Examples->Writing "Hello World" to a USB to serial converter

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.


This Java program can be compiled and run by typing the following from the command line:
D:\MyDocXp\MyJavaEclipse\HelloWorld\src> javac SimpleWrite.java
D:\MyDocXp\MyJavaEclipse\HelloWorld\src>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 19200 baud rate, 8 data bits, 1 stop bit, no parity and no handshaking.


c. SimpleWrite.java

import java.io.*;
import java.util.*;
//import javax.comm.*;
import gnu.io.*;
/** * 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 = "/dev/term/a";
String defaultPort = "COM4";
if (args.length > 0) { defaultPort = args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) { System.out.println("Found port " + defaultPort);
portFound = true;
try { serialPort = (SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) { System.out.println("Port in use.");
continue;
}
try { outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try { serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try { 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 { outputStream.write(messageString.getBytes());
} catch (IOException e) {}
try { Thread.sleep(2000); // Be sure data is xferred before closing } catch (Exception e) {} serialPort.close();
System.exit(1);
} } }
if (!portFound) { System.out.println("port " + defaultPort + " not found.");
} }
}

1 comment:

deepanshu said...

hi, I am facing an issue. The import gnu.io.* is showing an issue in NetBeans.