新聞中心

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

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

作者: 時(shí)間:2012-08-14 來(lái)源:網(wǎng)絡(luò) 收藏

  (2) 通過(guò)dde協(xié)議的服務(wù)名,主題名和項(xiàng)目名來(lái)訪問(wèn)具體某個(gè)項(xiàng)目的數(shù)據(jù)

  先定義dde的服務(wù)名和主題名

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

  //項(xiàng)目名的命名規(guī)則具體可以查看mbplus程序的說(shuō)明,這里400401表示的是項(xiàng)目在plc中的地址,這是一個(gè)//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來(lái)獲取服務(wù)器和主題名的字符句柄,使用ddeconnect來(lái)建立指定服務(wù)下的指定的主題的連接。之后使用ddeautorequest函數(shù)來(lái)向dde服務(wù)器請(qǐng)求置頂項(xiàng)目名的數(shù)據(jù),在數(shù)據(jù)發(fā)生變化后dde服務(wù)器會(huì)自動(dòng)將新的數(shù)據(jù)發(fā)送給客戶端,客戶端在回調(diào)函數(shù)中就可以獲取到新的數(shù)據(jù),具體方法見(jiàn)下文。如果某個(gè)主題下有多個(gè)項(xiàng)目需要處理,都用ddeautorequest來(lái)處理即可,此函數(shù)的實(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);
}

  接下來(lái)就可以通過(guò)dde的回調(diào)函數(shù)來(lái)獲取數(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ù)的長(zhǎng)度
ddegetdata(hdata,(unsigned char* )szbuffer,datalen,0);//獲取數(shù)據(jù)
if( hsz1==hsztopic4 hsz2 == hszwidth ) //判斷數(shù)據(jù)對(duì)應(yīng)于那個(gè)數(shù)據(jù)項(xiàng),并作出具體處理
{
_atoflt(fltval,szbuffer); //dde的數(shù)據(jù)以字符串形式創(chuàng)送過(guò)來(lái)的,這里將它轉(zhuǎn)換成數(shù)值
ptele1101->msgblock1.stripwidth=fltval.f; //將數(shù)字存入緩沖區(qū)
}
return ( hddedata )dde_fack;
}
return 0;
}

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



評(píng)論


相關(guān)推薦

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

關(guān)閉