-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathread_binary.py
75 lines (61 loc) · 2.4 KB
/
read_binary.py
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
import sys
import time
import serial
def get_file(serial_port, baud_rate, file_path, total_bytes, block_size):
try:
# Open serial port
ser = serial.Serial(serial_port, baud_rate)
time.sleep(4)
ser.write(b'\xAA') # Command byte
# Send block size as a byte
ser.write(b'\x01') # Command
ser.write(bytes([block_size])) # Block size
ser.write(b'\x00') #LSB
ser.write(b'\x00') #MSB
stoppage = (total_bytes >> 8) & 0xFF
ser.write(bytes([stoppage])) # Stoppage
start_time = time.time()
# Initialize a buffer to store received data
buffer = bytearray()
try:
bytes_received = 0
while True:
if (ser.inWaiting() > 0):
# Read one byte with timeout
byte = ser.read(1)
# Check if start of frame is received
if byte == b'\xAA':
#print(".", end="")
# Read specified block size bytes
frame = ser.read(block_size)
buffer.extend(frame)
bytes_received += len(frame) # Update total bytes received
if bytes_received >= total_bytes:
break
end_time = time.time()
# Calculate total duration
total_duration = end_time - start_time
print(f"File received in {total_duration:.2f} seconds")
except KeyboardInterrupt:
# Handle Ctrl+C gracefully
print("\nSerial reading stopped by user at ")
print(bytes_received)
# Write buffer to file
with open(file_path, 'wb') as f:
f.write(buffer)
finally:
# Close the serial port
ser.close()
if __name__ == "__main__":
# Check if correct number of command line arguments is provided
if len(sys.argv) != 6:
print("Usage: python read_binary.py <serial_port> <baud_rate> <file_path> <total_bytes> <block_size>")
sys.exit(1)
# Extract command line arguments
serial_port = sys.argv[1]
baud_rate = int(sys.argv[2])
file_path = sys.argv[3]
total_bytes = int(sys.argv[4])
block_size = int(sys.argv[5])
# Call the function to send the file
get_file(serial_port, baud_rate, file_path, total_bytes, block_size)