SiemensS7PLC.cs 1.6 KB

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