root/experimental/write_to_file/trunk/write_to_file/write_to_file.py @ 3696

Revision 3696, 8.1 KB (checked in by DrewCormier, 6 years ago)

copied over port connection stuff from plot tool since i am receiving data instead of sending data. changed from graph structure class instance to write to file class instance. might not work completely

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
9import CF, CF__POA    #core framework stuff
10import 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:
15    #TODO:add comment explainging this class
16
17    def __init__(self, orb, gui):       
18        self.orb = orb
19        self.gui = gui
20
21    def pushPacket(self, data, file_name):
22        #TODO: test this method
23        open_file = open(file_name, 'w')   #open the file for writing
24        open_file.write(data)
25        open_file.close()
26       
27   
28
29def create(parent,namespace, interface, ns_name, port_name):
30    #return MainFrame(parent, -1, namespace, interface, ns_name, port_name)
31    return MainFrame(parent, -1, "Don't know what this should be", namespace, interface, ns_name, port_name)
32
33#generate wx ids for my wx controls
34[wxID_MAINFRAME, wxID_SPLITTERWINDOW1, wxID_PUSHPACKETBTN, wxID_IFILENAMEEDITOR, wxID_QFILENAMEEDITOR, wxID_IFILESTATICTEXT,wxID_QFILESTATICTEXT] = [wx.NewId() for _init_ctrls in range(11)]
35
36
37class MainFrame(wx.Frame):
38    def __init__(self, parent, id, title, namespace, interface, component_name, port_name):
39
40        #TODO: get this from the sources class
41        tmp_sources_instance = sources.sources()
42        self.available_sources = tmp_sources_instance.get_sources_list()
43
44        self._init_ctrls(parent)
45         
46        self.parent = parent
47        self.namespace = namespace
48        self.interface = interface
49        self.my_local_controls = None
50        self.component_name = component_name
51        self.port_name = port_name
52        self.setup_graphics()
53       
54        # Now Create the menu bar and items
55        self.mainmenu = wx.MenuBar()
56
57        menu = wx.Menu()
58        menu.Append(205, 'E&xit', 'Enough of this already!')
59        self.Bind(wx.EVT_MENU, self.OnFileExit, id=205)
60        self.mainmenu.Append(menu, '&File')
61       
62        menu = wx.Menu()
63        menu.Append(300, '&About', 'About this thing...')
64        self.Bind(wx.EVT_MENU, self.OnHelpAbout, id=300)
65        self.mainmenu.Append(menu, '&Help')
66
67        self.SetMenuBar(self.mainmenu)
68
69        # Bind the close event so we can disconnect the ports
70        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
71
72        self.Show(True)
73       
74        self.iFileNameEditor.write('i.dat')
75        self.qFileNameEditor.write('q.dat')
76
77        #set the default source
78        #this way if the user presses the "PushPacket" button before selecting
79        #a source, the source that is being displayed will be used
80        self.iSourceChoice.SetSelection(0)
81        self.qSourceChoice.SetSelection(0)
82
83    def _init_ctrls(self, prnt):
84        wx.Frame.__init__(self, id=wxID_MAINFRAME, name='', parent=prnt,
85              pos=wx.Point(374, 370), size=wx.Size(520, 370),
86              style=wx.DEFAULT_FRAME_STYLE, title='Arbitrary Waveform Generator')
87
88        self.splitterWindow1 = wx.SplitterWindow(id=wxID_SPLITTERWINDOW1,
89              name='splitterWindow1', parent=self, point=wx.Point(1, 1),
90              size=wx.Size(570, 370), style=wx.SP_3D)
91        self.splitterWindow1.SetConstraints(LayoutAnchors(self.splitterWindow1,
92              True, True, True, True))
93
94        self.PushPacketBtn = wx.Button(id=wxID_PUSHPACKETBTN, label='Push Packet',
95              name='PushPacketBtn', parent=self.splitterWindow1, pos=wx.Point(155, 250),
96              size=wx.Size(145, 50))
97        self.PushPacketBtn.SetFont(wx.Font(16, wx.SWISS, wx.NORMAL, wx.BOLD, False))
98        self.PushPacketBtn.SetBackgroundColour("green")
99        self.PushPacketBtn.Bind(wx.EVT_BUTTON, self.OnPushPacketBtnButton,
100              id=wxID_PUSHPACKETBTN)
101 
102        #####
103        ## if you want an image on the button
104        # button_bmp = wx.Image("../AWG/my_image.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
105        # self.PushPacketBtn = buttons.GenBitmapTextButton(self.splitterWindow1,
106        #       wxID_PUSHPACKETBTN,button_bmp, 'Push Packet',
107        #       name='PushPacketBtn', pos=wx.Point(155, 250),
108        #       size=wx.Size(300, 100), style=0)
109        # self.PushPacketBtn.Bind(wx.EVT_BUTTON, self.OnPushPacketBtnButton,
110        #       id=wxID_PUSHPACKETBTN)
111        #####
112
113       
114        self.iFileNameEditor = wx.TextCtrl(id=wxID_IFILENAMEEDITOR,
115              name=u'iFileNameEditor', parent=self.splitterWindow1, pos=wx.Point(215, 150),
116              size=wx.Size(250, 30), style=0, value=u'')
117                         
118
119        self.qFileNameEditor = wx.TextCtrl(id=wxID_QFILENAMEEDITOR,
120              name=u'qFileNameEditor', parent=self.splitterWindow1, pos=wx.Point(215, 200),
121              size=wx.Size(250, 30), style=0, value=u'')
122
123
124        self.iFileStaticText = wx.StaticText(id=wxID_IFILESTATICTEXT,
125              label=u'I channel file:', name='qFileStaticText', parent=self.splitterWindow1,
126              pos=wx.Point(55, 150), size= wx.Size(100, 20), style=0)
127        self.iFileStaticText.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,True,u'Sans'))
128
129        self.qFileStaticText = wx.StaticText(id=wxID_QFILESTATICTEXT,
130              label=u'Q channel file:', name='qFileStaticText', parent=self.splitterWindow1,
131              pos=wx.Point(55, 200), size= wx.Size(100, 20), style=0)
132        self.qFileStaticText.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.BOLD,True,u'Sans'))
133 
134 
135
136       
137
138    def OnPushPacketBtnButton(self,event):
139        '''This button is not supported yet'''
140        print "this button is not supported at the moment"
141
142
143
144    def OnFileExit(self, event):
145        '''This is what will happen when you select File -> Exit in the menu bar'''
146        self.Close()      #close the frame
147 
148    def OnHelpAbout(self, event):
149        '''Stuff that gets displayed when you select Help -> About in the menu bar'''
150        from wx.lib.dialogs import ScrolledMessageDialog
151        about = ScrolledMessageDialog(self, "Write to file tool.\nA product of Wireless@VT.", "About...")
152        about.ShowModal()
153
154    def setup_graphics(self):
155        self.CORBA_being_used = False
156
157        if True:               
158         self.CORBA_being_used = True
159         self.orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
160         obj = self.orb.resolve_initial_references("NameService")
161         rootContext = obj._narrow(CosNaming.NamingContext)
162         if rootContext is None:
163             print "Failed to narrow the root naming context"
164             sys.exit(1)
165         name = [CosNaming.NameComponent(self.component_name[0],""),
166             CosNaming.NameComponent(self.component_name[1],""),
167             CosNaming.NameComponent(self.component_name[2],"")]
168     
169         try:
170             ResourceRef = rootContext.resolve(name)
171     
172         except:
173             print "Required resource not found"
174             sys.exit(1)
175     
176         ResourceHandle = ResourceRef._narrow(CF.Resource)
177         PortReference = ResourceHandle.getPort(self.port_name)
178         if PortReference is None:
179             print "Failed to get Port reference"
180         self.PortHandle = PortReference._narrow(CF.Port)
181         
182       
183         #create the class instance of the write_to_file class
184         self.my_file_writer = write_to_file(self.orb, self)
185
186         obj_poa = self.orb.resolve_initial_references("RootPOA")
187         poaManager = obj_poa._get_the_POAManager()
188         poaManager.activate()
189         obj_poa.activate_object(self.my_file_writer)
190         self.PortHandle.connectPort(self.my_file_writer._this(), "thisismyconnectionid_w2file")
191         #orb.run()
192
193    def OnCloseWindow(self,event):
194        if hasattr(self.parent, 'removeToolFrame'):
195            self.parent.removeToolFrame(self)
196        self = None
197        event.Skip()
198
199    def __del__(self):
200        if self.CORBA_being_used:
201            self.PortHandle.disconnectPort("thisismyconnectionid_plot")
202            while (_time.time() - self.my_local_plot.end_time) < 1.5:
203                #print (time.time() - self.my_local_plot.end_time)
204                pass
205                #_time.sleep(1)
206
207
208
Note: See TracBrowser for help on using the browser.