java监听串口,接收和发送数据,在接收的时候出现乱码,不知道怎么回事,求助,代码如下:
package com.venus.serialport;import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
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;class S_Frame extends Frame implements Runnable, ActionListener,
SerialPortEventListener {
static CommPortIdentifier portId; //检测系统中可用的通讯端口类 
static Enumeration portList; //Enumeration 为枚举型类,在util中
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort; //RS-232的串行口
// 最小的功能集,用户可以直接对串口进行读写和设置工作
Thread readThread;
String str = "";
Panel p = new Panel();
Panel p1 = new Panel();
TextArea in_message = new TextArea();
TextArea out_message = new TextArea();
Label label = new Label("右边是您接收到得信息");
Button btnOpen = new Button("打开串口, 发送数据");
Button btnClose = new Button("关闭串口, 停止发送数据");
byte data[] = new byte[1024];
boolean ; //设置判断要是否关闭串口的标志

S_Frame() {//安排窗体
super("串口接收发送数据");
setSize(200, 200);
setVisible(true);
add(out_message, "Center");
add(p, "North");
p.add(btnOpen);
p.add(btnClose);
add(p1, "South");
p1.add(label);
p1.add(in_message);
btnOpen.addActionListener(this);
btnClose.addActionListener(this);
}  public void actionPerformed(ActionEvent event) {//点击按扭打开串口
if (event.getSource() == btnClose) {
serialPort.close(); // 关闭串口
 = true; // 用于中止线程的run()方法
in_message.setText("串口COM1已经关闭,停止接收和发送数据.");
} else {
 = false;
/*打开串口,从文本区按字节读取数据 */
start();
}
}

public void start() { //打开串口,并调用线程发送数据
portList = CommPortIdentifier.getPortIdentifiers(); //获取系统中所有的通讯端口
while (portList.hasMoreElements()) { //用循环结构找出串口 
portId = (CommPortIdentifier) portList.nextElement(); //强制转换为通讯端口类型
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM5")) {
try {//打开串口 
serialPort = (SerialPort) portId.open("ReadComm", 2000);
} catch (PortInUseException e){}
try {//设置串口监听器
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);//侦听到串口有数据,触发串口事件
try { //设置串口输出流
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}



try {
readThread = new Thread(this);//调用线程发送数据
readThread.start();// 线程负责每发送一次数据,休眠2秒钟
} catch (Exception e) {}
}  public void run() {//发送数据,休眠2秒钟后重发
try { //设置串口通讯参数
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
try { //发送数据流(将数组data[]中的数据发送出去)
DataDao dd = new DataDao();
DataBean db = dd.getData();
// 从数据库里面取出数据的,不断的发送数据
out_message.setText(db.getData()+"\r\n");//把得到的内容显示到文本框
data = out_message.getText().getBytes();//这是从文本框里面取数据,
outputStream.write(data);
} catch (IOException e){}
try {
Thread.sleep(5000);//发送数据后休眠5秒钟,然后再重发
if () {
return; // 结束run方法,停止线程
}
start();
} catch (InterruptedException e) {}
} public void serialEvent(SerialPortEvent event) {
try {//设置串口通讯参数:波特率、数据位、停止位、奇偶校验
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
byte[] readBuffer = new byte[15];
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
while (inputStream.available() > 0) {//从线路上读取数据流
int numBytes = inputStream.read(readBuffer);

str = new String(readBuffer); 
 
//if(

//str.substring(0,1).equals("S")
   
// )
             
//
in_message.setText(str);
 //把发送过来的值显示到文本框中 

System.out.println(str);
String br = in_message.getText();
Transfer pt = new Transfer();
String[] a = new String[15];
for (int i = 0; i < 15;i++) {

a[i] = str.substring(i, i + 1);

}
pt.transfer(a);
               

 
}catch (IOException e){}

}
public class SendComm {
public static void main(String args[]) {
S_Frame S_win = new S_Frame();
S_win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
S_win.pack();
}
}
小弟请大家帮帮忙,感激不尽,就是接收数据的时候是乱码,但是从串口发来的数据在超级终端和串口调试助手都是正常的,
就是用java 监听接收的是乱码,谢谢了