tags:

views:

3632

answers:

13

I have followed the instructions to setup rxtx on windows from http://www.jcontrol.org/download/readme_rxtx_en.html.

What I did exactly was copy rxtxSerial.dll to "C:\Program Files\Java\jdk1.6.0_07\jre\bin" and copied RXTXcomm.jar to "C:\Program Files\Java\jdk1.6.0_07\jre\lib\ext" (my JAVA_HOME variable is set to C:\Program Files\Java\jdk1.6.0_07\jre)

I also added RXTXcomm.jar to my eclipse project.

But when I run it, it still says "NoSuchPortException"

Devel Library
=========================================
Native lib Version = RXTX-2.0-7pre1
Java lib Version   = RXTX-2.0-7pre1
java.lang.ClassCastException: gnu.io.RXTXCommDriver cannot be cast to gnu.io.CommDriver thrown while loading gnu.io.RXTXCommDriver
gnu.io.NoSuchPortException
    at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:218)
    at TwoWaySerialComm.connect(TwoWaySerialComm.java:20)
    at TwoWaySerialComm.main(TwoWaySerialComm.java:107)

In my java file, I tell it:

        try
        {
            (new TwoWaySerialComm()).connect("COM4");
        }

and I've also tried the Java Comm API. Both cannot recognize my serial port but I am sure I followed the instruction correctly. There files are there.

Does anybody have any idea what it could be?

+1  A: 

Try putting rxtxSerial.dll in

C:\Program Files\Java\jdk1.6.0_07\jre\lib\bin
                                      ^^^
Ken Gentle
A: 

It may be that your system does not have a COM4 defined or it's not accessible. It's hard to guess what may be wrong, because you haven't posted you port init code - what you posted looks like wrapper code.

Here is my working init code using the javax.comm API (but using SerialPort from serialio.com):

// name comes from config and is "COM1", "COM2", ...
SerialPort port=(SerialPort)CommPortIdentifier.getPortIdentifier(name).open("YourPortOwnerIdHere",5000);      // owner and ms timeout
port.setSerialPortParams(bau,dtb,stb,par);
port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN|SerialPort.FLOWCONTROL_RTSCTS_OUT);
port.enableReceiveTimeout(1000);

Hopefully this points you in the right direction.

Software Monkey
A: 

you can use CommPortIdentifier.getPortIdentifiers()

to identify all possible ports your system finds.

Peter
Found this via search as I've got an instance of a new updated app not finding the com port (and the old one still does). Tried the getPortIdentifiers and interestingly enough, it just returns null. Does that mean no ports available, or am I doing something else wrong?
Brian Knoblauch
Update on mine. Appears to be something horribly broken somewhere. I have to checkout from SVN to a Solaris box, copy the files to Windows, compile there, then drag the build back to Solaris to make it go. For some reason the built version fails if I either SVN checkout on Windows or do the build on Solaris. Sigh. Apparently something's corrupted somewhere.
Brian Knoblauch
A: 

I am not too familiar with RXTX, but is this normal?

java.lang.ClassCastException: gnu.io.RXTXCommDriver cannot be cast to gnu.io.CommDriver thrown while loading gnu.io.RXTXCommDriver

Otherwise maybe the problem is not with the port itself after all, but something with the classes themselves? Just a guess.

Daniel Schneller
A: 

I agree that you're problem looks like a ClassCastException and not the other.

For windows, I'm using "Windows Java Serial Com Port Driver" at http://www.engidea.com/blog/informatica/winjcom/winjcom.html and it is much easier for me to set up.

In either case, you want the DLL in the BIN directory, not LIB\BIN as was suggested. At least that's what's working for me. I'm using NetBeans and I've also found it helpful to put the jar and dll into various bin and lib\ext folders in the JDK.

Note that if you have multiple versions of the JRE on your machine, you might not be using the one that you think you are using. Also, as a practical matter I've found it more helpful to just copy both the jar and dll into the various bin and lib\ext folders. Makes it just a paste, paste, paste operation.

For windows, I recommend "Windows Java Serial Com Port Driver" because it solved my problems with USB serial ports. I had fits with RXTX because it would crash when the USB was unplugged. winjcom solved that problem and others as well. It has very helpful error exceptions.

Also, make sure your serial drivers are up-to-date. Downloading an update fixed my other bug. -Stosh

johnstosh
A: 

Hi there

I am not able to close the serial port.... Can anybody help me out !.

Below is my code

import java.io.; import java.util.; import gnu.io.*;

public class ReadCommPort implements SerialPortEventListener {

static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; OutputStream outputStream; public SerialPort serialPort; List byteList = new ArrayList(); public static Message message = null;

public void readData() { boolean portFound = false; String defaultPort = "COM1"; 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;
               buildSerialPort();
           }
       }
   }
   if ( ! portFound ) {
       System.out.println( "port " + defaultPort + " not found." );
   }

}

public void buildSerialPort() { try { serialPort = (SerialPort) portId.open( "ReadCommPort", 1 ); inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); serialPort.addEventListener( this ); serialPort.notifyOnDataAvailable(true); serialPort.setSerialPortParams( 2400, SerialPort.DATABITS_7, SerialPort.STOPBITS_1,

SerialPort.PARITY_NONE ); } catch ( Exception e ) { e.printStackTrace(); } }

@SuppressWarnings("unchecked") public void serialEvent( SerialPortEvent event ) {

   switch ( event.getEventType() ) {
       case SerialPortEvent.BI:
           System.out.println( "BI");
           break;

       case SerialPortEvent.OE:
           System.out.println( "OE");
           break;

       case SerialPortEvent.FE:
           System.out.println( "FE");
           break;

       case SerialPortEvent.PE:
           System.out.println( "PE");
           break;

       case SerialPortEvent.CD:
           System.out.println( "CD");
           break;

       case SerialPortEvent.CTS:
           System.out.println( "CTS");
           break;

       case SerialPortEvent.DSR:
           System.out.println( "DSR");
           break;

       case SerialPortEvent.RI:
           System.out.println( "RI");
           break;

       case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
           System.out.println( "OUTPUT_BUFFER_EMPTY");
           break;

       case SerialPortEvent.DATA_AVAILABLE :
           try {
               int len = inputStream.available();
               byte[] readBuffer = new byte[ len ];
       // processing data code..
       // close the port
           // release all resources...
                                  serialPort.removeEventListener();
                               try
               {
                                   serialPort.addEventListener( null );
                               }
                               catch (TooManyListenersException e)
                               {
                                   e.printStackTrace();
                               }
                               inputStream.close();
                               outputStream.close();
                               serialPort.close();
           }
           catch ( IOException e )
   {
               e.printStackTrace();
           }
       break;
   }

}

public static void main(String[] args) { new ReadCommPort().readData(); } }

A: 

I also had a problem when closing the serialPort within the serialEvent function. Maybe it's a deadlock problem, where the close method waits forever for serialEvent's lock to be released. Starting a new thread to close the port worked for me.

Jean
A: 

Hi!

I'm using lib RXTX on Linux Centos 5.2 and I'm alway getting the following error:

gnu.io.NoSuchPortException
        at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:269)

The lib is configured following the specifications from the lib and the port is present. Could anyone help me with this question?

Thanks in advance,

Pinheiro

Pinheiro
A: 

You can also try an alternative solution that was specifically implemented for Windows. There should be plenty available, one of them you can get from http://www.caerustech.com/JCommWin32.php

Shultz

wiking
A: 

For linux, make sure that you added your user to the lock in /etc/group.

Steven
A: 

Hello, I need some help with rxtx. I run:

Enumeration pList = CommPortIdentifier.getPortIdentifiers();

and it returns empy :(. I have a windows 7 OS on HP system. I don't know how to fix this to retrieve the list of available ports.

Thanks.

Dorix
A: 

Hello Dorix,

For your question, my code is the following:

if (idPuerto == null) { formulario = form; boolean encontrado = false;

            listaPuertos = CommPortIdentifier.getPortIdentifiers();

            while( listaPuertos.hasMoreElements() && encontrado == false )
            {
              idPuerto = (CommPortIdentifier)listaPuertos.nextElement();
              //System.out.println(idPuerto.getName());

              if( idPuerto.getPortType() == CommPortIdentifier.PORT_SERIAL )
              {
                if( idPuerto.getName().equals(RFIDBascApp.ComBasc) )
                {        
                    encontrado = true;
                    logger.AddInfoUser("Puerto serie encontrado");

                  }
                }
              }
Memafe
A: 

Hello,

I need that somebody help me. I have another problem. I have a class which manage all function about serial port. I have a method that adds the event listener and I have another method that removes the event listener, but my program dies and does nothing in this last method.

public class PuertoSerie extends Thread implements Runnable, SerialPortEventListener {

public PuertoSerie(RFIDBascApp form) {
try { if (idPuerto == null) { formulario = form; boolean encontrado = false;

            listaPuertos = CommPortIdentifier.getPortIdentifiers();

            while( listaPuertos.hasMoreElements() && encontrado == false )
            {
              idPuerto = (CommPortIdentifier)listaPuertos.nextElement();
              //System.out.println(idPuerto.getName());

              if( idPuerto.getPortType() == CommPortIdentifier.PORT_SERIAL )
              {
                if( idPuerto.getName().equals(RFIDBascApp.ComBasc) )
                {        
                    encontrado = true;
                    logger.AddInfoUser("Puerto serie encontrado");


                  }
                }
              }

           AbrirPuerto();
        }
    }
    catch(Exception e)
    {
        logger.AddError("Excepcion en puerto serie: " + e.getMessage());
    }
}

public void AbrirPuerto() { try { puerto = (SerialPort)idPuerto.open( "RFIDBasc",2000 ); } catch( Exception e ) { logger.AddError("Excepcion 1 en abrir puerto serie: " + e.getMessage()); }

    //Se obtiene un canal de entrada
    if (puerto != null)
    {
        try
        {
            entradaPuerto = puerto.getInputStream();
        } 
        catch( Exception e )
        {
            logger.AddError("Excepcion 2 en abrir puerto serie: " + e.getMessage());
        }


        //Se fijan los parámetros de comunicación del puerto
        try
        {           
            puerto.setSerialPortParams( 9600,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE );

        }    
        catch( Exception e )
        {
            logger.AddError("Excepcion 3 en abrir puerto serie: " + e.getMessage());
        }
    }
}

public boolean Arrancar() {

//Se añade un manejador de eventos al puerto serie
    try
    {
        if (puerto != null)
        {
            puerto.addEventListener( this );
            puerto.notifyOnDataAvailable( true );

            manejadorEventos = true;
            pesocomunicado = false;

        }
        else
        {
            logger.AddMessage("No se puede añadir manejador de eventos porque puerto = null");
        }
    }
    catch( Exception e )
    {
        logger.AddError("Excepcion en añadir manejador eventos puerto serie: " + e.getMessage());
        manejadorEventos = false;
    }

    return manejadorEventos;
}

public void Parar() { manejadorEventos = false;

    //Desactivo el manejador de eventos del puerto serie
    try
    {
        if (puerto != null)
        {
            puerto.removeEventListener();

        }
        else
        {
            logger.AddError("El manejador de eventos no se ha eliminado pq puerto = null");
        }
    }
    catch (Exception e)
    {
        logger.AddError("Excepcion en eliminar manejador eventos puerto serie: " + e.getMessage());
    }

}

When I call to method Parar() from another class, my program dies in this sentence:

puerto.removeEventListener();

Does anyone know what is happening?

Thanks in advance,

Memafe

Memafe