]> Pileus Git - ~andy/csm213a-hw/blob - syc/src/com/example/sycapp/SerialIO.java
Add Android app from Yue
[~andy/csm213a-hw] / syc / src / com / example / sycapp / SerialIO.java
1 package com.example.sycapp;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileDescriptor;
7 import java.io.FileInputStream;
8 import java.io.FileOutputStream;
9 import java.io.FileWriter;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import java.nio.ByteBuffer;
14 import java.util.Vector;
15
16 import android.content.Context;
17
18 public class SerialIO{
19         private OutputStream sout;
20         private InputStream  sin;
21         
22         
23         private Vector<SerialData> parsedData;
24         // Serial Data
25         private final short HEADER       = SerialData.HEADER;
26         private final byte  HEADER_FST   = (byte)(HEADER>>8&0xFF);
27         private final byte  HEADER_SCD   = (byte)(HEADER&0xFF);
28         private final int   int16Cnt     = SerialData.evt_int16Cnt;  
29         private final int   int32Cnt     = SerialData.evt_int32Cnt;
30         
31         
32         // Serial Buffer
33         private int       int16Idx;
34         private int       int32Idx;
35         private byte[]    buffer        = new byte[4];
36         private int       int16_;
37         private int       int32_;
38         private int       parseStatus   = 0;
39         private short     int16Buffer[] = new short[int16Cnt];
40         private int       int32Buffer[] = new int  [int32Cnt];
41         private int       portId;
42         
43         
44         // Constructors
45         SerialIO(String portName, int portId){
46                 try {
47                         this.sout       = new BufferedOutputStream(new FileOutputStream(new File(portName)));
48                         this.sin        = new  FileInputStream(new File(portName));
49                         this.parsedData = new Vector<SerialData>();
50                         this.portId     = portId;
51                 } catch (Exception e) {
52                         Util.debug("[S#0]"+e.toString());
53                 }
54         }
55         
56         SerialIO(FileOutputStream fo){
57                 try {
58                         this.sout       = new BufferedOutputStream(new FileOutputStream(new File("/dev/ttyO1")));
59                         this.sin        = new  FileInputStream(new File("/dev/ttyO1"));
60                         this.parsedData = new Vector<SerialData>();
61                 } catch (Exception e) {
62                         Util.debug("[S#0]"+e.toString());
63                 }
64         }
65         
66         // Write & Read Routines
67         public void send(byte[] content){
68                 try{
69                         sout.write(content);
70                         sout.flush();
71                 }catch(Exception e){
72                         Util.debug("[S#5]"+e.toString());
73                 }
74         }
75         public void send(String content){
76                 try{
77                         sout.write(content.getBytes());
78                         sout.flush();
79                 }catch(Exception e){
80                         Util.debug("[S#4]"+e.toString());
81                 }
82         }
83         
84         public Vector<SerialData> checkPort(){
85                 parsedData.clear();
86                 try {
87                         if( sin.available()>0){
88                                 onDataReceived();
89                         }
90                 } catch (Exception e) {
91                         Util.debug("[S#2]" +e.toString());
92                 }
93                 return parsedData;
94         }
95         
96         
97         private void onDataReceived(){
98                 //Util.debug("[Serial] OnDataReceived!");
99                 SerialData sData = new SerialData();
100                 try   {
101                                 byte buffer[] = new byte[sin.available()];
102                                 sin.read(buffer,0, sin.available());
103                                 parseByteFlow(buffer);
104                                 }
105                 catch (Exception e){Util.debug("[S#1]"+e.toString());}
106         }
107         
108
109         // TODO: need to be changed back
110         public void parseByteFlow(byte[] byteFlow){
111                 for (int i=0; i<byteFlow.length; ){
112                         switch(parseStatus){
113                         case 0:                                                 // looking for 1st part of HEADER
114                                 //if (byteFlow[i++]==HEADER_FST){ parseStatus = 1;} 
115                                 if (byteFlow[i++]==HEADER_SCD){ parseStatus = 1;} 
116                                 break;
117                         case 1:                                                 // looking for 2nd half of HEADER
118                                 //if (byteFlow[i++]!=HEADER_SCD){
119                                 if (byteFlow[i++]!=HEADER_FST){
120                                         parseStatus = 0;
121                                 }else {
122                                         parseStatus = 2;
123                                         int16Idx=0; int32Idx=0;
124                                 }
125                                 break;
126                         case 2:                                                 // packing int16
127                                 buffer[int16_++]=byteFlow[i++];
128                                 if (int16_==2){
129                                         int16_ = 0;
130                                         int16Buffer[int16Idx++] = ByteBuffer.wrap(flipInt16(buffer)).getShort();
131                                         if(int16Idx==int16Cnt){
132                                                 int16Idx = 0;
133                                                 parseStatus = 3;
134                                         }
135                                 }
136                                 break;
137                         case 3:                                                 // packing int32
138                                 buffer[int32_++]=byteFlow[i++];
139                                 if (int32_==4){
140                                         int32_ = 0;
141                                         int32Buffer[int32Idx++] = ByteBuffer.wrap(flipInt32(buffer)).getInt();
142                                         if(int32Idx==int32Cnt){
143                                                 int32Idx = 0;
144                                                 parseStatus = 0;
145                                                 if (isValidData(int16Buffer, int32Buffer)){
146                                                         parsedData.add(new SerialData(int16Buffer, int32Buffer, this.portId));
147                                                 }
148                                                 int j=0;
149                                         }
150                                 }
151                                 break;
152                         }
153                         
154                 }
155                 //return logEnable? parsedData:null;
156         }
157         
158         private byte[] flipInt16(byte[] buff){
159                 byte temp = buff[0];
160                 buff[0] = buff[1];
161                 buff[1] = temp;
162                 return buff;
163         }
164         
165         private byte[] flipInt32(byte[] buff){
166                 byte temp = buff[0];
167                 buff[0] = buff[3];
168                 buff[3] = temp;
169                 
170                 temp = buff[1];
171                 buff[1] = buff[2];
172                 buff[2] = temp;
173                 return buff;
174         }
175         
176         private boolean isValidData(short[] int16, int[] int32){
177                 return true;
178         }
179                 
180         private byte[] combineBytes(byte[] b1, byte[] b2){
181                 byte[] result = new byte[b1.length+b2.length];
182                 System.arraycopy(b1, 0, result, 0, b1.length);
183                 System.arraycopy(b2, 0, result, b1.length, b2.length);
184                 return result;
185         }
186
187         
188 }