The VisAO Camera
visaofifos.py
Go to the documentation of this file.
1 ##@file visaofifos.py
2 #
3 # The visaofifos python module provides the basic interface to a VisAO FIFO device.
4 #
5 #
6 
7 
8 """@module visaofifos.py
9 
10  The visaofifos python module provides the basic interface to a VisAO FIFO device.
11 
12 """
13 
14 
15 
16 import os, time, math, select, errno, signal, numpy
17 
18 import numpy
19 from visaoutils import *
20 
21 
23  """
24  A basic VisAO FIFO device. Establishes basic fifo bichannel communications with a VisAO process.
25 
26  Derived classes should provide self.base_name in their __init__ method, and should set connected = 0 and control = 0.
27  """
28  def setup_fifo_names(self):
29  """
30  Construct the script fifo paths based on the base_name variable.
31  """
32  self.base_path = os.environ['VISAO_ROOT']
33  self.fifo_out_name = self.base_path + '/fifos/' + self.base_name + '_com_script_in'
34  self.fifo_in_name = self.base_path + '/fifos/' + self.base_name + '_com_script_out'
35 
36  #print self.fifo_out_name
37  #print self.fifo_in_name
38 
39  def open_fifoch(self):
40  """
41  Open the script fifo channel to this device.
42  """
43  self.pollobj = select.poll()
44  self.fifo_out = open(self.fifo_out_name, 'w')
45  fd = os.open(self.fifo_in_name, os.O_RDONLY|os.O_NONBLOCK)
46  self.fifo_in = os.fdopen(fd)
47 
48  self.pollobj.register(fd, select.POLLIN)
49 
50  #Here we clear the input fifo of any waiting garbage.
51  while 1:
52  try:
53  ready = self.pollobj.poll(1)
54  except select.error, v:
55  if v[0] != errno.EINTR: raise
56  else: break
57  if len(ready) > 0: self.fifo_in.read()
58 
59  def write_fifoch(self, com):
60  """
61  Write com to the script fifo for this device, read and return the response.
62  """
63  self.fifo_out.write(com)
64  self.fifo_out.flush()
65  while 1:
66  try:
67  ready = self.pollobj.poll(2000)
68  except select.error, v:
69  if v[0] != errno.EINTR: raise
70  else: break
71 
72  if len(ready) > 0:
73  return self.fifo_in.read()
74  else:
75  print "timeout"
76  return ""
77 
78  def close_fifoch(self):
79  """
80  Close the fifo channel.
81  """
82  self.fifo_out.close()
83  self.fifo_in.close()
84  self.connected = 0
85 
86  def connect(self):
87  """
88  Connect to the script fifos of this device.
89  """
90  if self.connected == 1:
91  self.close_fifoch()
92 
93  self.open_fifoch()
94  self.connected = 1
95 
96  def take_control(self, override = 0):
97  """
98  Take SCRIPT control of this device.
99 
100  Input: override Default is 0, no override. If 1, override.
101  """
102  if self.connected == 0:
103  self.connect()
104  if override == 1:
105  ans = self.write_fifoch('XSCRIPT')
106  else:
107  ans = self.write_fifoch('SCRIPT')
108 
109  ans = ans.rstrip('\n\x00')
110  if ans == 'SCRIPT':
111  print 'you have Script Control of ' + self.base_name
112  self.control = 1
113  return ans
114 
115  else:
116  print 'Error taking Script Control of ' + self.base_name
117  print 'Response was: ' + ans
118  self.control = 0
119  return ans
120 
121  def giveup_control(self):
122  """
123  Give up SCRIPT control of this process, returning to its default.
124  """
125  if self.connected == 0:
126  self.connect()
127  ans = self.write_fifoch('~SCRIPT')
128 
129  ans = ans.rstrip('\n\x00')
130  if ans == 'NONE' or ans == 'REMOTE' or ans == 'LOCAL' or ans == 'AUTO':
131  print 'We no longer have Script Control of ' + self.base_name
132  self.control = 0
133  return ans
134 
135  else:
136  print 'Error relinquishing Script Control of ' + self.base_name
137  print 'Response was: ' + ans
138  return ans
def setup_fifo_names(self)
Definition: visaofifos.py:28
def write_fifoch(self, com)
Definition: visaofifos.py:59