#! demovenv/bin/python3 """ This demo script shows how the "parse" function of "analyzeTcpDump.py" can be used to easily visualize data from a PCAP file created by the tcpdump tool / wireshark. A suitable virtual environment can be created with the "makedemovenv" script. Stefan Mathis, January 2025 """ import matplotlib.pyplot as plt import matplotlib.dates as mdates from datetime import datetime, timedelta from analyzeTcpDump import parse if __name__ == "__main__": data = parse("demo.pcap") plt.figure(figsize=(12, 6)) # Plot the position of axis 5 over time # Actual position position_valid = [] dates_valid = [] position_all = [] dates_all = [] for (timestamp, item) in data["172.28.101.24"]["Q0510"].items(): date = datetime.fromtimestamp(timestamp) value = item["response"]["value"] dates_all.append(date) position_all.append(value) if item["response"]["valid"]: dates_valid.append(date) position_valid.append(value) else: command = item["command"]["ascii"] response = item["response"]["ascii"] # Replace non-renderable characters command = command.replace("\0", "\\x00") command = command.replace("\r", "\\x0d") command = command.replace("\x12", "\\x12") response = response.replace("\r", "\\x0d") response = response.replace("\06", "\\x06") # Shift the text a bit to the right plt.text(date, value, f"Command: {command}\nResponse: {response}", horizontalalignment="right", verticalalignment="top") # Target position position_target = [position_valid[0]] dates_target = [dates_valid[0]] for (timestamp, item) in data["172.28.101.24"]["Q0501="].items(): date = datetime.fromtimestamp(timestamp) value = item["command"]["value"] dates_target.append(date) position_target.append(position_target[-1]) dates_target.append(date) position_target.append(value) dates_target.append(dates_valid[-1]) position_target.append(position_target[-1]) plt.plot(dates_target, position_target, "k--", label="Target position") plt.plot(dates_all, position_all, "r-", label="All responses") plt.plot(dates_valid, position_valid, "b-", label="Valid responses") plt.xlabel("Time (ISO 8601)") plt.ylabel("Axis position in degree") plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%dT%H:%M:%S")) plt.xticks(rotation=45) plt.grid(True) plt.legend(loc="lower left") plt.title("Position of axis 5") plt.tight_layout() plt.show()