SiemensS7PLC.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using HslCommunication.Profinet.Siemens;
  2. using WCS.Core;
  3. using WCS.Core.BaseExtensions;
  4. using WCS.Core.DataTrans;
  5. using WCS.Core.Virtual_PLC;
  6. namespace WCS.Service.PLCAccessors
  7. {
  8. public class SiemensS7PLC : SiemensS7Net, IPlcAccessor
  9. {
  10. private SiemensS7Net plc;
  11. public SiemensS7PLC(SiemensPLCS type) : base(type)
  12. {
  13. }
  14. public SiemensS7PLC(SiemensPLCS type, string ip) : base(type, ip)
  15. {
  16. plc = new SiemensS7Net(type, ip);
  17. }
  18. public SiemensS7PLC(int type, string ip) : base((SiemensPLCS)type, ip)
  19. {
  20. plc = new SiemensS7Net((SiemensPLCS)type, ip);
  21. }
  22. public byte[] ReadBytes(ushort db, ushort address, ushort length, ushort dataLength)
  23. {
  24. if (Configs.Any(SystemMode.虚拟plc))
  25. {
  26. return PlcData.Read(new PLCData { IP = plc.IpAddress, DB = db, Length = length, DataLength = dataLength });
  27. }
  28. var addr = "DB" + db + "." + address;
  29. var res = plc.Read(addr, length);
  30. if (res.IsSuccess)
  31. return res.Content;
  32. throw new WarnException("读取PLC数据失败:" + res.Message);
  33. }
  34. public void WriteBytes(ushort db, ushort address, byte[] data)
  35. {
  36. if (Configs.Any(SystemMode.虚拟plc))
  37. {
  38. PlcData.Write(new PLCData { IP = plc.IpAddress, DB = db }, address, data);
  39. }
  40. else
  41. {
  42. var res = plc.Write("D" + address, data);
  43. if (!res.IsSuccess)
  44. throw new WarnException("写入PLC数据失败:" + res.Message);
  45. }
  46. }
  47. }
  48. }