-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.py
More file actions
53 lines (39 loc) · 1.05 KB
/
Copy pathsend.py
File metadata and controls
53 lines (39 loc) · 1.05 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
import serial
import time
PORT = "COM6"
BAUD = 9600 # make sure this matches your BSP
# Load raw 784-byte image
data = bytearray(open("img_28x28.bin", "rb").read())
assert len(data) == 784
print("BEFORE INVERT:")
print("len", len(data),
"min", min(data),
"max", max(data),
"avg", sum(data)/len(data))
# Invert for MNIST polarity (white digit on black background)
for i in range(784):
data[i] = 255 - data[i]
print("\nAFTER INVERT:")
print("len", len(data),
"min", min(data),
"max", max(data),
"avg", sum(data)/len(data))
# Open serial
ser = serial.Serial(PORT, BAUD, timeout=0.2)
time.sleep(0.5)
print("\nSending 784 bytes (inverted)...")
ser.write(data)
print("Reading for 5 seconds...")
end = time.time() + 5.0
buf = bytearray()
while time.time() < end:
chunk = ser.read(256)
if chunk:
buf.extend(chunk)
ser.close()
print("\n--- RAW BYTES (hex) ---")
print(buf.hex())
print("\n--- DECODED (best effort) ---")
print(buf.decode(errors="replace"))
print("\n--- STATS ---")
print("Bytes received:", len(buf))