This is for JAVA JDK 1.1 and up. (Win)Initialization is done via the MODE.COM utility. Then to write, simply open a stream using the OS logical name attached to the serial port. You can use the same technique to print to the printer port (in this case the local name would be "LPTx:").
=================================================
import java.io.*;
public class SerialTest {
public static void main( String args[]) {
Runtime rt = Runtime.getRuntime();
Process p = null;
String portname = "com1:";
// for WinNT: c:\\winnt\\system32\\cmd.exe
// c:\\windows\\command\\mode.com
String cmd[] = {
"c:\\windows\\command.com", "/c",
"start", "/min",
"c:\\windows\\command\\mode.com", portname,
"baud=9600", "parity=n", "data=8",
"stop=1",
};
try {
p = rt.exec( cmd );
if( p.waitFor() != 0 ) {
System.out.println("Error executing command: " + cmd );
System.exit( -1 );
}
byte data[] =
"Writing a byte stream out of a serial port.".getBytes();
FileOutputStream fos = new FileOutputStream( portname );
BufferedOutputStream bos = new BufferedOutputStream( fos );
fos.write( data, 0, data.length );
fos.close();
}
catch( Exception e ) {
e.printStackTrace();
}
}
}