接触串口很久了,一直以来将都是将串口读取出来的数组转换成字符串通过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;ibufSize|| (endIndex>wIndex&&endIndex rIndex&&endIndex>wIndex)|| (endIndex 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