Python to access the Ping raw data on Raspberry PI

It would be really helpful if you could illustrate the extraction of data from ‘profile-data’ variable say to JSON?

It’s as simple as print(data)

Here is an example program:

#!/usr/bin/env python

from brping import Ping1D
import argparse

##Parse Command line options
############################

parser = argparse.ArgumentParser(description="Ping python library example.")
parser.add_argument('--device', action="store", required=True, type=str, help="Ping device port.")
parser.add_argument('--baudrate', action="store", type=int, default=115200, help="Ping device baudrate.")
args = parser.parse_args()

#Make a new Ping
myPing = Ping1D(args.device, args.baudrate)
if myPing.initialize() is False:
    print("Failed to initialize Ping!")
    exit(1)

data = myPing.get_profile()
print("dictionary data looks like this:", data)
print("access distance:", data["distance"])

And the output:

$ python ex.py --device=/dev/ttyUSB0 --baudrate=9600
Opening /dev/ttyUSB0 at 9600 bps
('dictionary data looks like this:', {'distance': 13917, 'confidence': 0, 'scan_start': 0, 'profile_data': "\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd\xfd'0\x08\x00\x01\x01\x08\x01\x00\x01\x00\x02\x04\x03\x06\x08\x10\x03\x00\x01\x00\x00\x00\x00\x03\x01\x01\x01\x03\n\x03\x03\x01\x00\x03\x08\x03\x03\x00\x00\x00\x02\x02\x05\x00\x00\x00\x00\x01\x05\x01\x04\x04\x06\x06\x00\x00\x00\x00\x01\x00\x02\x02\x02\x02\x00\x00\x02\x03\x00\x00\x00\x00\x00\x03\x04\x03\x00\x00\x00\x00\x03\x02\x05\x04\x04\x04\x00\x01\x00\x00\x00\x00\x00\x00\x03\x03\x03\x01\x00\x00\x00\x00\x00\x00\x00\x03\x05\x04\x02\x00\x00\x01\x02\x0b\x0c\x04\x07\r+?5.\x16\t\x02\x07\x01\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x01\x00\x00\x00\x00\x04\x02\x03\x05\x00\x00\x01\x01\x00\x00\x03\x02\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00.\xfd8\x00", 'scan_length': 20750, 'gain_index': 6, 'ping_number': 570, 'pulse_duration': 556})
('access distance:', 13917)