斑马打印机(zebraZT410R-203dpi)打印RFID标签流程

1.首先连接到打印机
下面代码为连接打印机的代码,其中ip为打印机ip地址,
"TcpConnection.DEFAULT_CPCL_TCP_PORT"的值为6101。

public Zt410Util(String ip) throws ConnectionException {
  	    Connection con = new TcpConnection(ip, TcpConnection.DEFAULT_CPCL_TCP_PORT);
        con.open();
 }

2.接着利用dom4j把xml(里面放着标签的模板信息)分别转化为Set和Model
其中最后的list[0]存的是这个模板的set也就是设置,list[1]及以后存的是这个模板的模板信息也就是这个标签上有多少个字段以及每个字段所具有的属性(如位置,内容等),获取的方式类似于爬虫的dom元素获取。

public List ZT410Convert(String xml) throws Exception {
        List list = new ArrayList();
        //将xml字符串转为对象
        Document doc = DocumentHelper.parseText(xml);
        Element rootElement = doc.getRootElement();
        Element items = rootElement.element("items");
        List item = items.elements("item");

        Element setup = rootElement.element("setup");
        Element ifSubString = setup.element("ifSubString");
        Element printPaper = setup.element("printPaper");
        // list.add(printPaper.element("yLength").getText()+","+printPaper.element("xLength").getText());
        Element printsetup = setup.element("printSetup");
        //Element fontset = printsetup.element("fontSet");
        Element printerType = printsetup.element("PrinterType");
        // list.add(error.element("isError").getText()+","+error.element("repeatNum").getText()+","+error.element("repeatPaper").getText());
        Element autocal = printsetup.element("autocal");
        //list.add(autocal.element("isAutocal").getText()+","+autocal.element("feedNum").getText()+","+autocal.element("unwindingNum").getText()+","+
        // autocal.element("feedLimit").getText()+","+autocal.element("unwindingLimit").getText()+","+printsetup.element("move").getText());
        Element move = printsetup.element("move");
        list.add(printPaper.element("yLength").getText()+","+printPaper.element("xLength").getText()+","+printsetup.element("fontSet").getText()+","+printerType.element("type").getText()+","+printerType.element("repeatNum").getText()+","+printerType.element("repeatPaper").getText()+","+
                autocal.element("isAutocal").getText()+","+autocal.element("feedNum").getText()+","+autocal.element("unwindingNum").getText()+","+
                autocal.element("feedLimit").getText()+","+autocal.element("unwindingLimit").getText()+","+printsetup.element("move").getText()+","+ifSubString.getText());
        for (Element element : item) {
            Element type = element.element("type");
            Element isVariable = element.element("isVariable");
            Element content = element.element("content");
            Element cordinateX = element.element("cordinateX");
            Element cordinateY = element.element("cordinateY");
            Element font = element.element("font");
            Element size = element.element("size");
            list.add(type.getText()+","+isVariable.getText()+","+content.getText()+","+cordinateX.getText()+","+cordinateY.getText()+","+font.getText()+","+size.getText());
        }

        return list;

    }

下面代码就是获取刚才的list[0]也就是set设置

public static List Zt410XmlToSet(String xml) throws Exception {
        Dom4jXmlToDocument dom4jXmlToDocument = new Dom4jXmlToDocument();
        List setUp = new ArrayList();
        List convert = dom4jXmlToDocument.ZT410Convert(xml);

        //设置
        if (convert != null) {
            setUp.add(convert.get(0));
        }
        return setUp;
    }

下面代码就是获取刚才的list[1]及以后也就是model

public static List Zt410XmlToModel(String xml) throws Exception {
        Dom4jXmlToDocument dom4jXmlToDocument = new Dom4jXmlToDocument();
        List convert = dom4jXmlToDocument.ZT410Convert(xml);
        List printModel = new ArrayList();
        //模板
        for (int i = 1; i < convert.size(); i++) {
            printModel.add(convert.get(i));
        }
        return printModel;
    }

3.接下来向打印机发送命令并获取反馈
第3步发送命令又分为x步

public PrintResult sendCommandAndGetResponse(List setUp, List printModel, List printValue) throws Exception {
        //3.1
        ParamToCommands_Zt410 paramToCommands_zt410 = new ParamToCommands_Zt410();
        String commands = paramToCommands_zt410.convertParamtoCommands_RFID(setUp, printModel, printValue);
        //3.2
        byte[] byteZpl = commands.getBytes();
        byte[] result = con.sendAndWaitForResponse(byteZpl, 6000, 6000, "");
        //3.3
        PrintResult pt = new PrintResult();
        try {
            String result_str = new String(result);

            if (result_str.length() < 20) {
                for (int i = 0; i < 2; i++) {
                    byte[] bytes = con.sendAndWaitForResponse(byteZpl, 6000, 6000, "");
                    //读取RFID
                    printlogicUtil.zt410ReadRFID(bytes,pt);
                    //con.close();
                    if (bytes.length > 20) {
                        return pt;
                    }
                }
                //再一次自动校验
                String checkcommands = paramToCommands_zt410.checkRFID();
                byte[] bytes = checkcommands.getBytes("UTF-8");
                con.sendAndWaitForResponse(bytes, 30000, 30000, "");
                //校验完毕读取RFID
                printlogicUtil.zt410ReadRFID(bytes,pt);
            }
            String[] rfid = result_str.split("\\r\\n");

            String epc = rfid[0].substring(5);
            String tid = rfid[1].substring(5);
            if (epc.equals(tid) && epc.length() == 24 && tid.length() == 24) {
                pt.setEpc(epc);
                pt.setTid(tid);
                con.close();
            }
            con.close();
//            pt.setTid(result_str.substring(6,30));
//            pt.setEpc(result_str.substring(36,68));
        } catch (Exception e) {
            e.printStackTrace();
            pt.setEpc("");
            pt.setTid("");


        }
        return pt;
    }

3.1把参数转换为指令
①把标签的TID读取到然后写入EPC区。
②获取打印对象列表,把printValue(值)赋给printModel中的变量中去
③把打印设置赋值给打印设置对象
④设置模板,将set转化为打印机能够识别的命令
⑤设置对象,将model转化为打印机能够识别的命令

public String convertParamtoCommands_RFID(List setUp,List printModel, List printValue){
		//①
        readTIDAndWriteEPC();
        //②
        List printObjectList = ConvertParamToList.convertParamToList(setUp,printModel,printValue);
        //③
        ZT410PrintSetUp printSetUp = ConverParamToObject.ZTlistToObject(setUp);
        //④
        setModel(printSetUp.getYLength(),printSetUp.getXLength(),printSetUp.getType());
        //⑤
        for(PrintObject printObjects:printObjectList){
            if(printObjects.getType().equals(PrintModel.CONTENT)){
                setContent(printSetUp.getFontSet(),printObjects.getContent(),printObjects.getX(),printObjects.getY(),printObjects.getSize());
            }else if(printObjects.getType().equals(PrintModel.BARCODE)){
                if (printObjects.getSize() == 8.0) {
                    setBarCode300(printObjects.getContent(),printObjects.getX(),printObjects.getY(),printObjects.getSize());
                }else {
                setBarCode(printObjects.getContent(),printObjects.getX(),printObjects.getY(),printObjects.getSize());
                }
            }
            else if(printObjects.getType().equals(PrintModel.QRCODE)){
                if (printObjects.getSize() == 8.0) {
                    setQRCode300(printObjects.getContent(),printObjects.getX(),printObjects.getY(),printObjects.getSize());
                }else {
                    setQRCode(printObjects.getContent(),printObjects.getX(),printObjects.getY(),printObjects.getSize(),printSetUp.getType());
                }
            }else if(printObjects.getType().equals(PrintModel.RFID_USER)){
                writeUSER(printObjects.getContent());

            }
        }
//        writeEPC();
//        readTID();
//        readEPC();

        return getPrintContent();
    }

3.2把指令转换为字节码并发送
3.3写入epc和tid
至此打印结束

你可能感兴趣的:(RFID标签,斑马打印机)