#!/usr/bin/env python3

import serial
import time
from grovepi import *

ser = serial.Serial("/dev/ttyS0", baudrate=9600, timeout=1)
ser.flush()

while True:
    data = b""
    print(data)
    while len(data) < 14:
        c = ser.read(1)
        if c:
            data += c
        else:
            continue

    if len(data) >= 11:
        hex_value = data[5:11].decode(errors='ignore')

        try:
            res = int(hex_value, 16)
            print(res)

        except ValueError:
            print("Invalid hex:", hex_value)
    else:
        print("Received incomplete data:", data)