我正在开发一个Java应用程序,它从使用串行端口的设备获取输入。有问题的设备是条形码扫描仪,连接到Prolific PL2303 USB适配器。我相信该适配器的驱动程序存在,因为您在运行lsusb命令时可以看到此行:
Bus 001 Device 005: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
我已经安装了java RXTX 64位库。我在/ usr / lib / jni下有.so文件,在我的eclipse项目中,我指的是我的本地库位置。我已将RXTXComm.jar放在项目的lib目录中,并成功将其加载到我的应用程序中。
我可以看到列出的/ dev / ttyUSB0设备。我用chmod 777命令修改了它的权限是安全的。我还将我的用户添加到了拨出组。
有了这个,并设置正确的参数(波特率,奇偶校验,停止位和数据位),我能从/ dev / ttyUSB0串行读取吗?我试过写一个简单的应用程序来实现这一目标。在一个类中,我使用以下代码连接端口:
public void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
listener.setEntrada(commPort.getInputStream());
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.addEventListener(listener);
} else {
System.out.println("Error. Not a serial Port");
}
}
监听器是Spring注入的,并实现了SerialPortEventListener。但是,当我使用扫描程序时,永远不会调用重写的serialEvent(SerialPortEvent ev)方法。
我错过了什么?我是否必须将设备映射到ttyS *端口才能从中读取?
*编辑* 似乎我忘了向监听器添加通知,通过添加“serialPort.notifyOnDataAvailable(true);”来解决。添加事件监听器后。
但是,现在从inputStream读取时出现以下错误:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f3b82aee462, pid=12531, tid=139893572269824
#
# JRE version: Java(TM) SE Runtime Environment (8.0_66-b17) (build 1.8.0_66-b17)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.66-b17 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [librxtxSerial.so+0x6462] read_byte_array+0x52
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/zanni/workspace/BarcodeComms/hs_err_pid12531.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
这是图书馆的问题吗?