我正在尝试用 python 编写一个 DDE 服务器,它需要将不断变化的字符串发送到作为 DDE 客户端连接的程序。
连接到 DDE 服务器的程序使用以下 DDE 设置来连接 [服务:Orbitron,主题:Tracking,项目:Tracking]。
该程序必须接收 DDE 服务器以以下字符串格式发送的信息:
“UP0 DN145000001 UMusb DMfm AZ040 EL005 SNNO 卫星”。
该字符串的内容大约每秒都会发生变化,我希望 DDE 服务器将新字符串发送到连接的 DDE 客户端,例如每秒。
我目前正在使用下面的代码,它是原始代码的稍加修改的版本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)
当我运行代码时,我可以成功连接该程序,并且一次接收到该字符串(如代码中提供的)。我尝试了一些不同的方法,但我还想不出如何更改为 python 代码,以便让 DDE 服务器在循环或类似的情况下不断重新发送字符串。
任何建议都将非常受欢迎。
附:我对 python、DDE 和这个论坛比较陌生,如果有不清楚的地方,我很抱歉。请告诉我。
# 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)