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

Revision 3714, 8.4 KB (checked in by DrewCormier, 6 years ago)

write method wants a string, not an int

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