How do I initialize serial card parameters in java?

Posted on 2009-02-10
Medium Priority
Last Modified: 2013-11-23
I am trying to send files through a serial port to an external device. However there are some parameters of the serial card that I don't know how to change and thought maybe you might know how. The java commport library no longer supports Windows so I had to use the rxtx library instead. The parameters that I can't seem to change are the Timeout:// to "ON", CTS Handshaking://to "On", DTR circuit:// to" HANDSHAKE",RTS circuit:// to" HANDSHAKE". I have looked through the API and found nothing. Any ideas? The code it needs to go to is pasted in the "code" field and attached is a picture of the parameters that need to be changed. Any and all help is appreciated.

Thank you,
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package transfer;
import java.io.*;
import java.util.*;
//import javax.comm.*; // for SUN's serial/parallel port libraries
import gnu.io.*; // for rxtxSerial library
public class Main implements Runnable, SerialPortEventListener {
   static CommPortIdentifier portId;
   static CommPortIdentifier saveportId;
   static Enumeration        portList;
   InputStream           inputStream;
   SerialPort           serialPort;
   Thread           readThread;
   static File testFile = new File("Desktop\dislin8.hpg");
   static OutputStream      outputStream;
   static OutputStream f0 = new FileOutputStream(testFile); 
   static boolean        outputBufferEmptyFlag = false;
   public static void main(String[] args) {
      boolean           portFound = false;
      String           defaultPort;
      // determine the name of the serial port on several operating systems
      String osname = System.getProperty("os.name","").toLowerCase();
      if ( osname.startsWith("windows") ) {
         // windows
         defaultPort = "COM1";
      } else if (osname.startsWith("linux")) {
         // linux
        defaultPort = "/dev/ttyS0";
      } else if ( osname.startsWith("mac") ) {
         // mac
         defaultPort = "????";
      } else {
         System.out.println("Sorry, your operating system is not supported");
      if (args.length > 0) {
         defaultPort = args[0];
      System.out.println("Set default port to "+defaultPort);
		// parse ports and if the default port is found, initialized the reader
      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;
               // init reader thread
               Main reader = new Main();
      if (!portFound) {
         System.out.println("port " + defaultPort + " not found.");
   public void initwritetoport() {
      // initwritetoport() assumes that the port has already been opened and
      //    initialized by "public nulltest()"
      try {
         // get the outputstream
         outputStream = serialPort.getOutputStream();
      } catch (IOException e) {}
      try {
         // activate the OUTPUT_BUFFER_EMPTY notifier
      } catch (Exception e) {
         System.out.println("Error setting event notification");
   public void writetoport() {
      System.out.println("Writing \""+messageString+"\" to "+serialPort.getName());
      try {
         // write string to serial port
      } catch (IOException e) {}
   public Main() {
      // initalize serial port
      try {
         serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
      } catch (PortInUseException e) {}
      try {
         inputStream = serialPort.getInputStream();
      } catch (IOException e) {}
      try {
      } catch (TooManyListenersException e) {}
      // activate the DATA_AVAILABLE notifier
      try {
         // set port parameters
         serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, 
                   // I need help changing more parameters here please.What can I do?
      } catch (UnsupportedCommOperationException e) {}
      // start the read thread
      readThread = new Thread(this);
   public void run() {
      // first thing in the thread, we initialize the write operation
      try {
         while (true) {
            // write string to port, the serialEvent will read it
      } catch (InterruptedException e) {}
   public void serialEvent(SerialPortEvent event) {
      switch (event.getEventType()) {
      case SerialPortEvent.BI:
      case SerialPortEvent.OE:
      case SerialPortEvent.FE:
      case SerialPortEvent.PE:
      case SerialPortEvent.CD:
      case SerialPortEvent.CTS:
      case SerialPortEvent.DSR:
      case SerialPortEvent.RI:
      case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
      case SerialPortEvent.DATA_AVAILABLE:
         // we get here if data has been received
         byte[] readBuffer = new byte[20];
         try {
            // read data
            while (inputStream.available() > 0) {
               int numBytes = inputStream.read(readBuffer);
            // print data
            String result  = new String(readBuffer);
            System.out.println("Read: "+result);
         } catch (IOException e) {}

Open in new window

Question by:eze2227
  • 2
LVL 92

Accepted Solution

objects earned 2000 total points
ID: 23609671
javacomm for windows  is still around if you really wanted to try it


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question