尝试从COM端口读取数据导致JVM崩溃

4

我正试图从 COM 端口读入一些数据。但是我得到了一个响应 "9",而我写了一个像这样的 System.out.println("This is i/p stream") 的语句。然而,我收到了致命错误:

java.lang.NoSuchFieldError: eis
# A fatal error has been detected by the Java Runtime Environment:
#
    at gnu.io.RXTXPort.readByte(Native Method)
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x10009775, pid=3048, tid=2188
#
# JRE version: 6.0_24-b07
# Java VM: Java HotSpot(TM) Client VM (19.1-b02 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [rxtxSerial.dll+0x9775]
#
# An error report file with more information is saved as:
# D:\Manly\PCMC\hs_err_pid3048.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
    at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1250)
#
    at pcmc.PCMC.send(PCMC.java:136)
    at pcmc.PCMC.main(PCMC.java:189)
Exception in thread "main" Java Result: 1

 package pcmc;



import gnu.io.*;
import java.io.*;
import java.io.InputStream;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class PCMC implements  SerialPortEventListener
{
    Enumeration portList;
    CommPortIdentifier portId;
    SerialPort serialPort;
    DataInputStream dis;
    OutputStream outputStream;
    InputStream inputStream;
    Thread readThread;

     String messageString;
     String messageString1;
 static PCMC f;
     String strResponse="";
     PCMC pWriter;
     String msg[]=new String[200];
     int ix=0;

     boolean msgEnd=true;

    String className;
     static Enumeration ports;
    static CommPortIdentifier pID;
    static String messageToSend = "ComPortSendMsg deatails!\n";


    public PCMC(String className) throws NoSuchPortException, IOException, TooManyListenersException
    {

            this.className=className;
            //portList =  CommPortIdentifier.getPortIdentifiers();
           ports = CommPortIdentifier.getPortIdentifiers();
         System.out.println("ports name"+ports);
        while(ports.hasMoreElements())
        {
            pID = (CommPortIdentifier)ports.nextElement();
            System.out.println("Port Name " + pID.getName());

            if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                System.out.println("Port Name 1 " + pID.getName());
                if (pID.getName().equals("COM1"))
                {
                    try {
                        System.out.println("Port Name 2 " + pID.getName());
                         System.out.println("COM1 found");
                         serialPort=(SerialPort)pID.open(className, 1000);

                         outputStream=serialPort.getOutputStream();

                         inputStream=serialPort.getInputStream();
                     //    dis=new DataInputStream(inputStream);
                          //f.serialEvent1(inputStream);
                         //serialPort.addEventListener(this);
                         break;
                    } catch (PortInUseException ex) {
                        Logger.getLogger(PCMC.class.getName()).log(Level.SEVERE, null, ex);
                    }


                }
            }
        }

        //outputStream.write(messageToSend.getBytes());





    }

      public void closePort()
    {
        try
        {
            System.out.println((char)13);
            inputStream.close();
            System.out.println("Finished2");
            outputStream.close();
            System.out.println("Finished1");

            serialPort.close();
            System.out.println("Finished");


        }
        catch(Exception e)
        {
            System.out.println("Close Error"+e);
        }


    }


    public void send(String phno,String msg)
    {
String s = "AT+CMGF="+1+"\n";
  int data;
        String r;
        byte[] buffer=new byte[1000];
s+=(char)13;
System.out.println("AT+CMGF command :"+s);

       messageString = "AT+CMGS=\""+phno+"\"\r";
        messageString1 = msg+"\n" +(char)26;
        System.out.println("AT CMGS "+messageString);
        System.out.println("AT CMGS  "+messageString1);

        try
        {
            outputStream.write(s.getBytes());
            //outputStream.notifyAll();
            Thread.sleep(1000);

           System.out.println(inputStream.available()+" : ramdn : ");

          //  System.out.println(dis.readInt()+" : radsgas : ");

        try
        {
            int len = 0;
            while ( ( data = inputStream.read())!= -1 )
            {
                System.out.println("This is i/p stream");
                buffer[len++] = (byte) data;
            }
            r = new String(buffer,0,len);
            System.out.println("this is input stream msg"+r);
        }
        catch ( IOException e )
        {
            e.printStackTrace();
            System.exit(-1);
        }






           System.out.print("this is send try block");


        }
         catch (Exception e)
         {
           System.out.println(e);
          }



    }



        public static void main(String args[]) throws NoSuchPortException, IOException, TooManyListenersException
        {

            PCMC f=new PCMC("Msg Sending");

            try {
                f.send("9994106650","Wish U Happy New Year in advance.");
               // f.send("9884345649","Wish U Happy New Year in advance.");
                Thread.sleep(1000);

                 //  f.send("9597502571","Good Bye..");
                 //  f.send("9597502571","Good Bye..");
            } catch (InterruptedException ex) {
                Logger.getLogger(PCMC.class.getName()).log(Level.SEVERE, null, ex);
            }
          //  }


            System.out.println("---------END--------");

              //f.deleteAll();

            f.closePort();


        }






       }

    @Override
    public void serialEvent(SerialPortEvent spe) {

            int data;
        String r;
        byte[] buffer=new byte[1000];
        try
        {
            int len = 0;
            while ( ( data = inputStream.read()) > -1 )
            {
                buffer[len++] = (byte) data;
            }
            r = new String(buffer,0,len);
            System.out.println("this is input stream msg"+r);
        }
        catch ( IOException e )
        {
            e.printStackTrace();
            System.exit(-1);
        }
    }





}
2个回答

0

这段代码可能有一些问题。

首先,错误日志似乎与代码不匹配:从错误日志来看,PCMC.java:189 应该是一个调用 send() 的语句,但实际上却是一个空行。你发布的代码是否完全与导致错误的代码相同?

根据导致错误的代码版本的猜测,我认为问题可能出在 inputstream.read() 中的可重入性上。如果 PCMC 构造函数通过 serialPort.addEventListener(this) 注册了自己,那么以下序列可能会发生:

  1. Main 调用 PCMC.send(),它写入一条消息,然后在 send 的 inputstream.read() 中阻塞。
  2. 串口输入数据变得可用。RXTX 想要调用 PCMC.serialEvent(),但它被阻塞在 inputstream.read() 中。接下来会发生糟糕的事情。

为了成功使用 RXTX,代码应该在 serialEvent() 中执行 I/O,或者不能成为监听器,并且必须执行传统的阻塞式 I/O。如果你尝试在主序列和 serialEvent() 中都从串口读取数据,RXTX 可能会崩溃。

我注意到的第三件事是:您发布的代码版本之一可能尝试在未注销自身作为侦听器的情况下关闭串行端口。如果调用addEventListener(),则应在调用close()之前注销该侦听器。
再次说明,我所有的笔记都是关于实际生成错误日志的代码版本的猜测。将来,请确保发布生成错误的相同代码版本。如果我错误地认为代码与错误日志不匹配,请接受我的道歉,并祝您好运!

-2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接