init_serial.py 974 Bytes
Newer Older
1 2 3
import serial
import time

4 5 6
from app import Meteorologger

ser = Meteorologger().get_serial()
7
time.sleep(2) # waiting for board reset
8

9
def send(command_str, response_wait=0):
10 11 12
    '''
    Send the string 'command_str' to the serial port and return the response.
    '''
13
    ser.write(bytes(command_str, encoding='utf-8'))
14
    time.sleep(response_wait)
15
    try:
16
        raw = ser.readall()
17 18 19
        return raw.decode('ascii').strip()
    except:
        print("Unable do decode raw line read:\n{}".format(raw))
20

21 22 23 24 25 26 27 28 29 30 31 32 33
print("\nSending 'help' to the serial... waiting for board response ...")
response = send('help')
print("\nCommands available:\n" + '-'*40 + '\n' + response + '\n' + '-'*40)
print(
"""\
You are now inside the Python interpreter! The commands above
must be passed as a string to the send() function!

Examples:
    >>> send('read,t,l')    # to read both temperature and luminosity
    >>> send('setrtc,2015,7,15,17,45,0')  # set the real time clock, if present
""")