/// <summary> /// Parses a set of byte data based on a format string. /// </summary> /// <param name="format">The format string.</param> /// <param name="bytes">The set of byte data to parse.</param> /// <returns>Returns a formatted string of the data.</returns> public static string Parse(string format, byte[] bytes) { index = 0; if (bytes == null) { return(""); } if (format.Length < 1) { return(""); } data = bytes; string[] tokens = format.Split(' '); string output = ""; for (int i = 0; i < tokens.Length; i++) { if (tokens[i] == "call") { //format the argument string string args = FormatParser.Parse("%", data).Replace(delim, " "); string filename = format.Substring(5, format.Length - 5); try { Process p = new Process(); p.StartInfo.FileName = filename; p.StartInfo.Arguments = args; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); } catch { } return(output); } else if (tokens[i] != "" && tokens[i] != "%") { try { output += group(tokens[i], tokens[++i], tokens[++i]); } catch { } //fail silently if format string is wrong for some reason } } //format remaining data while (index < data.Length) { output += Format.AsHex(data[index]) + delim; index++; } output = output.Substring(0, Math.Max(output.Length - delim.Length, 0)); return(output); }
public void LogData(Message completedMessage) { String message; MessageFormat format = GetMessageFormat(completedMessage.id_high, completedMessage.id_low); if (format == null) { message = String.Format("{0:MM/dd/yyyy HH:mm:ss.fff tt}\t\t", DateTime.Now) + completedMessage.ToString(); } else { message = format.name; if (completedMessage.data == null || completedMessage.data.Length == format.length) { String str = FormatParser.Parse(format.format, completedMessage.data); // if data length == 0, this can still return string via external program if (str != null && str.Length != 0) { message += ": " + str; } } else { message += ": Message Format Does Not Match Data Length"; } } lstDisplayWindow.Items.Add(message); lstDisplayWindow.SelectedIndex = numberOfEntries++; }
public void LogData(Message completedMessage) { String message; MessageFormat format = GetMessageFormat(completedMessage.IdHigh, completedMessage.IdLow); if (format == null) { message = completedMessage.ToString(); } else { message = completedMessage.GetTimestamp() + format.Name; if (completedMessage.Data == null || completedMessage.Data.Length == format.Length) { String formattedString = FormatParser.Parse(format.FormatString, completedMessage.Data); // if data length == 0, this can still return string via external program if (formattedString != null && formattedString.Length != 0) { message += ": " + formattedString; } } else { message += ": Message Format Does Not Match Data Length"; } } recordedMessages.Add(message); if (cmbViews.SelectedItem.ToString().Equals(ALL_MESSAGES) || message.Contains(cmbViews.SelectedItem.ToString())) { if (listBoxOneSelected) { lstDisplayWindowOne.Items.Add(message); lstDisplayWindowOne.SelectedIndex = lstDisplayWindowOne.Items.Count - 1; } else { lstDisplayWindowTwo.Items.Add(message); lstDisplayWindowTwo.SelectedIndex = lstDisplayWindowTwo.Items.Count - 1; } } }