【问题标题】:Create DDE server in python and send data continuously在python中创建DDE服务器并连续发送数据
【发布时间】:2015-10-22 21:43:54
【问题描述】:

我正在尝试在 python 中编写一个 DDE 服务器,它需要将不断变化的字符串发送到作为 DDE 客户端连接的程序。

连接到 DDE 服务器的程序使用以下 DDE 设置连接 [Service: Orbitron, Topic: Tracking, Item: Tracking]。 程序必须接收 DDE 服务器以以下字符串格式发送的信息: "UP0 DN145000001 UMusb DMfm AZ040 EL005 SNNO 卫星"。

此字符串的内容大约每秒更改一次,我希望 DDE 服务器将新字符串发送到连接的 DDE 客户端,例如每秒一次。

我目前正在使用下面的代码,它是原始ddeserver.py文件see here的略微修改版本。

import win32ui
from pywin.mfc import object
import dde


class MySystemTopic(object.Object):
        def __init__(self):
                object.Object.__init__(self, dde.CreateServerSystemTopic())

        def Exec(self, cmd):
                print "System Topic asked to exec", cmd

class MyOtherTopic(object.Object):
        def __init__(self, topicName):
                object.Object.__init__(self, dde.CreateTopic(topicName))

        def Exec(self, cmd):
                print "Other Topic asked to exec", cmd

class MyRequestTopic(object.Object):
        def __init__(self, topicName):
                topic = dde.CreateTopic(topicName)
                topic.AddItem(dde.CreateStringItem(""))
                object.Object.__init__(self, topic)

        def Request(self, aString):
                print "Request Topic sent: ", aString
                a="UP0 DN145800001 UMusb DMfm AZ040 EL005 SNNO SATELLITE"
                print a
                return(a)


server = dde.CreateServer()
server.AddTopic(MyRequestTopic("Tracking"))
server.Create('Orbitron')    

while 1:
        win32ui.PumpWaitingMessages(0, -1)
        

当我运行代码时,我可以成功连接到程序并且字符串(如代码中提供的)被接收一次。我尝试了一些不同的方法,但我想不出如何更改为 python 代码以使 DDE 服务器连续重新发送循环或类似的字符串。

非常欢迎任何建议。

附:我对 python、DDE 和这个论坛比较陌生,如果有不清楚的地方,我很抱歉。请告诉我。

【问题讨论】:

    标签: python server dde


    【解决方案1】:
    # coded by JayleoPlayGround
    # use Portable Python 2.7.5.1 + pywin32-214
    
    
    import time
    import win32ui, dde
    from pywin.mfc import object
    
    
    class DDETopic(object.Object):
        def __init__(self, topicName):
            self.topic = dde.CreateTopic(topicName)
            object.Object.__init__(self, self.topic)
            self.items = {}
    
        def setData(self, itemName, value):
            try:
                self.items[itemName].SetData( str(value) )
            except KeyError:
                if itemName not in self.items:
                    self.items[itemName] = dde.CreateStringItem(itemName)
                    self.topic.AddItem( self.items[itemName] )
                    self.items[itemName].SetData( str(value) )
    
    
    ddeServer = dde.CreateServer()
    ddeServer.Create('Orbitron')
    ddeTopic = DDETopic('Tracking')
    ddeServer.AddTopic(ddeTopic)
    
    while True:
        yourData = time.ctime() + ' UP0 DN145000001 UMusb DMfm AZ040 EL005 SNNO SATELLITE'
        ddeTopic.setData('Tracking', yourData)
        win32ui.PumpWaitingMessages(0, -1)
        time.sleep(0.1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2020-05-01
      相关资源
      最近更新 更多