Coder Social home page Coder Social logo

common-rxtx's Introduction

简单的串口操作

如何使用

  1. 引入依赖
<dependency>
    <groupId>cn.qqhxj.common</groupId>
    <artifactId>rxtx</artifactId>
    <version>1.3.1-RELEASE</version>
<dependency>
  1. 设置串口

portName such as COM1 或者使用 SerialUtils.getCommNames();

 SerialPort connect = SerialUtils.connect(portName, 9600);
  1. 设置串口读写器
SerialContext.setSerialReader(new VariableLengthSerialReader('{', '}'));
  1. 设置串口数据解析器
SerialContext.getSerialDataParserSet().add(new StringSerialDataParser());
  1. 设置串口事件监听器
SerialContext.setSerialPortEventListener(new DefaultSerialDataListener());
  1. 设置串口byte数据处理器(可选)
SerialContext.setSerialByteDataProcessor(new SerialByteDataProcessor() {
    @Override
    public void process(byte[] bytes) {
        System.out.println(bytes);
    }
});
  1. 设置自定义的串口数据解析器

需要实现 interface SerialDataParser<T>

 SerialContext.getSerialDataParserSet().add(new SerialDataParser<Object>() {
    @Override
    public Object parse(byte[] bytes) {
        return null;
    }
});
  1. 设置串口对象处理器(可选,需要有对应的串口数据解析器)
SerialContext.getSerialDataProcessorSet().add(new SerialDataProcessor<T>() {
    @Override
    public void process(T t) {
        System.out.println(t);
    }
});

common-rxtx's People

Contributors

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