BaseCommand

 

import java.io.Serializable;

import android.util.Log;

public class BaseCommand implements Serializable {

/**
*
*/
private static final long serialVersionUID = 2677660408587776109L;

private String head;
private String id;
private String size;
private String data;
private String crc;

public BaseCommand() {

}

public BaseCommand(String head, String id, String data) {
this.head = head;
this.id = id;
this.data = data;
/*if (null != this.head && null != this.id && null != this.data) {
this.size = CommandHelper.intToHexString(head.length() / 2
+ id.length() / 2 + data.length() / 2 + 2);
Log.d("SerialPort", "size = " + size);
}*/
}

public String getHead() {
return this.head;
}

public void setHead(String head) {
this.head = head;
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public String getCrc() {
if(null != crc){
return crc;
} else {
return "";
}
}

public void setCrc(String crc){
this.crc = crc;
}

public String getCommand() {
if (null != this.head && null != this.id && null != this.data) {
String result = head + id;
if(null != size){
result = result + size;
}
result = result + data;
if(null != crc){
result = result + crc;
}
return result;
} else {
return "";
}
}

/*
* public String getSize(){ return this.size; }
*
* public void setSize(String size){ this.size = size; }
*/

public String getData() {
return this.data;
}

public void setData(String data) {
this.data = data;
if (null != this.head && null != this.id && null != this.data) {
/*this.size = CommandHelper.intToHexString(head.length() / 2
+ id.length() / 2 + data.length() / 2 + 2);
Log.d("SerialPort", "size = " + size);*/
}
}

public void caculateSize(){
if (null != this.head && null != this.id && null != this.data) {
this.size = CommandHelper.intToHexString(head.length() / 2
+ id.length() / 2 + data.length() / 2 + 2);
Log.d("SerialPort", "size = " + size);
}
}

public boolean caculateCrc() {
if (null == this.head || null == this.id
|| null == this.data || (data.length() % 2 != 0)) {
Log.d("SerialPort", "content null");
return false;
}
this.crc = CommandHelper.crcValue(head, id, size, data);
if (null != this.crc) {
Log.d("SerialPort", "crc is not null");
return true;
} else {
Log.d("SerialPort", "crc is null");
return false;
}

}
}

posted on 2015-07-29 09:58  善思善学  阅读(866)  评论(0编辑  收藏  举报

导航