Skip to content

Protocol Decode

Serial protocol decoding (I2C, SPI, UART)

Protocol decode framework for analyzing digital communication protocols.

EventType

Bases: Enum

Types of protocol events.

DecodedEvent dataclass

DecodedEvent(timestamp: float, event_type: EventType, data: Any, description: str, channel: str, valid: bool = True)

Represents a decoded protocol event.

Attributes:

Name Type Description
timestamp float

Event timestamp (seconds)

event_type EventType

Type of event

data Any

Event data (bytes, address, etc.)

description str

Human-readable description

channel str

Source channel(s)

valid bool

Whether event is valid (no errors)

ProtocolDecoder

ProtocolDecoder(name: str)

Bases: ABC

Abstract base class for protocol decoders.

All protocol decoders must inherit from this class and implement the decode() method.

Initialize protocol decoder.

Parameters:

Name Type Description Default
name str

Decoder name

required
Source code in scpi_control/protocol_decode.py
def __init__(self, name: str):
    """Initialize protocol decoder.

    Args:
        name: Decoder name
    """
    self.name = name
    self.events: List[DecodedEvent] = []
    logger.info(f"Protocol decoder initialized: {name}")

decode abstractmethod

decode(waveforms: Dict[str, Any], **params) -> List[DecodedEvent]

Decode protocol from waveforms.

Parameters:

Name Type Description Default
waveforms Dict[str, Any]

Dictionary of channel_name -> waveform_data

required
**params

Protocol-specific parameters

{}

Returns:

Type Description
List[DecodedEvent]

List of decoded events

Source code in scpi_control/protocol_decode.py
@abstractmethod
def decode(self, waveforms: Dict[str, Any], **params) -> List[DecodedEvent]:
    """Decode protocol from waveforms.

    Args:
        waveforms: Dictionary of channel_name -> waveform_data
        **params: Protocol-specific parameters

    Returns:
        List of decoded events
    """
    pass

get_required_channels abstractmethod

get_required_channels() -> List[str]

Get list of required channel names.

Returns:

Type Description
List[str]

List of required channel names (e.g., ['SDA', 'SCL'])

Source code in scpi_control/protocol_decode.py
@abstractmethod
def get_required_channels(self) -> List[str]:
    """Get list of required channel names.

    Returns:
        List of required channel names (e.g., ['SDA', 'SCL'])
    """
    pass

get_parameters abstractmethod

get_parameters() -> Dict[str, Any]

Get decoder parameters with default values.

Returns:

Type Description
Dict[str, Any]

Dictionary of parameter_name -> default_value

Source code in scpi_control/protocol_decode.py
@abstractmethod
def get_parameters(self) -> Dict[str, Any]:
    """Get decoder parameters with default values.

    Returns:
        Dictionary of parameter_name -> default_value
    """
    pass

clear_events

clear_events()

Clear all decoded events.

Source code in scpi_control/protocol_decode.py
def clear_events(self):
    """Clear all decoded events."""
    self.events.clear()
    logger.debug(f"{self.name}: Events cleared")

export_events_csv

export_events_csv(filename: str)

Export events to CSV file.

Parameters:

Name Type Description Default
filename str

Output CSV filename

required
Source code in scpi_control/protocol_decode.py
def export_events_csv(self, filename: str):
    """Export events to CSV file.

    Args:
        filename: Output CSV filename
    """
    import csv

    with open(filename, "w", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["Timestamp", "Event Type", "Data", "Description", "Channel", "Valid"])

        for event in self.events:
            writer.writerow(
                [
                    f"{event.timestamp:.9f}",
                    event.event_type.value,
                    str(event.data),
                    event.description,
                    event.channel,
                    "Yes" if event.valid else "No",
                ]
            )

    logger.info(f"{self.name}: Exported {len(self.events)} events to {filename}")

get_event_summary

get_event_summary() -> Dict[str, int]

Get summary of event counts by type.

Returns:

Type Description
Dict[str, int]

Dictionary of event_type -> count

Source code in scpi_control/protocol_decode.py
def get_event_summary(self) -> Dict[str, int]:
    """Get summary of event counts by type.

    Returns:
        Dictionary of event_type -> count
    """
    summary = {}
    for event in self.events:
        event_type = event.event_type.value
        summary[event_type] = summary.get(event_type, 0) + 1

    return summary