init_serial.py 1.47 KB
Newer Older
1 2
import serial
import time
3
from datetime import datetime
4

5
from app.main import Meteorologger
6

7
print("\nAttempting serial connection ...\n")
8
ser = Meteorologger().get_serial()
9 10
print(ser)
ser.flush()
11

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

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
def sync_rtc():
    '''
    Synchronizes the board clock with the system's.
    '''
    dt = datetime.now()
    ser.write(bytes('setRTC,{Y},{m},{d},{H},{M},{S}'.format(
        Y=dt.year, m=dt.month, d=dt.day, H=dt.hour, M=dt.minute, S=dt.second),
        encoding='utf-8'))
    try:
        raw = ser.readall()
        return raw.decode('ascii').strip()
    except:
        print("Unable to decode raw response to ASCII:\n{}".format(raw))

print("\nSent 'help'... waiting for board response ...")
39
response = send('help')
40
print("\nBoard commands:\n" + '-'*40 + '\n' + response + '\n' + '-'*40)
41 42 43 44 45 46
print(
"""\
You are now inside the Python interpreter! The commands above
must be passed as a string to the send() function!

Examples:
47 48 49 50 51
    >>> send('read,t,l')                   # for reading temp. and lum.
    >>> send('setrtc,2015,7,15,17,45,0')   # for setting the datetime on RTC

Hit Ctrl+D to exit!""")
print('-'*40 + '\n')
52