RS232通信(Android)


一、 添加依賴
dependencies {
implementation 'com.github.kongqw:AndroidSerialPort:1.0.1'
}

二、 使用方法
package com.bug01.tryserial;

import com.kongqw.serialportlibrary.SerialPortManager;
import com.kongqw.serialportlibrary.listener.OnOpenSerialPortListener;
import com.kongqw.serialportlibrary.listener.OnSerialPortDataListener;

import java.io.File;

public class SerialUtil {
    private SerialPortManager mSerialPortManager = new SerialPortManager();
    private static SerialUtil instance = null;

    // 此處使用單例模式
    public static SerialUtil getInstance() {
        if (instance == null) {
            synchronized (SerialUtil.class) {
                if (instance == null) {
                    instance = new SerialUtil();
                    instance.mSerialPortManager.setOnSerialPortDataListener(instance.onSerialPortDataListener);
                    instance.mSerialPortManager.setOnOpenSerialPortListener(instance.onOpenSerialPortListener);
                }
            }
        }
        return instance;
    }


    private OnSerialPortDataListener onSerialPortDataListener = new OnSerialPortDataListener() {
        @Override
        public void onDataReceived(byte[] bytes) {
            System.out.println("收到了數據。");
        }

        @Override
        public void onDataSent(byte[] bytes) {
            System.out.println("發送了數據。");
        }
    };

    private OnOpenSerialPortListener onOpenSerialPortListener = new OnOpenSerialPortListener() {
        @Override
        public void onSuccess(File device) {
            System.out.println("鏈接" + device.getName() +"成功");
        }

        @Override
        public void onFail(File device, Status status) {
            System.out.println("鏈接" + device.getName() +"失敗");
        }
    };


    /**
     * 鏈接串口
     * @return
     */
    public boolean Connect() {
        return Connect("ttysWK1");
    }

    /**
     * 鏈接串口
     * @param PortName 設備節點名稱
     * @return
     */
    public boolean Connect(String PortName) {
        return mSerialPortManager.openSerialPort(new File("dev/" + PortName), 9600);
    }

    /**
     * 寫入數據
     * @param val
     */
    public void write(String val) {
        mSerialPortManager.sendBytes(val.getBytes());
    }

    public void write(byte[] val) {
        mSerialPortManager.sendBytes(val);
    }
}

 

后面我想了一下,有可能會有人不知道自己用的是哪個節點,那你就遍歷出來,挨個測試一下。

遍歷串口測試代碼:

    public void trySerialTest() {
        SerialPortFinder serialPortFinder = new SerialPortFinder();
        SerialPortManager mSerialPortManager;
        ArrayList<Device> devices = serialPortFinder.getDevices();
        System.out.println("獲取到節點數量為:" + devices.size());
        Device device = null;
        mSerialPortManager = new SerialPortManager();

        mSerialPortManager.setOnSerialPortDataListener(new OnSerialPortDataListener() {
            @Override
            public void onDataReceived(byte[] bytes) {
                System.out.println("收到數據");
            }

            @Override
            public void onDataSent(byte[] bytes) {
            }
        });

        mSerialPortManager.setOnOpenSerialPortListener(new OnOpenSerialPortListener() {
            @Override
            public void onSuccess(File device) {
                System.out.print("串口鏈接成功,節點為:");
                System.out.println(device.getName());//打印節點ID
            }

            @Override
            public void onFail(File device, Status status) {
                System.out.print("串口鏈接失敗,節點為:");
                System.out.println(device.getName() + status);
            }
        });

        for (Device dev : devices
        ) {
            boolean openSerialPort = mSerialPortManager.openSerialPort(dev.getFile(), 9600);
            if (openSerialPort) {
                mSerialPortManager.sendBytes("send something".getBytes());

                try {
                    Thread.sleep(3000);//給點時間用來接收
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("結束測試");
        mSerialPortManager.closeSerialPort();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM