Blame view

src/com/hdx/lib/serial/SerialPortOperaion.java 2.89 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  package com.hdx.lib.serial;
  
  import android.os.Handler;
  import android.os.Message;
  import android_serialport_api.SerialPort;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  public class SerialPortOperaion
  {
    public static final int SERIAL_RECEIVED_DATA_MSG = 1;
    private static final String TAG = "SerialPortOperaion";
    private Handler mHandler;
    private SerialParam mSerialParam;
    private SerialPort mSerialPort;
    private OutputStream mOutputStream = null;
    private InputStream mInputStream = null;
    private ReadThread mReadThread;
  
    public SerialPortOperaion(Handler callback, SerialParam param)
    {
      this.mHandler = callback;
      this.mSerialParam = param;
    }
  
    public void StartSerial() throws IOException
    {
      this.mSerialPort = new SerialPort(this.mSerialParam.device, this.mSerialParam.baudrate, this.mSerialParam.openFlag);
      this.mOutputStream = this.mSerialPort.getOutputStream();
      this.mInputStream = this.mSerialPort.getInputStream();
      this.mReadThread = new ReadThread();
      this.mReadThread.start();
    }
  
    public void StopSerial() {
      try {
        if(this.mInputStream!=null){
      	  this.mInputStream.close();
        }
        if(this.mOutputStream!=null){
      	  this.mOutputStream.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      this.mInputStream = null;
      this.mOutputStream = null;
      this.mSerialPort.close();
      try {
        this.mReadThread.join(5000L);
      } catch (InterruptedException localInterruptedException) {
      }
    }
  
    public void WriteData(int[] data) {
      try {
        for (int i = 0; i < data.length; i++)
          this.mOutputStream.write(data[i]);
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
  
    public void WriteData(byte[] data) {
      try {
        this.mOutputStream.write(data);
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    class ReadThread extends Thread {
      ReadThread() {
      }
  
      public void run() { super.run();
        while (!isInterrupted())
          try
          {
            byte[] buffer = new byte[1024];
            if (SerialPortOperaion.this.mInputStream == null) return;
            int size = SerialPortOperaion.this.mInputStream.read(buffer);
            if ((size > 0) && (SerialPortOperaion.this.mHandler != null))
            {
              Message msg = SerialPortOperaion.this.mHandler.obtainMessage(1, new SerialReadData(SerialPortOperaion.this, buffer, size));
              SerialPortOperaion.this.mHandler.sendMessage(msg);
            }
          } catch (IOException e) {
            e.printStackTrace();
            return;
          } }
    }
  
    public class SerialReadData
    {
      public byte[] data;
      public int size;
  
      SerialReadData(byte[] buf, int n) {
        this.data = buf;
        this.size = n;
      }
     
      SerialReadData(SerialPortOperaion paramSerialPortOperaion, byte[] buf, int n)
      {
        this.data = buf;
        this.size = n;
      }
    }
  }