Ejemplo n.º 1
0
def ddefunc(datahubname, topic, filename, txtname):
    pythoncom.CoInitialize()
    xlApp = DispatchEx("Excel.Application")
    xlApp.Visible = 0  # 隐藏
    xlApp.Application.DisplayAlerts = 0  # 禁止弹出会话
    nChan = xlApp.Application.DDEInitiate(datahubname, topic)  # datahub名称
    arrname = Opentxt(filename).split(",")  # tagname
    timestamp = timefunc()  # timestamp
    if(os.path.exists(txtname)):
        os.remove(txtname)
    for i in arrname:
        DDEVALUE = xlApp.DDErequest(nChan, i)
        if not DDEVALUE[0]:
            linex = 'TAGNAME='+i.replace(".Value", "") + '\n'
            liney = 'ITEM=VALUE,VALUE={},TIMESTAMP={},QUALITY=192'.format(
                0, timestamp) + '\n'
            linez = linex+liney
        else:
            # dde = DDE.DDEClient(datahubname, topic)
            # if(os.path.exists(txtname)):
            #     os.remove(txtname)
            # for i in arrname:
            #     # repi = i.replace(".value", "")
            #     DDEVALUE = dde.request(i)
            # print(DDEVALUE)

            linex = 'TAGNAME='+i.replace(".Value", "") + '\n'
            liney = 'ITEM=VALUE,VALUE={},TIMESTAMP={},QUALITY=192'.format(
                DDEVALUE[0], timestamp) + '\n'
            linez = linex+liney
    #             print(linez)
        with open(txtname, "a+") as f:
            f.write(linez)
    xlApp.Quit()
    pythoncom.CoUninitialize()
def ddefunc(datahubname, topic, filename):
    pythoncom.CoInitialize()
    xlApp = DispatchEx("Excel.Application")
    xlApp.Visible = 0  #隐藏
    xlApp.Application.DisplayAlerts = 0  #禁止弹出会话
    nChan = xlApp.Application.DDEInitiate(datahubname, topic)  #datahub名称
    arrname = Opentxt(filename).split(",")  #tagname
    timestamp = timefunc()  #timestamp
    ValueResult = []
    Jsonlist = []
    for i in arrname:
        repi = i.replace(".value", "")
        DDEVALUE = xlApp.DDErequest(nChan, i)

        if not DDEVALUE[0]:
            pass
        else:
            ValueResult.append(
                Tag(i.replace(".Value", ""), timestamp, str(DDEVALUE[0])))
    for tag in ValueResult:
        Jsonlist.append(eval(json.dumps(tag.__dict__)))
    xlApp.Quit()
    pythoncom.CoUninitialize()
    return Jsonlist