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

Python解码未知字符串:树莓派UART读取RFID数据异常

Troubleshooting RFID UART Decoding on Raspberry Pi

Hey there, let's work through this issue step by step. It sounds like your RFID scanner is sending data over UART, but the output you're getting doesn't match the expected ID—let's break down the possible fixes and checks.

First: Confirm UART Configuration Matches Your Scanner

The most common culprit here is mismatched serial parameters between your Raspberry Pi and RFID scanner. If these don't line up, you'll get garbled or misinterpreted data every time.

  1. Check your Pi's UART settings: Run this command in the terminal to see current serial config (replace /dev/ttyS0 with your actual serial port, like /dev/ttyAMA0):
    stty -F /dev/ttyS0
    
  2. Compare with your scanner's specs: Most RFID scanners default to 9600 baud, 8 data bits, 1 stop bit, no parity (8N1). If your Pi's settings differ, update them with:
    stty -F /dev/ttyS0 9600 cs8 -cstopb -parenb
    
  3. Test with a serial tool: Install minicom to rule out Python script issues:
    sudo apt install minicom
    minicom -b 9600 -D /dev/ttyS0
    
    Scan a tag and see if the output matches what your Python script is reading. If it's still garbled, the issue is definitely serial config.

Second: Analyze Raw Byte Data (Not String Conversions)

Sometimes converting raw serial data to a string too early can introduce encoding errors. Modify your Python script to read and print raw bytes in hex format:

import serial

# Adjust port/baud rate to match your scanner
ser = serial.Serial('/dev/ttyS0', 9600, timeout=1)

while True:
    if ser.in_waiting > 0:
        # Read until ETX (end of text, hex 0x03)
        raw_bytes = ser.read_until(b'\x03')
        print(f"Full raw data (hex): {raw_bytes.hex()}")
        
        # Strip STX (0x02) and ETX (0x03)
        data_bytes = raw_bytes.strip(b'\x02\x03')
        print(f"Cleaned data bytes (hex): {data_bytes.hex()}")

This will show you the exact bytes the scanner is sending, so you can rule out string encoding mix-ups.

Third: Decode Based on Scanner Output Format

Your current output 070080BB5B67 likely includes extra data or uses a format you haven't accounted for. Here are common scenarios:

1. Extra Header/Length Bytes

The first two characters (07) might be a length indicator (hex 07 = decimal 7, meaning 7 bytes follow). Try stripping this header and see if the remaining data can be converted:

  • Stripped data: 0080BB5B67
  • Check if this needs byte reversal: 675BBB8000 (unlikely to match your expected 0008436571, but worth testing)

2. BCD (Binary-Coded Decimal) Conversion

If your expected ID is all decimal digits, the scanner might be sending BCD-encoded data. Each byte represents two decimal digits. For example:

  • Byte 00 → 00, 08 → 08, 43 → 43, etc.
  • Test this by splitting your cleaned data into 2-character chunks, converting each to a byte, then decoding as BCD.

3. EPC vs. User ID Mismatch

Many RFID scanners output the tag's raw EPC code by default, but you might be expecting a human-readable user ID stored on the tag. Check your scanner's manual for commands to switch output formats (e.g., from EPC to user memory or decoded ID).

4. Parity/Stop Bit Errors

If you have parity enabled on the scanner but disabled on the Pi (or vice versa), the parity bit will be interpreted as part of the data, causing garbled bytes. Double-check that parity settings are identical on both devices.

Final Checks

  • Consult your scanner's configuration manual: Most scanners let you send serial commands to adjust output (e.g., disable headers, switch to ASCII output, or set specific ID formats).
  • Test with a known good tag: If you have another tag with a confirmed ID, scan it to see if the output follows a pattern you can reverse-engineer.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:09:58