package com.bonait.bnframework.modules.home.fragment.mode;


import android.serialport.SerialPort;
import android.util.Log;

import org.apache.commons.codec.binary.Hex;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;

public class SerialPortRead {
    private volatile static SerialPortRead _instance;
    public static SerialPortRead Getinstance(){
        if (_instance==null){
            _instance = new SerialPortRead();
        }
        return _instance;
    }
    public void Open(String path,int baudrate){
        try{
            SerialPort sp = SerialPort.newBuilder(path,baudrate)
                    .dataBits(8)
                    .stopBits(1)
                    .parity(0)
                    .build();
            InputStream inputStream = sp.getInputStream();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        while (true){
                            byte[] bytes = new  byte[5];
                            String myString ="";
                            int size = inputStream.read(bytes);
                            if (size>0)
                                Log.e("ceshi",Integer.toHexString(size));
                        }
                    }catch (Exception e){

                    }

                }
            }).start();
        }catch (Exception e){

        }
    }

    public String toHexString(byte[] bytes){
        String HexString="";
        for(byte b:bytes){
            HexString += String.format("%02x",b);
        }
        return HexString;
    }






}
