|
|
@@ -1,55 +1,81 @@
|
|
|
-using HslCommunication.Profinet.Siemens;
|
|
|
-using WCS.Core;
|
|
|
-using WCS.Core.BaseExtensions;
|
|
|
-using WCS.Core.DataTrans;
|
|
|
-using WCS.Core.Virtual_PLC;
|
|
|
+using WCS.Core.DataTrans;
|
|
|
|
|
|
namespace WCS.Service.PLCAccessors
|
|
|
{
|
|
|
- public class SiemensS7PLC : SiemensS7Net, IPlcAccessor
|
|
|
- {
|
|
|
- private SiemensS7Net plc;
|
|
|
+ //public class SiemensS7PLC : SiemensS7Net, IPlcAccessor
|
|
|
+ //{
|
|
|
+ // private SiemensS7Net plc;
|
|
|
|
|
|
- public SiemensS7PLC(SiemensPLCS type) : base(type)
|
|
|
- {
|
|
|
- }
|
|
|
+ // public SiemensS7PLC(SiemensPLCS type) : base(type)
|
|
|
+ // {
|
|
|
+ // }
|
|
|
|
|
|
- public SiemensS7PLC(SiemensPLCS type, string ip) : base(type, ip)
|
|
|
- {
|
|
|
- plc = new SiemensS7Net(type, ip);
|
|
|
- }
|
|
|
+ // public SiemensS7PLC(SiemensPLCS type, string ip) : base(type, ip)
|
|
|
+ // {
|
|
|
+ // plc = new SiemensS7Net(type, ip);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public SiemensS7PLC(int type, string ip) : base((SiemensPLCS)type, ip)
|
|
|
+ // {
|
|
|
+ // plc = new SiemensS7Net((SiemensPLCS)type, ip);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public byte[] ReadBytes(ushort db, ushort address, ushort length, ushort dataLength)
|
|
|
+ // {
|
|
|
+ // if (Configs.Any(SystemMode.虚拟plc))
|
|
|
+ // {
|
|
|
+ // return PlcData.Read(new PLCData { IP = plc.IpAddress, DB = db, Length = length, DataLength = dataLength });
|
|
|
+ // }
|
|
|
+
|
|
|
+ // var addr = "DB" + db + "." + address;
|
|
|
+ // var res = plc.Read(addr, length);
|
|
|
+ // if (res.IsSuccess)
|
|
|
+ // return res.Content;
|
|
|
+ // throw new WarnException("读取PLC数据失败:" + res.Message);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public void WriteBytes(ushort db, ushort address, byte[] data)
|
|
|
+ // {
|
|
|
+ // if (Configs.Any(SystemMode.虚拟plc))
|
|
|
+ // {
|
|
|
+ // PlcData.Write(new PLCData { IP = plc.IpAddress, DB = db }, address, data);
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // var res = plc.Write("D" + address, data);
|
|
|
+ // if (!res.IsSuccess)
|
|
|
+ // throw new WarnException("写入PLC数据失败:" + res.Message);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+
|
|
|
+ public class SiemensS7PLC : IPLCAccessor
|
|
|
+ {
|
|
|
+ private PLC.Siemens.O.SimenssPlc plc;
|
|
|
|
|
|
- public SiemensS7PLC(int type, string ip) : base((SiemensPLCS)type, ip)
|
|
|
+ public SiemensS7PLC(string ip, int port, int rack, int slot)
|
|
|
{
|
|
|
- plc = new SiemensS7Net((SiemensPLCS)type, ip);
|
|
|
+ plc = new PLC.Siemens.O.SimenssPlc(ip, rack, slot);
|
|
|
+ plc.Connect();
|
|
|
}
|
|
|
|
|
|
- public byte[] ReadBytes(ushort db, ushort address, ushort length, ushort dataLength)
|
|
|
+ public byte[] ReadBytes(ushort db, ushort address, ushort length)
|
|
|
{
|
|
|
- if (Configs.Any(SystemMode.虚拟plc))
|
|
|
- {
|
|
|
- return PlcData.Read(new PLCData { IP = plc.IpAddress, DB = db, Length = length, DataLength = dataLength });
|
|
|
- }
|
|
|
-
|
|
|
- var addr = "DB" + db + "." + address;
|
|
|
- var res = plc.Read(addr, length);
|
|
|
- if (res.IsSuccess)
|
|
|
- return res.Content;
|
|
|
- throw new WarnException("读取PLC数据失败:" + res.Message);
|
|
|
+ if (!plc.Connected)
|
|
|
+ plc.Connect();
|
|
|
+ var res = plc.ReadArea(PLC.Siemens.Protocol.Common.AreaType.DB, db, address, length, PLC.Siemens.Protocol.Common.DataType.Byte);
|
|
|
+ if (res == null)
|
|
|
+ throw new Exception("读取DB块数据失败");
|
|
|
+ return res.Data;
|
|
|
}
|
|
|
|
|
|
public void WriteBytes(ushort db, ushort address, byte[] data)
|
|
|
{
|
|
|
- if (Configs.Any(SystemMode.虚拟plc))
|
|
|
- {
|
|
|
- PlcData.Write(new PLCData { IP = plc.IpAddress, DB = db }, address, data);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- var res = plc.Write("D" + address, data);
|
|
|
- if (!res.IsSuccess)
|
|
|
- throw new WarnException("写入PLC数据失败:" + res.Message);
|
|
|
- }
|
|
|
+ if (!plc.Connected)
|
|
|
+ plc.Connect();
|
|
|
+ var res = plc.WriteArea(PLC.Siemens.Protocol.Common.AreaType.DB, db, address, (ushort)data.Length, PLC.Siemens.Protocol.Common.DataType.Byte, data);
|
|
|
+ if (!res)
|
|
|
+ throw new Exception("写入DB块数据失败");
|
|
|
}
|
|
|
}
|
|
|
}
|