sakura12345
級(jí)別: 正式會(huì)員
精華主題: 0
發(fā)帖數(shù)量: 34 個(gè)
工控威望: 92 點(diǎn)
下載積分: 732 分
在線時(shí)間: 25(小時(shí))
注冊(cè)時(shí)間: 2018-07-25
最后登錄: 2018-12-21
查看sakura12345的 主題 / 回貼
樓主  發(fā)表于: 2018-11-13 16:42
c#寫opc服務(wù)器,有點(diǎn)問(wèn)題,多點(diǎn)指教
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using OPCAutomation;
using System.Net;

namespace OPC通訊
{
    public partial class Form1 : Form
    {
        #region 私有變量
        OPCServer myOPCServer;//OPC服務(wù)器變量聲明
        OPCGroups myOPCGroups;//OPC服務(wù)器組集合
        OPCGroup myOPCGroup;//OPCGroups 是OPCGroup對(duì)象的集合,外加創(chuàng)建,移除,管理他們的方法。
        OPCItems myOPCItems;//OPCItems OPCItems OPCItem對(duì)象的集合這是OPCgroup對(duì)象的默認(rèn)屬性。
        OPCItem myOPCItem;
        string strHostIP = "";
        string strHostName = "";
        bool opc_connected = false;
        int itmHandleClient = 0;//客戶端句柄
        int itmHandleServer = 0;//服務(wù)端句柄
        #endregion
        public Form1()
        {
            InitializeComponent();
        }
        #region 加載Form1_Load
        private void Form1_Load(object sender, EventArgs e)
        {
            //獲取本地計(jì)算機(jī)IP、計(jì)算機(jī)名稱
            IPHostEntry IPHost = Dns.Resolve(Environment.MachineName);
            if (IPHost.AddressList.Length > 0)
            {
                strHostIP = IPHost.AddressList[0].ToString();
                textBox1.Text = strHostIP;
            }
            else
            {
                return;
            }

            //通過(guò)IP來(lái)獲取計(jì)算機(jī)名稱,可用在局域網(wǎng)內(nèi)
            IPHostEntry ipHostEntry = Dns.GetHostByAddress(strHostIP);
            strHostName = ipHostEntry.HostName.ToString();

            //獲取本地計(jì)算機(jī)上的OPCServerName
            try
            {
                myOPCServer = new OPCServer();
                object serverList = myOPCServer.GetOPCServers(strHostName);
                foreach (string turn in (Array)serverList)
                {
                    comboBox1.Items.Add(turn);
                }
                comboBox1.SelectedIndex = 0;
              button1.Enabled = true;
            }
            catch (Exception err)
            {
                MessageBox.Show("枚舉本地OPC服務(wù)器出錯(cuò):" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 建立連接
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ConnectRemoteServer(textBox1.Text, comboBox1.Text))
                {
                    return;
                }
                btnSetGroupPro.Enabled = true;
                opc_connected = true;
                GetServerInfo();
                RecurBrowse(myOPCServer.CreateBrowser());
                if (!CreateGroup())
                {
                    return;
                }
              
            }
            catch (Exception err)
            {
                MessageBox.Show("初始化出錯(cuò):" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
[ 此帖被sakura12345在2018-11-13 17:01重新編輯 ]
autoctrl
級(jí)別: 家園?
精華主題: 0
發(fā)帖數(shù)量: 354 個(gè)
工控威望: 574 點(diǎn)
下載積分: 1574 分
在線時(shí)間: 130(小時(shí))
注冊(cè)時(shí)間: 2016-05-05
最后登錄: 2024-12-09
查看autoctrl的 主題 / 回貼
1樓  發(fā)表于: 2018-11-15 10:12
C#的OPC服務(wù)端開發(fā)包,有詳細(xì)范例:
http://blog.sina.com.cn/s/blog_16d7d3ecb0102x218.html