2011-12-26 46 views
4

我試圖從COM端口讀取一些數據。但我收到了「9」的迴應,但我寫了一個s.o.p,如System.out.println("This is i/p stream")。取而代之的是,我得到一個致命的錯誤:試圖從COM端口讀取崩潰JVM

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); 
     } 
    } 





} 

回答

0

有幾件事情可能是錯誤在此代碼。

首先,錯誤日誌似乎不符合代碼:從錯誤日誌中,PCMC.java:189應該是對send()的調用,但似乎是空白行。您發佈的代碼是否是您運行導致錯誤的代碼?

猜測導致錯誤的代碼版本,我認爲這個問題可能是inputstream.read()中的重入問題。如果PCMC構造通過serialPort.addEventListener(本)註冊本身,則可能會發生以下順序:

  1. 主要調用PCMC.send(),它寫入一條消息,然後在發送的inputstream.read塊()。
  2. 串行輸入數據變爲可用。 RXTX想要調用PCMC.serialEvent(),但它在inputstream.read()中被阻塞。壞事發生。

要成功使用RXTX,代碼應該在serialEvent()中執行I/O,或者不能是偵聽器,並且必須執行常規的阻塞I/O。如果您嘗試在主序列和serialEvent()內從串口讀取數據,則RXTX可能會中斷。

我注意到的第三件事:您發佈的代碼的一個版本可能試圖關閉串口,而不先註銷自己作爲監聽器。如果調用addEventListener(),則應該在調用端口上的close()之前取消註冊該偵聽器。

同樣,我的所有筆記都是關於您的代碼版本的推測,它們實際上會在帖子中生成錯誤日誌。將來,請確保發佈產生錯誤的代碼的相同版本。如果我錯誤地認爲代碼與錯誤日誌不符 - 抱歉,祝你好運!