PLC.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. var res = master.ReadHoldingRegisters(1, addr, 1);
  50. return res[0];
  51. }
  52. public bool ReadBool(string addr)//addr=1025.1,不能有字母
  53. {
  54. ushort address = ushort.Parse(addr.Split('.')[0]);
  55. ushort position = ushort.Parse(addr.Split('.')[1]);
  56. bool[] Array = Read(address);
  57. bool mybool = Array[position];
  58. return mybool;
  59. }
  60. public bool[] Read(ushort addr)
  61. {
  62. var res = master.ReadHoldingRegisters(1, addr, 1);
  63. bool[] boolArray = ConvertWordToBoolArray(res[0]);
  64. Array.Reverse(boolArray);
  65. return boolArray;
  66. }
  67. //------------
  68. //一个16bit的word转换成16bit的bool
  69. public bool[] ConvertWordToBoolArray(ushort word)
  70. {
  71. bool[] boolArray = new bool[16];
  72. for (int i = 0; i < 16; i++)
  73. {
  74. // 右移i位并与1做与运算来提取每一位的值
  75. boolArray[i] = (word & (1 << i)) != 0;
  76. }
  77. // 反转数组使得最左边的bit对应boolArray[0]
  78. Array.Reverse(boolArray);
  79. return boolArray;
  80. }
  81. // 将一个bool数组转换成16位的ushort
  82. public static ushort BoolArrayToWord(bool[] boolArray)
  83. {
  84. if (boolArray.Length != 16)
  85. throw new ArgumentException("数组必须包含16个bool值");
  86. ushort word = 0;
  87. for (int i = 0; i < boolArray.Length; i++)
  88. {
  89. // 如果bool值为true,将对应位设置为1
  90. if (boolArray[i])
  91. {
  92. word |= (ushort)(1 << (15 - i)); // 位移并通过按位或设置对应的位
  93. }
  94. }
  95. return word;
  96. }
  97. }
  98. }