| | |
| | | import com.zy.acs.conveyor.core.constant.StationStatusField; |
| | | import com.zy.acs.conveyor.core.model.StaProtocol; |
| | | import com.zy.acs.conveyor.core.properties.DevpSlave; |
| | | import com.zy.acs.conveyor.core.service.DevpS7Service; |
| | | import com.zy.acs.conveyor.core.service.StationService; |
| | | import com.zy.acs.conveyor.entity.Devp; |
| | | import com.zy.acs.conveyor.service.DevpService; |
| | | import com.zy.acs.conveyor.utils.SpringContextUtil; |
| | |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | private SiemensS7Net siemensS7Net; |
| | | |
| | | private Map<Integer, StaProtocol> station; |
| | | |
| | | private volatile boolean connected = false; |
| | | |
| | | private static final int WRITE_RETRY_MAX = 5; |
| | | |
| | |
| | | * 读取状态 ====> 整块plc |
| | | */ |
| | | private void read() throws InterruptedException { |
| | | if (!connected || siemensS7Net == null) { |
| | | if (siemensS7Net == null) { |
| | | DevpS7Service devpS7Service = SpringContextUtil.getBean(DevpS7Service.class); |
| | | if (devpS7Service != null) { |
| | | siemensS7Net = devpS7Service.get(slave.getId()); |
| | | } |
| | | log.warn("PLC未连接,跳过读取 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | if (station == null) { |
| | | StationService stationService = SpringContextUtil.getBean(StationService.class); |
| | | if (stationService != null) { |
| | | station = stationService.getStationMap(slave.getId()); |
| | | } |
| | | log.warn("站点未连接,跳过读取 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | if (!result.IsSuccess) { |
| | | log.error("读取站点状态失败 [id:{}] [error:{}]", slave.getId(), result.Message); |
| | | connected = false; |
| | | return; |
| | | } |
| | | |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < staNoSize; i++) { |
| | | StaProtocol staProtocol = station.get(staNos.get(staNoSize)); |
| | | StaProtocol staProtocol = station.get(staNos.get(i)); |
| | | parseStationStatus(content, i, staProtocol); |
| | | } |
| | | |
| | | // 读取条码 |
| | | readBarcodes(); |
| | | |
| | | // 称重 |
| | | readWeight(); |
| | | |
| | | // 读取外形检测错误 |
| | | readDimensionErrors(); |
| | |
| | | */ |
| | | private void parseStationStatus(byte[] content, int index, StaProtocol staProtocol) { |
| | | int offset = index * StationStatusField.ALL.getByteLength(); |
| | | staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(content, offset)); |
| | | staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt32(content, offset + StationStatusField.TASK_NUMBER.getOffset())); |
| | | staProtocol.setStaNo((int) siemensS7Net.getByteTransform().TransInt16( |
| | | content, offset + StationStatusField.FINAL_TARGET.getOffset())); |
| | | |
| | |
| | | * 读取条码信息 |
| | | */ |
| | | private void readBarcodes() { |
| | | List<Integer> barcodeArr = slave.getBarcodeArr(); |
| | | if (barcodeArr == null || barcodeArr.isEmpty()) { |
| | | if (slave.getBarcode() == null || !slave.getBarcode()) { |
| | | return; |
| | | } |
| | | |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | DeviceField.BARCODE.buildAddress(), |
| | | (short) (barcodeArr.size() * DeviceField.BARCODE.getByteLength())); |
| | | (short) ( DeviceField.BARCODE.getByteLength() * DeviceField.BARCODE.getArrLength())); |
| | | |
| | | if (!result.IsSuccess) { |
| | | log.warn("读取条码失败 [id:{}]", slave.getId()); |
| | |
| | | } |
| | | |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < barcodeArr.size(); i++) { |
| | | for (int i = 0; i < DeviceField.BARCODE.getArrLength(); i++) { |
| | | int[] seg = DeviceField.BARCODE.getSeg(); |
| | | Short staNo = siemensS7Net.getByteTransform().TransInt16( |
| | | content, seg[0]+i * DeviceField.BARCODE.getByteLength()); |
| | | String barcode = siemensS7Net.getByteTransform().TransString( |
| | | content, i * DeviceField.BARCODE.getByteLength(), |
| | | DeviceField.BARCODE.getByteLength(), "UTF-8"); |
| | | content, seg[1]+i * DeviceField.BARCODE.getByteLength(), |
| | | DeviceField.BARCODE.getByteLength()-seg[1], "UTF-8"); |
| | | |
| | | if (!Cools.isEmpty(barcode)) { |
| | | StaProtocol staProtocol = station.get(barcodeArr.get(i)); |
| | | if (!Cools.isEmpty(barcode)&& staNo !=null) { |
| | | StaProtocol staProtocol = station.get(staNo); |
| | | if (staProtocol == null) { |
| | | log.warn("扫码站点不存在 [id:{}] [staNo:{}]", slave.getId(), staNo); |
| | | continue; |
| | | } |
| | | staProtocol.setBarcode(barcode); |
| | | log.info("料箱码:{}", barcode); |
| | | News.info("料箱码:{}", barcode); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取重量信息 |
| | | */ |
| | | private void readWeight() { |
| | | if ( slave.getWeight() == null || ! slave.getWeight()) { |
| | | return; |
| | | } |
| | | |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | DeviceField.WEIGHT.buildAddress(), |
| | | (short) (DeviceField.WEIGHT.getArrLength() * DeviceField.WEIGHT.getByteLength())); |
| | | |
| | | if (!result.IsSuccess) { |
| | | log.warn("读取重量失败 [id:{}]", slave.getId()); |
| | | return; |
| | | } |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < DeviceField.WEIGHT.getArrLength() ; i++) { |
| | | int[] seg = DeviceField.WEIGHT.getSeg(); |
| | | Short staNo = siemensS7Net.getByteTransform().TransInt16( |
| | | content, seg[0]+i * DeviceField.WEIGHT.getByteLength()); |
| | | Double weight = (double) siemensS7Net.getByteTransform().TransSingle( |
| | | content, seg[1] + i * DeviceField.WEIGHT.getByteLength()); |
| | | if (!Cools.isEmpty(weight)&& staNo !=null) { |
| | | StaProtocol staProtocol = station.get(staNo); |
| | | if (staProtocol == null) { |
| | | log.warn("称重站点不存在 [id:{}] [staNo:{}]", slave.getId(), staNo); |
| | | continue; |
| | | } |
| | | staProtocol.setWeight(BigDecimal.valueOf(weight).setScale(4, RoundingMode.HALF_UP).doubleValue()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取外形检测错误 |
| | | */ |
| | | private void readDimensionErrors() { |
| | | List<Integer> staNosError = slave.getStaNosError(); |
| | | if (staNosError == null || staNosError.isEmpty()) { |
| | | if ( slave.getStaNosError() == null || !slave.getStaNosError()) { |
| | | return; |
| | | } |
| | | |
| | | OperateResultExOne<byte[]> result = siemensS7Net.Read( |
| | | DeviceField.DIMENSION_WORD.buildAddress(), |
| | | (short) (staNosError.size() * DeviceField.DIMENSION_WORD.getByteLength())); |
| | | (short) (DeviceField.DIMENSION_WORD.getArrLength() * DeviceField.DIMENSION_WORD.getByteLength())); |
| | | |
| | | if (!result.IsSuccess) { |
| | | log.warn("读取外形检测错误失败 [id:{}]", slave.getId()); |
| | |
| | | } |
| | | |
| | | byte[] content = result.Content; |
| | | for (int i = 0; i < staNosError.size(); i++) { |
| | | Integer siteId = staNosError.get(i); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | for (int i = 0; i < DeviceField.DIMENSION_WORD.getArrLength(); i++) { |
| | | int[] seg = DeviceField.DIMENSION_WORD.getSeg(); |
| | | Short staNo = siemensS7Net.getByteTransform().TransInt16( |
| | | content, seg[0]); |
| | | if ( staNo !=null) { |
| | | StaProtocol staProtocol = station.get(staNo); |
| | | if (staProtocol == null){ |
| | | log.warn("异常站点不存在 [id:{}] [staNo:{}]", slave.getId(), staNo); |
| | | continue; |
| | | } |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool( |
| | | content, seg[1]+i * DeviceField.DIMENSION_WORD.getByteLength(), |
| | | 1); |
| | | //未使用,预留 |
| | | // boolean[] status1 = siemensS7Net.getByteTransform().TransBool( |
| | | // content, seg[1]+i * DeviceField.DIMENSION_WORD.getByteLength()+1, |
| | | // 1); |
| | | |
| | | boolean[] status = siemensS7Net.getByteTransform().TransBool( |
| | | content, i * DeviceField.DIMENSION_WORD.getByteLength(), |
| | | DeviceField.DIMENSION_WORD.getByteLength()); |
| | | staProtocol.setLeftErr(status[0]); |
| | | staProtocol.setRightErr(status[1]); |
| | | staProtocol.setFrontErr(status[2]); |
| | | staProtocol.setBackErr(status[3]); |
| | | staProtocol.setHighErr(status[4]); |
| | | staProtocol.setLoadErr(status[5]); |
| | | staProtocol.setWeightErr(status[6]); |
| | | staProtocol.setBarcodeErr(status[7]); |
| | | } |
| | | |
| | | staProtocol.setFrontErr(status[0]); |
| | | staProtocol.setBackErr(status[1]); |
| | | staProtocol.setHighErr(status[2]); |
| | | staProtocol.setLeftErr(status[3]); |
| | | staProtocol.setRightErr(status[4]); |
| | | staProtocol.setWeightErr(status[5]); |
| | | staProtocol.setBarcodeErr(status[6]); |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | Integer siteId = staNos.get(i); |
| | | StaProtocol staProtocol = station.get(siteId); |
| | | if (staProtocol == null) { |
| | | log.warn("站点不存在 [id:{}] [staNo:{}]", slave.getId(), siteId); |
| | | continue; |
| | | } |
| | | |