-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendtime.py
More file actions
70 lines (56 loc) · 2.13 KB
/
sendtime.py
File metadata and controls
70 lines (56 loc) · 2.13 KB
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
import serial
import time
from datetime import datetime
# Open serial port
ser = serial.Serial('COM10', 9600, timeout=1)
time.sleep(2) # Wait for serial connection to establish
print("=== FPGA Clock/Date System ===")
print("Waiting for button press from FPGA...")
print("Button 0 (Center): Request Time")
print("Button 1 (Up): Request Date")
print("Press Ctrl+C to exit\n")
try:
while True:
# Check if data is available from FPGA
if ser.in_waiting > 0:
# Read the button option sent by FPGA
data = ser.read(1)
opt = data[0]
print(data)
# Get current time and date
now = datetime.now()
hour = now.hour
minute = now.minute
second = now.second
date = now.day
month = now.month
year = now.year % 100
if opt == 0:
# Send time data as binary bytes
print(f"[Time Request] Sending: {hour:02d}:{minute:02d}:{second:02d}")
ser.write(bytes([hour]))
time.sleep(0.05)
ser.write(bytes([minute]))
time.sleep(0.05)
ser.write(bytes([second]))
time.sleep(0.05)
elif opt == 1:
# Send date data as binary bytes
print(f"[Date Request] Sending: {date:02d}/{month:02d}/{year:02d}")
ser.write(bytes([date]))
time.sleep(0.05)
ser.write(bytes([month]))
time.sleep(0.05)
ser.write(bytes([year]))
time.sleep(0.05)
else:
print(f"[Warning] Unknown option received: {opt}")
# Small delay to prevent CPU overuse
time.sleep(0.1)
except KeyboardInterrupt:
print("\n\nClosing serial port...")
ser.close()
print("Done.")
except Exception as e:
print(f"\nError: {e}")
ser.close()