-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_image_transfer.py
More file actions
91 lines (75 loc) · 2.56 KB
/
basic_image_transfer.py
File metadata and controls
91 lines (75 loc) · 2.56 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
"""
Basic Image Transfer Example
This example demonstrates sending and receiving images between
PC and Device via UART using PyDIPLink.
"""
import serial
import time
from pydiplink import (
send_image_data,
capture_image_and_show,
IMAGE_FORMAT_RGB888,
IMAGE_FORMAT_GRAYSCALE,
)
def main():
# Configure serial port
PORT = "/dev/ttyUSB0" # Change to your port (COM3 on Windows)
BAUD = 500000
print(f"Connecting to {PORT} at {BAUD} baud...")
try:
ser = serial.Serial(PORT, BAUD, timeout=5)
print("Connected successfully!")
# Example 1: Send RGB888 image to device
print("\n=== Example 1: Send RGB888 Image ===")
try:
send_image_data(
ser,
image_path="test_image.jpg", # Your image path
width=320,
height=240,
image_format=IMAGE_FORMAT_RGB888,
)
print("Image sent successfully!")
except FileNotFoundError as e:
print(f"Error: {e}")
print("Please provide a valid image path")
time.sleep(1)
# Example 2: Send grayscale image
print("\n=== Example 2: Send Grayscale Image ===")
try:
send_image_data(
ser,
image_path="test_image.jpg",
width=160,
height=120,
image_format=IMAGE_FORMAT_GRAYSCALE,
)
print("Grayscale image sent successfully!")
except FileNotFoundError as e:
print(f"Error: {e}")
# Example 3: Receive image from device
print("\n=== Example 3: Receive Image from Device ===")
print("Waiting for device to send image...")
print("(Device should send STW command)")
try:
capture_image_and_show(ser, save_dir="received_images")
print("Image received and saved!")
except TimeoutError as e:
print(f"Timeout: {e}")
print("Make sure your device is sending data")
except serial.SerialException as e:
print(f"Serial port error: {e}")
print("\nTroubleshooting:")
print("- Check that device is connected")
print("- Verify correct port name")
print("- On Linux, add user to dialout group:")
print(" sudo usermod -a -G dialout $USER")
except KeyboardInterrupt:
print("\nInterrupted by user")
finally:
if 'ser' in locals():
ser.close()
print("\nSerial port closed")
if __name__ == "__main__":
main()