美文网首页
用python实现日立DCS的atdoc文件整合

用python实现日立DCS的atdoc文件整合

作者: LienZzzz | 来源:发表于2017-07-04 01:50 被阅读83次

    项目目的

    • 日立DCS的atdoc文件夹中,有各个控制器的IO清册,但清册分散在各个文件夹中,并无总表

    • 此项目的目的在于将各个文件夹的csv文件整合在一个总表中

    项目说明

    • 编程软件 Python 2.7.6
    • 环境 Win 10
    • UI实现 wxPython
    • 软件打包 py2exe

    项目目录

    image.png

    代码实现

    功能实现

    CSV_Process.py

    # -*- coding: UTF-8 -*-
    import os
    import csv
    import sys 
    reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入 
    sys.setdefaultencoding('utf-8')
    
    """
    lienDir = "C:\Python27/lelab/atdoc/LH"
    resultDir = "C:\Python27/lelab"
    """
    def combinecsv(exedir):
        resultDir = exedir[:-6]
        lienDir = resultDir + "/atdoc/LH"
        sheetDI = []
        sheetDO = []
        sheetAI = []
        sheetAO = []
        sheetRTD = []
        sheetTHC = []
        sheetALL = []
    
        HeadLineAI = ["数据类型".encode('GB18030'), "数据类型".encode('GB18030'), "PI层逻辑页号".encode('GB18030'), "控制器".encode('GB18030'), "", "", "单元号".encode('GB18030'), "槽号".encode('GB18030'), "卡件型号".encode('GB18030'), "", "", "通道序号".encode('GB18030'), "通道号".encode('GB18030'), "信号名称".encode('GB18030'), "物理量程下限".encode('GB18030'), "物理量程上限".encode('GB18030'), "物理量程单位".encode('GB18030'), "span", "工程量程下限".encode('GB18030'), "工程量程上限".encode('GB18030'), "工程量程单位".encode('GB18030'), "执行量程下限".encode('GB18030'), "执行量程上限".encode('GB18030'), "执行量程单位".encode('GB18030'), "诊断量程下限".encode('GB18030'), "诊断量程上限".encode('GB18030'), "IL层逻辑页号".encode('GB18030'), "", "", "KKS".encode('GB18030'), "端子坐标".encode('GB18030'), "内部地址1".encode('GB18030'), "内部地址2".encode('GB18030'), "内部地址3".encode('GB18030')]
        sheetAI.append(HeadLineAI)
    
        HeadLineALL = ["数据类型".encode('GB18030'), "数据类型".encode('GB18030'), "PI层逻辑页号".encode('GB18030'), "控制器".encode('GB18030'), "", "", "单元号".encode('GB18030'), "槽号".encode('GB18030'), "卡件型号".encode('GB18030'), "", "", "通道序号".encode('GB18030'), "通道号".encode('GB18030'), "信号名称".encode('GB18030'), "IL层逻辑页号".encode('GB18030'), "", "", "KKS".encode('GB18030'), "端子坐标".encode('GB18030'), "内部地址1".encode('GB18030'), "内部地址2".encode('GB18030'), "内部地址3".encode('GB18030')]
        sheetALL.append(HeadLineALL)
    
        Fatherdir = os.listdir(lienDir)
        for fatherlist in Fatherdir:
            if fatherlist != ".DS_Store":
                    liendir = lienDir + "/" + fatherlist + "/pio_ref/pio_csv"
                    try:
                        sonDir = os.listdir(liendir)
                        for sonList in sonDir:
                            if sonList != ".DS_Store":
                                csvSample = open(liendir + "/" + sonList, 'rU')
                                csvReader = csv.reader(csvSample)
                                for row in csvReader:
                                    if row[0] == "DI32":
                                            row[17] = row[17].replace(' ', '')
                                            sheetDI.append(row)
                                            sheetALL.append(row)
                                    elif row[0] == "DO32":
                                            row[17] = row[17].replace(' ', '')
                                            sheetDO.append(row)
                                            sheetALL.append(row)
                                    elif row[0] == "AO8 ":
                                            sheetAO.append(row)
                                            row = row[0:14] + row[24:]
                                            row[17] = row[17].replace(' ', '')
                                            sheetALL.append(row)
                                    elif row[0] == "AI16":
                                            row[29] = row[29].replace(' ', '')
                                            sheetAI.append(row)
                                            row = row[0:14] + row[26:]
                                            row[17] = row[17].replace(' ', '')
                                            sheetALL.append(row)
                                    elif row[0] == "RTD16":
                                            sheetRTD.append(row)
                                            row = row[0:14] + row[24:]
                                            row[17] = row[17].replace(' ', '')
                                            sheetALL.append(row)
                                    elif row[0] == "THC ":
                                            sheetTHC.append(row)
                                            row = row[0:14] + row[24:]
                                            row[17] = row[17].replace(' ', '')
                                            sheetALL.append(row)
                                csvSample.close()
                                 
                    except OSError:
                        print fatherlist + " NO DATA"
    
        def _savesheet(sourcesheet, desitination):
            writeObj = open(resultDir + '/' + desitination, 'wb')
            writer = csv.writer(writeObj)
            for row in sourcesheet:
                writer.writerow(row)
            writeObj.close()
    
        _savesheet(sheetDI, "DI.csv")
        _savesheet(sheetDO, "DO.csv")
        _savesheet(sheetAI, "AI.csv")
        _savesheet(sheetAO, "AO.csv")
        _savesheet(sheetRTD, "RTD.csv")
        _savesheet(sheetTHC, "THC.csv")
        _savesheet(sheetALL, "ALL.csv")
    
        print u"完成"
    

    UI框架

    LeGUI_setup.py

    # -*- coding: UTF-8 -*-
    import os
    import csv
    import sys 
    reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入 
    sys.setdefaultencoding('utf-8')
    
    import wx
    from CSV_Process import *
    
    """
    def load(event):
        
        file = open(filename.GetValue())
        
        contents.SetValue(file.read())
        
    
        file.close()
    """
    
    def load(event):
        dialog = wx.DirDialog(None, 'choose a directory: ', style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        if dialog.ShowModal() == wx.ID_OK:
            path = dialog.GetPath()
            filename.SetValue(path)
            if path[-5:] == "atdoc":
                contents.SetValue("You can execute now")
            else:
                contents.SetValue("Please choose the right path of atdoc")
        dialog.Destroy()
    
    
    """
    def save(event):
        
        file = open(filename.GetValue(), 'w')
        
        file.write(contents.GetValue())
        
        file.close()
    """
    
    def save(event):
        exedir = filename.GetValue()
        try:
            combinecsv(exedir)
            messageshow("Complete!You can find the csv files in " + exedir[:-6])
        except:
            messageshow("Something is Wrong")
    
    
    def messageshow(msg):
        dlg = wx.MessageDialog(None, msg, 'A Message Box', wx.OK|wx.ICON_INFORMATION)
        retCode = dlg.ShowModal()
        dlg.Destroy()
    
    
    app = wx.App(True)
    
    win = wx.Frame(None, title="LeGUI", size=(410,335))
    
    
    
    bkg = wx.Panel(win)
    
    loadButton = wx.Button(bkg, label='OPEN')
    
    loadButton.Bind(wx.EVT_BUTTON, load)
    
    
    saveButton = wx.Button(bkg, label='EXECUTE')
    
    saveButton.Bind(wx.EVT_BUTTON, save)
    
    
    filename = wx.TextCtrl(bkg)
    
    contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL)
    
    
    hbox = wx.BoxSizer()
    
    hbox.Add(filename, proportion=1, flag=wx.EXPAND)
    
    hbox.Add(loadButton, proportion=0, flag=wx.LEFT, border=5)
    
    hbox.Add(saveButton, proportion=0, flag=wx.LEFT, border=5)
    
    
    vbox = wx.BoxSizer(wx.VERTICAL)
    
    vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
    
    vbox.Add(contents, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
    
    
    bkg.SetSizer(vbox)
    
    win.Show()
    
    
    app.MainLoop()
    

    软件打包

    LeGUI_setup.py

    # -*- coding: utf-8 -*-
    
    from distutils.core import setup
    import py2exe
    
    includes = ["encodings", "encodings.*"]
    zipfile = r'lib/sharezip'
    options = {"py2exe":
                {
                    "compressed": 1,
                    "optimize": 2,
                    "includes": includes
                }
            }
    
    setup(
        version = "1.0",
        description = "H5000M atdoc Combine Tools",
        name = "HAC Tool",
        options = options,
        zipfile = zipfile,
        console = [{"script": "LeGUI.py", "iconresources": [(1, "Hac.icon")] }]
        )
    
    • Console中进入在LeGUI_setup.py所在的目录中,并执行如下命令生成exe文件
    python LeCUI_setup.py install
    python LeCUI_setup.py py2exe
    
    • 在项目目录下生成build、dist文件夹
    image.png
    • dist文件夹下有exe执行文件和必须的系统文件,将dist文件夹拷贝到其他WIN10电脑上,即可使用

    相关文章

      网友评论

          本文标题:用python实现日立DCS的atdoc文件整合

          本文链接:https://www.haomeiwen.com/subject/jukthxtx.html