本文介绍了在python中创建DDE服务器并连续发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

连接到 DDE 服务器的程序使用以下 DDE 设置来连接 [服务:Orbitron,主题:跟踪,项目:跟踪].该程序必须接收由 DDE 服务器以以下字符串格式发送的信息:UP0 DN145000001 UMusb DMfm AZ040 EL005 SNNO 卫星".

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".

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

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.

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

I am currently using the code below, which is a slightly modified version of the original ddeserver.py file, 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 服务器在循环或类似情况下不断重新发送字符串.

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.

欢迎提出任何建议.

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

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

推荐答案

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

这篇关于在python中创建DDE服务器并连续发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 14:17