25 from wxPython.wx import * |
25 from wxPython.wx import * |
26 import wx |
26 import wx |
27 from time import localtime |
27 from time import localtime |
28 from datetime import datetime |
28 from datetime import datetime |
29 |
29 |
30 import sys, os |
30 import os, re, platform, sys, time, traceback, getopt, commands |
31 base_folder = os.path.split(sys.path[0])[0] |
31 base_folder = os.path.split(sys.path[0])[0] |
32 sys.path.append(os.path.join(base_folder, "plcopeneditor")) |
32 sys.path.append(os.path.join(base_folder, "plcopeneditor")) |
33 sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen")) |
33 sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen")) |
34 |
34 |
35 from PLCOpenEditor import PLCOpenEditor, ProjectDialog |
35 from PLCOpenEditor import PLCOpenEditor, ProjectDialog |
36 from PLCControler import PLCControler |
36 from PLCControler import PLCControler |
37 |
37 |
38 from networkedit import networkedit |
38 from networkedit import networkedit |
39 from nodelist import NodeList |
39 from nodelist import NodeList |
40 from nodemanager import NodeManager |
40 from nodemanager import NodeManager |
41 |
41 import config_utils, gen_cfile |
42 import os, re, platform, sys, time, traceback, getopt |
|
43 |
42 |
44 __version__ = "$Revision$" |
43 __version__ = "$Revision$" |
45 |
44 |
46 def create(parent): |
45 def create(parent): |
47 return Beremiz(parent) |
46 return Beremiz(parent) |
67 usage() |
66 usage() |
68 sys.exit() |
67 sys.exit() |
69 elif len(args) == 1: |
68 elif len(args) == 1: |
70 projectOpen = args[0] |
69 projectOpen = args[0] |
71 CWD = sys.path[0] |
70 CWD = sys.path[0] |
|
71 |
|
72 re_texts = {} |
|
73 re_texts["letter"] = "[A-Za-z]" |
|
74 re_texts["digit"] = "[0-9]" |
|
75 LOCATED_MODEL = re.compile("__LOCATED_VAR\(([A-Z]*),([_A-Za-z0-9]*)\)") |
|
76 |
|
77 |
|
78 class LogPseudoFile: |
|
79 """ Base class for file like objects to facilitate StdOut for the Shell.""" |
|
80 def __init__(self, output = None): |
|
81 self.output = output |
|
82 |
|
83 def writelines(self, l): |
|
84 map(self.write, l) |
|
85 |
|
86 def write(self, s): |
|
87 self.output.SetValue(self.output.GetValue() + s) |
|
88 |
|
89 def flush(self): |
|
90 self.output.SetValue("") |
|
91 |
|
92 def isatty(self): |
|
93 return false |
72 |
94 |
73 [wxID_BEREMIZ, wxID_BEREMIZLOGCONSOLE, wxID_BEREMIZEDITPLCBUTTON, |
95 [wxID_BEREMIZ, wxID_BEREMIZLOGCONSOLE, wxID_BEREMIZEDITPLCBUTTON, |
74 wxID_BEREMIZBUILDBUTTON, wxID_BEREMIZSIMULATEBUTTON, |
96 wxID_BEREMIZBUILDBUTTON, wxID_BEREMIZSIMULATEBUTTON, |
75 wxID_BEREMIZRUNBUTTON, wxID_BEREMIZBUSLIST, |
97 wxID_BEREMIZRUNBUTTON, wxID_BEREMIZBUSLIST, |
76 wxID_BEREMIZADDBUSBUTTON, wxID_BEREMIZDELETEBUSBUTTON, |
98 wxID_BEREMIZADDBUSBUTTON, wxID_BEREMIZDELETEBUSBUTTON, |
152 parent.Append(help='', id=wxID_BEREMIZRUNMENUITEMS3, |
174 parent.Append(help='', id=wxID_BEREMIZRUNMENUITEMS3, |
153 kind=wx.ITEM_NORMAL, text=u'Run') |
175 kind=wx.ITEM_NORMAL, text=u'Run') |
154 parent.AppendSeparator() |
176 parent.AppendSeparator() |
155 parent.Append(help='', id=wxID_BEREMIZRUNMENUITEMS5, |
177 parent.Append(help='', id=wxID_BEREMIZRUNMENUITEMS5, |
156 kind=wx.ITEM_NORMAL, text=u'Save Log') |
178 kind=wx.ITEM_NORMAL, text=u'Save Log') |
157 self.Bind(wx.EVT_MENU, self.OnEditPLCMenu, |
179 self.Bind(wx.EVT_MENU, self.OnBuildMenu, |
158 id=wxID_BEREMIZRUNMENUITEMS0) |
180 id=wxID_BEREMIZRUNMENUITEMS0) |
159 self.Bind(wx.EVT_MENU, self.OnSimulateMenu, |
181 self.Bind(wx.EVT_MENU, self.OnSimulateMenu, |
160 id=wxID_BEREMIZRUNMENUITEMS2) |
182 id=wxID_BEREMIZRUNMENUITEMS2) |
161 self.Bind(wx.EVT_MENU, self.OnRunMenu, |
183 self.Bind(wx.EVT_MENU, self.OnRunMenu, |
162 id=wxID_BEREMIZRUNMENUITEMS3) |
184 id=wxID_BEREMIZRUNMENUITEMS3) |
269 self.SetClientSize(wx.Size(600, 300)) |
291 self.SetClientSize(wx.Size(600, 300)) |
270 self.SetMenuBar(self.menuBar1) |
292 self.SetMenuBar(self.menuBar1) |
271 |
293 |
272 self.LogConsole = wx.TextCtrl(id=wxID_BEREMIZLOGCONSOLE, value='', |
294 self.LogConsole = wx.TextCtrl(id=wxID_BEREMIZLOGCONSOLE, value='', |
273 name='LogConsole', parent=self, pos=wx.Point(0, 0), |
295 name='LogConsole', parent=self, pos=wx.Point(0, 0), |
274 size=wx.Size(400, 200), style=wxTE_MULTILINE) |
296 size=wx.Size(0, 0), style=wxTE_MULTILINE) |
275 |
297 |
276 self.EditPLCButton = wx.Button(id=wxID_BEREMIZEDITPLCBUTTON, label='Edit\nPLC', |
298 self.EditPLCButton = wx.Button(id=wxID_BEREMIZEDITPLCBUTTON, label='Edit\nPLC', |
277 name='EditPLCButton', parent=self, pos=wx.Point(0, 0), |
299 name='EditPLCButton', parent=self, pos=wx.Point(0, 0), |
278 size=wx.Size(48, 48), style=0) |
300 size=wx.Size(48, 48), style=0) |
279 self.EditPLCButton.Bind(wx.EVT_BUTTON, self.OnEditPLCButton, |
301 self.EditPLCButton.Bind(wx.EVT_BUTTON, self.OnEditPLCButton, |
297 self.RunButton.Bind(wx.EVT_BUTTON, self.OnRunButton, |
319 self.RunButton.Bind(wx.EVT_BUTTON, self.OnRunButton, |
298 id=wxID_BEREMIZRUNBUTTON) |
320 id=wxID_BEREMIZRUNBUTTON) |
299 |
321 |
300 self.BusList = wx.ListBox(choices=[], id=wxID_BEREMIZBUSLIST, |
322 self.BusList = wx.ListBox(choices=[], id=wxID_BEREMIZBUSLIST, |
301 name='BusList', parent=self, pos=wx.Point(0, 0), |
323 name='BusList', parent=self, pos=wx.Point(0, 0), |
302 size=wx.Size(-1, -1), style=0) |
324 size=wx.Size(-1, -1), style=wxLB_SINGLE|wxLB_NEEDED_SB) |
303 self.BusList.Bind(wx.EVT_LEFT_DCLICK, self.OnBusListDClick) |
325 self.BusList.Bind(wx.EVT_LISTBOX_DCLICK, self.OnBusListDClick, |
|
326 id=wxID_BEREMIZBUSLIST) |
304 |
327 |
305 self.AddBusButton = wx.Button(id=wxID_BEREMIZADDBUSBUTTON, label='Add', |
328 self.AddBusButton = wx.Button(id=wxID_BEREMIZADDBUSBUTTON, label='Add', |
306 name='AddBusButton', parent=self, pos=wx.Point(0, 0), |
329 name='AddBusButton', parent=self, pos=wx.Point(0, 0), |
307 size=wx.Size(48, 48), style=0) |
330 size=wx.Size(48, 48), style=0) |
308 self.AddBusButton.Bind(wx.EVT_BUTTON, self.OnAddBusButton, |
331 self.AddBusButton.Bind(wx.EVT_BUTTON, self.OnAddBusButton, |
322 self.CurrentProjectPath = "" |
345 self.CurrentProjectPath = "" |
323 |
346 |
324 self.PLCManager = None |
347 self.PLCManager = None |
325 self.PLCEditor = None |
348 self.PLCEditor = None |
326 self.BusManagers = {} |
349 self.BusManagers = {} |
|
350 |
|
351 self.Log = LogPseudoFile(self.LogConsole) |
327 |
352 |
328 self.RefreshButtons() |
353 self.RefreshButtons() |
329 self.RefreshMainMenu() |
354 self.RefreshMainMenu() |
330 |
355 |
331 def RefreshButtons(self): |
356 def RefreshButtons(self): |
533 def OnDeleteBusButton(self, event): |
563 def OnDeleteBusButton(self, event): |
534 self.DeleteBus() |
564 self.DeleteBus() |
535 event.Skip() |
565 event.Skip() |
536 |
566 |
537 def OnBusListDClick(self, event): |
567 def OnBusListDClick(self, event): |
538 selected = self.BusList.GetSelection() |
568 selected = event.GetSelection() |
539 busidlist = self.BusManagers.keys() |
569 busidlist = self.BusManagers.keys() |
540 busidlist.sort() |
570 busidlist.sort() |
541 bus_infos = self.BusManagers[busidlist[selected]] |
571 bus_infos = self.BusManagers[busidlist[selected]] |
542 if bus_infos["Type"] == "CanFestival": |
572 if bus_infos["Type"] == "CanFestival": |
543 if bus_infos["Editor"] == None: |
573 if bus_infos["Editor"] == None: |
600 self.PLCEditor.RefreshFileMenu() |
630 self.PLCEditor.RefreshFileMenu() |
601 self.PLCEditor.RefreshEditMenu() |
631 self.PLCEditor.RefreshEditMenu() |
602 self.PLCEditor.RefreshToolBar() |
632 self.PLCEditor.RefreshToolBar() |
603 self.PLCEditor.Show() |
633 self.PLCEditor.Show() |
604 |
634 |
|
635 def BuildAutom(self): |
|
636 if self.PLCManager: |
|
637 self.TargetDir = os.path.join(self.CurrentProjectPath, "build") |
|
638 if not os.path.exists(self.TargetDir): |
|
639 os.mkdir(self.TargetDir) |
|
640 self.Log.flush() |
|
641 sys.stdout = self.Log |
|
642 try: |
|
643 print "Building ST Program..." |
|
644 plc_file = os.path.join(self.TargetDir, "plc.st") |
|
645 result = self.PLCManager.GenerateProgram(plc_file) |
|
646 if not result: |
|
647 raise Exception |
|
648 print "Compiling ST Program in to C Program..." |
|
649 status, result = commands.getstatusoutput("../matiec/iec2cc %s -I ../matiec/lib %s"%(plc_file, self.TargetDir)) |
|
650 if status: |
|
651 print result |
|
652 raise Exception |
|
653 print "Extracting Located Variables..." |
|
654 location_file = open(os.path.join(self.TargetDir,"LOCATED_VARIABLES.h")) |
|
655 locations = [] |
|
656 lines = [line.strip() for line in location_file.readlines()] |
|
657 for line in lines: |
|
658 result = LOCATED_MODEL.match(line) |
|
659 if result: |
|
660 locations.append(result.groups()) |
|
661 print "Generating Network Configurations..." |
|
662 for bus_id, bus_infos in self.BusManagers.items(): |
|
663 if bus_infos["Type"] == "CanFestival": |
|
664 master = config_utils.GenerateConciseDCF(locations, bus_id, bus_infos["NodeList"]) |
|
665 result = gen_cfile.GenerateFile("%s.c"%os.path.join(self.TargetDir, gen_cfile.FormatName(bus_infos["Name"])), master) |
|
666 if result: |
|
667 raise Exception |
|
668 print "Generating Makefiles..." |
|
669 |
|
670 print "Compiling Project..." |
|
671 |
|
672 print "\nBuild Project completed" |
|
673 except Exception, message: |
|
674 pass |
|
675 sys.stdout = sys.__stdout__ |
|
676 |
605 #------------------------------------------------------------------------------- |
677 #------------------------------------------------------------------------------- |
606 # Add Bus Dialog |
678 # Add Bus Dialog |
607 #------------------------------------------------------------------------------- |
679 #------------------------------------------------------------------------------- |
608 |
680 |
609 [wxID_ADDBUSDIALOG, wxID_ADDBUSDIALOGMAINPANEL, |
681 [wxID_ADDBUSDIALOG, wxID_ADDBUSDIALOGMAINPANEL, |