日期:2014-05-20  浏览次数:20729 次

java实现串口线通信问题
我用java写了个串口线通信程序,数据可以传输,但是接收时出现了问题,分段接收了,如发送“123456789”,接收到的是
“1234”和“56789”。我想实现的是接收到的也是“123456789”。
程序如下:
package nio;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.*;
import javax.comm.*;

public class MyFrame extends Frame implements ActionListener,SerialPortEventListener{
//检测系统中可用的通讯端口类
private static CommPortIdentifier portId;
//Enumeration 为枚举类型类,在util中
private static Enumeration portList;
private OutputStream outputStream;
private InputStream inputStream;
//RS-232的串行口
private SerialPort serialPort;
//声明Frame上的元素
Panel p=new Panel();
Panel p1=new Panel();
TextField in_message=new TextField("利用串口COM3传输数据,波特率9600,数据位8,停止位1.");
TextArea out_message=new TextArea(10,50);
TextArea in_message1=new TextArea(10,50);
Button btnOpen=new Button("打开串口,发送数据");
Button btnClose=new Button("关闭串口,停止发送");
byte data[]=new byte[10240];//用来存储要发送的数据
//设置串口是否关闭的标志
boolean mark;
//人为的为接收到的数据编号
int counter;

public MyFrame(){
super("串口发送/接收数据");
setSize(800,280);
setVisible(true);
in_message1.setEditable(false);
add(p1,"Center");
p1.add(out_message,"West");
p1.add(in_message1,"East");
add(p,"North");
p.add(btnOpen);
p.add(btnClose);
add(in_message,"South");
//对按钮添加事件响应
btnOpen.addActionListener(this);
btnClose.addActionListener(this);
mark=true;
btnClose.setEnabled(false);
counter=0;

}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==btnClose){
btnClose.setEnabled(false);
btnOpen.setEnabled(true);
serialPort.close();//关闭串口
mark=true;//用于终止线程的run()方法
in_message.setText("串口COM3已经关闭,停止发送数据。");
}
else{
btnOpen.setEnabled(false);
btnClose.setEnabled(true);
//打开串口
open();
mark=false;
in_message.setText("串口COM3已经打开,正在发送一次数据....");
String tempstr = out_message.getText();
data=tempstr.getBytes();
if(data.length>0){
send(data);
out_message.setText("");
}
}
}

public void open(){

//获取系统中所有的通讯端口
portList=CommPortIdentifier.getPortIdentifiers();
//找出所需串口
while(portList.hasMoreElements()){
portId=(CommPortIdentifier)portList.nextElement();
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL){
if(portId.getName().equals("COM4")){//COM4是串口线对应的串口号
//打开串口
try {
serialPort=(SerialPort)portId.open("MyFrame",2000);
outputStream=serialPort.getOutputStream();
inputStream=serialPort.getInputStream();
} catch (PortInUseException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}//while

try {
serialPort.addEventListener(this);// 给当前串口天加一个监听器
serialPort.notifyOnDataAvailable(true); // 当有数据时通知
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, // 设置串口读写参数
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
} catch (TooManyListenersException e) {
e.printStackTrace();

catch (UnsupportedCommOperationException e) {
e.printStackTrace();
}
}
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD: