Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ubuntu Linux上用Java读取大量串行设备_Java_Linux_Serial Port_Usb_Rxtx - Fatal编程技术网

在Ubuntu Linux上用Java读取大量串行设备

在Ubuntu Linux上用Java读取大量串行设备,java,linux,serial-port,usb,rxtx,Java,Linux,Serial Port,Usb,Rxtx,我正在开发一个Java应用程序,它从使用串行端口的设备获取输入。该设备是一个条形码扫描仪,连接到一个多产的PL2303 USB适配器。我相信该适配器的驱动程序已经存在,正如您在运行lsusb命令时看到的: Bus 001 Device 005: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port 我已经安装了java RXTX 64位库。我在/usr/lib/jni下有.so文件,在我的eclipse项目中,我将其作为我的本机库位

我正在开发一个Java应用程序,它从使用串行端口的设备获取输入。该设备是一个条形码扫描仪,连接到一个多产的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.
#

这是库的问题吗?

观察:我在使用minicom时正确地从扫描仪获得输入。因此我知道在某些环境中,JVM中的垃圾收集会中断串行设备的数据流。我真的不认为这是GC问题。流块中的第一个读取调用。我还收到日志消息“Experiative:JNI_OnLoad called.”。观察:我在使用minicom时正确地从扫描仪获得输入。因此我知道在某些环境中,JVM中的垃圾收集会中断串行设备的数据流。我真的不认为这是GC问题。流块中的第一个读取调用。我还收到日志消息“Experimental:JNI_OnLoad called.”。