#!/usr/bin/python3 
from time import sleep
import serial
import sys

arg1 = sys.argv[1] + "\n"

exceptions = 0
while (exceptions < 10):
    try:
        ser = serial.Serial('/dev/ttyACM0', 115200, timeout=1) 
    except: 
        exceptions = exceptions + 1
        sleep(0.5)
    else:
        exceptions = 999

if (exceptions != 999):
  print("Cannot open Serial Port")
  sys.exit()

    
sleep(0.1) 
# Wir leeren den Input Buffer
while ser.inWaiting():
    #print("AAA: "+ser.readline().decode('utf-8'), end='')
    ser.readline()

ser.write(arg1.encode('utf-8'))
sleep(0.1)
ch=" "
while (ch != b'}'):
    try:
        data_available = ser.inWaiting()
    except: 
        sys.exit()
    else:
        while data_available:
            try:
                ch=ser.read()
            except: 
                sys.exit()
            else:
                print(ch.decode('utf-8'), end='')
                if ch == b'}':
                    print()
                    sys.exit()
