TruckScaleProtocol.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using Core.StlMes.Client.Mcp.Mch.Mcms.entity;
  7. namespace Core.StlMes.Client.Mcp.Mch.Mcms
  8. {
  9. class TruckScaleProtocol : IProtocol
  10. {
  11. private CmmBaseProtocolEntity _cmmBaseProtocol;
  12. private int len = 0;
  13. private Dictionary<int, int> checkPositon = new Dictionary<int, int>();
  14. private int _dataLocaiton=0;
  15. private int _dataLen =0;
  16. private string dataType = "";
  17. private bool Valid = false;
  18. public TruckScaleProtocol(CmmBaseProtocolEntity cmmBaseProtocol)
  19. {
  20. _cmmBaseProtocol = cmmBaseProtocol;
  21. try
  22. {
  23. if (_cmmBaseProtocol != null)
  24. {
  25. int position = 0;
  26. if (cmmBaseProtocol.ListC != null && cmmBaseProtocol.ListC.Any())
  27. {
  28. foreach (var cmmBaseProtocolCEntity in cmmBaseProtocol.ListC)
  29. {
  30. len = len+ int.Parse(cmmBaseProtocolCEntity.DataLen.ToString3());
  31. if ("1" == cmmBaseProtocolCEntity.DataType)
  32. {
  33. checkPositon.Add(position, int.Parse(cmmBaseProtocolCEntity.DataValue, NumberStyles.HexNumber));
  34. }
  35. else if ("3" == cmmBaseProtocolCEntity.DataType)
  36. {
  37. _dataLocaiton = position;
  38. _dataLen = int.Parse(cmmBaseProtocolCEntity.DataLen.ToString3());
  39. dataType = cmmBaseProtocolCEntity.ValueType;
  40. }
  41. position = position + int.Parse(cmmBaseProtocolCEntity.DataLen.ToString3());
  42. }
  43. Valid = true;
  44. }
  45. }
  46. }
  47. catch (Exception)
  48. {
  49. Valid = false;
  50. }
  51. }
  52. public byte[] Framing(byte[] byteArray)
  53. {
  54. return null;
  55. }
  56. public DeframeType AnalaysData(List<byte> ReceiveData, out object Data)
  57. {
  58. Data = "";
  59. if (!Valid) return DeframeType.WrongFrame;
  60. if (_cmmBaseProtocol == null) return DeframeType.UnKnow;
  61. HandleHead(ReceiveData);
  62. if (ReceiveData.Count < len) return DeframeType.LengthLess;
  63. List<byte> dataArea = ReceiveData.Skip(_dataLocaiton).Take(_dataLen).ToList();
  64. ReceiveData.RemoveRange(0, len);
  65. Data = ("2"==dataType)?
  66. Encoding.Default.GetString(dataArea.SkipWhile(p => p == 0x20).TakeWhile(p => p != 0x20).ToArray())
  67. .Trim() : int.Parse(dataArea.Select(p=>p.ToString("X2")).Aggregate((a,b)=>a+b), NumberStyles.HexNumber).ToString();
  68. return DeframeType.RightFrame;
  69. return DeframeType.UnKnow;
  70. }
  71. private void HandleHead(List<byte> ReceiveData)
  72. {
  73. while (ReceiveData.Count > 0 && !check(ReceiveData))
  74. {
  75. ReceiveData.RemoveAt(0);
  76. }
  77. }
  78. public bool check(List<byte> ReceiveData)
  79. {
  80. if (checkPositon == null || !checkPositon.Any()) return true;
  81. foreach (var keyValuePair in checkPositon)
  82. {
  83. if (ReceiveData[keyValuePair.Key] != keyValuePair.Value) return false;
  84. }
  85. return true;
  86. }
  87. }
  88. }