不卡影视,扒开女人下面使劲桶视频,高冷动漫女头像侧脸,欧洲av在线
                                    咨詢熱線

                                    18621916738

                                    當前位置:首頁  >  技術文章  >  運用VC#編程通過OPC方式實現PC機與西門子PLC通訊

                                    運用VC#編程通過OPC方式實現PC機與西門子PLC通訊

                                    更新時間:2016-01-06      點擊次數:2577

                                    運用VC#編程通過OPC方式實現PC機與西門子PLC通訊

                                    1、 配置OPC服務器
                                      對于服務器的配置與同步通訊的配置一樣,這里不需再講解,若有不清楚的,可以參閱之前發布的<運用VC#編程通過OPC方式實現PC機與西門子PLC通訊>

                                    2、 OPC編程
                                      變量組、項的命名規則與同步通訊的一樣,這里不再描敘,下面主要就開發一個異步通訊類 AsynServer來講解如何編程。

                                    <1>、引用
                                      在VC#開發環境中添加對OpcRcw.Da庫以及OpcRcw.Comn庫的引用,該庫屬于.NET庫,不屬于COM庫,西門子雖然編寫了類庫,以提供對.NET平臺的支持,但這些類庫仍然難于編程,里面包含了大量的在托管和非托管區傳輸數據,因此我們需要在它的基礎上再開發一個類庫,以簡化以后的編程,首先在類的開頭使用命名空間:
                                    using OpcRcw.Comn;
                                    using OpcRcw.Da;
                                    using System.Runtime.InteropServices;
                                    using System.Collections;

                                    <2>、編程
                                      異步編程的原理就是在OPC服務器那邊檢測當前活動的變量組,一但檢測到某一個變量,譬如變量Q0.0從1變成0,就會執行一個回調函數,以實現針對變量發生變化時需要實現的動作,在這里可以采用委托來實現該功能。

                                      1、 在命名空間的內部、類 AsynServer聲明之前添加委托的申明:
                                    // 定義用于返回發生變化的項的值和其對應的客戶句柄
                                    public delegate void DataChange(object[] values,int[] itemsID);

                                      2、 該類繼承于西門子提供的庫接口IOPCDataCallback
                                    public class AsynServer:IOPCDataCallback
                                    在類的開頭部分聲明變量:
                                    struct groupStru
                                    {
                                    public int groupID;
                                    public object groupObj;
                                    }
                                    internal const int LOCALE_ID = 0x407; //本地語言
                                    private Guid iidRequiredInterface;
                                    private string serverType="";
                                    private int hClientGroup = 0; //客戶組號
                                    private int nSvrGroupID; // server group handle for the added group 
                                    private Hashtable hashGroup; //用于把組收集到一起
                                    private int hClientItem=0; //Item號

                                       3、編寫構造函數,接收委托參數已確定當數據發生變化時需要執行的方法入口點:
                                    //創建服務器
                                    //svrType 服務器類型的枚舉
                                    //dataChange 提供用于在數據發生變化時需要執行的函數入口
                                    public AsynServer(ServerType svrType,DataChange dataChange)
                                    {
                                    switch(svrType)
                                    {
                                    case ServerType.OPC_SimaticHMI_PTPR
                                    serverType="OPC.SimaticHMI.PTPro";break;
                                    case ServerType.OPC_SimaticNET:
                                    serverType="OPC.SimaticNET";break;
                                    case ServerType.OPC_SimaticNET_DP:
                                    serverType="OPC.SimaticNET.DP";break;
                                    case ServerType.OPC_SimaticNET_PD:
                                    serverType="OPC.SimaticNET.PD";break;
                                    case ServerType.OPCServer_WinCC:
                                    serverType="OPCServer.WinCC";break;

                                    }
                                    hashGroup=new Hashtable(11);
                                    dtChange=dataChange;
                                    }
                                      
                                      4、創建服務器
                                    // 創建一個OPC Server接口
                                    //error 返回錯誤信息
                                    //若為true,創建成功,否則創建失敗
                                    public bool Open(out string error)
                                    {
                                    error="";bool success=true;
                                    Type svrComponenttyp ;
                                    //獲取 OPC Server COM 接口
                                    iidRequiredInterface = typeof(IOPCItemMgt).GUID;
                                    svrComponenttyp = System.Type.GetTypeFromProgID(serverType);
                                    try
                                    {
                                    //創建接口
                                    pIOPCServer =(IOPCServer)System.Activator.CreateInstance(svrComponenttyp);
                                    error="";
                                    }
                                    catch (System.Exception err) //捕捉失敗信息
                                    {
                                    error="錯誤信息:"+err.Message;success=false;
                                    }
                                    return success;
                                    }

                                      5、 編寫添加Group的函數
                                    ///


                                    /// 添加組
                                    /// 
                                    /// 組名
                                    /// /創建時,組是否被激活
                                    /// //組的刷新頻率,以ms為單位
                                    /// 返回錯誤信息
                                    /// 若為true,添加成功,否則添加失敗
                                    public bool AddGroup(string groupName,int bActive,int updateRate,out string error)
                                    { 
                                    error="";bool success=true;
                                    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, //返回組中的變量改變時的zui短通知時間間隔
                                    ref iidRequiredInterface,
                                    out pobjGroup1); //指向要求的接口
                                    hClientGroup=hClientGroup+1; 
                                    groupStru grp=new groupStru();
                                    grp.groupID=nSvrGroupID;grp.groupObj=pobjGroup1;
                                    this.hashGroup.Add(groupName,grp);//儲存組信息
                                    // 對異步操作設置回調,初始化接口
                                    pIConnectionPointContainer = (IConnectionPointContainer)pobjGroup1;
                                    Guid iid = typeof(IOPCDataCallback).GUID;
                                    pIConnectionPointContainer.FindConnectionPoint(ref iid,out pIConnectionPoint);
                                    pIConnectionPoint.Advise(this,out dwCookie);
                                    }
                                    catch (System.Exception err) //捕捉失敗信息
                                    {
                                    error="錯誤信息:"+err.Message;success=false;
                                    }
                                    finally
                                    {
                                    if (hDeadband.IsAllocated) hDeadband.Free();
                                    }
                                    return success;
                                    }

                                      6、 編寫激活、或者取消激活組的函數

                                      在同步編程中對于組的激活或者取消激活沒有實質的意義,但在異步通訊編程中卻異常重要,這是因為OPC服務器只對當前處于活動狀態的組中的變量進行監控,同時這也是很有必要的,因為我們可以把不同界面中的變量編程不同的組,即同一界面中的變量規成一個組,而在某一時刻提供給用戶的只有一個界面,讓該界面中用到的組處于活動狀態,這樣執行委托調用時只會執行于該界面中有關的變量檢測,而如果讓所有的組處于活動狀態,則當前沒有顯示給用戶的界面用到的變量若發生變化也會觸發對委托函數的調用,這根本是沒有必要的,同時會大大降低程序的性能,請嚴格控制組的激活。
                                    ///
                                    /// 激活或者取消激活組
                                    /// 
                                    /// 組名
                                    /// true為激活,false為取消激活
                                    /// 若有錯誤,返回錯誤信息
                                    /// 若為true,添加成功,否則添加失敗
                                    public bool AciveGroup(string groupName,bool toActive,out string error)
                                    {
                                    error="";bool success=true;
                                    //通過名稱獲取組
                                    object grp=((groupStru)hashGroup[groupName]).groupObj;
                                    IOPCGroupStateMgt groupStateMgt=(IOPCGroupStateMgt)grp;
                                    //初始化傳遞參數
                                    IntPtr pRequestedUpdateRate = IntPtr.Zero; //由客戶的Item更新間隔時間
                                    int nRevUpdateRate = 0; //由服務器返回的能夠更新的zui短時間間隔
                                    IntPtr hClientGroup = IntPtr.Zero; //客戶組
                                    IntPtr pTimeBias = IntPtr.Zero;
                                    IntPtr pDeadband = IntPtr.Zero;
                                    IntPtr pLCID = IntPtr.Zero; 

                                    // 激活或者取消激活組
                                    int nActive = 0; 
                                    GCHandle hActive = GCHandle.Alloc(nActive,GCHandleType.Pinned);
                                    if(toActive)
                                    hActive.Target = 1;
                                    else
                                    hActive.Target = 0;
                                    try 
                                    {
                                    groupStateMgt.SetState(pRequestedUpdateRate,out nRevUpdateRate,hActive.AddrOfPinnedObject(),pTimeBias,pDeadband,pLCID,hClientGroup); 
                                    }
                                    catch(System.Exception err)
                                    {
                                    error="錯誤信息:"+err.Message;success=false;
                                    }
                                    finally
                                    {
                                    hActive.Free(); 
                                    }
                                    return success;
                                    }

                                    7、 向的組中添加變量的函數
                                    ///
                                    /// 向的組添加一系列項
                                    /// 
                                    /// 組名
                                    /// 完整的item名數組
                                    /// 由服務器返回讀寫數據時需要使用的item號
                                    /// 無錯誤,返回true,否則返回false
                                    public bool AddItems(string groupName,string[] iteame,int[] itemsID)
                                    {
                                    bool success=true;
                                    OPCITEMDEF[] ItemDefArray=new OPCITEMDEF[iteame.Length];
                                    for(int i=0;i {
                                    hClientItem=hClientItem+1; //客戶項自動加1
                                    ItemDefArray[i].szAccessPath = ""; // 可選的通道路徑,對于Simatiic Net不需要。
                                    ItemDefArray[i].szItemID = iteame[i]; // ItemID, see above
                                    ItemDefArray[i].bActive = 1; // item is active
                                    ItemDefArray[i].hClient = hClientItem; // client handle ,在OnDataChange中會用到
                                    ItemDefArray[i].dwBlobSize = 0; // blob size
                                    ItemDefArray[i].pBlob = IntPtr.Zero; // pointer to blob 
                                    ItemDefArray[i].vtRequestedDataType = 4; //DWord數據類型
                                    }
                                    //初始化輸出參數
                                    IntPtr pResults = IntPtr.Zero;
                                    IntPtr pErrors = IntPtr.Zero;
                                    try 
                                    {
                                    // 添加項到組
                                    object grp=((groupStru)hashGroup[groupName]).groupObj;
                                    ((IOPCItemMgt)grp).AddItems(iteame.Length,ItemDefArray,out pResults,out pErrors);

                                    int[] errors = new int[iteame.Length]; 
                                    IntPtr pos = pResults;
                                    Marshal.Copy(pErrors, errors, 0,iteame.Length);
                                    for(int i=0;i {
                                    if (errors[i] == 0)
                                    {
                                    OPCITEMRESULT result = (OPCITEMRESULT)Marshal.PtrToStructure(pos, typeof(OPCITEMRESULT));
                                    itemsID[i] = result.hServer; 
                                    pos = new IntPtr(pos.ToInt32() + Marshal.SizeOf(typeof(OPCITEMRESULT)));
                                    }
                                    else
                                    {
                                    String pstrError;
                                    pIOPCServer.GetErrorString(errors[0],0x407,out pstrError); 
                                    success=false;
                                    break;
                                    }
                                    }
                                    SetItenClient(groupName,itemsID,itemsID); //要求始終只有一個組被激活,才不會引起沖突。
                                    }
                                    catch (System.Exception err) // catch for error in adding items.
                                    {
                                    success=false;
                                    //error="錯誤信息:"+error+err.Message;
                                    }
                                    finally
                                    {
                                    // 釋放非托管內存
                                    if(pResults != IntPtr.Zero)
                                    {
                                    Marshal.FreeCoTaskMem(pResults);
                                    pResults = IntPtr.Zero;
                                    }
                                    if(pErrors != IntPtr.Zero)
                                    {
                                    Marshal.FreeCoTaskMem(pErrors);
                                    pErrors = IntPtr.Zero; 
                                    }
                                    }
                                    return success;
                                    }

                                    聯系我們

                                    上海翰粵自動化系統有限公司 公司地址:上海市松江區思賢路2399弄137號   技術支持:化工儀器網
                                    • 聯系人:黃政武
                                    • QQ:76868608
                                    • 公司傳真:021-57657276
                                    • 郵箱:76868608@qq.com

                                    掃一掃 更多精彩

                                    微信二維碼

                                    網站二維碼

                                    主站蜘蛛池模板: 欧美口味重另类牲交视频| 激情偷乱视频一区二区三区| xxx18满| 黄桃图片黄桃图库网站| 花臂夫夫1-42集免费观| 91成人在线| 小说区 图片区 自偷自拍另类| 国产激情视频在线观看首页| aaajiepai| 手机壁纸图片简约| 白丝精品一区二区三区 | 羞羞漫网| 西施脚趾踝足粘液图片| 时光与你都很甜免费观看| 欧美Ⅴa| 冷狐移植100款绅士游戏安卓直装 中文一区二区三区精华液使用方法 | 头像大全高清图片女| t66y.com新ip地址怎么获取| 丰满专区视频| 91系列在线观看| 好吊妞av| 黄油软件库下载| 激情久久久| 萌鸡小队动画片全集| 疯狂原始人国语高清| av日韩av天堂一区二区三区| v电影app| 请到天涯海角来歌词| c.oww| 69免费福利视频| 二人转正戏全集大全| 69AV桔子| 日剧《我们再来一次》高清版| 国产69精品久久久久男男系列| kkk4444在线观看| 99久久精品二区| 体脂率越低越好吗| 胸大的姑娘播放| B站直播夜月| 超碰在线免费看| 五月婷婷手机在线图|