How do I initialize serial card parameters in java?

Posted on 2009-02-10
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.util.*;

//import javax.comm.*; // for SUN's serial/parallel port libraries

import*; // 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("","").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)"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 =;


            // print data

            String result  = new String(readBuffer);

            System.out.println("Read: "+result);

         } catch (IOException e) {}






Open in new window

Question by:eze2227
    LVL 92

    Accepted Solution

    javacomm for windows  is still around if you really wanted to try it

    LVL 92

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    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…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now