package com.maple.demo.serialport;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.bjw.ComAssistant.MyFunc;
import com.bjw.ComAssistant.SerialHelper;
import com.bjw.bean.ComBean;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by maple on 2020-09-08 11:03
* E-Mail Address:740917401@qq.com
*/
public class CardManager extends SerialHelper {
public CardManager() {
super("/dev/ttyS1", 9600);
}
Context context;
public void init(Context context) {
this.context = context;
open();
}
public void setPortAndBaudRate(String sPort, String iBaudRate) {
destory();
setPort(sPort);
setBaudRate(iBaudRate);
open();
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
public void writeBlock() {
String time = simpleDateFormat.format(new Date());//14bit
String timeHex = BinaryHexUtils.stringToHexString(time);
String sumString = "00231600FFFFFFFFFFFF02" + timeHex;
String hexString = "20" + sumString;
send(BinaryHexUtils.hexStringToBytes(hexString + BinaryHexUtils.getHexReverse(BinaryHexUtils.getXorSum(sumString)) + "03"));
}
public void open() {
try {
super.open();
Toast.makeText(context, "IC卡模块打开成功,请刷卡", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("tag", "open 错误");
Toast.makeText(context, "请输入正确的端口跟波特率", Toast.LENGTH_LONG).show();
}
}
public void destory() {
try {
stopSend();
super.close();
} catch (Exception e) {
Log.e("", "destory 错误");
}
}
String realCardID = "";
@Override
protected void onDataReceived(ComBean comRecData) {
String data = MyFunc.ByteArrToHex(comRecData.bRec);
String cardID = data.trim().replace(" ", "");
Log.i("tag", "收到数据:" + cardID);
if (cardID.length() == 28) {
realCardID = cardID;
readBlock();
} else if (cardID.length() == 46) {//第二块
String time = simpleDateFormat.format(new Date());//14bit
String oldTime = BinaryHexUtils.hexStringToString(cardID.substring(10, 38));
Log.i("tag", "时间差:" + DateDistance.getDistanceSec(time, oldTime));
if (DateDistance.getDistanceSec(time, oldTime) > 1) {
writeBlock();
}
} else if (cardID.equals("2000230100DD03")) {
//写卡成功代表刷卡成功
Log.i("tag", "卡号:" + realCardID.substring(realCardID.length() - 12, realCardID.length() - 4));
}
}
}
网友评论