操作系统linux:red hat enterprise Linux Server release 6.3,kernel 2.6.32-279.e16.x86_64 on x86_64
java版本:自带的/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.
使用串口:RXTX,版本:rxtx-2.1-7-bins-r2。目前已将 rxtx-2.1-7-bins-r2\rxtx-2.1-7-bins-r2\Linux\x86_64-unknown-linux-gnu\librxtxSerial.so拷贝到了/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/amd64下.
代码在windows中运行是正常的,打开串口、发送数据、接收数据都正常。但在linux下只有打开串口是成功的,一发送数据或接收数据就报错。(程序编写时,已注意到windows中打开串口"COM1",linux中打开"/dev/ttyS0")。打开串口代码:
public SerialPort openPort() throws SerialPortParameterFailure, NotASerialPort, NoSuchPort, PortInUse {        try {            //通过端口名识别端口
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(m_comPortNumString);            //打开端口,并给端口名字和一个timeout(打开操作的超时时间)
            CommPort commPort = portIdentifier.open(m_comPortNumString, 2000);            //判断是不是串口
            if (commPort instanceof SerialPort) {
                m_serialPort = (SerialPort) commPort;
                try {    
                    //设置一下串口的波特率等参数
                 m_serialPort.setSerialPortParams(m_bound, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);                              
                } catch (UnsupportedCommOperationException e) {  
                 throw new SerialPortParameterFailure();
                }
                addListener(new SerialListener());
                System.out.println("Open " + m_comPortNumString + " sucessfully !");
                return m_serialPort;
            }        
            else {
             //不是串口
             throw new NotASerialPort();
            }
        } catch (NoSuchPortException e1) {
            throw new NoSuchPort();
        } catch (PortInUseException e2) {
         System.out.println("err:port In Use");
         throw new PortInUse();
        }
    }发送数据代码:
 public void sendToPort( byte[] order) {//throws SendDataToSerialPortFailure, SerialPortOutputStreamCloseFailure {
     OutputStream out = null;
        try {
            out = m_serialPort.getOutputStream();
            out.write(order);
            out.flush();
        } catch (IOException e) {
         //TODO throw new SendDataToSerialPortFailure();
         System.out.println("err:SendDataToSerialPort Failure");
        } finally {
         try {
         if (out != null) {
         out.close();
         out = null;
         }
} catch (IOException e) {
//TODO throw new SerialPortOutputStreamCloseFailure();
}
        }
    }目前在打开串口时可见打印出“"Open /dev/ttyS0 sucessfully ”,但一调用到发送数据就报错:#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f8faa8e4733, pid=1991, tid=0x00007f8faa7db700
#
# JRE version: Java(TM) SE Runtime Environment (8.0_121-b13) (build 1.8.0_121-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [librxtxSerial.so+0x7733]  Java_gnu_io_RXTXPort_nativeDrain+0xc3
#
# 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:
# /root/out---/hs_err_pid1991.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.
#
Aborted (core dumped)打开hs_err_pid1991.log文件中,有如下描述:
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  gnu.io.RXTXPort.nativeDrain(Z)Z+0
j  gnu.io.RXTXPort$SerialOutputStream.flush()V+72
j  serialPort.SerialTool.sendToPort([B)V+15
j  serialPort.mainTest.heartthrobTimer()V+12
j  serialPort.HeartthrobTimes.run()V+46
v  ~StubRoutines::call_stub可见问题发生在sendToPort函数的这一行out = m_serialPort.getOutputStream();-------------
因初接触使用linux,百思不得其解,以为是jdk环境问题,安装了 jre1.8.0_121版本(放到了/usr/lib/jvm//jre1.8.0_121),还是以上错误,请大家走过路过帮帮忙

解决方案 »

  1.   

    最后写错了,纠正一下:
    可见问题发生在sendToPort函数的这一行
     out.flush();
      

  2.   

    搞定了,原来是RXTX库的问题,最后给个用起来正常的地址:http://download.csdn.net/download/bhq2010/5141550
      

  3.   

    你好,能分享一下java代码读取和接收串口信息的代码吗?我现在服务器上装了一个短信猫 现在要接收短信 然后读取 用到程序里面。谢谢
      

  4.   

    楼主在吗,遇到了同样的问题,求分享一下rxtx的包!