GithubHelp home page GithubHelp logo

alangrey / usbserialport Goto Github PK

View Code? Open in Web Editor NEW

This project forked from andjdd/usbserialport

0.0 1.0 0.0 307 KB

License: Apache License 2.0

Kotlin 32.00% Java 64.86% CMake 0.77% C 2.27% Shell 0.10%

usbserialport's Introduction

usbserialport

提供android系统下usb转串口及串口(UART,RS232)通信方式

android studio 添加

dependencies {
      implementation 'com.hd:usbserialport:last-version'
  }

注意:usbserialport只实现了简单的读写,更深度化的使用可查看 usb-serial-port-measure

用法:

1.在application 初始化:

public class AIOApp extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            DeviceMeasureController.init(this,BuildConfig.DEBUG);
        }
  }

2.扫描设备

  DeviceMeasureController.INSTANCE.scanSerialPort();//扫描串口
  
  DeviceMeasureController.INSTANCE.scanUsbPort();//扫描usb转串口

3.开始测量

3.1 usb转串口测量

//测量所有设备
  @Override
     protected void onResume() {
         super.onResume();
         DeviceMeasureController.INSTANCE.measure(DeviceMeasureController.INSTANCE.scanUsbPort(),new UsbMeasureParameter(), new UsbMeasureListener() {
             @Override
             public void measuring(@Nullable Object tag,UsbSerialPort usbSerialPort, byte[] data) {
                 //处理返回数据
             }
 
             @Override
             public void write(@Nullable Object tag,UsbSerialPort usbSerialPort) {
                 //允许持续性写入数据
                 try {
                     usbSerialPort.write(new byte[]{(byte) 0xff, (byte) 0xff},1000);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
 
             @Override
             public void measureError(@Nullable Object tag,String message) {
                
             }
         });
     }
   //测量单个目标设备 
    @Override
    protected void onResume() {
        super.onResume();
        //根据实际选择情况
        UsbSerialPort port=DeviceMeasureController.INSTANCE.scanUsbPort().get(0).getPorts().get(0);
        DeviceMeasureController.INSTANCE.measure(port, new UsbMeasureParameter(115200,8,1,0), new UsbMeasureListener() {
            @Override
            public void measuring(@Nullable Object tag,UsbSerialPort usbSerialPort, byte[] data) {
                //处理返回数据
            }

            @Override
            public void write(@Nullable Object tag,UsbSerialPort usbSerialPort) {
                //允许持续性写入数据
                try {
                    usbSerialPort.write(new byte[]{(byte) 0xff, (byte) 0xff},1000);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void measureError(@Nullable Object tag,String message) {
                
            }
        });
    } 

3.2 串口测量

//测量所有设备
   @Override
    protected void onResume() {
        super.onResume();
        DeviceMeasureController.INSTANCE.measure(null, new SerialPortMeasureParameter(), new SerialPortMeasureListener() {
            @Override
            public void measuring(@Nullable Object tag,String path, byte[] data) {
                //处理返回数据
            }

            @Override
            public void write(@Nullable Object tag,OutputStream outputStream) {
                //允许持续性写入数据
                try {
                    outputStream.write(new byte[]{(byte) 0xff, (byte) 0xff});
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
                
            @Override
            public void measureError(@Nullable Object tag,String message) {

            }
        });
    }
   //测量单个目标设备 
     @Override
        protected void onResume() {
            super.onResume();
            String targetDevicePath="/dev/ttyS3";
            DeviceMeasureController.INSTANCE.measure(new SerialPortMeasureParameter(targetDevicePath,115200,0), new SerialPortMeasureListener() {
                @Override
                public void measuring(@Nullable Object tag,String path, byte[] data) {
                    //处理返回数据
                }

                @Override
                public void write(@Nullable Object tag,OutputStream outputStream) {
                    //允许持续性写入数据
                    try {
                        outputStream.write(new byte[]{(byte) 0xff, (byte) 0xff});
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                
                @Override
                public void measureError(@Nullable Object tag,String message) {
    
                }
            });
        }


3.测量阶段发送指令

DeviceMeasureController.INSTANCE.write()

4.停止测量

DeviceMeasureController.INSTANCE.stop()

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

usbserialport's People

Contributors

alangrey avatar hellohudi avatar

Watchers

 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.