称重优化

This commit is contained in:
zc
2026-04-09 11:05:57 +08:00
parent 95242f772e
commit 90eb4a1af8
12 changed files with 124 additions and 43 deletions

View File

@@ -1,6 +1,5 @@
package top.wms.admin.common.enums; package top.wms.admin.common.enums;
/** /**
* DPA6024V-2T-1.0 数字控制器命令类型枚举 * DPA6024V-2T-1.0 数字控制器命令类型枚举
* 定义控制器支持的指令字 * 定义控制器支持的指令字
@@ -35,6 +34,7 @@ public enum CommandTypeEnum {
/** /**
* 获取指令字字符 * 获取指令字字符
*
* @return 指令字 (如 '1', '2', '3', '4') * @return 指令字 (如 '1', '2', '3', '4')
*/ */
public char getCommandCode() { public char getCommandCode() {
@@ -43,6 +43,7 @@ public enum CommandTypeEnum {
/** /**
* 根据指令字获取对应的命令类型 * 根据指令字获取对应的命令类型
*
* @param commandCode 指令字字符 * @param commandCode 指令字字符
* @return 对应的CommandType找不到返回null * @return 对应的CommandType找不到返回null
*/ */

View File

@@ -30,7 +30,7 @@ public class LightService {
serialHandler.close(); serialHandler.close();
serialHandler = null; serialHandler = null;
} }
// 使用COM1串口 // 使用COM1串口
String portName = "COM1"; String portName = "COM1";
// 默认波特率9600 // 默认波特率9600
@@ -74,6 +74,7 @@ public class LightService {
/** /**
* 检查是否已连接 * 检查是否已连接
*
* @return 已连接返回true否则返回false * @return 已连接返回true否则返回false
*/ */
public boolean isConnected() { public boolean isConnected() {
@@ -84,6 +85,7 @@ public class LightService {
/** /**
* 检查连接状态并尝试重连 * 检查连接状态并尝试重连
*
* @return 重连成功返回true否则返回false * @return 重连成功返回true否则返回false
*/ */
public boolean checkAndReconnect() { public boolean checkAndReconnect() {
@@ -98,6 +100,7 @@ public class LightService {
/** /**
* 重新连接控制器 * 重新连接控制器
*
* @return 重连成功返回true否则返回false * @return 重连成功返回true否则返回false
*/ */
public boolean reconnect() { public boolean reconnect() {
@@ -117,6 +120,7 @@ public class LightService {
/** /**
* 打开指定通道 * 打开指定通道
*
* @param channel 通道号 (1-2) * @param channel 通道号 (1-2)
*/ */
public boolean turnOn(int channel) { public boolean turnOn(int channel) {
@@ -125,6 +129,7 @@ public class LightService {
/** /**
* 关闭指定通道 * 关闭指定通道
*
* @param channel 通道号 (1-2) * @param channel 通道号 (1-2)
*/ */
public boolean turnOff(int channel) { public boolean turnOff(int channel) {
@@ -133,7 +138,8 @@ public class LightService {
/** /**
* 设置通道亮度 * 设置通道亮度
* @param channel 通道号 (1-2) *
* @param channel 通道号 (1-2)
* @param brightness 亮度等级 (0-255) * @param brightness 亮度等级 (0-255)
*/ */
public boolean setBrightness(int channel, int brightness) { public boolean setBrightness(int channel, int brightness) {
@@ -147,6 +153,7 @@ public class LightService {
/** /**
* 读取通道亮度 * 读取通道亮度
*
* @param channel 通道号 (1-2) * @param channel 通道号 (1-2)
* @return 亮度等级 (0-255),失败返回-1 * @return 亮度等级 (0-255),失败返回-1
*/ */
@@ -230,7 +237,7 @@ public class LightService {
char cmdChar = type.getCommandCode(); char cmdChar = type.getCommandCode();
// 通道字 (1-2) // 通道字 (1-2)
char channelChar = (char) (channel + '0'); char channelChar = (char)(channel + '0');
// 数据: 3字节亮度的十六进制表示高位在前 // 数据: 3字节亮度的十六进制表示高位在前
// 亮度值范围0-255需要转换为3个十六进制ASCII字符 // 亮度值范围0-255需要转换为3个十六进制ASCII字符
@@ -286,6 +293,7 @@ public class LightService {
/** /**
* 便捷方法:设置所有通道亮度 * 便捷方法:设置所有通道亮度
*
* @param brightness 亮度等级 (0-255) * @param brightness 亮度等级 (0-255)
*/ */
public void setAllBrightness(int brightness) { public void setAllBrightness(int brightness) {
@@ -314,5 +322,5 @@ public class LightService {
turnOn(channel); turnOn(channel);
} }
} }
} }

View File

@@ -1,6 +1,5 @@
package top.wms.admin.light; package top.wms.admin.light;
import com.fazecast.jSerialComm.SerialPort; import com.fazecast.jSerialComm.SerialPort;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -20,13 +19,13 @@ public class SerialPortHandler {
private InputStream inputStream; private InputStream inputStream;
private OutputStream outputStream; private OutputStream outputStream;
private SerialPort serialPort; private SerialPort serialPort;
/** /**
* 串口名称 * 串口名称
*/ */
@Getter @Getter
private final String portName; private final String portName;
/** /**
* 波特率 * 波特率
*/ */
@@ -35,6 +34,7 @@ public class SerialPortHandler {
/** /**
* 构造函数 * 构造函数
*
* @param portName 串口名称,如 "COM3" (Windows) 或 "/dev/ttyUSB0" (Linux) * @param portName 串口名称,如 "COM3" (Windows) 或 "/dev/ttyUSB0" (Linux)
* @param baudRate 波特率 * @param baudRate 波特率
*/ */
@@ -45,6 +45,7 @@ public class SerialPortHandler {
/** /**
* 打开串口连接 * 打开串口连接
*
* @return 打开成功返回true失败返回false * @return 打开成功返回true失败返回false
*/ */
public boolean open() { public boolean open() {
@@ -99,6 +100,7 @@ public class SerialPortHandler {
/** /**
* 检查串口是否已打开 * 检查串口是否已打开
*
* @return 已打开返回true否则返回false * @return 已打开返回true否则返回false
*/ */
public boolean isOpen() { public boolean isOpen() {
@@ -107,6 +109,7 @@ public class SerialPortHandler {
/** /**
* 发送数据 * 发送数据
*
* @param data 要发送的字节数组 * @param data 要发送的字节数组
*/ */
public void sendData(byte[] data) { public void sendData(byte[] data) {
@@ -116,7 +119,7 @@ public class SerialPortHandler {
log.error("串口 {} 未打开,无法发送数据", portName); log.error("串口 {} 未打开,无法发送数据", portName);
return; return;
} }
if (outputStream == null) { if (outputStream == null) {
log.error("串口 {} 输出流未初始化", portName); log.error("串口 {} 输出流未初始化", portName);
return; return;
@@ -131,6 +134,7 @@ public class SerialPortHandler {
/** /**
* 发送字符串数据 * 发送字符串数据
*
* @param data 要发送的字符串 * @param data 要发送的字符串
*/ */
public void sendData(String data) { public void sendData(String data) {
@@ -144,6 +148,7 @@ public class SerialPortHandler {
/** /**
* 接收响应数据 * 接收响应数据
*
* @param timeoutMs 超时时间(毫秒) * @param timeoutMs 超时时间(毫秒)
* @return 接收到的字符串超时或无数据返回null * @return 接收到的字符串超时或无数据返回null
*/ */
@@ -154,7 +159,7 @@ public class SerialPortHandler {
log.error("串口 {} 未打开,无法接收数据", portName); log.error("串口 {} 未打开,无法接收数据", portName);
return null; return null;
} }
// 验证输入流是否初始化 // 验证输入流是否初始化
if (inputStream == null) { if (inputStream == null) {
log.error("串口 {} 输入流未初始化", portName); log.error("串口 {} 输入流未初始化", portName);
@@ -163,8 +168,7 @@ public class SerialPortHandler {
// 等待数据到达 // 等待数据到达
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
while (inputStream.available() == 0 && while (inputStream.available() == 0 && (System.currentTimeMillis() - startTime) < timeoutMs) {
(System.currentTimeMillis() - startTime) < timeoutMs) {
Thread.sleep(10); Thread.sleep(10);
} }
@@ -185,7 +189,8 @@ public class SerialPortHandler {
/** /**
* 接收指定长度的响应数据 * 接收指定长度的响应数据
* @param length 期望接收的字节数 *
* @param length 期望接收的字节数
* @param timeoutMs 超时时间(毫秒) * @param timeoutMs 超时时间(毫秒)
* @return 接收到的字符串超时或无数据返回null * @return 接收到的字符串超时或无数据返回null
*/ */
@@ -196,7 +201,7 @@ public class SerialPortHandler {
log.error("串口 {} 未打开,无法接收数据", portName); log.error("串口 {} 未打开,无法接收数据", portName);
return null; return null;
} }
// 验证输入流是否初始化 // 验证输入流是否初始化
if (inputStream == null) { if (inputStream == null) {
log.error("串口 {} 输入流未初始化", portName); log.error("串口 {} 输入流未初始化", portName);
@@ -207,8 +212,7 @@ public class SerialPortHandler {
int bytesRead = 0; int bytesRead = 0;
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
while (bytesRead < length && while (bytesRead < length && (System.currentTimeMillis() - startTime) < timeoutMs) {
(System.currentTimeMillis() - startTime) < timeoutMs) {
if (inputStream.available() > 0) { if (inputStream.available() > 0) {
int read = inputStream.read(buffer, bytesRead, length - bytesRead); int read = inputStream.read(buffer, bytesRead, length - bytesRead);
if (read > 0) { if (read > 0) {
@@ -276,7 +280,7 @@ public class SerialPortHandler {
} }
} catch (IOException e) { } catch (IOException e) {
log.error("关闭串口失败: {}", e.getMessage()); log.error("关闭串口失败: {}", e.getMessage());
}finally { } finally {
serialPort = null; serialPort = null;
} }
} }

View File

@@ -38,9 +38,9 @@ public class MaterialInfoDO extends BaseDO {
private BigDecimal unitWeight; private BigDecimal unitWeight;
/* /*
物料规格 物料直径
*/ */
private String materialSpec; private Double materialSpec;
/** /**
* 物料照片地址 * 物料照片地址
@@ -61,4 +61,9 @@ public class MaterialInfoDO extends BaseDO {
* 灯光等级 * 灯光等级
*/ */
private Integer lightLevel; private Integer lightLevel;
/**
* 颜色
*/
private String color;
} }

View File

@@ -46,10 +46,10 @@ public class MaterialImportRowReq implements Serializable {
private String lightLevelName; private String lightLevelName;
/* /*
* 物料规格 * 物料直径
* */ * */
@Schema(description = "物料规格") @Schema(description = "物料直径")
private String materialSpec; private Double materialSpec;
/** /**
* 物料类型名称 * 物料类型名称
@@ -63,4 +63,10 @@ public class MaterialImportRowReq implements Serializable {
@Schema(description = "物料流程") @Schema(description = "物料流程")
private String materialProcess; private String materialProcess;
/**
* 颜色
*/
@Schema(description = "颜色")
private String color;
} }

View File

@@ -48,10 +48,10 @@ public class MaterialInfoReq implements Serializable {
private Double unitWeight; private Double unitWeight;
/* /*
* 物料规格 * 物料直径
* */ * */
@Schema(description = "物料规格") @Schema(description = "物料直径")
private String materialSpec; private Double materialSpec;
/** /**
* 物料照片地址 * 物料照片地址
@@ -79,4 +79,10 @@ public class MaterialInfoReq implements Serializable {
*/ */
@Schema(description = "灯光等级") @Schema(description = "灯光等级")
private Integer lightLevel; private Integer lightLevel;
/**
* 颜色
*/
@Schema(description = "颜色")
private String color;
} }

View File

@@ -45,11 +45,11 @@ public class MaterialInfoResp extends BaseDetailResp {
private Double unitWeight; private Double unitWeight;
/** /**
* 物料规格 * 物料直径
*/ */
@Schema(description = "物料规格") @Schema(description = "物料直径")
@ExcelProperty(value = "物料规格", order = 5) @ExcelProperty(value = "物料直径", order = 5)
private String materialSpec; private Double materialSpec;
/** /**
* 物料照片地址 * 物料照片地址
@@ -100,4 +100,11 @@ public class MaterialInfoResp extends BaseDetailResp {
@ExcelProperty(value = "灯光等级", converter = LightLevelEnumConverter.class, order = 4) @ExcelProperty(value = "灯光等级", converter = LightLevelEnumConverter.class, order = 4)
private Integer lightLevel; private Integer lightLevel;
/**
* 颜色
*/
@Schema(description = "颜色")
@ExcelProperty(value = "颜色", order = 6)
private String color;
} }

View File

@@ -51,7 +51,6 @@ import top.wms.admin.material.model.resp.MaterialImportParseResp;
import top.wms.admin.material.model.resp.MaterialInfoImportResp; import top.wms.admin.material.model.resp.MaterialInfoImportResp;
import top.wms.admin.material.model.resp.MaterialInfoResp; import top.wms.admin.material.model.resp.MaterialInfoResp;
import top.wms.admin.material.service.MaterialInfoService; import top.wms.admin.material.service.MaterialInfoService;
import top.wms.admin.materialProcess.model.entity.MaterialProcessDO;
import top.wms.admin.materialType.mapper.MaterialTypeMapper; import top.wms.admin.materialType.mapper.MaterialTypeMapper;
import top.wms.admin.materialType.model.entity.MaterialTypeDO; import top.wms.admin.materialType.model.entity.MaterialTypeDO;
import top.wms.admin.system.service.FileService; import top.wms.admin.system.service.FileService;
@@ -238,8 +237,11 @@ public class MaterialInfoServiceImpl extends BaseServiceImpl<MaterialInfoMapper,
} }
MaterialInfoDO materialDO = BeanUtil.toBeanIgnoreError(row, MaterialInfoDO.class); MaterialInfoDO materialDO = BeanUtil.toBeanIgnoreError(row, MaterialInfoDO.class);
materialDO.setUnitWeight(NumberUtil.isValidNumber(row.getUnitWeight()) ? row.getUnitWeight() : null); materialDO.setUnitWeight(NumberUtil.isValidNumber(row.getUnitWeight()) ? row.getUnitWeight() : null);
materialDO.setMaterialSpec(StrUtil.isNotBlank(row.getMaterialSpec()) ? row.getMaterialSpec() : null); materialDO.setMaterialSpec(row.getMaterialSpec());
materialDO.setMaterialProcess(StrUtil.isNotBlank(row.getMaterialProcess()) ? row.getMaterialProcess() : null); materialDO.setColor(row.getColor());
materialDO.setMaterialProcess(StrUtil.isNotBlank(row.getMaterialProcess())
? row.getMaterialProcess()
: null);
materialDO.setMaterialTypeId(materialTypeMap.get(row.getTypeName())); materialDO.setMaterialTypeId(materialTypeMap.get(row.getTypeName()));
materialDO.setLightLevel(lightLevelMap.get(row.getLightLevelName())); materialDO.setLightLevel(lightLevelMap.get(row.getLightLevelName()));
// 修改 or 新增 // 修改 or 新增

View File

@@ -67,10 +67,10 @@ public class WorkOrderResp extends BaseDetailResp {
private BigDecimal unitWeight; private BigDecimal unitWeight;
/** /**
* 物料规格 * 物料直径
*/ */
@Schema(description = "物料规格") @Schema(description = "物料直径")
private String materialSpec; private Double materialSpec;
/** /**
* 物料图片 * 物料图片

View File

@@ -38,7 +38,7 @@ public class LightController {
if (connected) { if (connected) {
return R.ok(); return R.ok();
} else { } else {
return R.fail("500","灯光连接失败"); return R.fail("500", "灯光连接失败");
} }
} }
@@ -79,12 +79,13 @@ public class LightController {
if (success) { if (success) {
return R.ok(); return R.ok();
} else { } else {
return R.fail("500","设置灯光亮度失败,请检查连接状态"); return R.fail("500", "设置灯光亮度失败,请检查连接状态");
} }
} }
/** /**
* 读取通道亮度 * 读取通道亮度
*
* @param channel 通道号 (1-2) * @param channel 通道号 (1-2)
*/ */
@GetMapping("/brightness") @GetMapping("/brightness")
@@ -96,6 +97,7 @@ public class LightController {
/** /**
* 打开指定通道 * 打开指定通道
*
* @param channel 通道号 (1-2) * @param channel 通道号 (1-2)
*/ */
@PostMapping("/turn-on") @PostMapping("/turn-on")
@@ -107,6 +109,7 @@ public class LightController {
/** /**
* 关闭指定通道 * 关闭指定通道
*
* @param channel 通道号 (1-2) * @param channel 通道号 (1-2)
*/ */
@PostMapping("/turn-off") @PostMapping("/turn-off")
@@ -118,6 +121,7 @@ public class LightController {
/** /**
* 设置所有通道亮度 * 设置所有通道亮度
*
* @param brightness 亮度等级 (0-255) * @param brightness 亮度等级 (0-255)
*/ */
@PostMapping("/all-brightness") @PostMapping("/all-brightness")

View File

@@ -1,15 +1,22 @@
package top.wms.admin.controller.vm; package top.wms.admin.controller.vm;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import top.continew.starter.core.exception.BusinessException;
import top.continew.starter.core.validation.CheckUtils; import top.continew.starter.core.validation.CheckUtils;
import top.wms.admin.controller.tcp.config.SimpleRequestMatcher; import top.wms.admin.controller.tcp.config.SimpleRequestMatcher;
import top.wms.admin.controller.tcp.manager.ChannelManager; import top.wms.admin.controller.tcp.manager.ChannelManager;
import top.wms.admin.controller.tcp.service.SaveBmpTaskService; import top.wms.admin.controller.tcp.service.SaveBmpTaskService;
import top.wms.admin.material.mapper.MaterialInfoMapper;
import top.wms.admin.material.model.entity.MaterialInfoDO;
import top.wms.admin.material.model.resp.MaterialInfoResp;
import top.wms.admin.material.service.MaterialInfoService;
@Slf4j @Slf4j
@RestController @RestController
@@ -25,11 +32,16 @@ public class VmCommandController {
@Autowired @Autowired
private SaveBmpTaskService saveBmpTaskService; private SaveBmpTaskService saveBmpTaskService;
@Autowired
private MaterialInfoMapper materialInfoMapper;
@PostMapping("/send") @PostMapping("/send")
public String sendAndWait(@RequestBody JSONObject js) { public String sendAndWait(@RequestBody JSONObject js) {
String materialProcess = js.getString("materialProcess"); String materialCode = js.getString("materialCode");
log.info("开始比对: {}", materialProcess); log.info("开始比对: {}", materialCode);
CheckUtils.throwIf(StrUtil.isBlank(materialProcess), "物料流程编码不能为空"); MaterialInfoDO materialInfoDO = materialInfoMapper.selectOne(new QueryWrapper<MaterialInfoDO>().eq("encoding", materialCode));
CheckUtils.throwIf(ObjectUtil.isEmpty(materialInfoDO), "物料数据异常");
CheckUtils.throwIf(StrUtil.isBlank(materialInfoDO.getMaterialProcess()), "物料流程编码不能为空");
// 1. 检查连接 // 1. 检查连接
Channel channel = channelManager.getFirstChannel(); Channel channel = channelManager.getFirstChannel();
@@ -38,16 +50,42 @@ public class VmCommandController {
} }
// 2. 清空之前的响应队列,避免影响当前请求 // 2. 清空之前的响应队列,避免影响当前请求
requestMatcher.clear(); requestMatcher.clear();
channel.writeAndFlush(materialProcess); channel.writeAndFlush(materialInfoDO.getMaterialProcess());
// 3. 等待响应 // 3. 等待响应
String response = requestMatcher.waitForResponse(20); String response = requestMatcher.waitForResponse(20);
log.info("sendAndWait-收到响应: {}", response); log.info("sendAndWait-收到响应: {}", response);
CheckUtils.throwIf("TIMEOUT".equals(response), "响应超时,请重试"); CheckUtils.throwIf("TIMEOUT".equals(response), "响应超时,请重试");
CheckUtils.throwIf("ERROR".equals(response), "设备连接异常!"); CheckUtils.throwIf("ERROR".equals(response), "设备连接异常!");
//如果是圆管,数据格式为yg_yellow_139.092或者yg_yellow
if (response.contains("yg_")) {
CheckUtils.throwIf(null == materialInfoDO.getMaterialSpec(), "物料直径不能为空");
String[] split = response.split("_");
if (split.length != 3) {
throw new BusinessException("位置摆放不标准,未检查出直径");
}
if (!StrUtil.equals(split[1], materialInfoDO.getColor())) {
throw new BusinessException("颜色不匹配");
}
try {
double measuredDiameter = Double.parseDouble(split[2]);
double difference = Math.abs(measuredDiameter - materialInfoDO.getMaterialSpec());
if (difference > 5) {
throw new BusinessException("直径不匹配");
}
} catch (NumberFormatException e) {
throw new BusinessException("相机识别异常");
}
} else if (StrUtil.equals(response, materialCode)) {
response = "success";
} else {
response = "failed";
}
return response; return response;
} }
@PostMapping("/start") @PostMapping("/start")
public boolean startTask() { public boolean startTask() {
return saveBmpTaskService.startTask(); return saveBmpTaskService.startTask();
@@ -57,4 +95,4 @@ public class VmCommandController {
public boolean stopTask() { public boolean stopTask() {
return saveBmpTaskService.stopTask(); return saveBmpTaskService.stopTask();
} }
} }