protected BaseCommand(IInterface inf, byte destinationAddress, byte cmd, byte[] data, string name) { m_Name = name; m_Interface = inf; m_Command = cmd; m_DestinationAddress = destinationAddress; m_Data = data; m_Latch = new CountdownLatch(1); //one telegram has to be received m_ReceivedDataQueue = new Queue <byte>(); m_ReceivedTelegrams = new List <ProtocolFrame>(); m_TransmitTelegram = null; }
/// <summary> /// This method is called by the Target in a separate thread /// </summary> public virtual void Execute(resend myRerun) { m_TransmitTelegram = new ProtocolFrame(m_DestinationAddress, m_Command, m_Data); { m_Latch.Reset(); IsTimeout = false; m_Interface.RegisterListener(this); m_CommandResult = WcaInterfaceCommandResult.InProgress; m_Interface.Send(m_TransmitTelegram.Telegram); try { if (!m_Latch.WaitOne(Timeout)) { //timeout IsTimeout = true; m_CommandResult = WcaInterfaceCommandResult.ExecutionTimeout; if (myRerun != null) { myRerun(m_Interface.GetName(), m_Command, m_Data, m_Name); } } else if (Validate(m_ReceivedTelegrams)) { m_CommandResult = WcaInterfaceCommandResult.PosAck; } else { m_CommandResult = WcaInterfaceCommandResult.NegAck; if (myRerun != null) { myRerun(m_Interface.GetName(), m_Command, m_Data, m_Name); } } m_Latch.Reset(); } catch (System.Exception ex) { Debug.WriteLine(ex); } m_Interface.UnregisterListener(this); NotifyListener(this); } }
public virtual void OnReceive(IInterface sender, byte[] data, int dataLength) { ProtocolFrame rf; foreach (byte b in data) { m_ReceivedDataQueue.Enqueue(b); } do { rf = ProtocolFrame.AnalyzeQueue(ref m_ReceivedDataQueue); if (rf != null) { m_ReceivedTelegrams.Add(rf); } } while (rf != null); if (m_ReceivedTelegrams.Count > 0) { m_Latch.SignalAll(); } }
public static ProtocolFrame AnalyzeQueue(ref Queue <byte> queue) { Crc16Ccitt crc = new Crc16Ccitt(InitialCrcValue.Zeros); ProtocolFramePosition state = ProtocolFramePosition.Start; ProtocolFrame result = null; byte[] temp_tel = queue.ToArray(); byte b; int start_pos = 0; int number_of_bytes_to_remove = 0; byte source_address = 0, destination_address = 0, code = 0; byte command = 0; UInt16 length_data = 0; int number_of_bytes_to_read = 0; List <byte> data = new List <byte>(); UInt16 crcValue = 0; bool first_checksum_byte = false; for (int i = 0; i < temp_tel.Length; i++) { b = temp_tel[i]; switch (state) { case ProtocolFramePosition.Start: if (b == StartByte) { start_pos = i; state = ProtocolFramePosition.Length; } break; case ProtocolFramePosition.Length: if ((i - start_pos) == 1) { length_data = b; } else { length_data += (UInt16)(b << 8); if (length_data >= 4 && length_data <= 200) { state = ProtocolFramePosition.Address_and_Code; number_of_bytes_to_read = length_data - 4; } else { number_of_bytes_to_remove = i + 1; i = temp_tel.Length; } } break; case ProtocolFramePosition.Address_and_Code: source_address = (byte)((b >> 5) & 0x07); destination_address = (byte)((b >> 2) & 0x07); code = (byte)(b & 0x03); if (destination_address == m_my_address) { state = ProtocolFramePosition.Command; } else { number_of_bytes_to_remove = i + 1; i = temp_tel.Length; } break; case ProtocolFramePosition.Command: command = b; if (length_data > 4) { state = ProtocolFramePosition.Data; } else { state = ProtocolFramePosition.Checksum; } break; case ProtocolFramePosition.Data: data.Add(b); number_of_bytes_to_read--; if (number_of_bytes_to_read == 0) { state = ProtocolFramePosition.Checksum; } break; case ProtocolFramePosition.Checksum: if (first_checksum_byte == false) { first_checksum_byte = true; crcValue = b; } else { crcValue += (UInt16)(b << 8); byte[] crc_v = crc.ComputeChecksumBytes(temp_tel, 0, i - 1); if (crcValue == (UInt16)BitConverter.ToInt16(crc_v, 0)) { //consider code info result = new ProtocolFrame(source_address, destination_address, command, code, data.ToArray()); //Checksumme ok //todo } else { //Checksumme nicht ok //todo } number_of_bytes_to_remove = i + 1; } break; } while (number_of_bytes_to_remove > 0) { number_of_bytes_to_remove--; queue.Dequeue(); } } return(result); }