|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using S7.Net;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace HKControl
- {
- internal class Siemens : CommunicationBase
- {
- Plc myPlc;
- public bool IsConnected => myPlc is null ? false : myPlc.IsConnected;
-
- private CpuType myCpuType;
- private string MyIp;
- private int Myport;
-
- /// <summary>
- /// 打开连接
- /// </summary>
- /// <param name="cpuType">PLC CPU 类型</param>
- /// <param name="ip">plc ip 地址</param>
- /// <param name="port">plc 端口号</param>
- /// <param name="rack">PLC 机架号</param>
- /// <param name="solt"> PLC 插槽号</param>
- public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0)
- {
- try
- {
- myCpuType = cpuType;
- MyIp = ip;
- Myport = port;
- myPlc = new Plc(cpuType, ip, port, rack, solt);
- while (!myPlc.IsConnected)
- {
- myPlc.Open();
- if (!myPlc.IsConnected) Thread.Sleep(3000);
- }
-
- if (myPlc.IsConnected)
- {
- ConnectOk?.Invoke();
- HKLog.HKLogImport.WriteInfo($"设备{ip}连接成功");
- //Debug.WriteLine($"设备{ip}连接成功");
- }
- }
- catch (Exception ex)
- {
- HKLog.HKLogImport.WriteInfo($"设备连接出错:{ip}");
- Thread.Sleep(3000);
- Connect(cpuType,ip,port);
- //Debug.WriteLine(ex.ToString());
- }
-
- }
-
- private void ExceptionDis()
- {
- myPlc?.Close();
- myPlc = null;
- Connect(myCpuType, MyIp, Myport);
- }
-
-
- /// <summary>
- /// 断开和PLC的连接
- /// </summary>
- public void Disconnect()
- {
- myPlc?.Close();
- }
-
- public TResult Read<TResult>(string address)
- {
- try
- {
- if (!IsConnected) return default;
- return (TResult)myPlc?.Read(address);
- }
- catch (Exception ex)
- {
- ExceptionDis();
- return default;
- }
-
- }
-
- public bool[] ReadBools(int address, int count)
- {
- if (!IsConnected) return default;
- var res = Read(DataType.Memory, 0, address, VarType.Bit, count);
- if (res != null && res is bool[] bools) return bools;
- return default;
- }
-
- public ushort[] ReadMW(int address, int count)
- {
- if (!IsConnected) return default;
- var res = Read(DataType.Memory, 0, address, VarType.Word, count);
- if (res != null && res is ushort[] ReturnValue) return ReturnValue;
- return default;
- }
-
- public float[] ReadMD(int address, int count)
- {
- if (!IsConnected) return default;
- var res = Read(DataType.Memory, 0, address, VarType.Real, count);
- if (res != null && res is float[] ReturnValue) return ReturnValue;
- return default;
- }
-
- private object Read(DataType dataType, int db, int address, VarType varType, int count)
- {
- try
- {
- if (!IsConnected) return default;
- return myPlc?.Read(dataType, db, address, varType, count);
- }
- catch (Exception ex)
- {
- ExceptionDis();
- return default;
- }
-
- }
-
- public void Write<TValue>(string address, TValue value)
- {
- try
- {
- myPlc?.Write(address, value);
- }
- catch (Exception ex)
- {
- ExceptionDis();
- }
-
- }
-
- public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
- {
- if (!IsConnected) return default;
- return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
- }
-
- public void WriteStruct(object structValue, int db, int startAddress = 0)
- {
- myPlc?.WriteStruct(structValue, db, startAddress);
- }
-
- public int ReadClass(object sourceClass, int db, int startAddress = 0)
- {
- if (!IsConnected) return -1;
- return myPlc.ReadClass(sourceClass, db, startAddress);
- }
-
- public TResult ReadClass<TResult>(int db, int startAddress = 0) where TResult : class
- {
- if (!IsConnected) return default;
- return myPlc.ReadClass<TResult>(db, startAddress);
- }
-
- public void WriteClass(object sourceClass, int db, int startAddress = 0)
- {
- myPlc?.WriteClass(sourceClass, db, startAddress);
- }
- }
- }
|