PTP master example in ANDi scripts

PTP master script

  • Running the following script will simulate running a PTP Master in order to synchronize between the PC running the script and devices using PTP (For example a Capture Module using firmware 18 or greater) connected to it via a certain adapter.

  • “channel_1” is the link channel between the PC and the device to be synchronized.

from andi.ptp import PtpMessageTypes, send_pdelay_response, send_pdelay_response_followup, sync
import time

# Callback function, invoked on each incoming PTP message
def on_msg_received(msg):
    if msg.message_type == PtpMessageTypes.PDelayReq:
        # Send peer delay response
        sent_time = send_pdelay_response(channel_1, msg)
        # Send peer delay response followup
        send_pdelay_response_followup(channel_1, msg, sent_time)

# Create PTP message
msg_ptp = message_builder.create_ptp_message("channel_1","channel_1")
# Register callback function
msg_ptp.on_message_received += on_msg_received
# Start live capturing for PTP message
msg_ptp.start_capture()

sequence_id = 1
try:
    while(True):
        # Get current timestamp
        timestamp = time.time()
        # Send sync message
        sync(channel_1, sequence_id = sequence_id)
        end_time = time.time()
        duration = end_time - timestamp
        # Increment sequence_id 
        sequence_id = sequence_id + 1
        time.sleep(max(0, 0.125-duration))
finally:
    msg_ptp.stop_capture()