Browse Source

Create OscScreenGrabLAN.py

RoGeorge 9 years ago
parent
commit
f25624a4be
1 changed files with 170 additions and 0 deletions
  1. 170
    0
      OscScreenGrabLAN.py

+ 170
- 0
OscScreenGrabLAN.py View File

@@ -0,0 +1,170 @@
1
+__author__ = 'RoGeorge'
2
+#
3
+# TODO: Port for Linux
4
+# TODO: Add command line parameters for IP, file path and file format
5
+# TODO: Add GUI
6
+# TODO: Add browse and custom filename selection
7
+# TODO: Create executable distributions
8
+# TODO: Use git, upload to GitHub
9
+#
10
+import telnetlib_receive_all
11
+import time
12
+import Image
13
+import StringIO
14
+import sys
15
+import os
16
+
17
+# Update the next lines for your own default settings:
18
+path_to_save = ""
19
+save_format = "PNG"
20
+IP_DS1104Z = "192.168.1.3"
21
+
22
+# Port used by Rigol LXI protocol
23
+port = 5555
24
+
25
+# Check parameters
26
+script_name = os.path.basename(sys.argv[0])
27
+
28
+# Print usage
29
+print
30
+print "Usage:"
31
+print "    " + script_name + " [oscilloscope_IP [save_path [PNG | BMP]]]"
32
+print
33
+print "Usage examples:"
34
+print "    " + script_name
35
+print "    " + script_name + " 192.168.1.3"
36
+print "    " + script_name + " 192.168.1.3 my_place_for_osc_captures"
37
+print "    " + script_name + " 192.168.1.3 my_place_for_osc_captures BMP"
38
+print
39
+print "This program capture the image displayed"
40
+print "    by a Rigol DS1000Z series oscilloscope, then save it on the computer"
41
+print "    as a PNG or BMP file with a timestamp in the file name."
42
+print
43
+print "    The program is using LXI protocol, so the computer"
44
+print "    must have LAN connection with the oscilloscope."
45
+print "    USB and/or GPIB connections are not used by this software."
46
+print
47
+print "    No VISA, IVI or Rigol drivers are needed."
48
+print
49
+
50
+# Create/check if 'path' exists
51
+
52
+
53
+# Check network response (ping)
54
+response = os.system("ping -n 1 " + IP_DS1104Z + " > nul")
55
+if response != 0:
56
+	print
57
+	print "No response pinging " + IP_DS1104Z
58
+	print "Check network cables and settings."
59
+	print "You should be able to ping the oscilloscope."
60
+
61
+# Open a modified telnet session
62
+# The default telnetlib drops 0x00 characters,
63
+#   so a modified library 'telnetlib_receive_all' is used instead
64
+tn = telnetlib_receive_all.Telnet(IP_DS1104Z, port)
65
+tn.write("*idn?")                       # ask for instrument ID
66
+instrument_id = tn.read_until("\n", 1)
67
+
68
+# Check if instrument is set to accept LAN commands
69
+if instrument_id == "command error":
70
+	print instrument_id
71
+	print "Check the oscilloscope settings."
72
+	print "Utility -> IO Setting -> RemoteIO -> LAN must be ON"
73
+	sys.exit("ERROR")
74
+
75
+# Check if instrument is indeed a Rigol DS1000Z series
76
+id_fields = instrument_id.split(",")
77
+if (id_fields[0] != "RIGOL TECHNOLOGIES") or \
78
+	((id_fields[1][:3] != "DS1") and (id_fields[1][-1] != "Z")):
79
+	print
80
+	print "ERROR: No Rigol from series DS1000Z found at ", IP_DS1104Z
81
+	sys.exit("ERROR")
82
+
83
+print "Instrument ID:"
84
+print instrument_id
85
+
86
+# Prepare filename as C:\MODEL_SERIAL_YYYY-MM-DD_HH.MM.SS
87
+timestamp = time.strftime("%Y-%m-%d_%H.%M.%S", time.localtime())
88
+filename = path_to_save + id_fields[1] + "_" + id_fields[2] + "_" + timestamp
89
+
90
+# Ask for an oscilloscope display print screen
91
+tn.write("display:data?")
92
+print "Receiving..."
93
+buff = tn.read_until("\n", 10)
94
+
95
+# Just in case the transfer did not complete in 10 seconds
96
+while len(buff) < 1152068:
97
+	tmp = tn.read_until("\n", 1)
98
+	if len(tmp) == 0:
99
+		break
100
+	buff += tmp
101
+
102
+# Strip TMC Blockheader and last 3 bytes
103
+buff = buff[11:-3]
104
+
105
+# Save as PNG
106
+im = Image.open(StringIO.StringIO(buff))
107
+im.save(filename + ".png", "PNG")
108
+print "Saved file:", filename + ".png"
109
+
110
+# Save as BMP
111
+# scr_file = open(filename + ".bmp", "wb")
112
+# scr_file.write(buff)
113
+# scr_file.close()
114
+
115
+tn.close()
116
+
117
+
118
+# The code after this line was only for debugging purposes.
119
+# It prints the BMP header params, which are always the same.
120
+#
121
+# def print_hex(prt_buff):
122
+# 	print "(",
123
+# 	for c in prt_buff:
124
+# 		print hex(ord(c)),
125
+# 	print ")"
126
+#
127
+#
128
+# def print_dec(prt_buff):
129
+# 	n = 0
130
+# 	for c in reversed(prt_buff):
131
+# 		n = 256 * n + ord(c)
132
+# 	print n,
133
+#
134
+#
135
+# def prt_dec_hex(prt_buff):
136
+# 	print_dec(prt_buff)
137
+# 	print_hex(prt_buff)
138
+#
139
+#
140
+# def print_next_as(description, nr_of_bytes):
141
+# 	print description + " " * (50 - len(description)),
142
+# 	prt_dec_hex(buff[print_next_as.offset:print_next_as.offset+nr_of_bytes])
143
+# 	print_next_as.offset += nr_of_bytes
144
+# print_next_as.offset = 2
145
+#
146
+#
147
+# print
148
+# print "BMP header"
149
+# print "----------"
150
+# print "Header type:                                      ", buff[0:2],
151
+# print_hex(buff[0:2])
152
+# print_next_as("BMP size in bytes:", 4)
153
+# print_next_as("reserved 2 bytes:", 2)
154
+# print_next_as("reserved 2 bytes:", 2)
155
+# print_next_as("Offset for BMP start of pixels array:", 4)
156
+# print
157
+# print "DIB header"
158
+# print "----------"
159
+# print_next_as("Number of DIB header bytes:", 4)
160
+# print_next_as("Image width (in pixels):", 4)
161
+# print_next_as("Image height (in pixels):", 4)
162
+# print_next_as("Number of color planes:", 4)
163
+# print_next_as("Number of bits per pixel:", 2)
164
+# print_next_as("Compression type used:", 4)
165
+# print_next_as("Size of the raw bitmap data (including padding):", 4)
166
+# print_next_as("Horizontal print resolution (pixels/meter):", 4)
167
+# print_next_as("Vertical print resolution (pixels/meter):", 4)
168
+# print_next_as("Number of colors in palette:", 4)
169
+# print_next_as("Important colors (0 means all):", 4)
170
+# print_next_as("First pixel (BGR):", 3)

Loading…
Cancel
Save