| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using Core.StlMes.Client.LgResMgt.Mcms.entity;
- namespace Core.StlMes.Client.LgResMgt.Mcms
- {
- class TruckScaleProtocol : IProtocol
- {
- private CmmBaseProtocolEntity _cmmBaseProtocol;
- private int len = 0;
- private Dictionary<int, int> checkPositon = new Dictionary<int, int>();
- private int _dataLocaiton=0;
- private int _dataLen =0;
- private string dataType = "";
- private bool Valid = false;
- public TruckScaleProtocol(CmmBaseProtocolEntity cmmBaseProtocol)
- {
- _cmmBaseProtocol = cmmBaseProtocol;
- try
- {
- if (_cmmBaseProtocol != null)
- {
- int position = 0;
- if (cmmBaseProtocol.ListC != null && cmmBaseProtocol.ListC.Any())
- {
- foreach (var cmmBaseProtocolCEntity in cmmBaseProtocol.ListC)
- {
- len = len+ int.Parse(cmmBaseProtocolCEntity.DataLen.ToString3());
- if ("1" == cmmBaseProtocolCEntity.DataType)
- {
- checkPositon.Add(position, int.Parse(cmmBaseProtocolCEntity.DataValue, NumberStyles.HexNumber));
- }
- else if ("3" == cmmBaseProtocolCEntity.DataType)
- {
- _dataLocaiton = position;
- _dataLen = int.Parse(cmmBaseProtocolCEntity.DataLen.ToString3());
- dataType = cmmBaseProtocolCEntity.ValueType;
- }
- position = position + int.Parse(cmmBaseProtocolCEntity.DataLen.ToString3());
-
- }
- Valid = true;
- }
- }
- }
- catch (Exception)
- {
- Valid = false;
- }
- }
- public byte[] Framing(byte[] byteArray)
- {
- return null;
- }
- public DeframeType AnalaysData(List<byte> ReceiveData, out object Data,out List<byte> rightData )
- {
- Data = "";
- rightData = ReceiveData;
- if (!Valid) return DeframeType.WrongFrame;
- if (_cmmBaseProtocol == null) return DeframeType.UnKnow;
- HandleHead(ReceiveData);
- rightData = ReceiveData;
- if (ReceiveData.Count < len) return DeframeType.LengthLess;
- List<byte> dataArea = ReceiveData.Skip(_dataLocaiton).Take(_dataLen).ToList();
- rightData = ReceiveData.Take(len).ToList();
- ReceiveData.RemoveRange(0, len);
- Data = ("2"==dataType)?
- Encoding.Default.GetString(dataArea.SkipWhile(p => p == 0x20).TakeWhile(p => p != 0x20).ToArray())
- .Trim() : int.Parse(dataArea.Select(p=>p.ToString("X2")).Aggregate((a,b)=>a+b), NumberStyles.HexNumber).ToString();
- return DeframeType.RightFrame;
- return DeframeType.UnKnow;
- }
- private void HandleHead(List<byte> ReceiveData)
- {
- while (ReceiveData.Count > 0 && !check(ReceiveData))
- {
- ReceiveData.RemoveAt(0);
- }
- }
- public bool check(List<byte> ReceiveData)
- {
- if (checkPositon == null || !checkPositon.Any()) return true;
- foreach (var keyValuePair in checkPositon)
- {
- if (ReceiveData[keyValuePair.Key] != keyValuePair.Value) return false;
- }
- return true;
- }
- }
- }
|