发表于:2004-01-04 17:28:00
12楼
在zhangchaoyu的帮助下,我的OPC XML 服务的GetStatus方法终于大功告成了,下面是我写的部分代码,请zhangchaoyu和其他朋友批评指教!
[WebMethod]
public ReplyBase GetStatus([System.Xml.Serialization.XmlAttributeAttribute()]string LocaleID,[System.Xml.Serialization.XmlAttributeAttribute()]string ClientRequestHandle,out ServerStatus Status)
{
string progid,nodename;
progid="OPCJ.SampleServer.1";
nodename="";
OPCAutomation.OPCServerClass opcs=new OPCAutomation.OPCServerClass();
opcs.Connect(progid,nodename);
Status=new ServerStatus();
Status.VendorInfo ="opc xml da server 1.0 by zxm";
Status.StartTime =opcs.StartTime ;
Status.ProductVersion ="zxm 1.0";
switch (opcs.ServerState)
{
case 1:
Status.StatusInfo="OPC server is running";
break;
case 2:
Status.StatusInfo="OPC server is failed";
break;
case 3:
Status.StatusInfo="OPC is noconfig";
break;
case 4:
Status.StatusInfo ="opc server is suspended";
break;
case 5:
Status.StatusInfo ="opc server is test";
break;
case 6:
Status.StatusInfo ="opc server is disconnected";
break;
}
Status.SupportedInterfaceVersions[0]=interfaceVersion.XML_DA_Version_1_0;
Status.SupportedLocaleIDs[0]="en_us";
Status.SupportedLocaleIDs[1]="de";
ReplyBase ReturnResult=new ReplyBase();
ReturnResult.RcvTime=System.DateTime.Now;
ReturnResult.RevisedLocaleID=LocaleID;
ReturnResult.ClientRequestHandle=ClientRequestHandle;
ReturnResult.ReplyTime=System.DateTime.Now;
switch(opcs.ServerState)
{
case 1:
ReturnResult.ServerState=serverState.running;
break;
case 2:
ReturnResult.ServerState=serverState.failed;
break;
case 3:
ReturnResult.ServerState=serverState.noConfig;
break;
case 4:
ReturnResult.ServerState=serverState.suspended;
break;
case 5:
ReturnResult.ServerState=serverState.test;
break;
case 6:
ReturnResult.ServerState=serverState.commFault;
break;
}
return ReturnResult;
}
程序运行通过,用OPC XML客户端测试能访问到正确结果。
与 OPC COM服务器的交互采用的是自动化接口,这样可能不太好,理想情况下应该是采用自定义接口,可是在用TLBIMP.EXE时出现了一些错误所以就引用了OPCAutomation组件,不知这样行不行。