#
luxiaotao1123
2020-08-13 0af65ec13ae4f3c880253586feb99b238df84d66
src/main/java/com/zy/core/thread/DevpThread.java
@@ -1,8 +1,10 @@
package com.zy.core.thread;
import com.zy.common.HslCommunication.Core.Types.OperateResult;
import com.zy.common.HslCommunication.Profinet.Siemens.SiemensPLCS;
import com.zy.common.HslCommunication.Profinet.Siemens.SiemensS7Net;
import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.alibaba.fastjson.JSON;
import com.zy.core.Slave;
import com.zy.core.ThreadHandler;
import com.zy.core.cache.MessageQueue;
@@ -12,6 +14,7 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -45,12 +48,11 @@
                switch (step) {
                    // 读数据
                    case 1:
                        readWorkNo();   // 读取工作号
                        readStaDest();  // 读取目标站
                        readStatus();   // 读取状态
                        readError();    // 读取异常信息
                        read();
                        break;
                    // 写数据
                    case 2:
                        write((StaProtocol)task.getData());
                        break;
                    default:
                        break;
@@ -67,6 +69,8 @@
    public boolean connect() {
        boolean result = false;
        siemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, slave.getIp());
        siemensS7Net.setRack((byte) 0);
        siemensS7Net.setSlot((byte) 1);
        OperateResult connect = siemensS7Net.ConnectServer();
        if(connect.IsSuccess){
            result = true;
@@ -79,36 +83,98 @@
    }
    /**
     * 读取工作号
     */
    private void readWorkNo(){
    }
    /**
     * 读取目标站
     */
    private void readStaDest(){
    }
    /**
     * 读取状态
     */
    private void readStatus(){
    private void read(){
        OperateResultExOne<byte[]> result = siemensS7Net.Read("DB100.0", (short) 40);
        for (int i = 1; i <= 8; i++) {
            StaProtocol staProtocol = station.get(i);
            if (null == staProtocol) {
                staProtocol = new StaProtocol();
                staProtocol.setSiteId(i);
                station.put(i, staProtocol);
            }
            System.out.println("------");
            System.out.println("站点编号:" + i);
            System.out.println("工作号:" + siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2));
            System.out.println("目标站:" + siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2+20));
            staProtocol.setWorkNo(siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2));
            staProtocol.setStaNo(siemensS7Net.getByteTransform().TransInt16(result.Content, (i-1)*2+20));
        }
        System.out.println("===> 状态");
        OperateResultExOne<byte[]> result1 = siemensS7Net.Read("DB100.40", (short) 10);
        if (result1.IsSuccess) {
//            System.out.println("状态:"+Arrays.toString(result1.Content));
            System.out.println(Arrays.toString(siemensS7Net.getByteTransform().TransBool(result1.Content, 0, 1)));
        }
    }
    /**
     * 读取异常信息
     * 写入
     */
    private void readError(){
    private void write(StaProtocol staProtocol){
        if (null == staProtocol) {
            return;
        }
        OperateResult write = siemensS7Net.Write("DB100." + (staProtocol.getSiteId() - 1) * 2, staProtocol.getWorkNo());    // 工作号
        System.out.println(((staProtocol.getSiteId() - 1) * 2) + 20);
        OperateResult write1 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) * 2 + 20), staProtocol.getStaNo());    // 目标站
        boolean[] status = new boolean[8];
        status[0] = staProtocol.isAutoing();
        status[1] = staProtocol.isLoading();
        status[2] = staProtocol.isInEnable();
        status[3] = staProtocol.isOutEnable();
        status[4] = staProtocol.isEmptyMk();
        OperateResult write2 = siemensS7Net.Write("DB100." + ((staProtocol.getSiteId() - 1) + 40), status);     //  状态
        if (!write.IsSuccess || !write1.IsSuccess || !write2.IsSuccess) {
            log.error("写入输送线站点数据失败。输送线plc编号={},站点数据={}", slave.getId(), JSON.toJSON(staProtocol));
        }
    }
    @Override
    public void close() {
    }
    public static void main(String[] args) {
        Slave slave = new Slave();
        slave.setIp("192.168.2.125");
        DevpThread devpThread = new DevpThread(slave);
        boolean connect = devpThread.connect();
        System.out.println(connect);
        devpThread.read();
        // 写
        StaProtocol staProtocol = devpThread.getStation().get(1);
        staProtocol.setWorkNo((short) 978);
        staProtocol.setStaNo((short) 150);
        staProtocol.setAutoing(true);
        staProtocol.setEmptyMk(true);
        devpThread.write(staProtocol);
        System.out.println("----------------------------------------");
        // 读
        devpThread.read();
        System.out.println(JSON.toJSONString(devpThread.station));
        System.out.println("[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]");
        boolean[] status = new boolean[8];
        status[0] = false;
        status[1] = false;
        status[2] = false;
        status[3] = false;
        status[4] = false;
        OperateResult write2 = devpThread.siemensS7Net.Write("DB100.40", status);     //  状态
        if (write2.IsSuccess) {
            System.out.println("success");
        }
        OperateResultExOne<byte[]> result1 = devpThread.siemensS7Net.Read("DB100.40", (short) 10);
        if (result1.IsSuccess) {
//            System.out.println("状态:"+Arrays.toString(result1.Content));
            System.out.println(Arrays.toString(result1.Content));
            System.out.println(Arrays.toString(devpThread.siemensS7Net.getByteTransform().TransBool(result1.Content, 0, 1)));
            System.out.println(Arrays.toString(devpThread.siemensS7Net.getByteTransform().TransBool(result1.Content, 1, 1)));
            System.out.println(Arrays.toString(devpThread.siemensS7Net.getByteTransform().TransBool(result1.Content, 2, 1)));
        }
    }
}