Create DDE server in python and send data continuously

Nils picture Nils · May 10, 2015 · Viewed 7.5k times · Source

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.

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

JayleoPlayGround picture JayleoPlayGround · Oct 22, 2015
# 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)