/// <summary> /// Constructs a new message object, taking a logged message /// from the transmission history /// </summary> /// <param name="loggedMessage"></param> internal MessageDecoder(TransmissionRecord loggedMessage) { // decode the message, setting form properties this.Flags = new List <bool> { false, false, false, false, false }; var len = loggedMessage.Message.Length; var msg = loggedMessage.Message; // sort out ID if (len >= 2) { this.ID = "0x" + msg[0] + msg[1]; } // sort out type and flags if (len >= 4) { var meta = Convert.ToInt16(msg[2].ToString() + msg[3].ToString(), 16); this.TypeOfMessage = (MessageType)(meta >> 5); // if it is an instruction get the type before we bust up the flags if (this.TypeOfMessage == MessageType.Instruction) { this.TypeOfInstruction = (InstructionType)(meta & 0x1F); } else { this.TypeOfInstruction = InstructionType.None; } for (var i = 4; i >= 0; --i) { this.Flags[i] = (meta & 1) == 1; meta >>= 1; } } // get the timestamp if (len >= 12) { this.Timestamp = (long)Convert.ToInt64(msg.Substring(4, 8), 16); } // and the rest is payload if (len > 12) { this.Payload = msg.Substring(12); } else { this.Payload = "empty"; } }
/// <summary> /// Logs a message to the terminal box /// </summary> /// <param name="message"></param> void LogMessage(string message, TransmissionType transmissionType) { if (this.InvokeRequired) { this.BeginInvoke(new LogMessageDelegate(this.LogMessage), message, transmissionType); return; } // create a new transmission record var record = new TransmissionRecord(transmissionType, message); this.transmissionHistory.Add(record); // add the message to the UI var lb = transmissionType == TransmissionType.ReceivedMessage ? this.ReceivedListBox : this.SentListBox; var other = transmissionType == TransmissionType.ReceivedMessage ? this.SentListBox : this.ReceivedListBox; other.Items.Add(""); lb.Items.Add(record.ToString()); lb.SelectedIndex = lb.Items.Count - 1; }