日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 基于DDE協(xié)議的Modbus Plus與Ethernet數(shù)據(jù)交換

          基于DDE協(xié)議的Modbus Plus與Ethernet數(shù)據(jù)交換

          作者: 時間:2012-08-14 來源:網(wǎng)絡 收藏

            (2) 通過dde協(xié)議的服務名,主題名和項目名來訪問具體某個項目的數(shù)據(jù)

            先定義dde的服務名和主題名

          tchar szapp[] = text("mbplus"); //服務名
          tchar sztopic4[]=text("plc4"); //主題名
          tchar szwidth[]=text("400401"); //項目名
          tchar vlwidth[16]; //儲存從dde服務器返回的數(shù)據(jù)
          hsz hszwidth; //項目的dde標識

            //項目名的命名規(guī)則具體可以查看mbplus程序的說明,這里400401表示的是項目在plc中的地址,這是一個//16位的整形數(shù)

          hconv4=ddeconnecttotopic(idinst,szapp,sztopic4,hsztopic4);
          if( hconv4 != null )
          {
          ddeautorequest(idinst,hconv4,szwidth,hszwidth);
          }

            此處的ddeconnecttotopic和ddeautorequest為自定義的函數(shù)

          hconv ddeconnecttotopic(dword idddeinst, lpctstr szapp, lpctstr
          sztopic, hsz * hsztopic)
          {
          hsz
          hszapp=ddecreatestringhandle(idddeinst,szapp,cp_winunicode);
          *
          hsztopic=ddecreatestringhandlew(idddeinst,sztopic,cp_winunicode);
          return ddeconnect(idddeinst,hszapp,*hsztopic,null);
          }

            使用ddecreatestringhandle來獲取服務器和主題名的字符句柄,使用ddeconnect來建立指定服務下的指定的主題的連接。之后使用ddeautorequest函數(shù)來向dde服務器請求置頂項目名的數(shù)據(jù),在數(shù)據(jù)發(fā)生變化后dde服務器會自動將新的數(shù)據(jù)發(fā)送給客戶端,客戶端在回調(diào)函數(shù)中就可以獲取到新的數(shù)據(jù),具體方法見下文。如果某個主題下有多個項目需要處理,都用ddeautorequest來處理即可,此函數(shù)的實現(xiàn)如下:

          void ddeautorequest(dword idddeinst, hconv hconv, lpctstr
          szitem, hsz * hszitem)
          {
          *hszitem=ddecreatestringhandle(idddeinst,szitem,cp_winunicode);
          hddedata hdata=ddeclienttransaction
          (null,0,hconv,*hszitem,cf_text,xtyp_advstart| xtypf_ackreq,5000,
          null);
          }

            接下來就可以通過dde的回調(diào)函數(shù)來獲取數(shù)據(jù)了。

          hddedata callback ddecallback(uint utype,uint ufmt,hconv
          hconv,hsz hsz1,hsz hsz2,hddedata hdata,
          dword dwdata1,dword dwdata2)
          {
          switch( utype )
          {
          case xtyp_advdata: //處理dde數(shù)據(jù)
          if( ufmt != cf_text )
          return dde_fnotprocessed;
          memset(szbuffer,0,64); //初始化緩沖區(qū)
          datalen=ddegetdata(hdata,null,64,0);//獲取數(shù)據(jù)的長度
          ddegetdata(hdata,(unsigned char* )szbuffer,datalen,0);//獲取數(shù)據(jù)
          if( hsz1==hsztopic4 hsz2 == hszwidth ) //判斷數(shù)據(jù)對應于那個數(shù)據(jù)項,并作出具體處理
          {
          _atoflt(fltval,szbuffer); //dde的數(shù)據(jù)以字符串形式創(chuàng)送過來的,這里將它轉(zhuǎn)換成數(shù)值
          ptele1101->msgblock1.stripwidth=fltval.f; //將數(shù)字存入緩沖區(qū)
          }
          return ( hddedata )dde_fack;
          }
          return 0;
          }

            本例中dde回調(diào)函數(shù)要處理的dde項目不止一個,任意一個dde項目的值發(fā)生變化時,回調(diào)函數(shù)都會被自動調(diào)用一次,具體實現(xiàn)時只需要用if語句對hsz1和hsz2進行逐一比較即可處理所有的項目。緩沖區(qū)ptele1101用于存儲通過以太網(wǎng)發(fā)送到板型儀的數(shù)據(jù),下文中將詳細介紹。



          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉