|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using S7.Net;
- using System.Net;
- using System.Text;
- using BPASmartClient.S7Net;
-
- 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;
- var value = myPlc?.Read(address);
- if (typeof(TResult).Name == "Single")
- {
- var obj = ((uint)value).ConvertToFloat();
- return (TResult)Convert.ChangeType(obj, typeof(TResult));
- }
- else
- {
- return (TResult)Convert.ChangeType(value, typeof(TResult));
- }
- }
-
- 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 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 WriteString(DataType dataType, int db, int startByteAdr, string value)
- {
- if (!IsConnected) return;
- var res = value.ToArrays();
- if (res != null)
- {
- List<byte> bytes = new List<byte>();
- bytes.Add((byte)res.Length);
- bytes.Add((byte)res.Length);
- bytes.AddRange(res);
- myPlc?.WriteBytes(dataType, db, startByteAdr, bytes.ToArray());
- }
- }
-
- public string Write<TValue>(string address, TValue value, int Retries = 1)
- {
- if (IsConnected)
- {
- int count = 0;
- if (Retries == 1 || Retries == 0)
- {
- myPlc?.Write(address, value);
- return $"成功,地址:{address},值:{value}";
- }
- else
- {
- while (count < Retries)
- {
- count++;
- myPlc?.Write(address, value);
- var res = myPlc?.Read(address);
- if (res != null && res.ToString() == value.ToString())
- {
- break;
- }
- }
- return $"成功,发送了{count}次,地址:{address},值:{value}";
- }
- }
- else
- {
- return $"失败,地址:{address},值:{value},断开连接";
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="db">DB号</param>
- /// <param name="txt">字符串</param>
- /// <param name="startAddress">字节偏移地址</param>
- public void WriteString(int db, string txt, int startAddress = 0)
- {
- var temp = Encoding.ASCII.GetBytes(txt);
- var bytes = S7.Net.Types.S7String.ToByteArray(txt, temp.Length);
- myPlc.WriteBytes(DataType.DataBlock, db, startAddress, bytes);
-
- }
- public void WriteInt16(int db, short txt, int startAddress = 0)
- {
- var bytes =BitConverter.GetBytes(txt);
- myPlc.WriteBytes(DataType.DataBlock, db, startAddress, bytes);
-
- }
- 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;
- if (IsConnected)
- {
- 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()
- {
- if (IsConnected)
- {
- 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);
- //}
-
- }
- }
|