我正试图从 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);
}
}
}