root/tools/alf_plugins/write_to_file/branches/write_to_file-0.6.2/write_to_file/write_to_file.py @ 5794

Revision 5794, 9.4 KB (checked in by jgaeddert, 6 years ago)

fixing imports

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env python
2
3'''Write incomming packet(s) to file(s)'''
4
5import wx         #needed for display stuff
6from omniORB import CORBA      #use this for the CORBA orb stuff (pushing packets)
7import sys       #for system commands (e.g., argv and argc stuff)
8import CosNaming   #narrowing naming context stuff
9from ossie.cf import CF, CF__POA    #core framework stuff
10from ossie.standardinterfaces import standardInterfaces__POA
11from wx.lib.anchors import LayoutAnchors  #used by splitter window
12# import wx.lib.buttons as buttons #for special wx buttons
13
14class write_to_file_short(standardInterfaces__POA.complexShort):
15    '''Writes I and Q data to 2 files'''
16
17    def __init__(self, orb, gui):       
18        self.orb = orb
19        self.gui = gui    #usually parent
20        self.delimiter = ','
21
22    def pushPacket(self, I_data, Q_data):
23        '''Store the data to be written to file when the Write Packet
24           button is bushed'''
25        self.gui.I_data = I_data
26        self.gui.Q_data = Q_data
27 
28
29
30def create(parent,namespace, interface, ns_name, port_name):
31    #return MainFrame(parent, -1, namespace, interface, ns_name, port_name)
32    return MainFrame(parent, -1, "Don't know what this should be",
33                     namespace, interface, ns_name, port_name)
34
35#generate wx ids for my wx controls
36[wxID_MAINFRAME, wxID_SPLITTERWINDOW1, wxID_WRITEPACKETBTN, wxID_WRITEBUFFERBTN, wxID_IFILENAMEEDITOR, wxID_QFILENAMEEDITOR, wxID_IFILESTATICTEXT,wxID_QFILESTATICTEXT] = [wx.NewId() for _init_ctrls in range(8)]
37
38
39class MainFrame(wx.Frame):
40    def __init__(self, parent, id, title, namespace, interface,
41                 component_name, port_name):
42
43        self._init_ctrls(parent)
44
45        self.I_data = []
46        self.Q_data = []
47         
48        self.parent = parent
49        self.namespace = namespace
50        self.interface = interface
51        self.my_local_controls = None
52        self.component_name = component_name
53        self.port_name = port_name
54        self.setup_graphics()
55       
56        # Now Create the menu bar and items
57        self.mainmenu = wx.MenuBar()
58
59        menu = wx.Menu()
60        menu.Append(205, 'E&xit', 'Enough of this already!')
61        self.Bind(wx.EVT_MENU, self.OnFileExit, id=205)
62        self.mainmenu.Append(menu, '&File')
63       
64        menu = wx.Menu()
65        menu.Append(300, '&About', 'About this thing...')
66        self.Bind(wx.EVT_MENU, self.OnHelpAbout, id=300)
67        self.mainmenu.Append(menu, '&Help')
68
69        self.SetMenuBar(self.mainmenu)
70
71        # Bind the close event so we can disconnect the ports
72        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
73
74        self.Show(True)
75       
76        self.iFileNameEditor.write('i_out.dat')
77        self.qFileNameEditor.write('q_out.dat')
78
79
80    def _init_ctrls(self, prnt):
81        wx.Frame.__init__(self, id=wxID_MAINFRAME, name='', parent=prnt,
82              pos=wx.Point(530, 680), size=wx.Size(520, 320),
83              style=wx.DEFAULT_FRAME_STYLE, title='Write to File Tool')
84
85        self.splitterWindow1 = wx.SplitterWindow(id=wxID_SPLITTERWINDOW1,
86              name='splitterWindow1', parent=self, point=wx.Point(1, 1),
87              size=wx.Size(570, 270), style=wx.SP_3D)
88        self.splitterWindow1.SetConstraints(LayoutAnchors(self.splitterWindow1,
89              True, True, True, True))
90
91        self.WritePacketBtn = wx.Button(id=wxID_WRITEPACKETBTN, label='Write Packet',
92              name='WritePacketBtn', parent=self.splitterWindow1, pos=wx.Point(155, 150),
93              size=wx.Size(165, 50))
94        self.WritePacketBtn.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD, False))
95        self.WritePacketBtn.SetBackgroundColour("green")
96        self.WritePacketBtn.Bind(wx.EVT_BUTTON, self.OnWritePacketBtnButton,
97              id=wxID_WRITEPACKETBTN)
98
99        self.WriteBufferBtn = wx.Button(id=wxID_WRITEBUFFERBTN, label='Write BUFFER',
100              name='WriteBufferBtn', parent=self.splitterWindow1, pos=wx.Point(155, 210),
101              size=wx.Size(165, 50))
102        self.WriteBufferBtn.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD, False))
103        self.WriteBufferBtn.SetBackgroundColour("green")
104        self.WriteBufferBtn.Bind(wx.EVT_BUTTON, self.OnWriteBufferBtnButton,
105              id=wxID_WRITEBUFFERBTN)
106
107        #####
108        ## if you want an image on the button
109        # button_bmp = wx.Image("../AWG/my_image.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
110        # self.WritePacketBtn = buttons.GenBitmapTextButton(self.splitterWindow1,
111        #       wxID_WRITEPACKETBTN,button_bmp, 'Write Packet',
112        #       name='WritePacketBtn', pos=wx.Point(155, 250),
113        #       size=wx.Size(300, 100), style=0)
114        # self.WritePacketBtn.Bind(wx.EVT_BUTTON, self.OnWritePacketBtnButton,
115        #       id=wxID_WRITEPACKETBTN)
116        #####
117
118       
119        self.iFileNameEditor = wx.TextCtrl(id=wxID_IFILENAMEEDITOR,
120              name=u'iFileNameEditor', parent=self.splitterWindow1, pos=wx.Point(215, 50),
121              size=wx.Size(250, 30), style=0, value=u'')
122                         
123
124        self.qFileNameEditor = wx.TextCtrl(id=wxID_QFILENAMEEDITOR,
125              name=u'qFileNameEditor', parent=self.splitterWindow1, pos=wx.Point(215, 100),
126              size=wx.Size(250, 30), style=0, value=u'')
127
128
129        self.iFileStaticText = wx.StaticText(id=wxID_IFILESTATICTEXT,
130              label=u'I channel file:', name='qFileStaticText', parent=self.splitterWindow1,
131              pos=wx.Point(55, 50), size= wx.Size(100, 20), style=0)
132        self.iFileStaticText.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,True,u'Sans'))
133
134        self.qFileStaticText = wx.StaticText(id=wxID_QFILESTATICTEXT,
135              label=u'Q channel file:', name='qFileStaticText', parent=self.splitterWindow1,
136              pos=wx.Point(55, 100), size= wx.Size(100, 20), style=0)
137        self.qFileStaticText.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,True,u'Sans'))
138 
139 
140
141    def OnWritePacketBtnButton(self,event):
142        #get the file names from the file name editors in the GUI
143        self.i_file_name = self.iFileNameEditor.GetLineText(0)
144        self.q_file_name = self.qFileNameEditor.GetLineText(0)
145
146        #write the data out
147        open_file = open(self.i_file_name, 'w')   #open the file for writing
148        open_file.write(str(self.I_data))
149        open_file.close()
150
151        open_file = open(self.q_file_name, 'w')   #open the file for writing
152        open_file.write(str(self.Q_data))
153        open_file.close()
154
155    def OnWriteBufferBtnButton(self,event):
156        #get the file names from the file name editors in the GUI
157        self.i_file_name = self.iFileNameEditor.GetLineText(0)
158        self.q_file_name = self.qFileNameEditor.GetLineText(0)
159
160        #write the data out
161        open_file = open(self.i_file_name, 'w')   #open the file for writing
162        open_file.write(str(self.I_data))
163        open_file.close()
164
165        open_file = open(self.q_file_name, 'w')   #open the file for writing
166        open_file.write(str(self.Q_data))
167        open_file.close()
168
169    def OnFileExit(self, event):
170        '''This is what will happen when you select File -> Exit
171           in the menu bar'''
172        self.Close()      #close the frame
173 
174    def OnHelpAbout(self, event):
175        '''Stuff that gets displayed when you select Help -> About in
176           the menu bar'''
177        from wx.lib.dialogs import ScrolledMessageDialog
178        about = ScrolledMessageDialog(self, "Write to file tool.\nA product of Wireless@VT.", "About...")
179        about.ShowModal()
180
181    def setup_graphics(self):
182        self.CORBA_being_used = False
183
184        if True:               
185         self.CORBA_being_used = True
186         self.orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
187         obj = self.orb.resolve_initial_references("NameService")
188         rootContext = obj._narrow(CosNaming.NamingContext)
189         if rootContext is None:
190             print "Failed to narrow the root naming context"
191             sys.exit(1)
192         name = [CosNaming.NameComponent(self.component_name[0],""),
193             CosNaming.NameComponent(self.component_name[1],""),
194             CosNaming.NameComponent(self.component_name[2],"")]
195     
196         try:
197             ResourceRef = rootContext.resolve(name)
198     
199         except:
200             print "Required resource not found"
201             sys.exit(1)
202     
203         ResourceHandle = ResourceRef._narrow(CF.Resource)
204         PortReference = ResourceHandle.getPort(self.port_name)
205         if PortReference is None:
206             print "Failed to get Port reference"
207         self.PortHandle = PortReference._narrow(CF.Port)
208         
209       
210         #create the class instance of the write_to_file class
211         self.my_file_writer = write_to_file_short(self.orb, self)
212
213         obj_poa = self.orb.resolve_initial_references("RootPOA")
214         poaManager = obj_poa._get_the_POAManager()
215         poaManager.activate()
216         obj_poa.activate_object(self.my_file_writer)
217         self.PortHandle.connectPort(self.my_file_writer._this(),
218                                     "thisismyconnectionid_w2file")
219         #orb.run()
220
221    def OnCloseWindow(self,event):
222        if hasattr(self.parent, 'removeToolFrame'):
223            self.parent.removeToolFrame(self)
224        self = None
225        event.Skip()
226
227    def __del__(self):
228        if self.CORBA_being_used:
229            self.PortHandle.disconnectPort("thisismyconnectionid_w2file")
230            while (_time.time() - self.my_local_plot.end_time) < 1.5:
231                #print (time.time() - self.my_local_plot.end_time)
232                pass
233                #_time.sleep(1)
234
235
236
Note: See TracBrowser for help on using the browser.