#!/bin/bash

SERIAL_PORT="/dev/ttyACM0"
BAUD_RATE="115200"

{
    stty -F $SERIAL_PORT $BAUD_RATE
    COUNT=0
    PREVIOUS_TIME=""

    while IFS= read -r LINE; do
        CURRENT_TIME=$(date +%s.%N)

        # 5 msgs
        if [ $COUNT -lt 5 ]; then
            echo "msg $COUNT: $LINE"

            if [[ -n $PREVIOUS_TIME ]]; then
                INTERVAL=$(echo "$CURRENT_TIME - $PREVIOUS_TIME" | bc)
                echo "interval: ${INTERVAL}s"
            fi

            PREVIOUS_TIME=$CURRENT_TIME
            COUNT=$((COUNT + 1))
        else
            break
        fi
    done
} <$SERIAL_PORT
