终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SiemensHelper.cs 1.9 KiB

vor 2 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. }