-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVSCode_SaveExcel.cpp
More file actions
54 lines (45 loc) · 2.02 KB
/
Copy pathVSCode_SaveExcel.cpp
File metadata and controls
54 lines (45 loc) · 2.02 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
import serial
import csv
from datetime import datetime
import os
ser = serial.Serial('COM5', 9600)
file_path = 'attendance_log.csv'
file_exists = os.path.exists(file_path)
scanned_uids = set()
# Load existing UIDs
if file_exists:
with open(file_path, mode='r', newline='') as file:
reader = csv.reader(file)
next(reader, None) # skip header
for row in reader:
if len(row) >= 1:
scanned_uids.add(row[0].strip())
# Start listening and open file in append mode
print("Listening for data... Press Ctrl+C to stop.")
try:
with open(file_path, mode='a', newline='') as file:
writer = csv.writer(file)
if not file_exists:
writer.writerow(['UID', 'Name', 'DateTime', 'Status'])
while True:
line = ser.readline().decode().strip()
print(f"Received line: {line}")
if line.startswith("Log:"):
parts = line[4:].split(',')
print(f"Parsed parts: {parts}")
if len(parts) == 4:
uid, name, timestamp, status = [p.strip() for p in parts]
if uid in scanned_uids:
current_time = datetime.now().strftime('%Y-%m-%d %I:%M:%S %p')
print(f"Already Present - Name: {name} - UID: {uid} - Time: {current_time}")
else:
scanned_uids.add(uid)
if timestamp == "UNKNOWN" or timestamp == "":
timestamp = datetime.now().strftime('%Y-%m-%d %I:%M:%S %p')
status = "Present"
print(f"Logging Attendance: UID={uid}, Name={name}, Status={status}, Time={timestamp}")
writer.writerow([uid, name, timestamp, status])
file.flush()
os.fsync(file.fileno()) # Ensure data is written to disk
except KeyboardInterrupt:
print("Stopped by user.") ("Stopped by user.")