Attempting to read from COM port causes JVM to crash

I am trying to read some data from a COM port. But I get the answer "9", but I wrote one sop, like System.out.println("This is i/p stream") . Instead, I get a fatal error:

 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); } } } 
+4
source share
2 answers

There may be several errors in this code.

First, the error log does not seem to match the code: from the error log, PCMC.java:189 should be a send () call, but instead it looks empty. Is the code that you sent exactly the code that you ran to cause the error?

Having guessed the version of this code that caused the error, I think that the problem may have been related to the return to inputstream.read (). If the PCMC constructor registered through serialPort.addEventListener (this), then the following sequence may occur:

  • The main PCMC.send () calls that write the message are then blocked in send inputstream.read ().
  • Serial input available. RXTX wants to call PCMC.serialEvent (), but it is blocked in inputstream.read (). Bad things happen.

To successfully use RXTX, the code must either do I / O inside serialEvent (), or not be a listener, but perform normal blocking I / O. If you try to read the serial port both in its main sequence and inside serialEvent (), RXTX will most likely break.

The third thing I noticed: one version of the code that you posted may have tried to close the serial port without first registering as a listener. If you call addEventListener (), you must unregister this listener before calling the close () function on the port.

Again, all my notes are speculations about the version of your code that actually created the error log in the message. In the future, be sure to submit the same version of the code that caused the error. I apologize if I mistakenly thought that the code did not match the error log - and good luck!

0
source

I had a similar error. I solved this by replacing librxtxSerial.so with the one that was found at http://jlog.org/rxtx-lin.html

-1
source

Source: https://habr.com/ru/post/1387998/


All Articles