java如何清除缓存区,怎样用java清空串口的输入缓冲区?解决方案

当前位置:我的异常网» J2SE » 怎样用java清空串口的输入缓冲区?解决方案

怎样用java清空串口的输入缓冲区?解决方案

www.myexceptions.net  网友分享于:2013-02-11  浏览:96次

怎样用java清空串口的输入缓冲区?

哪位兄弟知道怎样用java清空串口的输入缓冲区?

网上c/c++这方面的资料比较多,java一个也没有找到.

------解决方案--------------------

不知道具体串口的细节,因为java一般不直接操作底层的东西,java处理串口一般是jni,找javax.comm,或者rxtx,这些都实现得不错。

------解决方案--------------------

不懂对地层的操作,好象这不是java的专长吧

------解决方案--------------------

我也查资料作了一个,但是也有问题,连续发送数据有时会被截断,把代码发上来大家研究一下

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(); //调用方法放

}

文章评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值