-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTDS3000ToFile.py
125 lines (103 loc) · 3.56 KB
/
TDS3000ToFile.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#-------------------------------------------------------------------------------
# Name: TDS3000ToFile
# Save Hardcopy to computer using PyVisa for TDS3000 series oscilloscope
#
# Created: 2016-02-27
#
# Development Environment: Python 2.7 PyVisa 1.8, OS X 10.11.3
#
# Compatible Instruments: TDS3000 series
#
# Compatible Interfaces: Ethernet
#
#-------------------------------------------------------------------------------
import time, warnings # std visa libraries
import visa # https://pyvisa.readthedocs.org/en/stable/
import sys, getopt
# convenience method to convert an array of bytes to an int
def from_bytes (data, big_endian = False):
if isinstance(data, str):
data = bytearray(data)
if big_endian:
data = reversed(data)
num = 0
for offset, byte in enumerate(data):
num += byte << (offset * 8)
return num
# Some definitions
vc = visa.constants # Alias for easier reading
# Modify the following lines to configure this script for your instrument
#==============================================
visaResourceAddr = 'TDS3054'
fileSaveLocation = r'/Users/knud/foo'
bufferSize = 1024
#==============================================
def captureToFile (address, outputFileName):
# Open session to instrument
rm = visa.ResourceManager()
lib = rm.visalib
scope = rm.open_resource("TCPIP::"+address+"::inst0::INSTR")
scope.timeout = 10000
startDelaySec = 1
# Some settings depend on which interface is being used
interface = lib.get_attribute(scope.session, vc.VI_ATTR_INTF_TYPE)[0]
port = 'GPIB'
print(scope.query("*IDN?"))
scope.write("HARDCOPY:FORMAT BMPColor")
scope.write("HARDCOPY:INKSAVER OFF")
scope.write("HARDCOPY:LAYOUT PORTRait")
scope.write("HARDCOPY:PALETTE NORMAL")
scope.write("HARDCOPY:PORT " + port)
scope.write("HARDCOPY START")
print("Starting Transfer in: ")
for x in range(0, startDelaySec):
print(str.format("{0}...", startDelaySec - x))
time.sleep(1)
print("Now!")
# Read the BMP header bytes and extract the file size
warnings.filterwarnings("ignore", category=Warning) #The read will produce a VI_SUCCESS_MAX_CNT warning so suppress
imgBytes = lib.read(scope.session, 14)[0]
lengthBytes = imgBytes[2:6]
fileSize = from_bytes(lengthBytes, False)
print(str.format("file size is {0}", fileSize))
bytesLeft = fileSize - 14
# Read the rest of the image
while bytesLeft > 0:
imgBytes = imgBytes + lib.read(scope.session, bufferSize)[0]
bytesLeft = bytesLeft - bufferSize
if bytesLeft < bufferSize:
imgBytes = imgBytes + lib.read(scope.session, bytesLeft)[0]
bytesLeft = 0
# Save the bytes to a file
imgFile = open(outputFileName+'.bmp', "wb")
imgFile.write(imgBytes)
imgFile.close()
print("Transfer Complete!")
print("Image saved to " + outputFileName+'.bmp')
scope.close()
rm.close()
def main(argv):
inputfile = ''
outputfile = ''
if len(sys.argv) != 5:
print 'TDS3000ToFile.py -e <instrument ipv4 address> -o <outputfile>'
print 'Not enough arguments'
sys.exit(2)
try:
opts, args = getopt.getopt(argv,"he:o:",["ipv4Addr=","ofile="])
except getopt.GetoptError:
print 'TDS3000ToFile.py -e <instrument ipv4 address> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -e <instrument ipv4 address> -o <outputfile>'
sys.exit()
elif opt in ("-e", "--ipv4Addr"):
address = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Instrument address is ', address
print 'Output file is ', outputfile
captureToFile(address, outputfile)
if __name__ == "__main__":
main(sys.argv[1:])