rfid标签打印

This commit is contained in:
2025-09-15 09:56:24 +08:00
parent 8c6cc08cac
commit 7fea69e367
4 changed files with 228 additions and 77 deletions

View File

@@ -5,10 +5,10 @@ import com.ZMPrinter.LSF.LSFDecoder;
import com.ZMPrinter.UsbUtility.UsbConnection; import com.ZMPrinter.UsbUtility.UsbConnection;
import com.zg.project.Inventory.AutoInventory.domain.entity.PrintRfid; import com.zg.project.Inventory.AutoInventory.domain.entity.PrintRfid;
import com.zg.project.Inventory.AutoInventory.mapper.PrintRfidMapper; import com.zg.project.Inventory.AutoInventory.mapper.PrintRfidMapper;
import com.zg.project.information.domain.PcdeDetail;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping; import com.zg.project.information.mapper.PcdeDetailMapper;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -19,7 +19,8 @@ import java.util.Map;
public class PrintController { public class PrintController {
@Autowired @Autowired
PrintRfidMapper rfidMapper; PrintRfidMapper rfidMapper;
@Autowired
PcdeDetailMapper pcdeDetailMapper;
/** /**
* 代码定义标签格式 * 代码定义标签格式
* @return * @return
@@ -257,12 +258,29 @@ public class PrintController {
// return AjaxResult.success(result); // return AjaxResult.success(result);
// } // }
@PostMapping("/printaaa")
public String printaaa(@RequestBody Map<String,Object> params){
Integer printType = (Integer) params.get("printType");
//批量打印、单个打印
if(printType == 1){
ArrayList ids = (ArrayList) params.get("ids");
List<PcdeDetail> rfidList = pcdeDetailMapper.selectPcdeDetailListIds(ids);
Long aa = 3333L;
return "tttt";
}else if(printType == 2){ //手动输入信息打印
}
return "rrrr";
}
/** /**
* 模板定义标签格式 * 模板定义标签格式
* @return * @return
*/ */
@GetMapping("/printlsf") @PostMapping("/printlsf")
public String printlsf() { public String printlsf(@RequestBody Map<String,Object> params) {
zmprinter thiszmprinter = new zmprinter();//定义打印机对象 zmprinter thiszmprinter = new zmprinter();//定义打印机对象
zmlabel thiszmlabel = new zmlabel();//定义标签对象 zmlabel thiszmlabel = new zmlabel();//定义标签对象
List<zmlabelobject> thiszmlabelobjectlist = new ArrayList<>();//定义标签上需要打印的对象列表 List<zmlabelobject> thiszmlabelobjectlist = new ArrayList<>();//定义标签上需要打印的对象列表
@@ -321,36 +339,52 @@ public class PrintController {
//endregion //endregion
} }
//endregion //endregion
// 查print_rfid表打印标签 //批量打印、单个打印
PrintRfid printRfid = new PrintRfid(); Integer printType = (Integer) params.get("printType");
printRfid.setIsprint(0L); //批量打印、单个打印
List<PrintRfid> rfidList = new ArrayList<>(); if(printType == 1){
rfidList = rfidMapper.selectPrintRfidList(printRfid); ArrayList ids = (ArrayList) params.get("ids");
if(rfidList.size()>0){
//region 循环打印标签 List<PcdeDetail> rfidList = pcdeDetailMapper.selectPcdeDetailListIds(ids);
for(PrintRfid info : rfidList) { if(rfidList.size()>0){
for(PcdeDetail info : rfidList) {
// System.out.println("打印第"+pnum+"张"); // System.out.println("打印第"+pnum+"张");
//region 处理读取标签数据(如果需要) //region 处理读取标签数据(如果需要)
String tagTID=""; String tagTID="";
if(isRFIDread) { if(isRFIDread) {
//region 读取UHF标签数据 //region 读取UHF标签数据
UsbConnection usbConnection = new UsbConnection();//建立USB连接 UsbConnection usbConnection = new UsbConnection();//建立USB连接
if (!usbConnection.Open()) { if (!usbConnection.Open()) {
System.out.println("连接打印机失败,请检查。"); System.out.println("连接打印机失败,请检查。");
return "连接打印机失败,请检查"; return "连接打印机失败,请检查";
} }
if(isRFIDwrite) if(isRFIDwrite)
tagTID = usbConnection.readUHFTag(0, 3);//读TID后面需要写EPC操作 tagTID = usbConnection.readUHFTag(0, 3);//读TID后面需要写EPC操作
else else
tagTID=usbConnection.readUHFTag(0,2);//读TID不需要写EPC操作 tagTID=usbConnection.readUHFTag(0,2);//读TID不需要写EPC操作
usbConnection.Close();//关闭USB连接 usbConnection.Close();//关闭USB连接
if (tagTID.isEmpty() || tagTID.equals("X"))//未读到数据 if (tagTID.isEmpty() || tagTID.equals("X"))//未读到数据
{ {
System.out.println("未读到标签数据。"); System.out.println("未读到标签数据。");
continue;//退出当次循环 continue;//退出当次循环
}
//更新读到的数据
if(tagTID!=null&&!tagTID.isEmpty()) {
for (zmlabelobject thisLabelObject : thiszmlabelobjectlist) {
for (int vi = 0; vi < thisLabelObject.Variables.size(); vi++) {
objectvariable thisVariable = thisLabelObject.Variables.get(vi);
if (thisVariable.type == 4||thisVariable.type == 5) {//LSF文件中有读取HF或UHF的ID对象
thisVariable.data=tagTID;
thisLabelObject.Variables.set(vi, thisVariable);//更新数据
}
}
}
}
//endregion
} }
//更新读到的数据 //更新读到的数据
@@ -358,7 +392,7 @@ public class PrintController {
for (zmlabelobject thisLabelObject : thiszmlabelobjectlist) { for (zmlabelobject thisLabelObject : thiszmlabelobjectlist) {
for (int vi = 0; vi < thisLabelObject.Variables.size(); vi++) { for (int vi = 0; vi < thisLabelObject.Variables.size(); vi++) {
objectvariable thisVariable = thisLabelObject.Variables.get(vi); objectvariable thisVariable = thisLabelObject.Variables.get(vi);
if (thisVariable.type == 4||thisVariable.type == 5) {//LSF文件中有读取HF或UHF的ID对象 if (thisVariable.type == 4||thisVariable.type == 5) {
thisVariable.data=tagTID; thisVariable.data=tagTID;
thisLabelObject.Variables.set(vi, thisVariable);//更新数据 thisLabelObject.Variables.set(vi, thisVariable);//更新数据
} }
@@ -366,33 +400,18 @@ public class PrintController {
} }
} }
//endregion //endregion
}
//更新读到的数据 //region 设置对象里变量的值(如果需要)
if(tagTID!=null&&!tagTID.isEmpty()) { //注意参数varName是lsf文件里对象变量的“子字符串共享名称”varValue是需要设置的新值
for (zmlabelobject thisLabelObject : thiszmlabelobjectlist) { zmprintoperate.setVarValue(thiszmlabelobjectlist,"position",info.getPcode());//设置变量的子字符串共享名称为“name”的值
for (int vi = 0; vi < thisLabelObject.Variables.size(); vi++) { zmprintoperate.setVarValue(thiszmlabelobjectlist,"maposition",info.getPcode());//设置变量的子字符串共享名称为“model”的值
objectvariable thisVariable = thisLabelObject.Variables.get(vi); zmprintoperate.setVarValue(thiszmlabelobjectlist,"rfid",info.getEncodedId());//设置变量的子字符串共享名称为“location”的值
if (thisVariable.type == 4||thisVariable.type == 5) {
thisVariable.data=tagTID;
thisLabelObject.Variables.set(vi, thisVariable);//更新数据
}
}
}
}
//endregion
//region 设置对象里变量的值(如果需要)
//注意参数varName是lsf文件里对象变量的“子字符串共享名称”varValue是需要设置的新值
zmprintoperate.setVarValue(thiszmlabelobjectlist,"position",info.getPcde());//设置变量的子字符串共享名称为“name”的值
zmprintoperate.setVarValue(thiszmlabelobjectlist,"maposition",info.getPcde());//设置变量的子字符串共享名称为“model”的值
zmprintoperate.setVarValue(thiszmlabelobjectlist,"rfid",info.getRfid());//设置变量的子字符串共享名称为“location”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"department","国产操作系统研发13组");//设置变量的子字符串共享名称为“department”的值 // zmprintoperate.setVarValue(thiszmlabelobjectlist,"department","国产操作系统研发13组");//设置变量的子字符串共享名称为“department”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"purchasedate","2020-10-08");//设置变量的子字符串共享名称为“purchasedate”的值 // zmprintoperate.setVarValue(thiszmlabelobjectlist,"purchasedate","2020-10-08");//设置变量的子字符串共享名称为“purchasedate”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"assetcode","2021092300090002"+String.format("%04d",pnum));//设置变量的子字符串共享名称为“assetcode”的值 // zmprintoperate.setVarValue(thiszmlabelobjectlist,"assetcode","2021092300090002"+String.format("%04d",pnum));//设置变量的子字符串共享名称为“assetcode”的值
//endregion //endregion
//region 预览标签(即生成标签图片) //region 预览标签(即生成标签图片)
/* BufferedImage labelimage=zmprintoperate.CreateLabelImage(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签图片 /* BufferedImage labelimage=zmprintoperate.CreateLabelImage(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签图片
try { try {
if(labelimage!=null) { if(labelimage!=null) {
@@ -402,43 +421,158 @@ public class PrintController {
} catch (IOException e) { } catch (IOException e) {
System.out.println("保存预览图片出错,错误信息:"+e.getMessage()); System.out.println("保存预览图片出错,错误信息:"+e.getMessage());
}*/ }*/
//endregion //endregion
//region 将标签生成打印数据 //region 将标签生成打印数据
byte[] zmpclecommand = zmprintoperate.CreateLabelCommand(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签数据 byte[] zmpclecommand = zmprintoperate.CreateLabelCommand(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签数据
if (zmpclecommand == null) { if (zmpclecommand == null) {
System.out.println("生成标签数据异常为空."); System.out.println("生成标签数据异常为空.");
return "生成标签数据异常为空"; return "生成标签数据异常为空";
}
//endregion
//region 通过打印机USB端口发送打印数据
UsbConnection usbConnection = new UsbConnection();//建立USB连接
if (!usbConnection.Open()) {
System.out.println("连接打印机失败,请检查。");
return "连接打印机失败,请检查";
}
try {
usbConnection.write(zmpclecommand);//发送数据给打印机
if (thiszmprinter.printerinterface == printerstyle.RFID_USB) {
//如果是RFID打印机则通过打印机USB端口读回状态确定打印机收到数据并等待打印完成
String returnmsg = usbConnection.read(512);//读取打印机数据
if (!returnmsg.isEmpty())
System.out.println("打印机返回的数据:" + returnmsg);
zmprintoperate.WaitPrintProcess(thiszmprinter, thiszmlabel);//等待打印完成
}
}
finally {
usbConnection.Close();//关闭USB连接
}
//endregion
//修改表字段,标记为已打印
// rfidMapper.updatePrintRfid(info.getId());
} }
//endregion
//region 通过打印机USB端口发送打印数据 //endregion
}
}else if(printType == 2){ //手动输入信息打印
String printStr = (String) params.get("printStr");
// 将 printStr 转换为十六进制字符串
StringBuilder hex = new StringBuilder();
for (int i = 0; i < printStr.length(); i++) {
char c = printStr.charAt(i);
String hexPart = Integer.toHexString(c).toUpperCase();
hex.append(hexPart);
}
String rfidStr = hex.toString();
//region 处理读取标签数据(如果需要)
String tagTID="";
if(isRFIDread) {
//region 读取UHF标签数据
UsbConnection usbConnection = new UsbConnection();//建立USB连接 UsbConnection usbConnection = new UsbConnection();//建立USB连接
if (!usbConnection.Open()) { if (!usbConnection.Open()) {
System.out.println("连接打印机失败,请检查。"); System.out.println("连接打印机失败,请检查。");
return "连接打印机失败,请检查"; return "连接打印机失败,请检查";
} }
try { if(isRFIDwrite)
usbConnection.write(zmpclecommand);//发送数据给打印机 tagTID = usbConnection.readUHFTag(0, 3);//读TID后面需要写EPC操作
if (thiszmprinter.printerinterface == printerstyle.RFID_USB) { else
//如果是RFID打印机则通过打印机USB端口读回状态确定打印机收到数据并等待打印完成 tagTID=usbConnection.readUHFTag(0,2);//读TID不需要写EPC操作
String returnmsg = usbConnection.read(512);//读取打印机数据 usbConnection.Close();//关闭USB连接
if (!returnmsg.isEmpty())
System.out.println("打印机返回的数据:" + returnmsg); if (tagTID.isEmpty() || tagTID.equals("X"))//未读到数据
zmprintoperate.WaitPrintProcess(thiszmprinter, thiszmlabel);//等待打印完成 {
System.out.println("未读到标签数据。");
return "未读到标签数据";
}
//更新读到的数据
if(tagTID!=null&&!tagTID.isEmpty()) {
for (zmlabelobject thisLabelObject : thiszmlabelobjectlist) {
for (int vi = 0; vi < thisLabelObject.Variables.size(); vi++) {
objectvariable thisVariable = thisLabelObject.Variables.get(vi);
if (thisVariable.type == 4||thisVariable.type == 5) {//LSF文件中有读取HF或UHF的ID对象
thisVariable.data=tagTID;
thisLabelObject.Variables.set(vi, thisVariable);//更新数据
}
}
} }
} }
finally {
usbConnection.Close();//关闭USB连接
}
//endregion //endregion
//修改表字段,标记为已打印
rfidMapper.updatePrintRfid(info.getId());
} }
//更新读到的数据
if(tagTID!=null&&!tagTID.isEmpty()) {
for (zmlabelobject thisLabelObject : thiszmlabelobjectlist) {
for (int vi = 0; vi < thisLabelObject.Variables.size(); vi++) {
objectvariable thisVariable = thisLabelObject.Variables.get(vi);
if (thisVariable.type == 4||thisVariable.type == 5) {
thisVariable.data=tagTID;
thisLabelObject.Variables.set(vi, thisVariable);//更新数据
}
}
}
}
//endregion //endregion
//region 设置对象里变量的值(如果需要)
//注意参数varName是lsf文件里对象变量的“子字符串共享名称”varValue是需要设置的新值
zmprintoperate.setVarValue(thiszmlabelobjectlist,"position",printStr);//设置变量的子字符串共享名称为“name”的值
zmprintoperate.setVarValue(thiszmlabelobjectlist,"maposition",printStr);//设置变量的子字符串共享名称为“model”的值
zmprintoperate.setVarValue(thiszmlabelobjectlist,"rfid",rfidStr);//设置变量的子字符串共享名称为“location”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"department","国产操作系统研发13组");//设置变量的子字符串共享名称为“department”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"purchasedate","2020-10-08");//设置变量的子字符串共享名称为“purchasedate”的值
// zmprintoperate.setVarValue(thiszmlabelobjectlist,"assetcode","2021092300090002"+String.format("%04d",pnum));//设置变量的子字符串共享名称为“assetcode”的值
//endregion
//region 预览标签(即生成标签图片)
/* BufferedImage labelimage=zmprintoperate.CreateLabelImage(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签图片
try {
if(labelimage!=null) {
File imageoutputfile = new File("labelpreview.png");
ImageIO.write(labelimage, "png", imageoutputfile);//保存图片
}
} catch (IOException e) {
System.out.println("保存预览图片出错,错误信息:"+e.getMessage());
}*/
//endregion
//region 将标签生成打印数据
byte[] zmpclecommand = zmprintoperate.CreateLabelCommand(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签数据
if (zmpclecommand == null) {
System.out.println("生成标签数据异常为空.");
return "生成标签数据异常为空";
}
//endregion
//region 通过打印机USB端口发送打印数据
UsbConnection usbConnection = new UsbConnection();//建立USB连接
if (!usbConnection.Open()) {
System.out.println("连接打印机失败,请检查。");
return "连接打印机失败,请检查";
}
try {
usbConnection.write(zmpclecommand);//发送数据给打印机
if (thiszmprinter.printerinterface == printerstyle.RFID_USB) {
//如果是RFID打印机则通过打印机USB端口读回状态确定打印机收到数据并等待打印完成
String returnmsg = usbConnection.read(512);//读取打印机数据
if (!returnmsg.isEmpty())
System.out.println("打印机返回的数据:" + returnmsg);
zmprintoperate.WaitPrintProcess(thiszmprinter, thiszmlabel);//等待打印完成
}
}
finally {
usbConnection.Close();//关闭USB连接
}
} }
// 查print_rfid表打印标签
return "打印成功"; return "打印成功";
} }
} }

View File

@@ -27,6 +27,7 @@ public interface PcdeDetailMapper
*/ */
public List<PcdeDetail> selectPcdeDetailList(PcdeDetail pcdeDetail); public List<PcdeDetail> selectPcdeDetailList(PcdeDetail pcdeDetail);
public List<PcdeDetail> selectPcdeDetailListIds(Object ids);
/** /**
* 新增库位明细 * 新增库位明细
* *

View File

@@ -1,5 +1,6 @@
package com.zg.project.information.service; package com.zg.project.information.service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.zg.project.information.domain.PcdeDetail; import com.zg.project.information.domain.PcdeDetail;

View File

@@ -46,6 +46,21 @@
<if test="updatedAt != null">and d.updated_at = #{updatedAt}</if> <if test="updatedAt != null">and d.updated_at = #{updatedAt}</if>
</where> </where>
</select> </select>
<select id="selectPcdeDetailListIds" parameterType="PcdeDetail" resultMap="PcdeDetailResult">
<include refid="selectPcdeDetailVo"/>
<where>
<choose>
<when test="ids != null and ids.size > 0">
AND d.id IN
<foreach collection="ids" item="val" open="(" separator="," close=")">
#{val}
</foreach>
</when>
</choose>
</where>
</select>
<select id="selectPcdeDetailById" parameterType="Long" resultMap="PcdeDetailResult"> <select id="selectPcdeDetailById" parameterType="Long" resultMap="PcdeDetailResult">
<include refid="selectPcdeDetailVo"/> <include refid="selectPcdeDetailVo"/>