|
- using S7.Net;
-
- namespace BPASmartClient.S7Net
- {
- public class SiemensHelper
- {
- Plc myPlc;
- public bool IsConnected => myPlc is null ? false : myPlc.IsConnected;
-
- /// <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)
- {
- myPlc = new Plc(cpuType, ip, port, rack, solt);
- myPlc.Open();
- }
-
- /// <summary>
- /// 断开和PLC的连接
- /// </summary>
- public void Disconnect()
- {
- myPlc?.Close();
- }
-
- public TResult Read<TResult>(string address)
- {
- if (!IsConnected) return default;
- return (TResult)myPlc?.Read(address);
- }
-
- 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;
- }
-
- private object Read(DataType dataType, int db, int address, VarType varType, int count)
- {
- if (!IsConnected) return default;
- return myPlc?.Read(dataType, db, address, varType, count);
- }
-
- public void Write<TValue>(string address, TValue value)
- {
- if (IsConnected) myPlc?.Write(address, value);
- }
-
- public TResult ReadClass<TResult>(int db, int startByteAdr = 0) where TResult : class, new()
- {
- TResult sourceClass = new TResult();
- int num = (int)EntityClassResolution.GetClassSize(sourceClass);
- if (num <= 0) return sourceClass;
- byte[] array = myPlc.ReadBytes(DataType.DataBlock, db, startByteAdr, num);
- EntityClassResolution.FromBytes(sourceClass, array);
- return sourceClass;
- }
-
- public void WriteClass<TWriteModel>(TWriteModel sourceClass, int db, int startAddress = 0) where TWriteModel : class, new()
- {
- byte[] array = new byte[(int)EntityClassResolution.GetClassSize(sourceClass)];
- EntityClassResolution.ToBytes(sourceClass, array);
- myPlc.WriteBytes(DataType.DataBlock, db, startAddress, array);
- }
-
-
- //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;
- //}
-
-
-
- //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 void WriteClass(object sourceClass, int db, int startAddress = 0)
- //{
- // myPlc?.WriteClass(sourceClass, db, startAddress);
- //}
-
- }
- }
|