-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient2.py
More file actions
63 lines (48 loc) · 1.47 KB
/
client2.py
File metadata and controls
63 lines (48 loc) · 1.47 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
import time
import numpy as np
import serial
def send_matrix(ser, matrix):
for value in matrix.flatten():
ser.write(bytes([value + 128]))
def receive_matrix(ser, size):
matrix = np.zeros((size, size), dtype=np.int16)
for i in range(size):
for j in range(size):
value_str = ser.readline().decode().strip()
matrix[i, j] = int(value_str)
return matrix
ser = serial.Serial("/dev/tty.usbmodem2101", 9600, timeout=5)
time.sleep(2)
print("Connected...")
while True:
size = int(input("Matrix size (1-16, or 0 to quit): "))
if size == 0:
break
if size < 1 or size > 16:
print("Invalid size. Please enter a size between 1 and 16.")
continue
A = np.random.randint(-128, 127, (size, size), dtype=np.int8)
B = np.random.randint(-128, 127, (size, size), dtype=np.int8)
print("Matrix A:")
print(A)
print("Matrix B:")
print(B)
# Send size and matrices
ser.write(bytes([size]))
send_matrix(ser, A)
send_matrix(ser, B)
C = receive_matrix(ser, size)
print("Result Matrix C:")
print(C)
expected = np.matmul(A.astype(np.int32), B.astype(np.int32)).astype(np.int16)
print(f"Expected Matrix C: {expected}")
if np.array_equal(C, expected):
print("Result passed.")
else:
print("Result failed")
print("Expected:")
print(expected)
print("Difference:")
print(C - expected)
ser.close()
print("Connection closed.")