Android上的RXTX串口库

时间:2012-11-14 10:39:13

标签: android serial-port rxtx

我在我的Java项目中使用rxtx library来读取来自我的gps芯片的串行数据。现在,我尝试将其移植到我的Android设备上,这是一款带有Android 4.0.3存储功能的僵化的Galaxy Nexus。

我已经创建了一个项目并将我的类移植到了android版本。它使用rxtx library for android这样:

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();
    }

}

在此之后,我尝试使用serialEvent(SerialPortEvent event)方法读取传入数据。

但是由于库的本机实现存在一些问题,我无法读取数据。这是我的logcat输出:

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

最后三行重复,直到我在手机上终止该过程。串口上没有数据,实际上似乎还没有初始化。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

在Android中使用串口,​​我建议使用以下库:Serial Port API。适合我,我相信对你有用。