TruckScaleProtocol.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.LgResMgt.Mcms.entity;
  7. namespace Core.StlMes.Client.LgResMgt.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,out List<byte> rightData )
  57. {
  58. Data = "";
  59. rightData = ReceiveData;
  60. if (!Valid) return DeframeType.WrongFrame;
  61. if (_cmmBaseProtocol == null) return DeframeType.UnKnow;
  62. HandleHead(ReceiveData);
  63. rightData = ReceiveData;
  64. if (ReceiveData.Count < len) return DeframeType.LengthLess;
  65. List<byte> dataArea = ReceiveData.Skip(_dataLocaiton).Take(_dataLen).ToList();
  66. rightData = ReceiveData.Take(len).ToList();
  67. ReceiveData.RemoveRange(0, len);
  68. Data = ("2"==dataType)?
  69. Encoding.Default.GetString(dataArea.SkipWhile(p => p == 0x20).TakeWhile(p => p != 0x20).ToArray())
  70. .Trim() : int.Parse(dataArea.Select(p=>p.ToString("X2")).Aggregate((a,b)=>a+b), NumberStyles.HexNumber).ToString();
  71. return DeframeType.RightFrame;
  72. return DeframeType.UnKnow;
  73. }
  74. private void HandleHead(List<byte> ReceiveData)
  75. {
  76. while (ReceiveData.Count > 0 && !check(ReceiveData))
  77. {
  78. ReceiveData.RemoveAt(0);
  79. }
  80. }
  81. public bool check(List<byte> ReceiveData)
  82. {
  83. if (checkPositon == null || !checkPositon.Any()) return true;
  84. foreach (var keyValuePair in checkPositon)
  85. {
  86. if (ReceiveData[keyValuePair.Key] != keyValuePair.Value) return false;
  87. }
  88. return true;
  89. }
  90. }
  91. }