android串口读卡协议解析(例Uart接口)

在这里插入图片描述

定义串口对象、流对象、打开串口

    private ByteBuffer mBuffer;
    private SerialPort mSerialPort;
    private OutputStream mOutputStream;
    private InputStream mInputStream;

    public boolean init() {

        try {
            mSerialPort = new android_serialport_api.SerialPort("/dev/ttyS4", 9600, 0);
            mOutputStream = mSerialPort.getOutputStream();
            mInputStream = mSerialPort.getInputStream();
        } catch (Exception e) {
            //logger.error("serial open exception.");
            e.printStackTrace();
            return false;
        }

        return true;
    }

读卡线程

 // 读卡信息接收
    public byte[] read() {
        try {
            int len;
            if (mInputStream == null) {
                return null;
            }

            byte[] bt = new byte[16];
            byte[] tmp = new byte[1];
            int idx = 0;

            int totalLen = 0;
            byte sum = 0;
            int ibyte = 0;
            while (true) {
                len = mInputStream.read(tmp);
                if (len <= 0) {
                    break;
                }

                Log.d("facepad", "recv buf: " + Integer.toHexString(tmp[0] & 0xFF));

                ibyte = (tmp[0] & 0xff);
                if (idx == 0) {//起始位
                    if (ibyte == 0x02) {
                        bt[0] = tmp[0];
                        idx++;
                        sum += tmp[0];
                    } else {
                        // 首位不是0x02
                        Arrays.fill(bt, (byte) 0);
                        idx = 0;
                        sum = 0;
                        totalLen = 0;
                    }
                } else if (idx == 1) {//长度
                    if (ibyte != 0x09 && ibyte != 0x0a) { // 长度不对,重来
                        Arrays.fill(bt, (byte) 0);
                        idx = 0;
                        sum = 0;
                        totalLen = 0;
                    }else {// 长度正确
                        totalLen = ibyte;
                        bt[idx] = tmp[0];
                        idx++;
                        sum += tmp[0];
                    }
                    /*if (ibyte == 0x66 || ibyte == 0x55) {
                        bt[idx] = tmp[0];
                        idx++;
                        sum += tmp[0];
                    } else {
                        // 次位不是0x66或0x55
                        Arrays.fill(bt, (byte) 0);
                        idx = 0;
                        sum = 0;
                        totalLen = 0;
                    }*/
                } else if (idx == 2) {//卡片类型
                    bt[idx] = tmp[0];
                    idx++;
                    sum += tmp[0];
                } else {
                    bt[idx] = tmp[0];
                    idx++;
                    if (idx == totalLen) {
                        byte[] cardMsg = new byte[idx];
                        System.arraycopy(bt, 0, cardMsg, 0, idx);
                        if (ibyte == 0x03&&bccCheck(cardMsg)) {
                            // 合法数据
                            break;
                        } else {
                            // 校验失败
                            Arrays.fill(bt, (byte) 0);
                            idx = 0;
                            sum = 0;
                            totalLen = 0;
                        }
                    }
                    sum += tmp[0];
                }
            }


            return bt;

        } catch (Exception e) {
            Log.d("facepad", "serial read exception." + e.toString());
        }

        Log.d("facepad", "serial read failed");
        return null;
    }

bcc校验

private boolean bccCheck(byte[] data) {
        String bcc = "";
        byte BCC[] = new byte[1];
        for (int i = 1; i < data.length - 2; i++) {
            BCC[0] = (byte) (BCC[0] ^ data[i]);
        }
        String hex = Integer.toHexString(BCC[0] & 0xFF);
//        if (hex.length() == 1) {
//            hex = '0' + hex;
//        }
        bcc += hex.toUpperCase();

        String flg = Integer.toHexString(data[data.length - 2] & 0xFF);
        return flg.toUpperCase().equals(bcc);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你丶快乐吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值