--- /dev/null
+package com.example.sycapp;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.Vector;
+
+import android.content.Context;
+
+public class SerialIO{
+ private OutputStream sout;
+ private InputStream sin;
+
+
+ private Vector<SerialData> parsedData;
+ // Serial Data
+ private final short HEADER = SerialData.HEADER;
+ private final byte HEADER_FST = (byte)(HEADER>>8&0xFF);
+ private final byte HEADER_SCD = (byte)(HEADER&0xFF);
+ private final int int16Cnt = SerialData.evt_int16Cnt;
+ private final int int32Cnt = SerialData.evt_int32Cnt;
+
+
+ // Serial Buffer
+ private int int16Idx;
+ private int int32Idx;
+ private byte[] buffer = new byte[4];
+ private int int16_;
+ private int int32_;
+ private int parseStatus = 0;
+ private short int16Buffer[] = new short[int16Cnt];
+ private int int32Buffer[] = new int [int32Cnt];
+ private int portId;
+
+
+ // Constructors
+ SerialIO(String portName, int portId){
+ try {
+ this.sout = new BufferedOutputStream(new FileOutputStream(new File(portName)));
+ this.sin = new FileInputStream(new File(portName));
+ this.parsedData = new Vector<SerialData>();
+ this.portId = portId;
+ } catch (Exception e) {
+ Util.debug("[S#0]"+e.toString());
+ }
+ }
+
+ SerialIO(FileOutputStream fo){
+ try {
+ this.sout = new BufferedOutputStream(new FileOutputStream(new File("/dev/ttyO1")));
+ this.sin = new FileInputStream(new File("/dev/ttyO1"));
+ this.parsedData = new Vector<SerialData>();
+ } catch (Exception e) {
+ Util.debug("[S#0]"+e.toString());
+ }
+ }
+
+ // Write & Read Routines
+ public void send(byte[] content){
+ try{
+ sout.write(content);
+ sout.flush();
+ }catch(Exception e){
+ Util.debug("[S#5]"+e.toString());
+ }
+ }
+ public void send(String content){
+ try{
+ sout.write(content.getBytes());
+ sout.flush();
+ }catch(Exception e){
+ Util.debug("[S#4]"+e.toString());
+ }
+ }
+
+ public Vector<SerialData> checkPort(){
+ parsedData.clear();
+ try {
+ if( sin.available()>0){
+ onDataReceived();
+ }
+ } catch (Exception e) {
+ Util.debug("[S#2]" +e.toString());
+ }
+ return parsedData;
+ }
+
+
+ private void onDataReceived(){
+ //Util.debug("[Serial] OnDataReceived!");
+ SerialData sData = new SerialData();
+ try {
+ byte buffer[] = new byte[sin.available()];
+ sin.read(buffer,0, sin.available());
+ parseByteFlow(buffer);
+ }
+ catch (Exception e){Util.debug("[S#1]"+e.toString());}
+ }
+
+
+ // TODO: need to be changed back
+ public void parseByteFlow(byte[] byteFlow){
+ for (int i=0; i<byteFlow.length; ){
+ switch(parseStatus){
+ case 0: // looking for 1st part of HEADER
+ //if (byteFlow[i++]==HEADER_FST){ parseStatus = 1;}
+ if (byteFlow[i++]==HEADER_SCD){ parseStatus = 1;}
+ break;
+ case 1: // looking for 2nd half of HEADER
+ //if (byteFlow[i++]!=HEADER_SCD){
+ if (byteFlow[i++]!=HEADER_FST){
+ parseStatus = 0;
+ }else {
+ parseStatus = 2;
+ int16Idx=0; int32Idx=0;
+ }
+ break;
+ case 2: // packing int16
+ buffer[int16_++]=byteFlow[i++];
+ if (int16_==2){
+ int16_ = 0;
+ int16Buffer[int16Idx++] = ByteBuffer.wrap(flipInt16(buffer)).getShort();
+ if(int16Idx==int16Cnt){
+ int16Idx = 0;
+ parseStatus = 3;
+ }
+ }
+ break;
+ case 3: // packing int32
+ buffer[int32_++]=byteFlow[i++];
+ if (int32_==4){
+ int32_ = 0;
+ int32Buffer[int32Idx++] = ByteBuffer.wrap(flipInt32(buffer)).getInt();
+ if(int32Idx==int32Cnt){
+ int32Idx = 0;
+ parseStatus = 0;
+ if (isValidData(int16Buffer, int32Buffer)){
+ parsedData.add(new SerialData(int16Buffer, int32Buffer, this.portId));
+ }
+ int j=0;
+ }
+ }
+ break;
+ }
+
+ }
+ //return logEnable? parsedData:null;
+ }
+
+ private byte[] flipInt16(byte[] buff){
+ byte temp = buff[0];
+ buff[0] = buff[1];
+ buff[1] = temp;
+ return buff;
+ }
+
+ private byte[] flipInt32(byte[] buff){
+ byte temp = buff[0];
+ buff[0] = buff[3];
+ buff[3] = temp;
+
+ temp = buff[1];
+ buff[1] = buff[2];
+ buff[2] = temp;
+ return buff;
+ }
+
+ private boolean isValidData(short[] int16, int[] int32){
+ return true;
+ }
+
+ private byte[] combineBytes(byte[] b1, byte[] b2){
+ byte[] result = new byte[b1.length+b2.length];
+ System.arraycopy(b1, 0, result, 0, b1.length);
+ System.arraycopy(b2, 0, result, b1.length, b2.length);
+ return result;
+ }
+
+
+}