public class SerialBuffer { private String  Content  = "";

private String CurrentMsg,  TempContent;

private boolean  available  = false;

private int LengthNeeded = 1;

public void PutChar(int c) {
// TODO Auto-generated method stub

Character d = new Character((char) c);
Content = Content.concat(d.toString());

if(LengthNeeded  <  Content.length()){

available = true;

}
notifyAll();
}

public synchronized String GetMsg(int length){

System.out.println("3333333333333");
LengthNeeded = length;
notifyAll();

if(LengthNeeded  > Content.length()){

available = false;
while(available = false){

try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

CurrentMsg  =  Content.substring(0,LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();

return CurrentMsg;
}
}
package net.hot;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class SerialBean {

static String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
static OutputStream out;
static InputStream in ;
SerialBuffer SB1;
ReadSerial RT;


public SerialBean (int PortID){
PortName = "COM" + PortID;
}

public int Initialize()
    {
      int InitSuccess = 1;
      int InitFail    = -1;
    try
    {
      portId = CommPortIdentifier.getPortIdentifier(PortName);
      try
      {
        serialPort = (SerialPort)
        portId.open("Serial_Communication", 2000);
      } catch (PortInUseException e)
      {
        return InitFail;
      }
      //Use InputStream in to read from the serial port, and OutputStream
      //out to write to the serial port.
      try
      {
        in  = serialPort.getInputStream();
        out = serialPort.getOutputStream();
        
      } catch (IOException e)
      {
        return InitFail;
      }
      try
      {
         serialPort.setSerialPortParams(9600,
              SerialPort.DATABITS_8,
              SerialPort.STOPBITS_1,
              SerialPort.PARITY_NONE);
      } catch (UnsupportedCommOperationException e)
      {
        return InitFail;
      }
    } catch (NoSuchPortException e)
    {
      return InitFail;
    }
    SB1 = new SerialBuffer();
    RT = new ReadSerial(SB1, in);
    RT.start();
    // return success information
    return InitSuccess;
    }

public String ReadPort(int Length){

System.out.println("6666666666");
String Msg;
System.out.println("长度是:" + Length);

Msg = SB1.GetMsg(Length);
System.out.println("88888888888");
return Msg;
}

public void WritePort(String Msg){

int c;

try {
for (int i = 0; i < Msg.length(); i++) 

out.wait(Msg.charAt(i));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void ClosePort(){

RT.stop();
serialPort.close();
}
}
是SB1.GetMsg这抱的空指针异常!非常感谢