RXTX serial port library on Android

泪湿孤枕 提交于 2020-01-05 04:33:08

问题


I am using the rxtx library in my Java project to read the serial data coming from my gps chip. Now, I try to port this to my android device, a rooted Galaxy Nexus with an Android 4.0.3 stock rom.

I already created a project and ported my classes to an android version. It uses the rxtx library for android like this:

public void init() {
    try {
        Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();
        // / See what ports are available. and latch on desired port
        while (portIdentifiers.hasMoreElements()) {
            CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
            if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL && pid.getName().equals(PORT)) {
                portId = pid;
                break;
            }
        }
        serialPort = (SerialPort) portId.open("test", 5000);
        in = serialPort.getInputStream();
        dis = new DataInputStream(in);
        serialPort.addEventListener(this);
        serialPort.setSerialPortParams(BAUDRATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        serialPort.notifyOnDataAvailable(true);
    } catch (PortInUseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TooManyListenersException e) {
        e.printStackTrace();
    } catch (UnsupportedCommOperationException e) {
        e.printStackTrace();
    }

}

After this I try to read the incoming data with the serialEvent(SerialPortEvent event)-method.

But I do not get to read the data, because of some problems with the native implementations of the library. This is my logcat output:

11-14 11:24:49.793: I/GNU.IO(15496): JNI_OnLoad called.
11-14 11:24:49.793: I/GNU.IO(15496): Testing the version.
11-14 11:24:49.800: I/GNU.IO(15496): entering RXTXCommDriver:getDeviceDirectory 
11-14 11:24:49.800: I/GNU.IO(15496): TestREAD.
11-14 11:24:49.808: I/GNU.IO(15496): /dev/ttyO0
11-14 11:24:49.808: I/GNU.IO(15496): entering RXTXPort:testRead 
11-14 11:24:49.808: I/GNU.IO(15496): RXTX Warning:  Removing stale lock file. /data/local/tmp/LCK..ttyO0
11-14 11:24:49.816: I/GNU.IO(15496): leaving RXTXPort:testRead 
11-14 11:24:49.832: I/GNU.IO(15496): entering RXTXCommDriver:getDeviceDirectory 
11-14 11:24:49.840: I/GNU.IO(15496): TestREAD.
11-14 11:24:49.847: I/GNU.IO(15496): /dev/ttyO0
11-14 11:24:49.847: I/GNU.IO(15496): entering RXTXPort:testRead 
11-14 11:24:49.847: I/GNU.IO(15496): leaving RXTXPort:testRead 
11-14 11:24:49.871: I/GNU.IO(15496): entering RXTXPort:Initialize 
11-14 11:24:49.879: I/GNU.IO(15496): entering RXTXPort:open 
11-14 11:24:49.886: I/GNU.IO(15496): leaving RXTXPort:open 
11-14 11:24:49.886: I/GNU.IO(15496): entering eventLoop
11-14 11:24:49.886: I/GNU.IO(15496):  
11-14 11:24:49.886: I/GNU.IO(15496): entering send_event 
11-14 11:24:49.886: I/GNU.IO(15496): send_event: !eventloop_interupted
11-14 11:24:49.886: I/GNU.IO(15496): send_event: jclazz
11-14 11:24:49.886: I/GNU.IO(15496): send_event: calling
11-14 11:24:49.886: I/GNU.IO(15496): send_event: called
11-14 11:24:49.886: I/GNU.IO(15496): leaving send_event 
11-14 11:24:49.886: I/GNU.IO(15496): entering check_tiocmget_changes
11-14 11:24:49.886: I/GNU.IO(15496): leaving check_tiocmget_changes
11-14 11:24:49.886: I/GNU.IO(15496): port_has_changed_fionread: change is 0
11-14 11:24:49.886: I/GNU.IO(15496): entering send_event 
11-14 11:24:49.886: I/GNU.IO(15496): send_event: !eventloop_interupted
11-14 11:24:49.886: I/GNU.IO(15496): send_event: jclazz
11-14 11:24:49.886: I/GNU.IO(15496): send_event: calling
11-14 11:24:49.886: I/GNU.IO(15496): send_event: called
11-14 11:24:49.886: I/GNU.IO(15496): leaving send_event 
11-14 11:24:49.886: I/GNU.IO(15496): entering check_tiocmget_changes
11-14 11:24:49.886: I/GNU.IO(15496): leaving check_tiocmget_changes
11-14 11:24:49.886: I/GNU.IO(15496): port_has_changed_fionread: change is 0
11-14 11:24:49.894: I/GNU.IO(15496): entering send_event 
11-14 11:24:49.894: I/GNU.IO(15496): send_event: !eventloop_interupted
11-14 11:24:49.894: I/GNU.IO(15496): send_event: jclazz
11-14 11:24:49.910: I/GNU.IO(15496): leaving RXTXPort:translate_speed 
11-14 11:24:49.910: I/GNU.IO(15496): entering RXTXPort:nativeSetSerialPortParams 
11-14 11:24:49.910: I/GNU.IO(15496): entering translate_date_bits 
11-14 11:24:49.910: I/GNU.IO(15496): entering translate_stop_bits 
11-14 11:24:49.910: I/GNU.IO(15496): leaving RXTXPort:translate_stop_bits 
11-14 11:24:49.910: I/GNU.IO(15496): entering translate_parity 
11-14 11:24:49.910: I/GNU.IO(15496): leaving translate_parity 
11-14 11:24:49.910: I/GNU.IO(15496): leaving RXTXPort:nativeSetSerialPortParams 
11-14 11:24:49.910: I/GNU.IO(15496): send_event: calling
11-14 11:24:49.910: I/GNU.IO(15496): send_event: called
11-14 11:24:49.910: I/GNU.IO(15496): leaving send_event 
11-14 11:24:49.910: I/GNU.IO(15496): entering check_tiocmget_changes
11-14 11:24:49.910: I/GNU.IO(15496): leaving check_tiocmget_changes
11-14 11:24:49.910: I/GNU.IO(15496): port_has_changed_fionread: change is 0
11-14 11:24:49.910: I/GNU.IO(15496): entering send_event 
11-14 11:24:49.910: I/GNU.IO(15496): send_event: !eventloop_interupted
11-14 11:24:49.910: I/GNU.IO(15496): send_event: jclazz
11-14 11:24:49.910: I/GNU.IO(15496): send_event: calling
11-14 11:24:49.910: I/GNU.IO(15496): send_event: called
11-14 11:24:49.910: I/GNU.IO(15496): leaving send_event 
11-14 11:24:49.910: I/GNU.IO(15496): entering check_tiocmget_changes
11-14 11:24:49.910: I/GNU.IO(15496): leaving check_tiocmget_changes
11-14 11:24:49.910: I/GNU.IO(15496): port_has_changed_fionread: change is 0
11-14 11:24:49.910: I/GNU.IO(15496): entering check_tiocmget_changes
11-14 11:24:49.910: I/GNU.IO(15496): leaving check_tiocmget_changes
11-14 11:24:49.910: I/GNU.IO(15496): port_has_changed_fionread: change is 0
11-14 11:24:49.918: I/GNU.IO(15496): entering check_tiocmget_changes
11-14 11:24:49.918: I/GNU.IO(15496): leaving check_tiocmget_changes
11-14 11:24:49.918: I/GNU.IO(15496): port_has_changed_fionread: change is 0
11-14 11:24:49.918: I/GNU.IO(15496): entering check_tiocmget_changes
11-14 11:24:49.918: I/GNU.IO(15496): leaving check_tiocmget_changes
11-14 11:24:49.918: I/GNU.IO(15496): port_has_changed_fionread: change is 0

The last three lines repeat until I kill the process on my phone. There is not data on the serial port, in fact it seems, that is has not even been initialized. Any suggestions to this?


回答1:


In Android to work with serial port, I recommended to use the following library: Serial Port API. Works for me and I believe will be useful for you.



来源:https://stackoverflow.com/questions/13377212/rxtx-serial-port-library-on-android

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