SiemensS7PLC.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using HslCommunication.Profinet.Siemens;
  2. using System;
  3. namespace WCS.Service.PLCAccessors
  4. {
  5. public class SiemensS7PLC : SiemensS7Net
  6. {
  7. private SiemensS7Net plc;
  8. public SiemensS7PLC(SiemensPLCS type) : base(type)
  9. {
  10. }
  11. public SiemensS7PLC(SiemensPLCS type, string ip) : base(type, ip)
  12. {
  13. plc = new SiemensS7Net(type, ip);
  14. }
  15. public SiemensS7PLC(int type, string ip) : base((SiemensPLCS)type, ip)
  16. {
  17. plc = new SiemensS7Net((SiemensPLCS)type, ip);
  18. }
  19. public byte[] ReadBytes(ushort db, ushort address, ushort length)
  20. {
  21. var addr = "DB" + db + "." + address;
  22. var res = plc.Read(addr, length);
  23. if (res.IsSuccess)
  24. return res.Content;
  25. throw new Exception("读取PLC数据失败:" + res.Message);
  26. }
  27. public void WriteBytes(ushort db, ushort address, byte[] data)
  28. {
  29. var start = db + address / 2;
  30. var res = plc.Write("D" + start, data);
  31. if (!res.IsSuccess)
  32. throw new Exception("写入PLC数据失败:" + res.Message);
  33. }
  34. }
  35. }