RXTX not working in ubuntu

这一生的挚爱 提交于 2019-12-22 09:37:55

问题


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;

    }
}

回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/10620691/rxtx-not-working-in-ubuntu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!