Create DDE server in python and send data continuously
Question: I am trying to write a DDE server in python which needs to send a continuously
changing string to a program which is connected as a DDE client.

The program which connects to a DDE server uses the following DDE settings to
connect [Service: Orbitron, Topic: Tracking, Item: Tracking]. The program has
to receive information that is sent by the DDE server in the following string
format: "UP0 DN145000001 UMusb DMfm AZ040 EL005 SNNO SATELLITE".

The content of this string changes approximately every second and I want the
DDE server to send the new string to the connected DDE client, for example
every second.

I am currently using the code below, which is a slightly modified version of
the original _ddeserver.py_ file, [see
here](https://code.google.com/p/pythonxy/source/browse/src/python/pywin32/PLATLIB/win32/Demos/dde/ddeserver.py?repo=xy-27).

    
    
    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)
            

When I run the code I can successfully connect with the program and the string
(as provided in the code) is received one time. I tried a few different things
but I can not think of a way yet how to change to python code in order to have
the DDE server continuously resend the string in a loop or similar.

Any suggestions would be very welcome.

P.S. I am relatively new to python, DDE and this forum, my apologies if
something is unclear. Just let me know.


Answer: 
    # 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)
    

