|
@@ -1,147 +0,0 @@
|
|
|
-package com.warewms.hailiang.util;
|
|
|
-
|
|
|
-import HslCommunication.Core.Types.OperateResult;
|
|
|
-import HslCommunication.Core.Types.OperateResultExOne;
|
|
|
-import HslCommunication.Profinet.Siemens.SiemensPLCS;
|
|
|
-import HslCommunication.Profinet.Siemens.SiemensS7Net;
|
|
|
-import com.warewms.common.exception.ServiceException;
|
|
|
-import com.warewms.hailiang.config.PlcConfig;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-public class HslTools{
|
|
|
-
|
|
|
- private SiemensS7Net siemensS7Net;
|
|
|
- private Boolean IsSuccess;
|
|
|
-
|
|
|
- public Boolean getSuccess() {
|
|
|
- return IsSuccess;
|
|
|
- }
|
|
|
-
|
|
|
- public HslTools(PlcConfig plcConfig) {
|
|
|
- log.info("正在创建连接:{},ip:{}",plcConfig.getSiemensPLCS(),plcConfig.getIp());
|
|
|
- siemensS7Net = new SiemensS7Net(plcConfig.getSiemensPLCS(), plcConfig.getIp());
|
|
|
- siemensS7Net.setSlot(plcConfig.getSlot());
|
|
|
- siemensS7Net.setRack(plcConfig.getRack());
|
|
|
- OperateResult operateResult = siemensS7Net.ConnectServer();
|
|
|
- this.IsSuccess = operateResult.IsSuccess;
|
|
|
- if (!this.IsSuccess){
|
|
|
- log.info("连接结果:{}--{}--{}",operateResult.IsSuccess,operateResult.Message,operateResult.ErrorCode);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public OperateResult closeConn() {
|
|
|
- OperateResult result = OperateResult.CreateSuccessResult();
|
|
|
- try {
|
|
|
- result = siemensS7Net.ConnectClose();
|
|
|
- } catch (Exception e) {
|
|
|
-
|
|
|
- }
|
|
|
- siemensS7Net = null;
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean writeInt(String address, Integer value) {
|
|
|
- OperateResult result = siemensS7Net.Write(address, value);
|
|
|
- log.info(result.Message);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- closeConn();
|
|
|
- throw new ServiceException(String.format("写入数据失败,db:%s,数据:%d,错误信息:%s", address, value, result.Message));
|
|
|
- }
|
|
|
- return result.IsSuccess;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean writeShort(String address, short value) {
|
|
|
- OperateResult result = siemensS7Net.Write(address, value);
|
|
|
- log.info(result.Message);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
|
|
|
- } else {
|
|
|
- log.info(String.format("写入数据完成,db:%s,数据:%d", address, value));
|
|
|
- }
|
|
|
- return result.IsSuccess;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean writeBoolean(String address, Boolean value) {
|
|
|
- OperateResult result = siemensS7Net.Write(address, value);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, address, result.Message));
|
|
|
- } else {
|
|
|
- log.info(String.format("写入数据完成,db:%s,数据:%b", address, value));
|
|
|
- }
|
|
|
- return result.IsSuccess;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean writeString(String address, String value) {
|
|
|
- OperateResult result = siemensS7Net.Write(address, value.getBytes(StandardCharsets.US_ASCII));
|
|
|
- if (!result.IsSuccess) {
|
|
|
- throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
|
|
|
- } else {
|
|
|
- log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
|
|
|
- }
|
|
|
- return result.IsSuccess;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean writeStringStr(String address, String value) {
|
|
|
- OperateResult result = siemensS7Net.Write(address, value);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- throw new ServiceException(String.format("写入数据失败,db:%s,错误信息:%s", address, result.Message));
|
|
|
- } else {
|
|
|
- log.info(String.format("写入数据完成,db:%s,数据:%s", address, value));
|
|
|
- }
|
|
|
- return result.IsSuccess;
|
|
|
- }
|
|
|
-
|
|
|
- public String getString(String address) {
|
|
|
- OperateResultExOne<String> result = siemensS7Net.ReadString(address);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- log.error(String.format(String.format("String类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
|
|
|
- return "";
|
|
|
- } else {
|
|
|
- return result.Content;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public boolean getBool(String address) {
|
|
|
- OperateResultExOne<Boolean> result = siemensS7Net.ReadBool(address);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- log.error(String.format(String.format("Bool类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
|
|
|
- return false;
|
|
|
- } else {
|
|
|
- return result.Content;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public int getInt(String address) {
|
|
|
- OperateResultExOne<Integer> result = siemensS7Net.ReadInt32(address);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- log.error(String.format(String.format("int类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
|
|
|
- return 0;
|
|
|
- } else {
|
|
|
- return result.Content;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public Float getFlot(String address) {
|
|
|
- OperateResultExOne<Float> result = siemensS7Net.ReadFloat(address);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- log.error(String.format(String.format("flot类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
|
|
|
- return 0F;
|
|
|
- } else {
|
|
|
- return result.Content;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public Double getDabble(String address) {
|
|
|
- OperateResultExOne<Double> result = siemensS7Net.ReadDouble(address);
|
|
|
- if (!result.IsSuccess) {
|
|
|
- log.error(String.format(String.format("dabble类型读取数据失败,db:%s,错误信息:%s", address, result.Message)));
|
|
|
- return 0D;
|
|
|
- } else {
|
|
|
- return result.Content;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|