You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python修正移位的单片机十六进制数据流

Fixing Shifted Hex Data Streams from Microcontrollers with Python

Hey there, I’ve run into similar messy serial data glitches before—manual reformatting is such a tedious drag! Let’s get this sorted with a straightforward Python script that automatically adjusts those shifted packets so FIX2300 is back at the start where it belongs.

Problem Breakdown

Your normal valid packets look like FIX230000010056A898, but when errors hit, the stream shifts into something like 00010067A898FIX2300—the FIX2300 marker ends up stuck at the end instead of leading the packet. Our goal is to detect this shift and reorder each packet correctly in bulk.

Solution Code

First, let’s build a helper function to fix individual shifted packets, then apply it to your entire data stream:

def fix_shifted_packet(packet):
    # The fixed starting marker we need to restore
    marker = "FIX2300"
    
    # If the packet is already valid, no changes needed
    if packet.startswith(marker):
        return packet
    
    # Locate where the marker is hiding in the shifted packet
    marker_pos = packet.find(marker)
    if marker_pos == -1:
        # Marker not found—return original to avoid data loss (adjust this logic if needed)
        print(f"Warning: Marker '{marker}' missing in packet: {packet}")
        return packet
    
    # Reconstruct the correct packet: marker + the data that was misplaced before it
    fixed_packet = marker + packet[:marker_pos]
    return fixed_packet

# Example usage with your error-prone stream
error_data_stream = "00010067A898FIX2300 00020067A898FIX2300 00030067A898FIX2300"

# Split the stream into individual packets (assuming space-separated; adjust delimiter if needed)
packets = error_data_stream.split()

# Fix all packets in one go
fixed_packets = [fix_shifted_packet(pkt) for pkt in packets]

# Combine back into a clean, properly formatted stream
fixed_stream = " ".join(fixed_packets)

print("Fixed Data Stream:")
print(fixed_stream)

How It Works

  1. Helper Function: fix_shifted_packet first checks if a packet is already valid (starts with FIX2300). If not, it finds the misplaced marker and rearranges the data so the marker leads the packet.
  2. Stream Processing: We split the input stream into individual packets (using spaces as separators—swap this for newlines, commas, etc., if your stream uses a different delimiter), fix each one, then join them back into a clean, usable stream.
  3. Error Handling: If a packet doesn’t contain the FIX2300 marker, the function warns you and returns the original packet to avoid data loss. You can tweak this logic (e.g., log the faulty packet to a file, skip it) based on your workflow.

Testing the Code

Running the example will output exactly the corrected format you need:

Fixed Data Stream:
FIX230000010067A898 FIX230000020067A898 FIX230000030067A898

Customization Tips

  • If your packets are separated by something other than spaces (like newlines or commas), modify the split() argument (e.g., split("\n") for newline-separated data).
  • If you’re reading data directly from a serial port, integrate this function into your reading loop to fix packets on the fly as they come in.

内容的提问来源于stack exchange,提问作者munmu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:13:48