• DDE 的知识和使用


    github上下载.net 版本的NDde 开发包

     或者在此处下载开发包

    MSDN 地址

    创建服务器

        class BasicDDE:DdeServer
        {
            public BasicDDE(string ServiceName):base(ServiceName)
            {
            }
        }

    在主类中

            private void MakeServer()
            {
                BasicDDE server = new BasicDDE("BasicDDE");
                server.Register();
            }

    服务器的创建关键就是服务器名称。

    创建客户端

            private void MakeClient(string ServiceName,string topic)
            {try
                {
                    client = new DdeClient(ServiceName, topic,this);
                    client.Connect();
                }
                catch (Exception)
                {
              client
    = null; MessageBox.Show("DDE Client Creation failed"); } }

    关键是对应的服务器名称和topic名称。

    HotLink模式

    类似于长连接,服务器可以主动推送相关信息给客户端。

    这里的例子服务器开始定时器,每个一秒调用Advise函数

    Advise("*", "*");
    //向所有topic和item发送消息

    发送的时候,会调用

            protected virtual byte[] OnAdvise(string topic, string item, int format);

    发送具体信息。

    服务器全部代码

        class BasicDDE:DdeServer
        {
            public System.Timers.Timer timer = new System.Timers.Timer();
            public BasicDDE(string ServiceName):base(ServiceName)
            {
                timer.Elapsed += timer_Elapsed;
                timer.Interval = 1000;
                timer.SynchronizingObject = this.Context;
            }
    
            void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                Advise("*", "*");
            }
    
            protected override byte[] OnAdvise(string topic, string item, int format)
            {
                return System.Text.Encoding.UTF8.GetBytes("Server Advise:" + DateTime.Now.ToString());
            }
        }
    View Code

    客户端

    在连接前注册Advice回调函数

                    client.Advise += client_Advise;

    连接后针对某一个item建立hotlink

                client.StartAdvise("myadvice", 1, true, 60000);

    全部代码

        public partial class Form1 : Form
        {
            private DdeClient client;
            public Form1()
            {
                InitializeComponent();
                MakeServer();
            }
    
            private void MakeClient(string ServiceName,string topic)
            {
                if(string.IsNullOrEmpty(ServiceName)||string.IsNullOrEmpty(topic))
                {
                    MessageBox.Show("数值不可为空");
                    return;
                }
                try
                {
                    client = new DdeClient(ServiceName, topic,this);
                    client.Advise += client_Advise;
                    client.Connect();
    
                }
                catch (Exception)
                {
    
                    client = null;
                    MessageBox.Show("DDE Client Creation failed");
                }
                client.StartAdvise("myadvice", 1, true, 60000);
            }
    
            void client_Advise(object sender, DdeAdviseEventArgs e)
            {
                this.Text = "On Advise: " + e.Text;
            }
    
            private void MakeServer()
            {
                BasicDDE server = new BasicDDE("BasicDDE");
                server.Register();
                server.timer.Start();
            }
    
            private void connect_Click(object sender, EventArgs e)
            {
                MakeClient(tbDDEService.Text, tbTopic.Text);
            }
        }
    View Code

    执行命令

    客户端执行调用

    client.Execute("mycmd", 60000);

    命令名随意,

    服务器调用

            protected override ExecuteResult OnExecute(DdeConversation conversation, string command)
            {
                //命令处理
                return ExecuteResult.Processed;
            }

    其中conversion.topic 可获取topic名称。

    读写Item

    这里用的都是同步操作,会等待直到超时,也可用异步。

    Item的值

    client.Poke("myitem", DateTime.Now.ToString(), 60000);

    服务器响应函数

            protected override PokeResult OnPoke(DdeConversation conversation, string item, byte[] data, int format)
            {
                return PokeResult.Processed;
            }

    应该根据item将data存储。

    Item的值

    client.Request("myitem", 60000)

    服务器响应函数

            protected override RequestResult OnRequest(DdeConversation conversation, string item, int format)
            {
                return new RequestResult(System.Text.Encoding.ASCII.GetBytes("Your Requested Data" + ""));
            }

    Macro的知识

    使用记事本创建mymacro.mac的文件,

    内容如下

      Name MyMessage

        Print "This is s messge!"

      EndMacro

     载入Macro

      在控制台命令行输入     Macro "mymacro.mac"然后回车

      输入Macro 然后回车,调出所有可用的Macro。

      任何不在Name和Macro之间的命令在加载的时候被执行,可以用来做初始化。

    执行Macro

      输入macro的名字即可。比如MyMessage,然后回车

      输入Macro "mymacro.mac", go 然后回车,如果文件包含多个macro,执行最后一个。

     函数Macro

      

       名称必须以$结束,具有返回值。

            比如查看ChemStation执行路径,在控制台打印相关函数宏。Print _AutoPath$

     删除Macro

      Remove MyMessage即可

    变量

      字符串变量 末尾必须是$,

      数值变量末尾不是$

      本地变量,前面加 Local,作用范围是本Macro,不加则是全局变量

      如果数值变量需要变成字符串显示,使用val$(NumberTwo)

      Message$="Number ="+val$(NumberTwo)

     系统变量

  • 相关阅读:
    mysql中drop、delete、truncate的区别简述
    hadoop之数据倾斜
    Mysql相关:navicat for mysql 加注释
    泛型
    工银亚洲见证开户详细过程和攻略
    classpath:和classpath*:的区别
    单索引与唯一索引
    MySQL中SQL语句之反引号,单引号
    《集体智慧编程》学习笔记 第三章
    《集体智慧编程》 读书笔记 第二章
  • 原文地址:https://www.cnblogs.com/noigel/p/11909870.html
Copyright © 2020-2023  润新知