Exemplo n.º 1
0
 public void Parse(SECSBlock mHSMSItem)
 {
     try
     {
         SECSDecoding decoding = new SECSDecoding();
         string       str      = ByteStringBuilder.ToLogString(mHSMSItem.Header);
         string       str2     = ByteStringBuilder.ToLogString(mHSMSItem.DataItem);
         SECSMessage  msg      = decoding.Byte_TO_SecsMessage(mHSMSItem.Header);
         msg.Root   = decoding.Byte_TO_SecsItem(mHSMSItem.DataItem);
         msg.Header = mHSMSItem.Header;
         //this.logger.Info(string.Format("[RECV] S{0}F{1} {2} System Bytes={3} {4} {5}", new object[]
         //{
         //	msg.Stream,
         //	msg.Function,
         //	msg.WBit ? "W" : "",
         //	msg.SystemBytes,
         //	str,
         //	str2
         //}));
         //this.logger.Warn("[RECV] " + SecsItem2Str.GetSecsMessageStr(msg));
         if (this.OnReceived != null)
         {
             this.OnReceived(msg);
         }
     }
     catch (Exception exception)
     {
         this.logger.Error("Parser#Parse", exception);
         if (this.OnParseError != null)
         {
             this.OnParseError(string.Format("{0}: {1}", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ParseError), exception.Message));
         }
     }
 }
Exemplo n.º 2
0
 private void RcvdData(byte data)
 {
     if (this.secs1.PortStatus == eSECS1_PORT_STATUS.PortRcvd)
     {
         lock (this.syncRcvd)
         {
             this.rcvdBlock.Add(data);
             if (this.rcvdBlockLength == 0)
             {
                 this.rcvdBlockLength = (int)this.rcvdBlock[0];
                 this.secs1.StopTimer(eTimeout.T2);
                 if (this.rcvdBlockLength < 10 || this.rcvdBlockLength > 254)
                 {
                     this.Logger.Debug(string.Format("RECV Invalid Length Bytes: {0}.", this.rcvdBlockLength));
                     this.rcvdBlockLength = 0;
                     this.rcvdBlock.Clear();
                     this.secs1.UpdatePortStatus(eSECS1_PORT_STATUS.PortRcvd, eSECS1_PORT_STATUS.PortCmpl);
                     this.secs1.StartTimer(eTimeout.T1);
                     goto IL_3E2;
                 }
             }
             else
             {
                 this.secs1.StopTimer(eTimeout.T1);
             }
             byte[] buffer = this.rcvdBlock.ToArray();
             if (this.rcvdBlockLength == this.rcvdBlock.Count - 3)
             {
                 byte[] buffer2 = new byte[10];
                 Array.Copy(buffer, 1, buffer2, 0, 10);
                 byte[] buffer3 = new byte[2];
                 Array.Copy(buffer, buffer.Length - 2, buffer3, 0, 2);
                 byte[] buffer4 = new byte[buffer.Length - 13];
                 Array.Copy(buffer, buffer2.Length + 1, buffer4, 0, buffer4.Length);
                 //this.Logger.Info(string.Format("[RECV] HDR = {0}\nDATA = {1}\nCSUM = {2}", SecsItem2Str.GetBinaryStr(buffer2), SecsItem2Str.GetBinaryStr(buffer4), SecsItem2Str.GetBinaryStr(buffer3)));
                 int num  = (int)buffer3[0] << 8 | (int)buffer3[1];
                 int num2 = (int)(buffer2[4] & 128);
                 int num3 = 0;
                 for (int i = 1; i < this.rcvdBlock.Count - 2; i++)
                 {
                     num3 += (int)this.rcvdBlock[i];
                 }
                 if (num != num3)
                 {
                     this.secs1.UpdatePortStatus(this.secs1.PortStatus, eSECS1_PORT_STATUS.PortIdle);
                     this.secs1.SendNAK();
                     this.Logger.Debug("Checksum not match, Reply to send NAK.");
                 }
                 else
                 {
                     if (this.rcvdBlocks.Count > 0)
                     {
                         this.secs1.StopTimer(eTimeout.T4);
                     }
                     this.secs1.UpdatePortStatus(this.secs1.PortStatus, eSECS1_PORT_STATUS.PortIdle);
                     this.secs1.SendACK();
                     if (num2 == 0)
                     {
                         SECSBlock block = new SECSBlock
                         {
                             Header   = buffer2,
                             DataItem = buffer4,
                             CheckSum = buffer3
                         };
                         this.rcvdBlocks.Add(block);
                         this.Logger.Debug("RECV One of Multi-Block Ok, Send ACK.");
                         this.secs1.StartTimer(eTimeout.T4);
                     }
                     else if (this.rcvdBlocks.Count == 0)
                     {
                         this.Logger.Debug("RECV Single-Block Message Ok, Send ACK.");
                         SECSDecoding decoding = new SECSDecoding();
                         SECSMessage  msg      = decoding.Byte_TO_SecsMessage(buffer2);
                         SECSItem     item     = decoding.Byte_TO_SecsItem(buffer4);
                         if (item != null)
                         {
                             msg.Root = item;
                         }
                         this.eventExecutor.NofityRECV(msg);
                     }
                     else
                     {
                         this.Logger.Debug("RECV Multi-Block Message Ok, Send ACK.");
                         SECSDecoding decoding2 = new SECSDecoding();
                         SECSMessage  message2  = decoding2.Byte_TO_SecsMessage(buffer2);
                         List <byte>  list      = new List <byte>();
                         for (int j = 0; j < this.rcvdBlocks.Count; j++)
                         {
                             list.AddRange(this.rcvdBlocks[j].DataItem);
                         }
                         this.rcvdBlocks.Clear();
                         list.AddRange(buffer4);
                         byte[]   aDataByte = list.ToArray();
                         SECSItem item2     = decoding2.Byte_TO_SecsItem(aDataByte);
                         if (item2 != null)
                         {
                             message2.Root = item2;
                         }
                         this.eventExecutor.NofityRECV(message2);
                     }
                 }
             }
             else
             {
                 this.secs1.StartTimer(eTimeout.T1);
             }
             IL_3E2 :;
         }
         return;
     }
     if (this.secs1.PortStatus == eSECS1_PORT_STATUS.PortCmpl)
     {
         this.secs1.StopTimer(eTimeout.T1);
         this.Logger.Info(string.Format("PortCmpl, Invalid data {0}", data));
         this.secs1.StartTimer(eTimeout.T1);
         return;
     }
     this.Logger.Info(string.Format("Invalid Status - {0}, Invalid data: {1}", this.secs1.PortStatus, data));
 }