PlcConnectUtil.java 7.12 KB
package com.huaheng.pc.plc.util;


import HslCommunication.Core.Types.OperateResult;
import HslCommunication.Core.Types.OperateResultExOne;
import HslCommunication.Profinet.Siemens.SiemensPLCS;
import com.huaheng.pc.plc.domain.PlcEquipmentOffset;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.poi.ss.formula.functions.T;

import java.util.Map;

/**
 * 西门子plc连接工具类
 */
public class PlcConnectUtil {

    public static SiemensS7NetV siemensS7NetV_1=null;
    public static SiemensS7NetV siemensS7NetV_2=null;
    public static SiemensS7NetV siemensS7NetV_3=null;
    public static SiemensS7NetV siemensS7NetV_4=null;

    public static SiemensS7NetV start(String ipAddress,int order,int port){
        if(order==1){
            siemensS7NetV_1=new SiemensS7NetV(SiemensPLCS.S1500,ipAddress);
            siemensS7NetV_1.setPort(port);
            return siemensS7NetV_1;
        }else if(order==2){
            siemensS7NetV_2=new SiemensS7NetV(SiemensPLCS.S1500,ipAddress);
            siemensS7NetV_2.setPort(port);
            return siemensS7NetV_2;
        }else if(order==3){
            siemensS7NetV_3=new SiemensS7NetV(SiemensPLCS.S1500,ipAddress);
            siemensS7NetV_3.setPort(port);
            return siemensS7NetV_3;
        }else if(order==4){
            siemensS7NetV_4=new SiemensS7NetV(SiemensPLCS.S1500,ipAddress);
            siemensS7NetV_4.setPort(port);
            return siemensS7NetV_4;
        }
        return null;
    }

    public static OperateResult stop(SiemensS7NetV siemensS7Net){
        return siemensS7Net.ConnectClose();
    }

    public static Boolean checkConnect(int order){
        OperateResult result=null;
        try {
            if(order==1){
                result= siemensS7NetV_1.ConnectServer();
            }else if(order==2){
                result= siemensS7NetV_2.ConnectServer();
            }else if(order==3){
                result= siemensS7NetV_3.ConnectServer();
            }else if(order==4){
                result= siemensS7NetV_4.ConnectServer();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        boolean tag=result.IsSuccess;
        return tag;
    }

    /**
     * 获取地址
     * @param offset
     * @return
     */
    public static String getAddressByOffset(PlcEquipmentOffset offset) {
        String dataType = offset.getDataType();
        String address = null;
        switch (dataType) {
            case "int":
                //short a=7;         //int  16位
                address = offset.getMethodName() + "INT" + offset.getOffset();
                break;
            case "dint":
                //Integer aa=74; dint  32位
                address = offset.getMethodName() + "DINT" + offset.getOffset();
                break;
            case "dword":
                //32位  int aa=6666;
                address = offset.getMethodName() + "D" + offset.getOffset();
                break;
            case "word":
                //short aaa=21;
                address = offset.getMethodName() + "MW" + offset.getOffset();
                break;
            case "bool":
                //true  8位
                address = offset.getMethodName() + "X" + offset.getOffset();
                break;
            case "char":
                //byte b=8;  8位
                address = offset.getMethodName() + "CHAR" + offset.getOffset();
                break;
            case "string":
                //string1stringgstringggstringgggstringgggg
                address = offset.getMethodName() + "CHAR" + offset.getOffset() + ",20";
                break;
            default:
                address = offset.getMethodName() + "CHAR" + offset.getOffset() + ",20";
                break;
        }
        return address;
    }

    /**
     *  读plc数据
     * @param dataType
     * @param address
     * @param siemensS7Net
     * @return
     */
    public Map<String,Object> readData(String dataType, String  address,SiemensS7NetV siemensS7Net) {
        Map<String,Object> map=new HashedMap<>();
        map.put("dataType",dataType);
        OperateResultExOne<T> result=null;
        switch (dataType) {
            case "int":
                //short a=7;       //int  16位
                OperateResultExOne<Integer> result_data=siemensS7Net.ReadUInt16(address);
                map.put("Content",result_data.Content);
                map.put("IsSuccess",result_data.IsSuccess);
                map.put("Message",result_data.Message);
                break;
            case "dint":
                //Integer aa=74; dint  32位
                OperateResultExOne<Integer> result_dint=siemensS7Net.ReadInt32(address);
                map.put("Content",result_dint.Content);
                map.put("IsSuccess",result_dint.IsSuccess);
                map.put("Message",result_dint.Message);
                break;
            case "dword":
                //32位  int aa=6666;
                OperateResultExOne<Long> result_dword2=siemensS7Net.ReadUInt32(address);
                map.put("Content",result_dword2.Content);
                map.put("IsSuccess",result_dword2.IsSuccess);
                map.put("Message",result_dword2.Message);
                break;
            case "word":
                //short aaa=21;
                OperateResultExOne<Short> result_word=siemensS7Net.ReadInt16(address);
                map.put("Content",result_word.Content);
                map.put("IsSuccess",result_word.IsSuccess);
                map.put("Message",result_word.Message);
                break;
            case "bool":
                //true  8位
                OperateResultExOne<Boolean> result_bool=siemensS7Net.ReadBool(address);
                map.put("Content",result_bool.Content);
                map.put("IsSuccess",result_bool.IsSuccess);
                map.put("Message",result_bool.Message);
                break;
            case "char":
                //byte b=8;  8位
                OperateResultExOne<Byte> result_char=siemensS7Net.ReadByte(address);
                map.put("Content",result_char.Content);
                map.put("IsSuccess",result_char.IsSuccess);
                map.put("Message",result_char.Message);
                break;
            case "string":
                //string1stringgstringggstringgggstringgggg
                OperateResultExOne<String> result_string=siemensS7Net.ReadString(address);
                map.put("Content",result_string.Content);
                map.put("IsSuccess",result_string.IsSuccess);
                map.put("Message",result_string.Message);
                break;
            default:
                OperateResultExOne<String> result_default=siemensS7Net.ReadString(address);
                map.put("Content",result_default.Content);
                map.put("IsSuccess",result_default.IsSuccess);
                map.put("Message",result_default.Message);
                break;
        }
        return map;
    }


    public static void main(String[] args) {
        siemensS7NetV_1=new SiemensS7NetV(SiemensPLCS.S1500,"127.0.0.1");
        OperateResult result=siemensS7NetV_1.ConnectServer();
        System.out.println(result.IsSuccess);
    }


}