| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 712 人关注过本帖
标题:[求助]串口发送和接受数据遇到的问题
只看楼主 加入收藏
tianwugang
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2007-6-19
结帖率:100%
收藏
 问题点数:0 回复次数:0 
[求助]串口发送和接受数据遇到的问题
我做了一个串口发送和接受数据的程序,但是连续发送会出现数据被截断的情况,并且while(inputStream.available()>-1)
{
int numBytes=inputStream.read(readBuffer);//从输入流每次读取一个字节的数据
area.append(new String(readBuffer));//将下一个数据追加到尾部
} 后面的代码不会执行,希望高手帮忙,完整代码如下


import java.io.*;
import java.util.*;
import javax.comm.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
*
* @author tian
*/
public class ComTest {

/** Creates a new instance of ComTest */
public ComTest() {
com c=new com();
c.setTitle("串口测试");
c.setSize(400,400);
c.setResizable(false);
c.setVisible(true);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
c.setLocation((screen.width-400)/2,(screen.height-400)/2);
}
public static void main(String[]args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}
catch (Exception exception) {
exception.printStackTrace();
}
new ComTest();
}
});
}
}
class com extends JFrame implements ActionListener,SerialPortEventListener{
static Enumeration portList;//枚举类型
static CommPortIdentifier portId;/* 检测系统中可用的通讯端口类 */
static SerialPort serialPort;/* 声明RS-232串行端口的成员变量 */
static OutputStream outputStream;//输出流
static InputStream inputStream;//输入流
static boolean use=false;
static boolean flag=true;
JPanel panel;
JScrollPane scrollPane;
JTextField text=new JTextField();
JButton button1=new JButton("发送");
JButton button2=new JButton("自动发送");
JButton button3=new JButton("清除");
JTextArea area=new JTextArea();
JLabel label=new JLabel("手动发送");
String message;
com(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInt();
}
public void jbInt(){
panel=(JPanel)getContentPane();
panel.setLayout(null);
scrollPane=new JScrollPane(area);
label.setBounds(35,30,100,25);
text.setBounds(120,30,150,25);
button1.setBounds(290,30,80,23);
button2.setBounds(160,335,80,25);
button3.setBounds(280,335,80,25);
scrollPane.setBounds(35,70,340,250);
area.setFont(new Font("",Font.PLAIN,14));//设置字体大小
panel.add(label);
panel.add(text);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(scrollPane);
button1.addActionListener(this);//添加监听
button2.addActionListener(this);
button3.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
message=text.getText();//将text中的文本赋予message

if(message.length()>0)//判断message是否为空
{
System.out.println(message);
a(); //调用方法放
}
else
JOptionPane.showMessageDialog(null,"请输入数据"); //消息框
}
if(e.getSource()==button2){//判断是否button2
double d=Math.random();//产生随机数
String s=String.valueOf(d);//返回参数的字符串 表示形式
message=s.substring(2);//从指定索引处 返回一个新的字符串
System.out.println(message);
a(); //调用方法
}
if(e.getSource()==button3)
area.setText(null);

}
public void a(){
boolean portFound=false;
String defaultPort="COM1";
portList=CommPortIdentifier.getPortIdentifiers();//获得所有通信端口
while(portList.hasMoreElements())// 测试此枚举是否包含更多的元素
{
portId=(CommPortIdentifier)portList.nextElement();//返回此枚举的下一个元素
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)//判断是否串口
{
if(portId.getName().equals(defaultPort))//判断是否COM1
{
portFound=true;
if(!use)
{
try
{
serialPort=(SerialPort)portId.open("com",2000);//打开串口
//System.out.println("串口"+portId.getName()+"已经打开");
area.setText("串口"+portId.getName()+"已经打开\n");

}
catch(PortInUseException a){
JOptionPane.showMessageDialog(null,"端口正在使用");//消息框
return;
}
}
try
{
outputStream=serialPort.getOutputStream();//设置输出流
}
catch(IOException a){}
try
{
serialPort.setSerialPortParams(9600,//设置串口通信参数
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch(UnsupportedCommOperationException a){}
try
{
serialPort.notifyOnOutputEmpty(true);//串口输出缓冲区空闲
}
catch(Exception a){}
try
{
area.append("\n");
outputStream.write(message.getBytes());//将数据写入输出流
outputStream.flush();
area.append("输出流: "+message+"\n");
use=true;
}
catch(IOException a){}
}
}
}
if(!portFound)
{
JOptionPane.showMessageDialog(null,"没有发现该端口");//消息框
return;
}
try
{
inputStream=serialPort.getInputStream();//设置输入流
area.append("输入流: ");
}
catch(IOException a){}
try
{
serialPort.addEventListener(this);//添加串口监听
}
catch(TooManyListenersException a){}
serialPort.notifyOnDataAvailable(true);

}
public void serialEvent(SerialPortEvent event)//实现串口监听中的方法
{
switch(event.getEventType())
{
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE: //串口有数据
byte[] readBuffer=new byte[1];
try
{
while(inputStream.available()>-1)
{
int numBytes=inputStream.read(readBuffer);//从输入流每次读取一个字节的数据
area.append(new String(readBuffer));//将下一个数据追加到尾部
//button1.setEnabled(false);
//button2.setEnabled(false);
}
// button1.setEnabled(true);
// button2.setEnabled(true);
}
catch(Exception e){
}
break;
}
}
}
搜索更多相关主题的帖子: 串口 数据 
2007-07-30 16:00
快速回复:[求助]串口发送和接受数据遇到的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014040 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved