使用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
- Helper Function:
fix_shifted_packetfirst checks if a packet is already valid (starts withFIX2300). If not, it finds the misplaced marker and rearranges the data so the marker leads the packet. - 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.
- Error Handling: If a packet doesn’t contain the
FIX2300marker, 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

