• java SerialPort串口通讯的使用


    api文档

    http://fazecast.github.io/jSerialComm/javadoc/com/fazecast/jSerialComm/package-summary.html

     

     maven依赖

    <!-- https://mvnrepository.com/artifact/com.fazecast/jSerialComm -->
    <dependency>
        <groupId>com.fazecast</groupId>
        <artifactId>jSerialComm</artifactId>
        <!--<version>2.5.2</version>-->
        <version>[2.0.0,3.0.0)</version>
    </dependency> 
    
    

    基于事件的阅读用法示例

    可供读取的数据示例

    以下示例显示了使用此事件触发器的一种方法:

        SerialPort comPort = SerialPort.getCommPorts()[0];
        comPort.openPort();
        comPort.addDataListener(new SerialPortDataListener() {
            @Override
            public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_AVAILABLE; }
            @Override
            public void serialEvent(SerialPortEvent event)
            {
                if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
                    return;
                byte[] newData = new byte[comPort.bytesAvailable()];
                int numRead = comPort.readBytes(newData, newData.length);
                System.out.println("Read " + numRead + " bytes.");
            }
    
        });

     非阻塞阅读用法示例

    package com.bhu.utils;
    
    import com.fazecast.jSerialComm.SerialPort;
    
    
    public class SerialComm {
    
        /*public static void main(String[] args) throws InterruptedException {
            SerialComm serialComm = new SerialComm("COM3");
            serialComm.openPort();
            while (true) {
                byte[] bs = serialComm.writeAndRead("010300000002");
                Double[] d = Utils.analysisData(bs, 3, 5, 2);
                if (d != null) {
                    for (Double a : d) {
                        System.out.println(a);
                    }
                }
    
                Thread.sleep(1000);
            }
        }*/
    
    
        public SerialComm(String portDescriptor) {
            this.portDescriptor = portDescriptor;
        }
    
        public SerialComm(String portDescriptor, Integer baudRate) {
            this.portDescriptor = portDescriptor;
            this.baudRate = baudRate;
        }
    
        private String portDescriptor;
        private Integer baudRate;
    
        private SerialPort comPort;
    
        public boolean isOpen() {
            return comPort.isOpen();
        }
    
        /**
         * 打开串口
         *
         * @return 打开的状态
         */
        public boolean openPort() {
            return openPort(portDescriptor, baudRate);
        }
    
        /**
         * 打开串口
         *
         * @param portDescriptor COM口
         * @return 打开的状态
         */
        public boolean openPort(String portDescriptor) {
            return openPort(portDescriptor, null);
        }
    
        /**
         * 打开串口
         *
         * @param portDescriptor COM口
         * @param baudRate       波特率
         * @return 打开的状态
         */
        public boolean openPort(String portDescriptor, Integer baudRate) {
            comPort = SerialPort.getCommPort(portDescriptor);
            if (baudRate != null) {
                comPort.setBaudRate(baudRate);
            }
            if (!comPort.isOpen()) {
                return comPort.openPort();
            } else {
                return true;
            }
        }
    
    
        /**
         * 关闭串口
         *
         * @return
         */
        public boolean closePort() {
            if (comPort != null && comPort.isOpen()) {
                return comPort.closePort();
            }
            return true;
        }
    
        /**
         * 向com口发送数据并且读取数据
         */
        public byte[] writeAndRead(String instruct) {
            byte[] reslutData = null;
            try {
                if (!comPort.isOpen()) throw new BhudyException(BhudyExceptionCode.CODE_18);
    
                int numWrite = comPort.writeBytes(Utils.getCRC(instruct), Utils.getCRC(instruct).length);
                if (numWrite > 0) {
                    int i = 0;
                    while (comPort.bytesAvailable() > 0 && i++ < 5) Thread.sleep(20);
                    byte[] readBuffer = new byte[comPort.bytesAvailable()];
                    int numRead = comPort.readBytes(readBuffer, readBuffer.length);
                    if (numRead > 0) {
                        reslutData = readBuffer;
                    }
                }
            } catch (InterruptedException e) {
                throw new BhudyException(e.getMessage());
            }
            return reslutData;
        }
    
    }
     
  • 相关阅读:
    python接口测试
    python selenium 判断元素是否可见
    python学习之天气爬虫
    python学习之图片爬虫
    python学习之小说爬虫
    webdriver学习——css调试,jquery调用
    webdriver学习——百度设置
    bat中获取当前路径
    搭建ant脚本,全量/增量/减量包
    查询数据库中各个类型的方法名称
  • 原文地址:https://www.cnblogs.com/bhudy/p/11942110.html
Copyright © 2020-2023  润新知