串口数据缓存java版

接触串口很久了,一直以来将都是将串口读取出来的数组转换成字符串通过string.contains()查找是否包涵目标数组,自己感觉low到爆,所以写了一个byte-buffer,测试还是蛮好用的。希望借鉴和大神补充改正。

 

import java.util.Scanner;


public class BufTest {

    public static void main(String aeg[]){
        new BufTest().run();
    }

    private Scanner sn=new Scanner(System.in);
    private Buffer buf=new Buffer(100,new byte[]{(byte) 0xaa,(byte) 0xee,(byte) 0xdd} ,new byte[]{(byte) 0xee,(byte) 0xaa,(byte) 0xdd});
    
    public void run(){
        while(true){
            System.out.println("---------------------------INPUT---------------------------");
            passString(sn.next());
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            System.out.print("checkOut:");
            printBytes(buf.checkOut());
            System.out.println();
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            for(int i=0;i<buf.bufSize;i++)System.out.print("========");
            System.out.println();
            for(int i=0;i<buf.bufSize;i++)System.out.print("  "+(Integer.toHexString(buf.getByte(i)&0xff))+"\t|");
            System.out.println();
            for(int i=0;i<buf.bufSize;i++)System.out.print("========");
            System.out.println();
        }
    }

    /*输入16进制格式字符串 "aa/ee/dd/ff/ee/aa/dd"
     *转为byte组存进buffer 
     */
    public void passString(String arg){
        String args[]=arg.split("/");
        byte bs[]=new byte[args.length];
        for(int i=0;i<bs.length;i++){
            try {
                bs[i]=(byte) Integer.parseInt(args[i], 16);
            } catch (Exception e) {
                bs[i]=0;
            }
        }
        buf.putIn(bs);
    }


    /*将符合查找结果显示*/
    public void printBytes(byte bs[]){
        if(bs!=null)
            for(int i=0;i<bs.length;i++)
                System.out.print(Integer.toHexString(bs[i]&0xff)+" ");
    }



    public class Buffer{

        public int bufSize;
        public byte buf[]; 
        public byte head[];
        public byte foot[];
        public int wIndex=1;
        public int rIndex=0;
        
        public Buffer(int bufSize,byte head[],byte foot[]){
            this.bufSize=bufSize;
            this.buf=new byte[bufSize];
            this.head=head;
            this.foot=foot;
        }

        public byte getByte(int index){
            return buf[index];
        }


        public void putIn(byte bs[]){
            for(int i=0;i<bs.length&&putIn(bs[i]);i++);
        }

        public boolean putIn(byte b){
            if(canWrit(wIndex)){
                buf[wIndex]=b;
                wIndex=moveNext(wIndex);
                return true;
            }
            return false;
        }

        /*查找第一个符合头部的位置*/
        public byte[] checkOut(){
            if(head==null)return null;
            int index=rIndex;
            while(canRead(index)){
                if(findNext(index,head,true))return findHead(index);
                index=moveNext(index);
            }
            return null;
        }

        /* 递归查找在下一个尾部之间是否含有另一个头部
         * 如果有就讲读起始位置移到下一个头部
         * 如果没有查找尾部
         */
        public byte[] findHead(int index){
            index=moveNext(index,head.length);
            while(canRead(index)){
                if(findNext(index,head,false)){
                    rIndex=index;
                    return findHead(rIndex);
                }
                index=moveNext(index);
            }
            return findFoot(rIndex);
        }

        /* 查找尾部
         * 如果找到尾部就截取头部到尾部的数组
         * 如果没有找到返回null
         */
        public byte[] findFoot(int index){
            if(foot==null)return null;
            index=moveNext(index,foot.length);
            while(canRead(index)){
                if(findNext(index,foot,false)){
                    return getPart(moveNext(index,foot.length));
                }
                index=moveNext(index);
            }
            if(!canWrit(wIndex))rIndex=wIndex-1;
            return null;
        }

        /* 返回从读起始位置到当前位置的数组
         * 并将读起始位置置为当前截至位置
         */
        public byte[] getPart(int endIndex){
            if(rIndex==endIndex)return null;
            int i=0,size=getSize(endIndex);
            byte part[]=new byte[size];
            while(rIndex!=endIndex&&i<size){
                rIndex=moveNext(rIndex);
                part[i]=buf[rIndex];
                i++;
            }
            return part;
        }
        
        /*返回将要读取的数组大小*/
        public int getSize(int endIndex){
            if(endIndex>bufSize||
                    (endIndex>wIndex&&endIndex<rIndex)||
                    (endIndex>rIndex&&endIndex>wIndex)||
                    (endIndex<rIndex&&endIndex<wIndex)
            )return 0;
            return (rIndex>endIndex)?bufSize-rIndex+endIndex:endIndex-rIndex;
        }

        /*位置加一*/
        public int moveNext(int index){
            index++;
            return index%=bufSize;
        }

        /*位置加size*/
        public int moveNext(int index,int size){
            index+=size;
            return index%=bufSize;
        }

        /*是否可写*/
        public boolean canWrit(int index){
            index=moveNext(index);
            return index!=rIndex;
        }

        /*是否可读*/
        public boolean canRead(int index){
            index=moveNext(index);
            return index!=wIndex;
        }

        /* 返回当前位置是否等于将要比对的数组*/
        public boolean findNext(int index,byte erq[],boolean isHead){
            for(int i=0;i<erq.length;i++){
                index=moveNext(index);
                if(index!=wIndex){
                    if(buf[index]!=erq[i]){
                        if(isHead)rIndex=moveNext(rIndex, i++);
                        return false;
                    }
                }else{
                    return false;
                }
            }
            return true;
        }
    }


}

 

转载于:https://www.cnblogs.com/get-a/p/6769400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值