-
Notifications
You must be signed in to change notification settings - Fork 0
/
resetgem
executable file
·78 lines (68 loc) · 2 KB
/
resetgem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python3
import serial
import time
import sys
from datetime import datetime, timezone
# Configure the serial port
ser = serial.Serial(
port=sys.argv[1],
baudrate=115200,
timeout=0
)
# Function to send data
def send_data(data):
ser.write(data.encode())
print(f"Sent [{len(data)}]: {data}")
# Function to read data
def read_data(min=None):
crlf = False
data = b''
data_l = 0
while not crlf:
while ser.in_waiting > 0:
line_data = ser.read(ser.in_waiting)
line_data_l = len(line_data)
assert (line_data_l != 0), "Expected data to be read with ser.in_waiting > 0"
data += line_data
data_l += line_data_l
if (min is not None) and (data_l >= min):
break
crlf = (data[-2:] == b'\r\n')
if not crlf:
time.sleep(0.1)
return data.decode('utf-8')
# Main loop
try:
# Turn off real-time packets for the duration
send_data('^^^SYSOFF')
data = read_data(min=3)
print(f"Received [{len(data)}]: {data!r}")
# Set the GEM to use "BIN48-NET-TIME" (4)
send_data('^^^SYSPKT04')
data = read_data()
print(f"Received [{len(data)}]: {data!r}")
# Reset the counters
send_data('^^^SYSRSTH')
data = read_data(min=4)
print(f"Received [{len(data)}]: {data!r}")
send_data('^^^SYSRSTP')
data = read_data(min=4)
print(f"Received [{len(data)}]: {data!r}")
send_data('^^^SYSRSTA')
data = read_data(min=4)
print(f"Received [{len(data)}]: {data!r}")
send_data('^^^SYSRSTS')
data = read_data(min=4)
print(f"Received [{len(data)}]: {data!r}")
ct = datetime.now(timezone.utc)
time_s = ct.strftime("%y,%m,%d,%H,%M,%S")
send_data(f"^^^SYSDTM{time_s}\r")
data = read_data(min=3)
print(f"Received [{len(data)}]: {data!r}")
send_data('^^^SYS_ON')
data = read_data(min=3)
print(f"Received [{len(data)}]: {data!r}")
finally:
# Close the serial port
ser.close()
print("Serial port closed.")