i have made one program,but it will give none of the port number.
Following files i have made

SimpleRead.java

import java.io.*;
import java.util.*;
import javax.comm.*;
import java.nio.ByteBuffer;

public class SimpleRead extends SpeedometerExample implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration portList;
    byte[] readBuffer=null;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println(portList);
        System.out.println(portList.nextElement());

        while (portList.hasMoreElements()) {
            System.out.println("hi....");
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM9")) {
                    //if (portId.getName().equals("/dev/term/a")) {
                    SimpleRead reader = new SimpleRead();
                }
            }
        }
    }

    public SimpleRead() {
         /*portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM9")) {
            //                if (portId.getName().equals("/dev/term/a")) {
                    SimpleRead reader = new SimpleRead();
                }
            }
        }*/
        try {
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {System.out.println(e);}
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {System.out.println(e);}
    try {
            serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {System.out.println(e);}
        serialPort.notifyOnDataAvailable(true);
        try {
            serialPort.setSerialPortParams(9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {System.out.println(e);}
        readThread = new Thread(this);
        readThread.start();
    }

    public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {System.out.println(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:
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[20];

            try {
                while (inputStream.available() > 0) {
                    int numBytes = inputStream.read(readBuffer);

                    int value=ByteBuffer.wrap(readBuffer).getInt();
                SpeedometerExample speed = new SpeedometerExample(value);
                }
                //System.out.print(new String(readBuffer));

            } catch (IOException e) {System.out.println(e);}
            break;

        }
    }
}

Speedometer.java

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.accessibility.*;
import java.lang.reflect.*;

/**
 * Implements a "velocimeter"-like component. Its primarily purpose is to
 * graphically display a value into a pre-defined range as a hand.
 * Various pluggable look and feels are supported : <code>Metal</code>,
 * <code>Aqua</code> and the default <code>Basic</code>.
 *
 * Source code <a href="Speedometer.java.html">here</a>.
 * @see AquaSpeedometerUI
 * @see BasicSpeedometerUI
 * @see MetalSpeedometerUI
 * @author Jean-Baptiste Yunès
 * @date 03/2011
 */
public class Speedometer extends JComponent {
    private static final String packageName = "fast_and_furious";
    private BoundedRangeModel model;
    /**
     * Creates a <code>Speedometer</code> with the following default
     * values : <code>value</code> is 0, <code>minimum</code> is 0, and
     * <code>maximum</code> is 100.
     */
    public Speedometer() {
    this(0,0,100);
    }
    /**
     * Creates a <code>Speedometer</code>.
     * @param value the value of this <code>Speedometer</code>
     * @param minimum the minimum value this <code>Speedometer</code> can take
     * @param maximum the maximum value this <code>Speedometer</code> can take
     */
    public Speedometer(int value,int minimum,int maximum) {
    SpeedometerModel model = new SpeedometerModel(value,minimum,maximum);
    setModel(model);
    updateUI();
    }

    // Model
    private BoundedRangeModel getModel() {
    return model;
    }
    private void setModel(BoundedRangeModel m) {
    model = m;
    }

    // Model delegation services
    /**
     * Gets the current value of this <code>Speedometer</code>
     * @return the current value of this <code>Speedometer</code>
     */
    public int getValue() {
    return model.getValue();
    }
    /**
     * Sets the current value of this <code>Speedometer</code>
     * @param value the new value
     */
    public void setValue(int value) {
    model.setValue(value);
    repaint();
    }
    /**
     * Gets the minimum value this <code>Speedometer</code> can take.
     * @return the minimum value
     */
    public int getMinimum() {
    return model.getMinimum();
    }
    /**
     * Gets the maximum value this <code>Speedometer</code> can take.
     * @return the maximum value
     */
    public int getMaximum() {
    return model.getMaximum();
    }

    // UI delegation
    /**
     * Resets the UI property to a value from the current look and feel.
     * @see JComponent#updateUI()
     */
    @Override
    public void updateUI() {
    String lafName = UIManager.getLookAndFeel().getID();
        if (UIManager.get(getUIClassID()) != null) {
            setUI((ComponentUI)UIManager.getUI(this));
        } else {
        String className = packageName+"."+lafName+getUIClassID();
        try {
        Class<?> c = Class.forName(className);
        Method m = c.getMethod("createUI",JComponent.class);
        ComponentUI newUI = (ComponentUI)m.invoke(null,this);
        setUI(newUI);
        } catch(Exception ex) {
        setUI(BasicSpeedometerUI.createUI(this));
        }
        }
    }
    /**
     * Returns the <code>UIDefaults</code> key used to look up the name of the
     * <code>swing.plaf.ComponentUI</code> class that defines the
     * look and feel for this
     * component. Most applications will never need to call this method.
     * @return the <code>UIDefaults</code> key
     * @see UIDefaults#getUI(javax.swing.JComponent)
     */
    @Override
    public String getUIClassID() {
    return "SpeedometerUI";
    }
   //public static void main(String ar[])
   //{
       //Speedometer s=new Speedometer();
   //}
}

Also i have made this project using netbeans.I have attached my project along with this discussion.

I don't get the port number in SimpleRead.java, that will return me enumerator object.

So please tell me where's the problem..

you have to add two jar file to run this project. comm.jar & RXTXcomm.jar

Lines 16-22:

portList = CommPortIdentifier.getPortIdentifiers();
...
System.out.println(portList.nextElement());

while (portList.hasMoreElements()) {
  ...
  portId = (CommPortIdentifier) portList.nextElement();

This may be a problem. You call nextElement for the println, which will print the first element in the Enumeration, so your while loop will start with the second element (if any) in the Enumeration. The first element will never be processed in that while loop.

(This is a surprisingly common problem - someone adds a println(...nextElement()) to help debug their code, but by consuming an element the print breaks the code it's meant to help test.)

i have tried this things out, but still it gives me null as an output.

i have seen in computer properties that my COM1,LPT1,LPT2 are working.

but still it gives me null output.

Exactly which variable / method return is null?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.