12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using HslCommunication.Profinet.Siemens;
- using IMCS.CCS.Entitys;
- using IMCS_CCS.Utils;
- using S7.Net;
- using System;
- namespace IMCS.CCS.DeviceProtocol
- {
- public class DevicePlcS7
- {
-
- public static Plc S7(string ip,string type)
- {
- Plc s7Plc = new Plc(CpuType.S71500, ip, 0, 1);
- if (type == "S7_1200")
- {
- s7Plc = new Plc(CpuType.S71200, ip, 0, 1);
- }
- else if (type == "S7_400")
- {
- s7Plc = new Plc(CpuType.S7400, ip, 0, 1);
- }
- else if (type == "S7_300")
- {
- s7Plc = new Plc(CpuType.S7300, ip, 0, 1);
- }
- s7Plc.Open();
- return s7Plc;
- }
- public static SiemensS7Net SiemensS7(string ip, int port = 102)
- {
- SiemensS7Net SiemensS7Net = new SiemensS7Net(SiemensPLCS.S1200, ip) { ConnectTimeOut = 5000 };
- SiemensS7Net.ConnectionId = Guid.NewGuid().ToString();
- return SiemensS7Net;
- }
- public static string ReadValueAsString(SiemensS7Net s7, CcsActionAddress tagValueData)
- {
- if (s7 == null || tagValueData == null)
- return string.Empty;
- string result = string.Empty;
- string type = tagValueData.Type;
- if (type.Equals(TagValueReadTypeEnum.BOOL.ToString(), StringComparison.OrdinalIgnoreCase))
- {
- result = s7.ReadBool(tagValueData.Address).Content.ToString();
- }
- else if (type.Equals(TagValueReadTypeEnum.SHORT.ToString(), StringComparison.OrdinalIgnoreCase))
- {
- result = s7.ReadInt16(tagValueData.Address).Content.ToString();
- }
- else if (type.Equals(TagValueReadTypeEnum.String.ToString(), StringComparison.OrdinalIgnoreCase))
- {
- result = s7.ReadString(tagValueData.Address).Content?.ToString() ?? string.Empty;
- }
- else if (type.Equals(TagValueReadTypeEnum.Array.ToString(), StringComparison.OrdinalIgnoreCase))
- {
- var bytes = s7.Read(tagValueData.Address, 2048).Content;
- result = ToolUtils.ReturnStringByBytes(bytes);
- }
- return result;
- }
- }
- }
|