免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5904 | 回复: 0

WINDOWS环境下JAVA串口RXTX编程DEMO [复制链接]

论坛徽章:
0
发表于 2010-01-10 16:04 |显示全部楼层

本来可以用javax.comm这个包的,但是我机子上运行老是不成功,于是我换了rxtx包
javax.comm在windows下的开发维护已经停止了,rxtx的旧版本支持在javax.comm-win32-2.0基础上的扩展,rxtx新版本支持对javax.comm的覆盖式支持,也就是说原来用javax.comm的把所有import javax.comm.*改成import gnu.io.*就可以正常使用了,其他只须相关的dll文件,不用properties文件,支持的端口类型也明显多了很多
下载地址:
ftp://ftp.qbang.org/pub/rxtx/rxtx-2.1-7-bins-r2.zip

里面的然后配置环境
  • copy rxtxSerial.dll into your c:\program files\java\jre-version\bin dir
  • copy RXTXcomm.jar into your c:\program files\java\jre-version\lib\ext dir
    把下载包中Windows\i368-mingw32\rxtxSerial.dll 放到你%java_home%\jre\bin下面
    把下载包中RXTXcomm.jar放到%java_home%\jre\lib\ext下面
    OK,可以写程序了
    /**
    *
    */
    package cn.zhongxiaogang.test;
    import java.io.*;
    import java.util.*;
    import gnu.io.*;
    public class SimpleRead implements SerialPortEventListener { //SerialPortEventListener 监听器,我的理解是独立开辟一个线程监听串口数据
    static CommPortIdentifier portId; //串口通信管理类
    static Enumeration portList;   //已经连接上的端口的枚举
    InputStream inputStream; //从串口来的输入流
    OutputStream outputStream;//向串口输出的流
    SerialPort serialPort;     //串口的引用
    public SimpleRead() {
       try {
        serialPort = (SerialPort) portId.open("myApp", 2000);//打开串口名字为myapp,延迟为2毫秒
       } catch (PortInUseException e) {
       }
       try {
        inputStream = serialPort.getInputStream();
        outputStream = serialPort.getOutputStream();
       } catch (IOException e) {
       }
       try {
        serialPort.addEventListener(this);      //给当前串口天加一个监听器
       } catch (TooManyListenersException e) {
       }
       serialPort.notifyOnDataAvailable(true); //当有数据时通知
       try {
        serialPort.setSerialPortParams(2400, SerialPort.DATABITS_8,   //设置串口读写参数
          SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
       } catch (UnsupportedCommOperationException e) {
       }
    }
    public void serialEvent(SerialPortEvent event) {//SerialPortEventListener 的方法,监听的时候会不断执行
       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[20];
        try {
         while (inputStream.available() > 0) {
          int numBytes = inputStream.read(readBuffer);
         }
         outputStream.write("xiaogang".getBytes());
         System.out.println(new String(readBuffer));
        } catch (IOException e) {
        }
        break;
       }
    }
    public static void main(String[] args) {
       try {
        portList = CommPortIdentifier.getPortIdentifiers(); //得到当前连接上的端口
        while (portList.hasMoreElements()) {
         portId = (CommPortIdentifier) portList.nextElement();
         if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {//判断如果端口类型是串口
          if (portId.getName().equals("COM3")) { //判断如果COM3端口已经启动就连接
           SimpleRead reader = new SimpleRead(); //实例一个
          }
         }
        }
       } catch (Exception e) {
        e.printStackTrace();
       }
    }
    }
    程序调试成功,不过还有很多问题,比如有乱码,还有程序不面向对象,etc.


    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/94064/showart_2144948.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP