简体   繁体   中英

RXTX not working in ubuntu

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;

    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM