Exemplo n.º 1
0
 public void Decoding(TcpBuffer playload, DataAnalysisResults callback)
 {
     _list.AddRange(playload.Datas);
     while (true)
     {
         if (_list.Count <= 0)
         {
             break;
         }
         if (_list[0] != _head)
         {
             _list.RemoveAt(0);
             continue;
         }
         if (_list.Count >= 6)
         {
             var len = BitConverter.ToInt32(_list.Skip(2).Take(4).Reverse().ToArray(), 0);
             if (_list.Count >= len)
             {
                 var data = _list.Take(len).ToList();
                 if (data[len - 2] != 0xFF)
                 {
                     _list.RemoveAt(0);
                     continue;
                 }
                 var crc  = data[len - 1];
                 var crc2 = data.Take(len - 1).BccVerifica(0, len - 1);
                 if (crc != crc2)
                 {
                     _list.RemoveAt(0);
                     continue;
                 }
                 var code = data[1];
                 var temp = data.Skip(6).Take(len - 8).ToList();
                 temp.Insert(0, code);
                 var datas = temp.ToArray();
                 _list.RemoveRange(0, len);
                 callback?.Invoke(new TcpBuffer(datas, 0, datas.Length));
             }
             else
             {
                 break;
             }
         }
         else
         {
             break;
         }
     }
 }
 public void Decoding(TcpBuffer playload, DataAnalysisResults callback)
 {
     list.AddRange(playload.Datas);
     while (true)
     {
         if (list.Count <= 0)
         {
             break;
         }
         if (list.Count < 2)
         {
             break;
         }
         var head = list.Take(2).ToArray();
         if (!CheckShortArray(head, this._head))
         {
             list.RemoveAt(0);
             continue;
         }
         if (list.Count >= 8)
         {
             var len  = BitConverter.ToInt32(list.Skip(2).Take(4).Reverse().ToArray(), 0);
             var code = list.Skip(6).Take(2).Reverse().ToArray();
             if (list.Count < len + 10)
             {
                 break;
             }
             var tail = list.Skip(len + 8).Take(2).ToArray();
             if (!CheckShortArray(tail, this._tail))
             {
                 list.RemoveAt(0);
                 continue;
             }
             var data = list.Skip(8).Take(len).ToList();
             data.InsertRange(0, code);
             list.RemoveRange(0, len + 10);
             callback?.Invoke(new TcpBuffer(data.ToArray(), 0, data.Count));
         }
         else
         {
             break;
         }
     }
 }
Exemplo n.º 3
0
 public void Decoding(TcpBuffer playload, DataAnalysisResults callback)
 {
     _list.AddRange(playload.Datas);
     while (true)
     {
         if (_list.Count < 1)
         {
             break;
         }
         if (_list.Count >= 1)
         {
             if (_list[0] != 0xEA)
             {
                 _list.RemoveAt(0);
                 continue;
             }
         }
         if (_list.Count >= 2)
         {
             if (_list[1] != 0x56)
             {
                 _list.RemoveAt(0);
                 continue;
             }
         }
         if (_list.Count >= 10)
         {
             int pageLen = BitConverter.ToInt32(_list.Skip(6).Take(4).Reverse().ToArray(), 0);//包长度
             if (pageLen <= 0)
             {
                 _list.RemoveAt(0);
                 continue;
             }
             if (_list.Count < pageLen)
             {
                 break;
             }
             if (_list.Count >= pageLen)
             {
                 var arry = _list.Take(pageLen).Skip(pageLen - 3).ToArray();//消息尾有三个字节,因为校验码占2个字节
                 if (arry[0] != 0xFF)
                 {
                     _list.RemoveAt(0);
                     continue;
                 }
                 CRC    crc       = new CRC();
                 ushort checksum1 = crc.CRC16_MODBUS(_list.Take(pageLen - 2).ToArray());
                 ushort checksum2 = BitConverter.ToUInt16(arry.Skip(1).ToArray(), 0);
                 if (checksum1 != checksum2)
                 {
                     _list.RemoveAt(0);
                     continue;
                 }
                 var dataLen = BitConverter.ToInt32(_list.Skip(10).Take(4).Reverse().ToArray(), 0);
                 if (dataLen > pageLen - (2 + 4 + 4 + 4 + 1 + 2))
                 {
                     _list.RemoveAt(0);
                     continue;
                 }
                 var         data  = _list.Skip(14).Take(dataLen).ToArray();
                 List <byte> temps = new List <byte>();
                 temps.AddRange(_list.Skip(2).Take(4).Reverse());
                 temps.AddRange(data);
                 callback?.Invoke(new TcpBuffer()
                 {
                     Datas = temps.ToArray(), Count = temps.Count, Offset = 0
                 });
                 _list.RemoveRange(0, pageLen);
             }
         }
         else
         {
             break;
         }
     }
 }