import serial import json import re ser = serial.Serial('/dev/ttyUSB1', 9600, timeout=1) # Regex to capture JSON object json_pattern = re.compile(r'\{.*\}') while True: line = ser.readline().decode('utf-8', errors='ignore').strip() if not line: continue match = json_pattern.search(line) if match: try: data = json.loads(match.group()) print("Received JSON:", data) except json.JSONDecodeError: pass # ignore lines that aren't valid JSON