source: trigger/CommunicateWithVME.py@ 154

Last change on this file since 154 was 149, checked in by daqct3, 15 years ago
Changes to two .py files
File size: 21.4 KB
Line 
1#!/usr/bin/python
2##############################################################
3#
4# CommunicateWithVME.py
5# Handles the communication with the VME crate
6# Sends commands to the VME crate
7#
8#
9# Michael Rissi 05/2009
10#
11#############################################################
12
13import GlobalVariables
14import threading
15import _VME
16import time
17import datetime
18
19def GetStatus():
20 print "Trigger thresholds: ", GlobalVariables.TriggerThresholds
21 print "Hex Pattern: ", GlobalVariables.HexPattern
22 print "Majority Level: ",GlobalVariables.MajorityLevel
23
24
25
26
27
28def GetRate(module,channel):
29 _VME.V560_Clear_Scales( module)
30 beginCounter=_VME. V560_Read_Counter( module, channel)
31 GlobalVariables.ServerMessage = "Evaluating rates (2 seconds)..."
32 time.sleep(2)
33 endCounter = _VME. V560_Read_Counter( module, channel)
34 return (endCounter-beginCounter)/2000.
35
36
37
38def GetRates(module):
39 _VME.V560_Clear_Scales( module)
40 beginCounter =[0,0,0,0,
41 0,0,0,0,
42 0,0,0,0,
43 0,0,0,0]
44 endCounter = [0,0,0,0,
45 0,0,0,0,
46 0,0,0,0,
47 0,0,0,0]
48 rates = ["rates",
49 0,0,0,0,
50 0,0,0,0,
51 0,0,0,0,
52 0,0,0,0]
53
54 for i in range(0,16):
55 beginCounter[i]=_VME. V560_Read_Counter( module, i)
56 #GlobalVariables.ServerMessage = str(beginCounter[i])
57 #GlobalVariables.ServerMessage = "Evaluating rates..."
58 time.sleep(2)
59 for i in range(0,16):
60 endCounter[i] = _VME. V560_Read_Counter( module, i)
61 #GlobalVariables.ServerMessage = str(endCounter[i])
62 rates[i+1] =(endCounter[i]-beginCounter[i])/2000.
63 return rates
64
65
66class GimmeRates(threading.Thread):
67 rates=["rates",
68 0,0,0,0,
69 0,0,0,0,
70 0,0,0,0,
71 0,0,0,0]
72 module = 1
73 def run(self):
74 oldcommand=GlobalVariables.UserInput
75 while(GlobalVariables.UserInput[1:]!="exit" and GlobalVariables.UserInput[1:] != "EXIT"):
76 time.sleep(0.001)
77 newrates = GetRates(self.module)
78 self.rates=newrates
79
80
81
82
83class WriteOutput(threading.Thread): #thread that writes the rates and threshold to a file.
84 rates=["rates",
85 0,0,0,0,
86 0,0,0,0,
87 0,0,0,0,
88 0,0,0,0]
89 interval = 2
90 filename = "test.txt"
91 #gimmeRates =GimmeRates()
92 gimmeRates=GimmeRates()
93 #File=open(filename,'a')
94 def __init__(self, filename="test.txt", interval = 10):
95 threading.Thread.__init__(self)
96 #self.OpenNewFile(filename)
97
98
99
100 def writeToFile(self):
101 File = open(self.filename,'a')
102 #print "writing", (GlobalVariables.Rates)
103 #year = datetime.date.today().year
104 #month = datetime.date.today().month
105 #day = datetime.date.today().day
106 year = time.localtime(time.time())[0]
107 month =time.localtime(time.time())[1]
108 day = time.localtime(time.time())[2]
109 hour = time.localtime(time.time())[3]
110 minute = time.localtime(time.time())[4]
111 seconds = time.localtime(time.time())[5]
112 ms = time.localtime(time.time())[6]
113 #date=str(year)
114 #if (hour>=12):
115 # day = day+1
116
117 t = datetime.datetime.now()
118 EpochSeconds=int(time.mktime(t.timetuple()))
119
120 outstring = "Trigger " + " ThreshRate "+str(year) + " " + str(month) +" " + str(day) +" "+str(hour)+" " + str(minute) + " " + str(seconds)+ " "+str(ms)+" "+str(EpochSeconds)+" Thresh: "
121 File.write(outstring)
122 #print outstring
123 File.write(str(GlobalVariables.TriggerThresholds[0:]).strip('[]').replace(',', ''))
124 File.write(" rate: ")
125 GlobalVariables.Rates = self.gimmeRates.rates
126 File.write(str(GlobalVariables.Rates[0:]))#.strip('[]').replace(',', ''))
127 #print str(GlobalVariables.Rates[0:]).strip('[]').replace(',', ''))
128
129
130 #print str(GlobalVariables.TriggerThresholds[0:]).strip('[]').replace(',', '')
131 #print "\n"
132
133 File.write("\n")
134 File.close();
135
136
137
138 def run(self):
139 self.gimmeRates.start()
140 while(GlobalVariables.UserInput[1:]!="exit" and GlobalVariables.UserInput[1:] != "EXIT"):
141 time.sleep(0.001)
142 time.sleep(self.interval)
143 self.writeToFile()
144
145
146
147
148
149
150
151
152
153
154
155class ParseUserInput(threading.Thread):
156 gimmeRates=GimmeRates()
157 writeOutput=WriteOutput()
158 def Parse(self,Command):
159 Command = Command[1:]
160 #GlobalVariables.ServerMessage = "PARSING: " + Command
161 sCommand=Command.split()
162 error_code=0
163 #python lacks switch...
164 try:
165 VMEModule=sCommand[0]
166 if(VMEModule == "help"):
167 GlobalVariables.ServerMessage = "please use \'<V812|V560|Triggerboard> help\' "
168 #GlobalVariables.ServerMessage="please use \'<V812|V560|Triggerboard> help\' "
169
170 # now do the stuff for the V560:
171 if(VMEModule == "V560"):
172 V560Command=sCommand[1]
173 if(V560Command=="help"):
174 GlobalVariables.ServerMessage = "available functions are: \n"
175 GlobalVariables.ServerMessage += "V560 GetRate <module#> <channel#>\n"
176 GlobalVariables.ServerMessage += "V560 GetRates <module#\n "
177 time.sleep(0.05)
178
179 if(V560Command=="GetRate"):
180 #GlobalVariables.ServerMessage = "trying to get rate"
181 #time.sleep(0.05)
182
183
184 try:
185 module = int(sCommand[2])
186 channel= int(sCommand[3])
187 if(module!= self.gimmeRates.module):
188 self.gimmeRates.module=module
189 GlobalVariables.ServerMessage = "please wait... (5sec)"
190 time.sleep(5)
191
192 GlobalVariables.ServerMessage = str(self.gimmeRates.rates[channel])
193 time.sleep(0.01)
194 except:
195 GlobalVariables.ServerMessage = "Syntax Error (GetRate)"
196 time.sleep(0.01)
197 #GlobalVariables.Rates[int(channel)] = GetRates(int(module),int(channel))
198
199 if(V560Command=="GetRates" or V560Command=="GR"):
200 #GlobalVariables.ServerMessage = "trying to get rates"
201 #time.sleep(0.01)
202 try:
203 module = int(sCommand[2])
204 if(module!= self.gimmeRates.module):
205 self.gimmeRates.module=module
206 GlobalVariables.ServerMessage = "please wait... (5sec)"
207 time.sleep(5)
208 #GlobalVariables.ServerMessage = "RATES:"
209 GlobalVariables.ServerMessage = str(self.gimmeRates.rates)
210 time.sleep(0.01)
211 except:
212 GlobalVariables.ServerMessage = "Syntax Error (GetRates)"
213 time.sleep(0.05)
214 #GlobalVariables.Rates[int(channel)] = GetRates(int(module),int(channel))
215
216
217 # now do the stuff for the V812:
218 elif(VMEModule == "V812"):
219 V812Command=sCommand[1]
220 if(V812Command=="help"):
221 GlobalVariables.ServerMessage = "available functions are: \n"
222 GlobalVariables.ServerMessage += "V812 SetHexPat <module#[1-10]> <HexPattern[0x0000-0xFFFF>\n"
223 GlobalVariables.ServerMessage += "V812 SetThresh <module#[1-10]> <channel#[0-15]> <thresh[0-255]>\n"
224 GlobalVariables.ServerMessage += "V812 SetThreshDiff <module#[1-10]> <channel#[0-15]> <threshdiff[0-255]>\n"
225 GlobalVariables.ServerMessage += "V812 SetAllThresh <module#[1-10]> <thresh[0-255]>\n"
226 GlobalVariables.ServerMessage += "V812 SetAllThreshDiff <module#[1-10]> <threshdiff[0-255]>\n"
227 GlobalVariables.ServerMessage += "V812 SetMajLevel <module#[1-10]> <MajLev[1-20]>\n"
228 GlobalVariables.ServerMessage += "V812 SetMajThresh <module#[1-10]> <MajThr[0-255]>\n"
229 GlobalVariables.ServerMessage += "V812 SetDeadTime <module#[1-10]> <Block [0-1]> <DeadTime[0-255]>\n"
230 GlobalVariables.ServerMessage += "V812 SetOutputWidth <module#[1-10]> <Block [0-1]> <OutputWidth[0-255]>\n"
231 GlobalVariables.ServerMessage += "V812 Status\n"
232 GlobalVariables.ServerMessage += "V812 RateScan <module> <start> <step> <stop>\n"
233
234 time.sleep(0.05)
235 #set the hexpattern:
236 elif(V812Command=="SetHexPat"):
237 GlobalVariables.ServerMessage = "trying to set the hexpattern:"
238 time.sleep(0.05)
239 try:
240 module = int(sCommand[2])
241 hexpat = int(sCommand[3],16)
242 #print hexpat
243 # hexpat="ddd"
244 GlobalVariables.ServerMessage = "setting module "+str(module)+" to hexpat: "+str(hex(hexpat))
245 time.sleep(0.05)
246 try:
247 print "setting it now..."
248 error_code=_VME.V812_Set_Pattern_Inhibit_Hex(module, hexpat)
249 GlobalVariables.ServerMessage = "success! "
250 GlobalVariables.HexPattern =str(hex(hexpat))
251 time.sleep(0.05)
252
253 except:
254 VME_ErrorPrint(error_code)
255 print "FAILED"
256 except:
257 GlobalVariables.ServerMessage = "Syntax error (SetHexPat)"
258
259
260 time.sleep(0.05)
261 elif(V812Command=="SetAllThresh"):
262 print "trying to set the threshold:"
263 try:
264 module = int(sCommand[2])
265 thresh = int(sCommand[3])
266 GlobalVariables.ServerMessage="Setting all thresholds...\n"
267 for channel in range(0,16):
268 GlobalVariables.ServerMessage += "setting threshold of channel: "+str(channel)+ " in module "+str(module)+" to: "+str(thresh)+"\n"
269 GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Threshold(module, channel, thresh))+"\n"
270 GlobalVariables.TriggerThresholds[channel] = thresh
271 time.sleep(0.05)
272
273
274 except:
275 print "Syntax error (SetAllThresh) "
276
277 elif(V812Command=="SetAllThreshDiff"):
278 print "trying to set all the threshold (diff):"
279 try:
280 module = int(sCommand[2])
281 threshdiff = int(sCommand[3])
282 GlobalVariables.ServerMessage="Setting all thresholds (diff)...\n"
283 for channel in range(0,16):
284 GlobalVariables.ServerMessage += "increase threshold of channel: "+str(channel)+ " in module "+str(module)+" by: "+str(threshdiff)+"\n"
285 GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Threshold(module, channel, GlobalVariables.TriggerThresholds[channel]+ threshdiff))+"\n"
286 GlobalVariables.TriggerThresholds[channel]+= threshdiff
287 time.sleep(0.05)
288
289
290 except:
291 print "Syntax error (SetAllThreshDiff) "
292
293 elif(V812Command=="SetThreshDiff"):
294 GlobalVariables.ServerMessage = "trying to set the threshold:"
295 time.sleep(0.05)
296 try:
297 module = int(sCommand[2])
298 channel = int(sCommand[3])
299 threshdiff = int(sCommand[4])
300 #print module, channel, threshdiff
301 #print GlobalVariables.TriggerThresholds[channel]
302 newthresh = GlobalVariables.TriggerThresholds[channel] + threshdiff
303 #print newthresh
304 GlobalVariables.ServerMessage = "increase threshold of channel: "+str(channel)+ " in module "+str(module)+" by: "+str(threshdiff)+"\n"
305 GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Threshold(module, channel,newthresh))
306 GlobalVariables.TriggerThresholds[channel] = newthresh
307 time.sleep(0.05)
308 except:
309 GlobalVariables.ServerMessage = "Syntax error (SetThreshDiff) "
310 time.sleep(0.05)
311
312 elif(V812Command=="SetThresh"):
313 GlobalVariables.ServerMessage = "trying to set the threshold:"
314 time.sleep(0.05)
315 try:
316 module = int(sCommand[2])
317 channel = int(sCommand[3])
318 thresh = int(sCommand[4])
319 GlobalVariables.ServerMessage = "setting threshold of channel: "+str(channel)+ " in module "+str(module)+" to: "+str(thresh)
320 GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Threshold(module, channel, thresh))
321 GlobalVariables.TriggerThresholds[channel] = thresh
322 time.sleep(0.05)
323 except:
324 GlobalVariables.ServerMessage = "Syntax error (SetThresh) "
325 time.sleep(0.05)
326 elif(V812Command=="SetMajLevel"):
327 GlobalVariables.ServerMessage = "trying to set the majority level:"
328 time.sleep(0.05)
329 try:
330 module = int(sCommand[2])
331 level = int(sCommand[3])
332 GlobalVariables.ServerMessage = "setting maj. level of module "+str(module)+" to: "+str(level)
333 GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Majority_Level(module,level))
334 GlobalVariables.MajorityLevel = level
335 time.sleep(0.05)
336 except:
337 GlobalVariables.ServerMessage = "Syntax error (SetMajLevel)"
338 time.sleep(0.05)
339
340 elif(V812Command=="SetMajThresh"):
341 GlobalVariables.ServerMessage = "trying to set the majority threshold:"
342 time.sleep(0.05)
343 try:
344 module = int(sCommand[2])
345 thresh = int(sCommand[3])
346 GlobalVariables.ServerMessage = "setting maj. level of module "+str(module)+" to: "+str(thresh)
347 GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Majority_Threshold(module, thresh))
348 time.sleep(0.05)
349 except:
350 GlobalVariables.ServerMessage = "Syntax error (SetMajThresh)"
351 time.sleep(0.05)
352
353 elif(V812Command=="SetDeadTime"):
354 GlobalVariables.ServerMessage = "trying to set deadtime threshold:"
355 time.sleep(0.05)
356 try:
357 module = int(sCommand[2])
358 block = int(sCommand[3])
359 deadtime= int(sCommand[4])
360 GlobalVariables.ServerMessage = "setting deadtime of module "+str(module)+" block: "+str(block)+ " to: "+str(deadtime)
361 GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Dead_Time(module, block, deadtime))
362 time.sleep(0.05)
363 except:
364 GlobalVariables.ServerMessage = "Syntax error (SetDeadTime)"
365 time.sleep(0.05)
366 elif(V812Command=="SetOutputWidth"):
367 GlobalVariables.ServerMessage = "trying to set output width "
368 time.sleep(0.05)
369 try:
370 module = int(sCommand[2])
371 block = int(sCommand[3])
372 outputw= int(sCommand[4])
373 GlobalVariables.ServerMessage = "setting output width of module "+str(module)+" block: "+str(block)+ " to: "+str(outputw)
374 GlobalVariables.ServerMessage += "success: "+str(_VME.V812_Set_Output_Width(module, block, outputw))
375 time.sleep(0.05)
376 except:
377 GlobalVariables.ServerMessage = "Syntax error (SetOutputTime)"
378 time.sleep(0.05)
379 elif(V812Command=="Status"):
380 GetStatus();
381
382 elif(V812Command=="RateScan"):
383 GlobalVariables.ServerMessage = "trying to scan the rates "
384 time.sleep(0.05)
385 try:
386 module = int(sCommand[2])
387 start = int(sCommand[3])
388 step = int(sCommand[4])
389 stop = int(sCommand[5])
390 print module, start, step, stop
391 self.RateScan(module, start, step, stop)
392
393
394 except:
395 GlobalVariables.ServerMessage = "Syntax error (RateScan)"
396 time.sleep(0.05)
397
398
399
400
401 else:
402 GlobalVariables.ServerMessage = "Syntax Error (V812)"
403 time.sleep(0.05)
404
405
406 except:
407 GlobalVariables.ServerMessage = "syntax error"
408 time.sleep(0.05)
409 def RateScan(self,module, start, step, stop):
410 #print module, start, step,stop
411 #print "IN\n"
412 #First stop the automatic rate writeup
413 oldinterval = self.writeOutput.interval
414 self.writeOutput.interval=15
415 rates = ["rates",
416 0,0,0,0,
417 0,0,0,0,
418 0,0,0,0,
419 0,0,0,0]
420 print "rates:" ,rates
421 CurrentThresholds =[0,0,0,0,
422 0,0,0,0,
423 0,0,0,0,
424 0,0,0,0]
425 for pix in range(0,16):
426 CurrentThresholds[pix] = GlobalVariables.TriggerThresholds[pix]
427 print "Threshs: ",CurrentThresholds,"\n"
428 NumberOfScanSteps = int((stop-start)/step) + 1
429 print "NumberOfScanSteps: ", NumberOfScanSteps
430 for StepNumber in range(0,NumberOfScanSteps):
431 print "Step#" ,StepNumber
432 for pix in range(0,16):
433 #print (CurrentThresholds[pix] + start) + step*StepNumber
434 GlobalVariables.TriggerThresholds[pix] = (CurrentThresholds[pix] + start) + step*StepNumber
435 #print pix, GlobalVariables.TriggerThresholds[pix]
436 _VME.V812_Set_Threshold(module, pix, GlobalVariables.TriggerThresholds[pix])
437
438 time.sleep(2.1)
439 #print "Getting Rates: \n"
440 rates = self.gimmeRates.rates#GetRates(module);
441 self.writeOutput.writeToFile()
442 #print "Got rates..."
443 print "Thresh: ",GlobalVariables.TriggerThresholds, " rates: ",rates #_VME.V812_Set_Threshold(1, 0, 255)
444
445
446 self.writeOutput.interval = oldinterval
447
448 def __init__(self):
449 #self.writeOutput.OpenNewFile("test.txt")
450 threading.Thread.__init__(self)
451 def run(self):
452 self.gimmeRates.start()
453
454 self.writeOutput.start()
455 oldcommand=GlobalVariables.UserInput
456 while(GlobalVariables.UserInput[1:]!="exit" and GlobalVariables.UserInput[1:] != "EXIT"):
457 CommandToParse=GlobalVariables.UserInput
458 #Filename to write:
459 #year = datetime.date.today().year
460 #month = datetime.date.today().month
461 #day = datetime.date.today().day
462 MyTime = time.time()
463 #print MyTime
464
465
466 hour = time.gmtime(MyTime)[3]
467 minute = time.gmtime(MyTime)[4]
468 #print hour, minute
469 if(hour>=13):
470 MyTime+=12*60*60
471
472 year = time.gmtime(MyTime)[0]
473 month =time.gmtime(MyTime)[1]
474 day = time.gmtime(MyTime)[2]
475 date=str(year)
476 #if (hour>=12):
477 # day = day+1
478
479 date+="%(month)02d" % {"month":month}
480 date+="%(day)02d" % {"day":day}
481 #date+="_"
482 #date+="%(hour)02d" % {"hour":hour}
483 #date+="%(minute)02d" % {"minute":minute}
484 self.writeOutput.filename="/ct3data/SlowData/Trigger_"+date+".slow"
485
486
487 #self.writeOutput.filename = "/ct3data/SlowData/Trigger_"
488 #self.writeOutput.filename
489 if(oldcommand!=CommandToParse):
490 self.Parse(CommandToParse)
491 oldcommand=CommandToParse
492 time.sleep(0.05)
493
494
495
496 GlobalVariables.ServerMessage = "EXITING PARSER...press <enter> to exit"
497 time.sleep(0.05)
498
Note: See TracBrowser for help on using the repository browser.