我在我的 Java 项目中使用rxtx 库来读取来自我的 gps 芯片的串行数据。现在,我尝试将其移植到我的 Android 设备上,这是一个带有 Android 4.0.3 库存 ROM 的根深蒂固的 Galaxy Nexus。
我已经创建了一个项目并将我的类移植到了一个 android 版本。它使用rxtx 库为 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)
-method 读取传入的数据。
但由于库的本机实现存在一些问题,我无法读取数据。这是我的 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
最后三行重复,直到我终止手机上的进程。串口上没有数据,其实看起来,就是连初始化都没有。对此有什么建议吗?