1 package com.example.sycapp;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
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;
16 import android.content.Context;
18 public class SerialIO{
19 private OutputStream sout;
20 private InputStream sin;
23 private Vector<SerialData> parsedData;
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;
35 private byte[] buffer = new byte[4];
38 private int parseStatus = 0;
39 private short int16Buffer[] = new short[int16Cnt];
40 private int int32Buffer[] = new int [int32Cnt];
45 SerialIO(String portName, int portId){
47 this.sout = new BufferedOutputStream(new FileOutputStream(new File(portName)));
48 this.sin = new FileInputStream(new File(portName));
49 this.parsedData = new Vector<SerialData>();
51 } catch (Exception e) {
52 Util.debug("[S#0]"+e.toString());
56 SerialIO(FileOutputStream fo){
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());
66 // Write & Read Routines
67 public void send(byte[] content){
72 Util.debug("[S#5]"+e.toString());
75 public void send(String content){
77 sout.write(content.getBytes());
80 Util.debug("[S#4]"+e.toString());
84 public Vector<SerialData> checkPort(){
87 if( sin.available()>0){
90 } catch (Exception e) {
91 Util.debug("[S#2]" +e.toString());
97 private void onDataReceived(){
98 //Util.debug("[Serial] OnDataReceived!");
99 SerialData sData = new SerialData();
101 byte buffer[] = new byte[sin.available()];
102 sin.read(buffer,0, sin.available());
103 parseByteFlow(buffer);
105 catch (Exception e){Util.debug("[S#1]"+e.toString());}
109 // TODO: need to be changed back
110 public void parseByteFlow(byte[] byteFlow){
111 for (int i=0; i<byteFlow.length; ){
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;}
117 case 1: // looking for 2nd half of HEADER
118 //if (byteFlow[i++]!=HEADER_SCD){
119 if (byteFlow[i++]!=HEADER_FST){
123 int16Idx=0; int32Idx=0;
126 case 2: // packing int16
127 buffer[int16_++]=byteFlow[i++];
130 int16Buffer[int16Idx++] = ByteBuffer.wrap(flipInt16(buffer)).getShort();
131 if(int16Idx==int16Cnt){
137 case 3: // packing int32
138 buffer[int32_++]=byteFlow[i++];
141 int32Buffer[int32Idx++] = ByteBuffer.wrap(flipInt32(buffer)).getInt();
142 if(int32Idx==int32Cnt){
145 if (isValidData(int16Buffer, int32Buffer)){
146 parsedData.add(new SerialData(int16Buffer, int32Buffer, this.portId));
155 //return logEnable? parsedData:null;
158 private byte[] flipInt16(byte[] buff){
165 private byte[] flipInt32(byte[] buff){
176 private boolean isValidData(short[] int16, int[] int32){
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);