PLC.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  2. using Modbus.Device;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using TouchSocket.Core;
  12. namespace LogoForceTestApp.Modules.MainModule
  13. {
  14. public class PLC
  15. {
  16. TcpClient client;
  17. ModbusIpMaster master;
  18. public bool Connect()
  19. {
  20. try
  21. {
  22. IPAddress address = IPAddress.Parse("192.168.0.10");
  23. client = new TcpClient();
  24. client.Connect(address, 502);
  25. master = ModbusIpMaster.CreateIp(client);
  26. //连接成功
  27. return true;
  28. }
  29. catch (Exception ex)
  30. {
  31. return false;
  32. }
  33. }
  34. public async void WriteInt(ushort addr, int index)
  35. {
  36. await master.WriteMultipleRegistersAsync(1, addr, new ushort[] { (ushort)index });
  37. }
  38. public void Write(string addr, bool mybool)//addr=1025.1,不能有字母
  39. {
  40. ushort address = ushort.Parse(addr.Split('.')[0]);
  41. ushort position = ushort.Parse(addr.Split('.')[1]);
  42. bool[] Array = Read(address);
  43. Array[position] = mybool;
  44. ushort word = BoolArrayToWord(Array);
  45. WriteInt(address, word);
  46. }
  47. public int ReadInt(ushort addr)
  48. {
  49. try
  50. {
  51. var res = master.ReadHoldingRegisters(1, addr, 1);
  52. return res[0];
  53. }
  54. catch (Exception)
  55. {
  56. return -1;
  57. }
  58. }
  59. public bool ReadBool(string addr)//addr=1025.1,不能有字母
  60. {
  61. ushort address = ushort.Parse(addr.Split('.')[0]);
  62. ushort position = ushort.Parse(addr.Split('.')[1]);
  63. bool[] Array = Read(address);
  64. bool mybool = Array[position];
  65. return mybool;
  66. }
  67. public bool[] Read(ushort addr)
  68. {
  69. try
  70. {
  71. var res = master.ReadHoldingRegisters(1, addr, 1);
  72. bool[] boolArray = ConvertWordToBoolArray(res[0]);
  73. Array.Reverse(boolArray);
  74. return boolArray;
  75. }
  76. catch (Exception)
  77. {
  78. return null;
  79. }
  80. }
  81. //------------
  82. //一个16bit的word转换成16bit的bool
  83. public bool[] ConvertWordToBoolArray(ushort word)
  84. {
  85. bool[] boolArray = new bool[16];
  86. for (int i = 0; i < 16; i++)
  87. {
  88. // 右移i位并与1做与运算来提取每一位的值
  89. boolArray[i] = (word & (1 << i)) != 0;
  90. }
  91. // 反转数组使得最左边的bit对应boolArray[0]
  92. Array.Reverse(boolArray);
  93. return boolArray;
  94. }
  95. // 将一个bool数组转换成16位的ushort
  96. public static ushort BoolArrayToWord(bool[] boolArray)
  97. {
  98. if (boolArray.Length != 16)
  99. throw new ArgumentException("数组必须包含16个bool值");
  100. ushort word = 0;
  101. for (int i = 0; i < boolArray.Length; i++)
  102. {
  103. // 如果bool值为true,将对应位设置为1
  104. if (boolArray[i])
  105. {
  106. word |= (ushort)(1 << (15 - i)); // 位移并通过按位或设置对应的位
  107. }
  108. }
  109. return word;
  110. }
  111. }
  112. }