Giter VIP home page Giter VIP logo

simple_serial_port's Introduction

simple_serial_port

RXTX 项目提供了 Windows、Linux、Mac os X、Solaris 操作系统下的兼容 javax.comm 串口通讯包 API 的实现,为其他研发人员在此类系统下研发串口应用提供了方便。本仓库基于 RXTX 封装重构了部分代码,只需简单几步即可实现 java 串口通信,这不是一个可执行的程序,要将它作为你项目的依赖库或者编译成 jar 包引入到项目

中文 | English

准备工作

releases 中下载依赖包

  1. rxtx-2.2pre2-bins.zip是 RXTX 开源包,windows 平台配置方法如下,其他平台请阅读官方文档
  • rxtxSerial.dll 放入 <系统盘>\Windows\System32
  • rxtxSerial.dll 放入 <JAVA_HOME>\jre\bin
  • RXTXcomm.jar 放入 <JAVA_HOME>\jre\lib\ext
  1. RXTXcomm.jar 和 log4j 依赖的 jar 包(jar文件夹内)加入到项目中
  2. 将此模块编译成的 simple_serial_port.jar 加入到你的项目中,或者直接将该项目源码作为依赖库引入你的项目中

使用

  1. 查找可用串口
ArrayList<String> ports = SerialTool.findPort();
  1. 打开上述找到的串口
// 第一个参数用于给 serialPortVo 指定一个名字,便于识别不同的 serialPortVo ,可通过 serialPortVo.getName() 获取
// 打开上一步骤找到了串口集合中的第一个串口,可根据需求更改
SerialPortVo serialPortVo = SerialTool.openPort("我是dtu的串口", ports.get(0), baudRate);
  1. 新建监听器继承 PortListener 父类并重写 onReceive()onReadException() 方法,当串口接收到数据自动调用 onReceive() 方法,当接收数据时发生异常将调用 onReadException() 方法
public class MyListener extends PortListener {
    @Override
    public void onReceive(byte[] data) {
        String dataStr = new String(data).trim();
        System.out.println("串口接收到数据: " + dataStr);
    }
    
    @Override
    public void onReadException(Exception e) {
        System.out.println("发生异常: " + e.getMessage());
    }
}
  1. 利用打开串口时返回的 SerialPortVo 对象绑定监听器
// 第三个参数 500 为串口收到数据后等待 500 ms后再去读取串口数据,
// 防止读串口时数据没有完全到达串口,可根据数据大小适当调整等待时间
serialPortVo.bindListener(new MyListener(), 500);
  1. 给串口发送数据
  • 方式一:在监听器外,使用打开串口时返回的 SerialPortVo 对象发送字节数组类型的数据
// 使用打开串口返回的 SerialPortVo 对象发送方法
String reply = "我是在监听器类外面给你发送的消息";
serialPortVo.sendData(reply.getBytes());
  • 方式二:在监听器内,直接使用监听器内部 SerialPortVo 对象调用发送字节数组类型的数据
public class MyListener extends PortListener {
    @Override
    public void onReceive(byte[] data) {
        String dataStr = new String(data).trim();
        // 直接使用内置 SerialPortVo 对象调用发送方法
        String reply = "我收到你发送的数据啦";
        serialPortVo.sendData(reply.getBytes());
    }
    
    @Override
    public void onReadException(Exception e) {
        //...
    }
}

simple_serial_port's People

Contributors

vaf714 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.