Chinaunix首页 | 论坛 | 博客
  • 博客访问: 66268
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 14
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-16 16:29
文章分类
文章存档

2017年(3)

2016年(2)

2015年(17)

我的朋友

分类: Java

2015-07-18 21:43:23

本来可以用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文件,支持的端口类型也明显多了很多

下载地址:

里面的然后配置环境

  • 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.

  • 阅读(1966) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~