3

Finally managed to read from rxtx in windows but now I just cannot make it work in Ubuntu.I get the rxtx libs using apt-get ,but when i run the application,i can not see anything,tried a couple of try-catch blocks and i don't even get the exceptions,and since Ubuntu based debugging is not possible for now, i can not pinpoint the problem. (Ubuntu is 12.04 64 bit).

import gnu.io.*;
import java.io.*;
import javax.swing.JOptionPane;

public class ReadComPort {

    public static void main(String[] s) {
        readcomport();
    }

    public static String readcomport() {
        String value = null;

        try {
            // CommPortIdentifier portIdentifier = CommPortIdentifier
            // .getPortIdentifier("COM1");

            // String comportidentifier = "COM1"; //*win
            String comportidentifier = "/dev/ttyS0";

            CommPortIdentifier portIdentifier = null;
            portIdentifier = CommPortIdentifier.getPortIdentifier(comportidentifier);

            if (portIdentifier.isCurrentlyOwned()) {
                JOptionPane.showMessageDialog(null, "port in use");
            } else {

                SerialPort serialPort = (SerialPort) portIdentifier.open("ReadComPort", 500);
                JOptionPane.showMessageDialog(null, serialPort.getBaudRate());

                serialPort.setSerialPortParams(serialPort.getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                // serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
                serialPort.setDTR(true);
                serialPort.setRTS(true);

                InputStream mInputFromPort = serialPort.getInputStream();

                Thread.sleep(500);
                byte mBytesIn[] = new byte[32];
                mInputFromPort.read(mBytesIn);

                value = new String(mBytesIn);

                mInputFromPort.close();
                serialPort.close();
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Exception : " + ex.getMessage());

        }

        return value;

    }
}
Mohamed Jameel
  • 602
  • 5
  • 21
Sin5k4
  • 1,556
  • 7
  • 33
  • 57

2 Answers2

1

Check whether the configuration file javax.comm.properties is on the classpath. I have had endless issues with RXTX because of this file - it just fails silently.

mcfinnigan
  • 11,442
  • 35
  • 28
  • erm cant seem to find it...Where is it located normally? – Sin5k4 May 16 '12 at 15:01
  • ah. That's likely the issue then. Check here : http://pradnyanaik.wordpress.com/2009/04/07/communicating-with-ports-using-javaxcomm-package-for-windows/ - you will probably need to find a javax-comm package somewhere that contains the properties file – mcfinnigan May 16 '12 at 15:02
  • OK found a rar file which contains 3 files,and one of them is the .properties file.How do i add it? From build path/configure build path menu? – Sin5k4 May 16 '12 at 15:20
  • Add it as a project resource, I presume. – mcfinnigan May 16 '12 at 15:22
  • Tried adding it to resources and to my usr/jdk/... folders and stil nothing,meh :/ – Sin5k4 May 17 '12 at 06:46
  • Now i'm getting gnu.io.nosuchportexception error.At least it shows some signs of life :) – Sin5k4 May 17 '12 at 12:37
  • hah. yes, that's a bit healthier. Now you probably just need to make sure the serial ports are accessible to the user your process is running as :) – mcfinnigan May 17 '12 at 12:44
  • "Caught java.lang.UnsatisfiedLinkError: gnu.io.RXTXCommDriver.nativeGetVersion()Ljava/lang/String; while loading driver gnu.io.RXTXCommDriver" grrrrrrrrrr,seriously does anybody here got this rxtx thingie to work? – Sin5k4 May 17 '12 at 13:54
  • Have you succeeded in your problem? I've been working on the same problem almost a week. – gkiko Mar 18 '14 at 07:33
1

I had the same problem yesterday, and found this:

String serialPortID = "/dev/ttyAMA0";
System.setProperty("gnu.io.rxtx.SerialPorts", serialPortID);

Thath is, you need to set the gnu.io.rxtx.SerialPorts system property, and the value should be the name of the port you want to open.

Vidar S. Ramdal
  • 1,164
  • 1
  • 14
  • 38