终端一体化运控平台
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

69 wiersze
1.9 KiB

  1. using S7.Net;
  2. namespace BPASmartClient.S7Net
  3. {
  4. public class SiemensHelper
  5. {
  6. Plc myPlc;
  7. public bool IsConnected => myPlc is null ? false : myPlc.IsConnected;
  8. /// <summary>
  9. /// 打开连接
  10. /// </summary>
  11. /// <param name="cpuType">PLC CPU 类型</param>
  12. /// <param name="ip">plc ip 地址</param>
  13. /// <param name="port">plc 端口号</param>
  14. /// <param name="rack">PLC 机架号</param>
  15. /// <param name="solt"> PLC 插槽号</param>
  16. public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0)
  17. {
  18. myPlc = new Plc(cpuType, ip, port, rack, solt);
  19. myPlc.Open();
  20. }
  21. /// <summary>
  22. /// 断开和PLC的连接
  23. /// </summary>
  24. public void Disconnect()
  25. {
  26. myPlc?.Close();
  27. }
  28. public object Read(string address)
  29. {
  30. if (!IsConnected) return default;
  31. return myPlc?.Read(address);
  32. }
  33. public void Write(string address, object value)
  34. {
  35. myPlc?.Write(address, value);
  36. }
  37. public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)
  38. {
  39. if (!IsConnected) return default;
  40. return (ReadT)myPlc.ReadStruct(typeof(ReadT), db, startAddress);
  41. }
  42. public void WriteStruct(object structValue, int db, int startAddress = 0)
  43. {
  44. myPlc?.WriteStruct(structValue, db, startAddress);
  45. }
  46. public int ReadClass(object sourceClass, int db, int startAddress = 0)
  47. {
  48. if (!IsConnected) return -1;
  49. return myPlc.ReadClass(sourceClass, db, startAddress);
  50. }
  51. public void WriteClass(object sourceClass, int db, int startAddress = 0)
  52. {
  53. myPlc?.WriteClass(sourceClass, db, startAddress);
  54. }
  55. }
  56. }