Blame view

pm100d_waist.py 4.91 KB
d0c0cc957   mer0m   Add files via upload
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  #!/usr/bin/env python
  
  import argparse, time, os
  import matplotlib.pyplot as plt
  
  #==============================================================================
  
  # Path
  PATH = os.getcwd()
  # File footer
  OFILENAME = '130'
  
  #==============================================================================
  
  def parse():
      """
      Specific parsing procedure for transfering data from Thorlabs PM100D.
      :returns: populated namespace (parser)
      """
      parser = argparse.ArgumentParser(description = 'Acquire data from Thorlabs PM100D',
                                       epilog = 'Example: \'./pm100d.py -st 10 -o toto\' logs PM100D every 10 seconds to output file YYYYMMDD-HHMMSS-toto.dat')
  
      parser.add_argument('-p',
                          action='store',
                          dest='path',
                          default=PATH,
                          help='Absolute path (default '+PATH+')')
  
      parser.add_argument('-o',
                          action='store',
                          dest='ofile',
                          default=OFILENAME,
                          help='Output data filename (default '+OFILENAME+')')
  
      args = parser.parse_args()
      return args
  
  #==============================================================================
  
  class usbtmc:
      def __init__(self, device):
          self.device = device
          self.FILE = os.open(device, os.O_RDWR)
  
      def write(self, command):
          os.write(self.FILE, command);
  
      def read(self, length = 4000):
          return os.read(self.FILE, length)
  
      def getName(self):
          self.write("*IDN?")
          return self.read(300)
  
      def sendReset(self):
          self.write("*RST")
  
  #==============================================================================
  
  class instrument:
      """Class to control a SCPI compatible instrument"""
      def __init__(self, device):
          print 'Connecting to device %s...' %device
          self.meas = usbtmc(device)
          self.name = self.meas.getName()
          print self.name
          print '  --> Ok'
  
      def write(self, command):
          """Send an arbitrary command directly to the scope"""
          self.meas.write(command)
  
      def read(self, command):
          """Read an arbitrary amount of data directly from the scope"""
          return self.meas.read(command)
  
      def reset(self):
          """Reset the instrument"""
          self.meas.sendReset()
  
      def value(self):
          self.write('MEAS?')
          return self.read(300)
  
  #==============================================================================
  
  def acqu_PM100D(instrument, path, ofile):
  
      t0 = time.time()
      filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + ofile + '.dat'
      print('Opening %s' %filename)
      data_file = open(filename, 'wr', 0)
      dx = 0.
  
      xmes = []
      Pmes = []
      plt.ion()
      fig = plt.figure()
      ax1 = fig.add_subplot(111)
      line1, = ax1.plot(xmes, Pmes, 'o')
  
      # Infinite loop
      while True:
          try:
              keypressed = raw_input("Press Enter to continue...
  ")
  
              if keypressed=='':
                  # Power values
                  sensors_values = instrument.value()
                  sensors_values = sensors_values.replace('E', 'e')
                  string = "%f\t%s" % (dx , sensors_values)
                  data_file.write(string) # Write in a file
                  print(string)
  
                  xmes.append(dx)
                  Pmes.append(float(sensors_values))
  
                  dx = dx + 0.05
  
              elif keypressed.isdigit():
                  print('Closing %s' %filename)
                  data_file.close()
                  t0 = time.time()
                  filename = time.strftime("%Y%m%d-%H%M%S", time.gmtime(t0)) + '-' + keypressed + '.dat'
                  print('Opening %s' %filename)
                  data_file = open(filename, 'wr', 0)
                  dx = 0.
  
                  xmes = []
                  Pmes = []
                  line1, = ax1.plot(xmes, Pmes, 'o')
  
              else:
                  raise KeyboardInterrupt
  
  
              line1.set_data(xmes, Pmes)
              ax1.relim()
              ax1.autoscale_view()
              fig.canvas.draw()
  
          except Exception as ex:
              print 'Exception during controler data reading: ' + str(ex)
  
          except KeyboardInterrupt:
              print '
    --> Disconnected'
              data_file.close()
  
              # To stop the loop in a clean way
              break
  
  #==============================================================================
  
  def main():
      """
      Main script
      """
      # Parse command line
      args = parse()
      # path
      path = args.path
      # Data output filename
      ofile = args.ofile
  
      try:
          pm100 = instrument("/dev/usbtmc0")
  
          # acquisition with 2 sec. timeout
          acqu_PM100D(pm100, path, ofile)
  
      except Exception as ex:
              print 'Oups '+str(ex)
      print 'Program ending
  '
  
  #==============================================================================
  
  if __name__ == "__main__":
      main()