您当前的位置:五五电子网电子知识电器控制PLC工业控制运用VC#编程通过OPC方式实现PC机与西门子PLC通讯--同步篇 正文
运用VC#编程通过OPC方式实现PC机与西门子PLC通讯--同步篇

运用VC#编程通过OPC方式实现PC机与西门子PLC通讯--同步篇

点击数:7235 次   录入时间:03-04 11:49:09   整理:http://www.55dianzi.com   PLC工业控制

error="";
int dwLCID = 0x407; //本地语言为英语 
int pRevUpdateRate;
float deadband = 0;
// 处理非托管COM内存
GCHandle hDeadband;
IntPtr pTimeBias = IntPtr.Zero;
hDeadband = GCHandle.Alloc(deadband,GCHandleType.Pinned);
try
{
pIOPCServer.AddGroup(groupName, //组名
bActive, //创建时,组是否被激活
updateRate, //组的刷新频率,以ms为单位
hClientGroup, //客户号
pTimeBias, //这里不使用
(IntPtr)hDeadband,
dwLCID, //本地语言
out nSvrGroupID, //移去组时,用到的组ID号
out pRevUpdateRate, //返回组中的变量改变时的最短通知时间间隔
ref iidRequiredInterface,
out pobjGroup1); //指向要求的接口
hClientGroup=hClientGroup+1;
int groupID=nSvrGroupID;
groupsID.Add(groupName,groupID);
}
catch (System.Exception err) //捕捉失败信息
{
error="错误信息:"+err.Message;
}
finally
{
if (hDeadband.IsAllocated) hDeadband.Free();
}
if(error=="")
return true;
else
return false;
}

4、 向指定的组中添加变量的函数
/// 添加多个项到组
/// 
/// 指定组名
/// 指定项名
/// 由函数返回的服务器确定的项ID号
/// 无错误,返回true,否则返回false
public bool AddItems(string groupName,string[] itemsName,int[] itemsID)
{
bool success=true;
OPCITEMDEF[] ItemDefArray=new OPCITEMDEF[itemsName.Length];
for(int i=0;i<ITEMSNAME.LENGTH;I++)
{
hClientItem=hClientItem+1;
ItemDefArray[i].szAccessPath = ""; // 可选的通道路径,对于Simatiic Net不需要。
ItemDefArray[i].szItemID = itemsName[i]; // ItemID, see above
ItemDefArray[i].bActive = 1; // item is active
ItemDefArray[i].hClient = hClientItem; // client handle 
ItemDefArray[i].dwBlobSize = 0; // blob size
ItemDefArray[i].pBlob = IntPtr.Zero; // pointer to blob 
ItemDefArray[i].vtRequestedDataType = 2; //Word数据类型
}
//初始化输出参数
IntPtr pResults = IntPtr.Zero;
IntPtr pErrors = IntPtr.Zero;
try 
{
// 添加项到组
((IOPCItemMgt)GetGroupByName(groupName)).AddItems(itemsName.Length,ItemDefArray,out pResults,out pErrors);
// Unmarshal to get the server handles out fom the m_pItemResult 
// after checking the errors
int[] errors = new int[itemsName.Length]; 
Marshal.Copy(pErrors, errors, 0,itemsName.Length);
IntPtr pos = pResults;
for(int i=0;i
{
if (errors[i] == 0)
{
OPCITEMRESULT result = (OPCITEMRESULT)Marshal.PtrToStructure(pos, typeof(OPCITEMRESULT));
itemsID[i] = result.hServer;
this.hitemsID.Add(itemsName[i],result.hServer);
pos = new IntPtr(pos.ToInt32() + Marshal.SizeOf(typeof(OPCITEMRESULT)));

}
else
{
success=false;
break;
}
}
}
catch (System.Exception err) // catch for error in adding items.
{
success=false;
}
finally
{
// 释放非托管内存
if(pResults != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pResults);
pResults = IntPtr.Zero;
}
if(pErrors != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pErrors);
pErrors = IntPtr.Zero; 
}
}
return success;
}

  说明:使用该函数时,在类的开头,应该先声明整数数据,以用于保存由本函数返回的服务器对每一项分配的Item ID号:

5、 向指定组中指定的一系列项变量写入数据的公开方法
/// 
/// 一次性写入多个值
/// 
/// 指定组名
/// 由服务器给每个项分配的标志号
/// 一系列值
/// 无错误,返回true,否则返回false
public bool Write(string groupName,int[] itemID,object[] values)
{
bool success=true;
IntPtr pErrors = IntPtr.Zero;

上一页  [1] [2] 


本文关键字:暂无联系方式PLC工业控制电器控制 - PLC工业控制