| package com.doumee.keyCabinet.utils.i485; | 
|   | 
| import android.serialport.SerialPort; | 
| import android.util.Log; | 
|   | 
| import java.io.File; | 
| import java.io.IOException; | 
| import java.io.InputStream; | 
| import java.util.concurrent.ExecutorService; | 
| import java.util.concurrent.Executors; | 
| import java.util.concurrent.ScheduledExecutorService; | 
| import java.util.concurrent.ScheduledFuture; | 
| import java.util.concurrent.TimeUnit; | 
|   | 
| public class SerialPortModel { | 
|   | 
|     public static final String TAG = SerialPortModel.class.getSimpleName(); | 
|   | 
|     private void logI(String str) { | 
|         Log.i(TAG, str); | 
|     } | 
|   | 
|     private String serialPortPath; | 
|     private int baudrate; | 
|     private int dataBits; | 
|     private int stopBits; | 
|     private int parity; | 
|     private SerialPort serialPort; | 
|     private ScheduledExecutorService scheduledExecutorService; | 
|     private ExecutorService singleThreadExecutor; | 
|     private ScheduledFuture scheduledFuture; | 
|     private SerialPortReadObserver serialPortReadObserver; | 
|     private boolean isOk = false; | 
|   | 
|     private final Object writeLock = new Object(); | 
|   | 
|     public SerialPortModel(String serialPortPath, int baudrate, int dataBits, int stopBits, int parity,SerialPortReadObserver portReadObserver) { | 
|         this.serialPortPath = serialPortPath; | 
|         this.baudrate = baudrate; | 
|         this.dataBits = dataBits; | 
|         this.stopBits = stopBits; | 
|         this.parity = parity; | 
|         this.serialPortReadObserver = portReadObserver; | 
|     } | 
|   | 
|     public boolean open() { | 
|         try { | 
|             File device = new File(serialPortPath); | 
|             serialPort = SerialPort | 
|                     .newBuilder(device, baudrate) | 
|                     .dataBits(dataBits) | 
|                     .stopBits(stopBits) | 
|                     .parity(parity) | 
|                     .build(); | 
|             scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); | 
|             singleThreadExecutor = Executors.newSingleThreadExecutor(); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|             return false; | 
|         } | 
|         return true; | 
|     } | 
|   | 
|     public void close() { | 
|         try { | 
|             serialPort.getInputStream().close(); | 
|             serialPort.getOutputStream().close(); | 
|             serialPort.close(); | 
|             serialPort = null; | 
|             scheduledExecutorService.shutdown(); | 
|             singleThreadExecutor.shutdown(); | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
|   | 
|     public void startRead() { | 
|         scheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> { | 
|             InputStream inputStream = serialPort.getInputStream(); | 
|             byte[] bytes = new byte[2048]; | 
|             int length = 0; | 
|             try { | 
|                 length = inputStream.read(bytes); | 
|             } catch (IOException e) { | 
|                 e.printStackTrace(); | 
|             } | 
| //            logI("startRead - length: " + length); | 
|             if (length <= 0) return; | 
|             //String result = new String(bytes, 0, length); | 
|             byte[] bytes2 = new byte[length]; | 
|             for(int i=0;i<length;i++){ | 
|                 bytes2[i] = bytes[i]; | 
|             } | 
|             if(serialPortReadObserver!=null) { | 
|                 serialPortReadObserver.onResultBytes(bytes2); | 
|             } | 
|         }, 0, 100, TimeUnit.MILLISECONDS); | 
|     } | 
|   | 
|     public void stopRead() { | 
|         scheduledFuture.cancel(true); | 
|     } | 
|   | 
|     public void write(byte[] bytes) { | 
|         singleThreadExecutor.execute(() -> { | 
|             synchronized (writeLock) { | 
|                 try { | 
|                     serialPort.getOutputStream().write(bytes); | 
|                     if(serialPortReadObserver!=null) { | 
|                         serialPortReadObserver.onResult(null); | 
|                     } | 
|                 } catch (IOException e) { | 
|                     e.printStackTrace(); | 
|                     if(serialPortReadObserver!=null) { | 
|                         serialPortReadObserver.onResult(e.getMessage()); | 
|                     } | 
|                 } | 
|             } | 
|         }); | 
|     } | 
|   | 
|     public boolean isOk() { | 
|         return isOk; | 
|     } | 
|   | 
|     public void setOk(boolean ok) { | 
|         isOk = ok; | 
|     } | 
| } |