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

Revision 3768, 8.2 KB (checked in by DrewCormier, 6 years ago)

file is now written when the button is pushed. this way when you close the tool you don't run into the problem of the tool being half way through writing a file.

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