在没有侦听器事件的情况下从java中的串行端口进行读写

在没有侦听器事件的情况下从java中的串行端口进行读写,java,nullpointerexception,inputstream,Java,Nullpointerexception,Inputstream,我想从串行端口读取字节并按顺序写入。是否有任何方法可以通过创建两个单独的java读写函数从/写入串行端口?我编写了一个程序,但在这里,serialPort.getInputStream即使在打开所需的COM端口后也会发出空指针异常 这是我的密码 package PhyCom; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.

我想从串行端口读取字节并按顺序写入。是否有任何方法可以通过创建两个单独的java读写函数从/写入串行端口?我编写了一个程序,但在这里,serialPort.getInputStream即使在打开所需的COM端口后也会发出空指针异常

这是我的密码

        package PhyCom;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import javax.xml.bind.DatatypeConverter;




public class COMConnect_DLMS
{ 
   public static InputStream inputStream;
   public static OutputStream outputStream;  
   SerialPort serialPort;  
   public long baudRate=19200;
   public int dataBits=SerialPort.DATABITS_8;
   public int stopBits=SerialPort.STOPBITS_1;
   public int parity=SerialPort.PARITY_NONE;
   boolean portFound = false;  
   String defaultPort="COM1";  
   static Enumeration portList ;     
   public static  CommPortIdentifier portId;

   public  COMConnect_DLMS()
  {
   initialize();
  }

private void initialize()
{
    System.out.println("Establishing connection to UART");
    portList = CommPortIdentifier.getPortIdentifiers();  
    while (portList.hasMoreElements()) 
    {  
        portId = (CommPortIdentifier) portList.nextElement();  
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
        {  
            if (portId.getName().equals(defaultPort)) 
            {  
                System.out.println("Found port: "+defaultPort);  
                portFound = true; 
            }
        }
    }
     try 
    {  
     serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);  
    } 
    catch (PortInUseException e) {} 
    System.out.println(portId.getName());

     try
    {
        inputStream=serialPort.getInputStream();
    }
    catch (IOException e) {e.printStackTrace();}
    /*try
    {
        serialPort.addEventListener((SerialPortEventListener) this);
    }
    catch(TooManyListenersException e){}
    serialPort.notifyOnDataAvailable(true);*/
    try 
    {  
     // set port parameters  
     serialPort.setSerialPortParams((int) baudRate, dataBits,stopBits,parity);  
    }
    catch (UnsupportedCommOperationException e) {}


    try
    {
        outputStream=serialPort.getOutputStream();
    }
    catch (IOException e) {}
}


 public void serialEvent(SerialPortEvent event)
 {

    if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE)
    {
        byte[] readBuffer = new byte[20];

        try 
        {
            while (inputStream.available() > 0)
            {
                int numBytes = inputStream.read(readBuffer);
            }
            System.out.print(new String(readBuffer));
        } 
        catch (IOException e) {System.out.println(e);}

    }
}
public static void writePacket(byte[] msg)
{
   System.out.println("Writing \""+DatatypeConverter.printHexBinary(messageString));  
  try 
  {  
    // write string to serial port  
     outputStream.write(msg);  
    //  System.out.println("written");
  } 
  catch (IOException e) {}

}

}

此外,我还想了解更多有关串行端口情况下的NullPointerException的信息。谢谢。我们将非常感谢您的建议。

嗯,这是一个小问题:

try {
    serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {
}
如果出于任何原因,您无法打开特定端口,那么您将拥有一个空的catch块。更糟糕的是,你没有报告发生了什么,或者为什么它决定连接失败

但这就是让你崩溃的原因。忽略serialPort变量发生的任何错误,并将其设置为null。它以这种方式初始化为一个字段

如果无法从调用中获取实例,则不应继续。考虑一下:

在catch块中放置System.exit1调用以指示程序无法继续,或 更好的选择是在try…catch中使用与serialPort有关的初始化方法包装所有内容。
所以此NullPointerException发生在何处?它发生在以下语句中:inputStream=serialPort.getInputStream;这很可能是serialPort变量必须为null@ThusithaThilinaDayaratne:我能知道它为什么为空吗?在上面的代码中我做错了什么?可能是因为serialPort=serialPort portId.opensimpleradapp,2000;当静态串行端口为空时引发异常