WriteParamsRequest.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections.Generic;
  2. using Core.Communication.Transport;
  3. using Core.Util.Extension;
  4. using PLC.Siemens.Protocol.Common;
  5. using PLC.Siemens.Protocol.ReadData;
  6. namespace PLC.Siemens.Protocol.WriteData
  7. {
  8. public class WriteParamsRequest
  9. {
  10. public byte FunRead { get; set; }
  11. public byte ItemsCount { get; set; }
  12. public WriteItemRequest[] Items { get; set; }//最大长度20
  13. public byte PacketLength
  14. {
  15. get
  16. {
  17. return (byte)(0x0C * Items.Length + 2);
  18. }
  19. }
  20. public WriteParamsRequest()
  21. {
  22. }
  23. public void Build(List<DataItem> dataItems)
  24. {
  25. FunRead = (byte)PduFuncType.PduFuncWrite;
  26. ItemsCount = (byte)dataItems.Count;
  27. //项处理
  28. Items = new WriteItemRequest[ItemsCount];
  29. for (var i = 0; i < dataItems.Count; i++)
  30. {
  31. var dataItem = dataItems[i];
  32. var item = new WriteItemRequest();
  33. Items[i] = item;
  34. item.ItemHead[0] = 0x12;
  35. item.ItemHead[1] = 0x0A;
  36. item.ItemHead[2] = 0x10;
  37. item.TransportSize = (byte)dataItem.DataType;
  38. item.Length = dataItem.Length;
  39. if (dataItem.AreaType == AreaType.DB)
  40. item.DB = dataItem.Db;
  41. else
  42. item.DB = 0x0000;
  43. item.Area = (byte)dataItem.AreaType;
  44. //ushort address;
  45. long address;//修改
  46. if ((dataItem.DataType == DataType.Bit) || (dataItem.DataType == DataType.Counter) || (dataItem.DataType == DataType.Timer))
  47. address = dataItem.Start;
  48. else
  49. //address = (ushort)(dataItem.Start * 8);
  50. address = (dataItem.Start * 8);//修改
  51. item.Address[2] = (byte)(address & 0x000000FF);
  52. address = (ushort)(address >> 8);
  53. item.Address[1] = (byte)(address & 0x000000FF);
  54. address = (ushort)(address >> 8);
  55. item.Address[0] = (byte)(address & 0x000000FF);
  56. }
  57. }
  58. public void GetByteBuffer(ByteBuffer buffer)
  59. {
  60. buffer.Push(FunRead);
  61. buffer.Push(ItemsCount);
  62. Items.ForEach(t =>
  63. {
  64. buffer.Push(t.ItemHead);
  65. buffer.Push(t.TransportSize);
  66. buffer.Push(t.Length);
  67. buffer.Push(t.DB);
  68. buffer.Push(t.Area);
  69. buffer.Push(t.Address);
  70. });
  71. }
  72. }
  73. }