otsdaq_prepmodernization  v2_05_02_indev
readDataExample.py
1 import socket
2 import struct
3 import time
4 import calendar
5 import sys
6 #import matplotlib.pyplot as plt
7 import pylab as plt
8 #IP and port for board.
9 UDP_IP = "192.168.30.93"
10 HOST_IP = "192.168.30.49"
11 UDP_PORT = 2001
12 READ_PORT = 49387
13 #Commands to send to the board.
14 oneByte = struct.pack("@2p", "\x02")[1:]
15 setBurst = "\x01\x09\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"
16 setBurst = struct.pack("@18p", setBurst)
17 setBurstOff = "\x01\x09\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
18 setBurstOff = struct.pack("@18p", setBurstOff)
19 setTrigAttr = "\x01\x00\x05\x00\x00\x00\x00\x00\x00\x96\x10\x00\x20\x00\x00\x00\x08"
20 setTrigAttr = struct.pack("@18p", setTrigAttr)
21 reset = "\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00"
22 reset = struct.pack("@18p", reset)
23 requestData = "\x01\x00\x02\x00\x00\x00\x00\x00\x00\x0F\x44\x00\x00\x00\x00\x00\x00"
24 requestData = struct.pack("@18p", requestData)
25 erase = "\x01\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
26 erase = struct.pack("@18p", erase)
27 #Commands to adjust clock delay
28 cdUp = "\x01\x00\x03\x00\x00\x00\x00\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00"
29 cdUp = struct.pack("@18p", cdUp)
30 cdDown = "\x01\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00"
31 cdDown = struct.pack("@18p", cdDown)
32 cdReset = "\x01\x00\x03\x00\x00\x00\x00\x00\x00\x10\x00\x00\x01\x00\x00\x00\x00"
33 cdReset = struct.pack("@18p", cdReset)
34 #Clock Reset Commands
35 dcmOne = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"
36 dcmOne = struct.pack("@18p", dcmOne)
37 dcmTwo = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00"
38 dcmTwo = struct.pack("@18p", dcmTwo)
39 dcmThree = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00"
40 dcmThree = struct.pack("@18p", dcmThree)
41 dcmClearOne = "\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
42 dcmClearOne = struct.pack("@18p", dcmClearOne)
43 dcmFour = "\x01\x00\x03\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00"
44 dcmFour = struct.pack("@18p", dcmFour)
45 dcmClearTwo = "\x01\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
46 dcmClearTwo = struct.pack("@18p", dcmClearTwo)
47 clockLockingDelay = 0.7;
48 Last = 0
49 
50 continousMode = False
51 firstTime = True
52 
53 def question (question, message):
54  global continousMode
55 
56  response = raw_input(question)
57  if response == "s" or response == "S":
58  print "Command skipped."
59  elif response == "E" or response == "e":
60  sys.exit(0)
61  elif response == "C" or response == "c":
62  continousMode = not continousMode
63  elif response == "U" or response == "u":
64  send(cdUp)
65  time.sleep(clockLockingDelay)
66  send(dcmFour)
67  time.sleep(clockLockingDelay)
68  send(dcmClearTwo)
69  time.sleep(clockLockingDelay)
70  elif response == "D" or response == "d":
71  send(cdDown)
72  time.sleep(clockLockingDelay)
73  send(dcmFour)
74  time.sleep(clockLockingDelay)
75  send(dcmClearTwo)
76  time.sleep(clockLockingDelay)
77  elif response == "R" or response == 'r':
78  send(cdReset)
79  else:
80  send(message)
81  print "Command Completed."
82 
83 def send (message):
84  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
85  #sock.settimeout(3) #A bit generous, but might prevent errors.
86  sock.bind(( HOST_IP, 49387))
87  sock.sendto(message, (UDP_IP, UDP_PORT))
88  print "Command Completed."
89  sock.close()
90 
91 def receiveData ():
92  global firstTime
93  global Last
94  data = []
95  formatted = []
96  formattedPlace = 0
97 
98  send(setBurst)
99 
100  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
101  #sock.settimeout(3) #A bit generous, but might prevent errors.
102  sock.bind(( HOST_IP, 49387))
103  m = sock.recvfrom(2048)
104 
105  ipAddress = m[1][0]
106  port = m[1][1]
107  length = len(m[0])
108  header = struct.unpack("<B",m[0][0])[0]
109  counter = struct.unpack("<B",m[0][1])[0]
110  print "Counter Number: " + str(counter)
111  print "Packet Length: " + str(length)
112  print "From port: " + str(port)
113  print "From IP address: " + str(ipAddress)
114  relData = m[0][2:]
115  d0 = struct.unpack("<Q",m[0][2:10])[0]
116  line = "%10s %5d %s %3s %3s %10s"%(ipAddress, port, length, hex(header), hex(counter), hex(d0))
117  #print line
118  if(length > 100):
119  #print "data valid"
120  #print len(data)
121 
122  dataPoints = 0;
123  #d = struct.unpack("<Q", data[x][0:8])[0]
124  done = True;
125  place = 0
126  while done:
127  d = struct.unpack("<B", relData[place])[0]
128  formatted.append(d)
129  place = place + 1
130  if place == len(relData):
131  dataPoints = (dataPoints + len(relData))
132  done = False;
133  #print d
134  print "Number of Data Points: " + str(dataPoints)
135  now = calendar.timegm(time.gmtime());
136  difference = now - Last
137  print "Time since last pulse: " + str(difference)
138  Last = now;
139 
140  plt.clf()
141  plt.ylim([0,255])
142  plt.plot(formatted)
143  plt.ylim([0,255])
144  plt.draw()
145  plt.pause(0.01)
146  sock.close()
147 
148 print "Creating Socket:"
149 print "UDP target IP:", UDP_IP
150 print "UDP target port:", UDP_PORT
151 
152 #question("Send one byte? ", oneByte)
153 #question("Lock clocks?", dcmOne)
154 send(oneByte)
155 send(dcmOne)
156 time.sleep(clockLockingDelay)
157 send(dcmTwo)
158 time.sleep(clockLockingDelay)
159 send(dcmThree)
160 time.sleep(clockLockingDelay)
161 send(dcmClearOne)
162 time.sleep(clockLockingDelay)
163 send(dcmFour)
164 time.sleep(clockLockingDelay)
165 send(dcmClearTwo)
166 time.sleep(clockLockingDelay)
167 
168 send(setBurst)
169 send(setTrigAttr)
170 
171 plt.ion()
172 
173 while True:
174  #question("Erase write block? ", erase)
175  if continousMode:
176  receiveData()
177  elif not continousMode:
178  question("Turn off burst mode? ", setBurstOff)
179  receiveData()
180 
181