帮忙改下代码!在线等非常感谢!分全给了答对的以后有分再送

Honly0635 2007-07-02 11:15:58
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这抱的空指针异常!非常感谢
...全文
206 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Honly0635 2007-07-02
  • 打赏
  • 举报
回复
我初始化过了老大
shan1119 2007-07-02
  • 打赏
  • 举报
回复
那就是说SB1没有初始化

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧