Skip to content

Instantly share code, notes, and snippets.

@jrleeman
Created November 1, 2023 00:05
Show Gist options
  • Save jrleeman/62d4531a807269f82f740a71130a0df9 to your computer and use it in GitHub Desktop.
Save jrleeman/62d4531a807269f82f740a71130a0df9 to your computer and use it in GitHub Desktop.
MEMS Inclinometer Time Setting Script
import serial
import sys
from time import sleep
from datetime import datetime
def find_serial_ports():
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
:returns:
A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
connected_devices = find_serial_ports()
def connect_ports():
print('Serial Devices Connected\n---------------------------')
[print(l) for l in connected_devices]
print('---------------------------\n')
com_port = str(input('Type port to use: '))
if com_port in connected_devices:
return com_port
else:
connect_ports()
print('')
print('MEMS Inclinometer Time Set Utility')
print('Connect the inclinometer to the computer now.')
print('')
use_time = input('Enter U for UTC time or L for local time: ').upper()
if use_time == 'U':
use_utc = True
elif use_time == 'L':
use_utc = False
else:
print('Invalid Entry!')
sys.exit()
baud = int(input('Baud Rate: '))
print('')
instrument_com_port = connect_ports()
inst_ser = serial.Serial(instrument_com_port, baud, timeout=15)
sleep(2)
if use_utc:
time = datetime.utcnow()
else:
time = datetime.now()
time_str = f'ST {time.year} {time.month:02} {time.day:02} {time.hour:02} {time.minute:02} {time.second:02}\r\n'.encode()
inst_ser.write(time_str)
sleep(1)
print(f'Complete {time}')
inst_ser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment