| | |
| | | package com.zy.core.thread; |
| | | |
| | | import com.zy.common.HslCommunication.Core.Types.OperateResult; |
| | | import com.zy.common.HslCommunication.Core.Types.OperateResultExOne; |
| | | import com.zy.common.HslCommunication.Profinet.Siemens.SiemensPLCS; |
| | | import com.zy.common.HslCommunication.Profinet.Siemens.SiemensS7Net; |
| | | import com.zy.core.Slave; |
| | | import com.zy.core.ThreadHandler; |
| | | import com.zy.core.cache.MessageQueue; |
| | | import com.zy.core.enums.SlaveType; |
| | | import com.zy.core.model.Task; |
| | | import com.zy.core.model.protocol.StaProtocol; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * 输送线线程 |
| | | * Created by vincent on 2020/8/4 |
| | | */ |
| | | @Data |
| | | @Slf4j |
| | | public class DevpThread implements Runnable, ThreadHandler { |
| | | |
| | | private Slave slave; |
| | | private SiemensS7Net siemensS7Net; |
| | | private Map<Integer, StaProtocol> station = new ConcurrentHashMap<>(); |
| | | |
| | | public DevpThread(Slave slave) { |
| | | this.slave = slave; |
| | | connect(); |
| | | } |
| | | |
| | | @Override |
| | | @SuppressWarnings("InfiniteLoopStatement") |
| | | public void run() { |
| | | while (true) { |
| | | try { |
| | | int step = 1; |
| | | Task task = MessageQueue.poll(SlaveType.Devp, slave.getId()); |
| | | if (task != null) { |
| | | step = task.getStep(); |
| | | } |
| | | switch (step) { |
| | | // 读数据 |
| | | case 1: |
| | | readWorkNo(); // 读取工作号 |
| | | readStaDest(); // 读取目标站 |
| | | readStatus(); // 读取状态 |
| | | readError(); // 读取异常信息 |
| | | break; |
| | | case 2: |
| | | break; |
| | | case 3: |
| | | break; |
| | | case 4: |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | Thread.sleep(3000); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public boolean connect() { |
| | | boolean result = false; |
| | | siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp()); |
| | | OperateResult connect = siemensS7Net.ConnectServer(); |
| | | if(connect.IsSuccess){ |
| | | result = true; |
| | | log.info("输送线plc连接成功 ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } else { |
| | | log.info("输送线plc连接失败!!! ===>> [id:{}] [ip:{}] [port:{}]", slave.getId(), slave.getIp(), slave.getPort()); |
| | | } |
| | | siemensS7Net.ConnectClose(); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 读取工作号 |
| | | */ |
| | | private void readWorkNo(){ |
| | | OperateResultExOne<int[]> read = siemensS7Net.ReadInt32("DB100", (short) 8); |
| | | if (read.IsSuccess) { |
| | | int staNo1 = read.Content[0]; |
| | | int staNo2 = read.Content[1]; |
| | | int staNo3 = read.Content[2]; |
| | | int staNo4 = read.Content[3]; |
| | | int staNo5 = read.Content[4]; |
| | | int staNo6 = read.Content[5]; |
| | | int staNo7 = read.Content[6]; |
| | | int staNo8 = read.Content[7]; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取目标站 |
| | | */ |
| | | private void readStaDest(){ |
| | | OperateResultExOne<byte[]> read = siemensS7Net.Read("DB101", (short) 8); |
| | | if (read.IsSuccess) { |
| | | int staNo1 = siemensS7Net.getByteTransform().TransInt32(read.Content, 0); |
| | | int staNo2 = siemensS7Net.getByteTransform().TransInt32(read.Content, 4); |
| | | int staNo3 = siemensS7Net.getByteTransform().TransInt32(read.Content, 8); |
| | | int staNo4 = siemensS7Net.getByteTransform().TransInt32(read.Content, 12); |
| | | int staNo5 = siemensS7Net.getByteTransform().TransInt32(read.Content, 16); |
| | | int staNo6 = siemensS7Net.getByteTransform().TransInt32(read.Content, 20); |
| | | int staNo7 = siemensS7Net.getByteTransform().TransInt32(read.Content, 24); |
| | | int staNo8 = siemensS7Net.getByteTransform().TransInt32(read.Content, 28); |
| | | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取状态 |
| | | */ |
| | | private void readStatus(){ |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 读取异常信息 |
| | | */ |
| | | private void readError(){ |
| | | |
| | | } |
| | | |
| | | private void writeWorkNo(){ |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | @Override |