#!/usr/bin/python3
# ===================================================================
# Output
# ===================================================================
# From: www.youtube.com/watch?v=WV4U51TlRaQ
#       Arduino and Python Serial Communication with PySerial Part 2
#       - Adding User Input
# ===================================================================
# An arduino you can use baud rates 300, 600, 1200, 2400, 4800, 9600,
# 14400, 19200, 28800, 38400, 57600, or 115200. However 9600 is the
# standard baud rate usually used.
# ===================================================================
# ---- Establish a specific port and the connect
# ---- ser = serial.Serial('COM7',19200)
# ---- ser = serial.Serial('COM7', 115200, timeout=.1)
#ser = serial.Serial()          # create port
#ser.port     = 'COM7'
#ser.baudrate = 9600
#ser.timeout  = 0.1
#ser.open()                     # open connection
# ===================================================================

from time import sleep

import serial

import sys


# --------------------------------------------------------------------
# functions
# --------------------------------------------------------------------

def getValues(ser):
    ser.write(b'g')
    arduinoData = ser.readline().decode().strip()
    return arduinoData

def RunningPython3():
    ##print(sys.version_info)
    if sys.version_info[0] == 3:
        return True
    return False

def GetUserInput(prompt,py3):
    if py3:
        return input(prompt)
    else:
        return raw_input(prompt)

def Pause(py3):
    print('')
    GetUserInput('Press enter to continue ',py3)


# -------------------------------------------------------------------
# main
# -------------------------------------------------------------------

py3 = RunningPython3()

ser = serial.Serial('COM7',9600)    # automatically connect to port

print('Start Test')

while True:

    val = GetUserInput('Get data point? ',py3)

    sval = val.strip()

    if sval == '':
        break

    if sval == 'y' or sval == 'Y':
       arduinoData = getValues(ser)
       print('data: {}'.format(arduinoData))
       print('type: {}'.format(type(arduinoData)))
       print('Len : {}'.format(len(arduinoData)))

ser.close()