Exemplo n.º 1
0
        /// <summary>
        /// 从plc读有板信号
        /// </summary>
        /// <param name="client"></param>
        /// <param name="param"></param>
        /// <param name="tolocation">交地</param>
        /// <returns></returns>
        public static bool IsPanelAvailable(IOpcClient client, OPCParam param, string tolocation)
        {
            const string PANEL_OK = "2";
            var          s        = client.TryReadString(param.BAreaPanelState[tolocation]);

            return(s == PANEL_OK);
        }
Exemplo n.º 2
0
        public static string GetLabelCodeWhenWeigh(IOpcClient client, OPCParam param)
        {
            var code1 = client.TryReadString(param.WeighParam.LabelPart1).PadLeft(6, '0');
            var code2 = client.TryReadString(param.WeighParam.LabelPart2).PadLeft(6, '0');

            return(code1 + code2);
        }
Exemplo n.º 3
0
        private void Main_Load(object sender, EventArgs e)
        {
            try
            {
                #region 读取配置文件
                var opcConfig = ConfigurationManagerExtend.SectionsCast <OpcAddressConfiguration>("Address").FirstOrDefault().Value;
                var tagConfig = ConfigurationManagerExtend.SectionsCast <TagConfiguration>("Tag").FirstOrDefault().Value;
                groupTriggerUpdateRate = tagConfig.TriggerUpdateRate;
                groupDataUpdateRate    = tagConfig.DataUpdateRate;
                reconnectEnable        = opcConfig.ReconnectEnable;
                reConnectInterval      = opcConfig.ReconnectInterval;
                #endregion
                #region OpcClient初始化
                if (opcConfig.OpcProtocolByEnum == OpcProtocol.DA)
                {
                    client = new DaOpc();
                }
                else if (opcConfig.OpcProtocolByEnum == OpcProtocol.UA)
                {
                    client = new UaOpc();
                }

                client.Init(opcConfig);
                //断开服务通知
                client.OpcStatusChangeHandle += this.OpcServerDisConnected;
                ConnectOpc(client);
                #endregion
            }
            catch (Exception ex)
            {
                //记日志
                return;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 断线重连
        /// </summary>
        /// <param name="client"></param>
        private void ReconnectOpcAsync(IOpcClient client)
        {
            var msg = "10s后尝试重连";

            while (true)
            {
                if (!client.OpcStatus)
                {
                    //成功连接后直接返回
                    if (this.ConnectOpc(client))
                    {
                        return;
                    }
                    //连接失败则循环继续
                    else
                    {
                        AddMsgToList(msg);
                        //重连时间间隔
                        Thread.Sleep(reConnectInterval);
                    }
                }
                else
                {
                    return;
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 初始化参数同时添加订阅
        /// </summary>
        /// <param name="opc"></param>
        public OPCRobotParam(IOpcClient opc)
        {
            var dt = OPCParam.Query($"where Class='{CFG}'");

            if (dt == null || dt.Rows.Count < 1)
            {
                return;
            }
            foreach (DataRow dr in dt.Rows)
            {
                foreach (PropertyInfo property in typeof(OPCRobotParam).GetProperties())
                {
                    if (property.Name == dr["Name"].ToString())
                    {
                        property.SetValue(this,dr["Code"].ToString());
                        opc.AddSubscription(dr["Code"].ToString());
                    }
                }
            }

            PlcSnA = new PlcSignal();
            PlcSnA.LoadSN(ReadSignalA,WriteSignalA);

            PlcSnB = new PlcSignal();
            PlcSnB.LoadSN(ReadSignalB,WriteSignalB);
        }
Exemplo n.º 6
0
 /// <summary>
 /// 连接opc
 /// </summary>
 /// <param name="client"></param>
 private bool ConnectOpc(IOpcClient client)
 {
     try
     {
         if (client.Connect().Result)
         {
             //创建组及绑定组内的tags
             client.CreateGroup("GroupTrigger")
             .SetUpdateRate(groupTriggerUpdateRate)
             .AddItems(TagConfig.QueryTagsByGroupName <Tag>("GroupTrigger"))
             .ValueChangedHandle = TagValueChanged;
             client.CreateGroup("GroupData")
             .SetUpdateRate(groupDataUpdateRate)
             .AddItems(TagConfig.QueryTagsByGroupName <Tag>("GroupData"))
             .AddQueue(100)
             .ValueChangedHandle = TagValueChanged;
             OpcServerRefreshUI(client);
             return(true);
         }
         else
         {
             OpcServerRefreshUI(client);
             return(false);
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Exemplo n.º 7
0
 public void setup(Action <bool, string> errorhandler, Action <string, string, LogViewType> logfunc, IOpcClient c, OPCParam p)
 {
     onerror = errorhandler;
     _log    = logfunc;
     client  = c;
     param   = p;
 }
Exemplo n.º 8
0
        /// <summary>
        /// 初始化Opc服务
        /// </summary>
        private IOpcClient OpcFinder(string opcName)
        {
            ContainerBuilder builder = new ContainerBuilder();
            Type             opcType;
            Type             baseType = typeof(IOpcClient);

            // 获取所有OPC相关类库的程序集
            Assembly[] assemblies = GetOpcAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                try
                {
                    opcType = assembly.GetType(opcName);
                    if (opcType != null)
                    {
                        builder.RegisterType(opcType)
                        .Named <IOpcClient>(opcName)
                        .SingleInstance();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            Autofac.IContainer container = builder.Build();
            IOpcClient         opcClient = container.ResolveNamed <IOpcClient>(opcName);

            return(opcClient);
        }
Exemplo n.º 9
0
 private void Main_Load(object sender, EventArgs e)
 {
     try
     {
         //增加CLR搜索的路径
         CLRPrivatePathInit();
         #region 读取配置文件
         var opcConfig = ConfigurationManagerExtend.SectionsCast <OpcAddressConfiguration>("Address").FirstOrDefault().Value;
         var tagConfig = ConfigurationManagerExtend.SectionsCast <TagConfiguration>("Tag").FirstOrDefault().Value;
         groupTriggerUpdateRate = tagConfig.TriggerUpdateRate;
         groupDataUpdateRate    = tagConfig.DataUpdateRate;
         tagsQueueNum           = tagConfig.TagsQueueNum;
         reconnectEnable        = opcConfig.ReconnectEnable;
         reConnectInterval      = opcConfig.ReconnectInterval;
         #endregion
         #region OpcClient初始化
         client = OpcFinder(opcConfig.OpcTypeName);
         client.Init(opcConfig);
         //断开服务通知
         client.OpcStatusChangeHandle += this.OpcServerDisConnected;
         ConnectOpc(client);
         #endregion
         #region  连接远程服务器
         ServicesInit();
         ConnectServer();
         #endregion
     }
     catch (Exception ex)
     {
         //消息通知
         LogHelper.Info("通讯服务器校验结果:" + ex.ToString());
         AddMsgToList("通讯服务器校验结果:" + ex.ToString());
         return;
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// 读取完整标签号码。
        /// </summary>
        /// <param name="slot"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static string ReadCompleteLable(IOpcClient client, LCodeSignal slot)
        {
            const int MAX_LEN = 6;
            var       lable1  = client.TryReadString(slot.LCode1);
            var       lable2  = client.TryReadString(slot.LCode2);

            return(lable1.PadLeft(MAX_LEN, '0') + lable2.PadLeft(MAX_LEN, '0'));
        }
Exemplo n.º 11
0
 public void setup(Action <bool, string> errorhandler, Action <string, string, LogViewType> loghandler, IOpcClient client, OPCParam param)
 {
     this.loghandler = loghandler;
     this.client     = client;
     this.param      = param;
     loghandler?.Invoke("!fake robot setup.", LogType.ROBOT_STACK, LogViewType.OnlyForm);
     loghandler?.Invoke($"!ip: {ip}, job name: {jobName}", "fake robot", LogViewType.OnlyFile);
 }
Exemplo n.º 12
0
        public static string ReadACAreaLabel(IOpcClient client, LCodeSignal signal)
        {
            const int MAX_LEN = 6;
            var       r1      = client.TryReadString(signal.LCode1);
            var       r2      = client.TryReadString(signal.LCode2);

            return(r1.PadLeft(MAX_LEN, '0') + r2.PadLeft(MAX_LEN, '0'));
        }
Exemplo n.º 13
0
        public static void NotifyFullPanel(IOpcClient client, OPCParam param, string relloc)
        {
            client.TryWrite(param.BAreaPanelFinish[relloc], true);
            FrmMain.logOpt.Write($"{relloc}: 满板信号发出。slot: {param.BAreaPanelFinish[relloc]}", LogType.ROBOT_STACK);
            const int SIGNAL_3 = 3;

            client.TryWrite(param.BAreaPanelState[relloc], SIGNAL_3);
            FrmMain.logOpt.Write($"{relloc}: 板状态信号发出,状态值: {SIGNAL_3}。slot: {param.BAreaPanelState[relloc]}", LogType.ROBOT_STACK);
        }
Exemplo n.º 14
0
 /// <summary>
 /// 标签采集处,写布卷直径和去向。
 /// </summary>
 /// <param name="client"></param>
 /// <param name="diameter">直径</param>
 /// <param name="channel">去向, 1 or 2.</param>
 /// <param name="param"></param>
 public static void WriteLabelUpData(IOpcClient client, OPCParam param, decimal diameter, RollCatchChannel channel)
 {
     // diameter单位是毫米。
     client.TryWrite(param.LableUpParam.Diameter, diameter);
     client.TryWrite(param.LableUpParam.Goto, channel);
     Thread.Sleep(DELAY);
     // 复位标签采集处来料信号。
     client.TryWrite(param.LableUpParam.Signal, 0);
     param.LableUpParam.PlcSn.WriteSN(client);
 }
Exemplo n.º 15
0
 public static bool WriteScanResult(IOpcClient client, OPCParam param, string area, string locationNo, string codepart1, string codepart2)
 {
     client.TryWrite(param.ScanParam.ToLocationArea, area);
     client.TryWrite(param.ScanParam.ToLocationNo, locationNo);
     // write label.
     client.TryWrite(param.ScanParam.ScanLable1, codepart1);
     client.TryWrite(param.ScanParam.ScanLable2, codepart2);
     // write camera no. and set state true.
     return(client.TryWrite(param.ScanParam.ScanState, true));
 }
Exemplo n.º 16
0
 public void ResetSN(IOpcClient opc)
 {
     if (string.IsNullOrEmpty(groupName))
     {
         opc.TryWrite(WriteSignal,0);
     }
     else
     {
         opc.TryWrite(groupName,WriteSignal,0);
     }
 }
Exemplo n.º 17
0
        private void FormTest1_Load(object sender, EventArgs e)
        {
            client = new DaOpc <DaExtra>();
            List <string> list = client.GetServerList();

            foreach (string turn in list)
            {
                cmbServerName.Items.Add(turn);
            }
            cmbServerName.SelectedIndex = 0;
        }
Exemplo n.º 18
0
 /// <summary>
 /// 写缓存动作
 /// </summary>
 /// <param name="client">opc client.</param>
 /// <param name="job">动作编号。</param>
 /// <param name="posSave">动作编号。</param>
 /// <param name="posGet">动作编号。</param>
 /// <param name="param"></param>
 /// <param name="lcode"></param>
 public static void WriteCacheJob(IOpcClient client, OPCParam param, CacheState job, int posSave, int posGet, string lcode)
 {
     client.TryWrite(param.CacheParam.BeforCacheLable1, lcode.Substring(0, 6));
     client.TryWrite(param.CacheParam.BeforCacheLable2, lcode.Substring(6, 6));
     client.TryWrite(param.CacheParam.CacheStatus, job);
     client.TryWrite(param.CacheParam.CachePoint, posSave);
     client.TryWrite(param.CacheParam.GetPoint, posGet);
     Thread.Sleep(DELAY);
     // 复位来料信号。
     param.CacheParam.PlcSn.WriteSN(client);
     client.TryWrite(param.CacheParam.BeforCacheStatus, 0);
 }
Exemplo n.º 19
0
 /// <summary>
 /// 断开opc
 /// </summary>
 /// <param name="client"></param>
 private bool DisconnectOpc(IOpcClient client)
 {
     if (client.Disconnect())
     {
         OpcServerRefreshUI(client);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 20
0
 /// <summary>
 /// 断开opc
 /// </summary>
 /// <param name="client"></param>
 private bool DisconnectOpc(IOpcClient client)
 {
     if (client.Disconnect())
     {
         client.RemoveGroupsAll();
         OpcServerRefreshUI(client);
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 21
0
        public bool ReadSN(IOpcClient opc)
        {
            var rw = GetSignal(opc);

#if DEBUG
            return(true);
#endif
            if (rw[0] == 0 && rw[1] != rw[0])
            {
                rw = ResetReadCount(opc);
                if (rw[0] == 0 && rw[1] != rw[0])
                {
                    ResetSN(opc);
                    FrmMain.logOpt.Write($"来料归零 R {ReadSignal}: {rw[0]} W {WriteSignal}: {rw[1] },上次正常读到R:{readCount} W:{writeCount} !{guid}",
                                         LogType.SIGNAL,LogViewType.OnlyFile);
                    return(false);
                }
            }
            var tmp = rw[1] == 2000 ? 0 : rw[1];
            if (rw[0] == (tmp + 1))
            {
                guid       = Guid.NewGuid().ToString();
                readCount  = rw[0];
                writeCount = rw[1];
                FrmMain.logOpt.Write($"来料 R {ReadSignal}: {rw[0]} W {WriteSignal}: {rw[1] }!{guid}",LogType.SIGNAL,LogViewType.OnlyFile);
                return(true);
            }
            else
            {
                if (rw[0] != rw[1] && rw[1] != writeCount)  //异常信号修正
                {
                    rw = ResetReadCount(opc);
                    if (rw[0] != rw[1] && rw[0] != rw[1] + 1 && rw[1] != writeCount)
                    {
                        if (string.IsNullOrEmpty(groupName))
                        {
                            opc.TryWrite(WriteSignal,writeCount);
                        }
                        else
                        {
                            opc.TryWrite(groupName,WriteSignal,writeCount);
                        }
                        FrmMain.logOpt.Write($"ERR来料 R {ReadSignal}: {rw[0]} W {WriteSignal}: {rw[1] },上次正常读到R:{readCount} W:{writeCount} !{guid}",
                                             LogType.SIGNAL,LogViewType.OnlyFile);
                    }
                }
                return(false);
            }
        }
Exemplo n.º 22
0
        public bool InitBAreaUserFinalLayer(IOpcClient opc)
        {
            BAreaUserFinalLayer = new Dictionary <string,string>();
            var dt = Query("where Class='BAreaUserFinalLayer'");

            if (dt == null || dt.Rows.Count < 1)
            {
                return(false);
            }
            foreach (DataRow dr in dt.Rows)
            {
                BAreaUserFinalLayer.Add(dr["Name"].ToString(),dr["Code"].ToString());
                opc.AddSubscription(dr["Code"].ToString());
            }
            return(true);
        }
Exemplo n.º 23
0
        public bool InitBadShapeLocations(IOpcClient opc)
        {
            BadShapeLocations = new Dictionary <string,string>();
            var dt = Query("where Class='BadShapeLocations'");

            if (dt == null || dt.Rows.Count < 1)
            {
                return(false);
            }
            foreach (DataRow dr in dt.Rows)
            {
                BadShapeLocations.Add(dr["Name"].ToString(),dr["Code"].ToString());
                opc.AddSubscription(dr["Code"].ToString());
            }
            return(true);
        }
Exemplo n.º 24
0
        public bool InitHeartBeat(IOpcClient client)
        {
            HeartBeat = new Dictionary <string,string>();
            var dt = Query("where Class='heartbeat'");

            if (dt == null || dt.Rows.Count < 1)
            {
                return(false);
            }
            foreach (DataRow dr in dt.Rows)
            {
                HeartBeat.Add(dr["Name"].ToString(),dr["Code"].ToString());
                client.AddSubscription(dr["Code"].ToString());
            }
            return(true);
        }
Exemplo n.º 25
0
        /// <summary>
        /// 复位校正
        /// </summary>
        /// <param name="opc"></param>
        private int[] ResetReadCount(IOpcClient opc)
        {
            var rw    = new int[2];
            var count = 3;

            while (count-- > 0)
            {
                Thread.Sleep(100);
                rw = GetSignal(opc);
                if (rw[0] != 0)
                {
                    break;
                }
            }
            return(rw);
        }
Exemplo n.º 26
0
        private int[] GetSignal(IOpcClient opc)
        {
            var rw = new int[2];

            if (string.IsNullOrEmpty(groupName))
            {
                rw[0] = opc.TryReadInt(ReadSignal);
                rw[1] = opc.TryReadInt(WriteSignal);
            }
            else
            {
                rw[0] = opc.TryGroupReadInt(groupName,ReadSignal);
                rw[1] = opc.TryGroupReadInt(groupName,WriteSignal);
            }
            return(rw);
        }
Exemplo n.º 27
0
        public void _StartJob(IOpcClient client)
        {
            this.stoped = false;

            Task.Factory.StartNew(() => {
                while (!this.stoped)
                {
                    var s = tryReadLine();
                    if (!string.IsNullOrEmpty(s))
                    {
                        OnDataArrived(client, "", s, 1);
                    }
                    Thread.Sleep(1000);
                }
                logger?.Invoke("!扫描线程结束。");
            });
        }
Exemplo n.º 28
0
        public static bool IsPushedAside(IOpcClient client, OPCParam param, Action onResetError = null)
        {
            var slot = param.ScanParam.PlcPushAside;

            if (client.TryReadBool(slot))
            {
                var resetok = client.TryWrite(slot, 0);
                if (!resetok)
                {
                    onResetError?.Invoke(); // 复位失败
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 29
0
 /// <summary>
 /// 标签扫描处理完成以后,发消息告知OPC。
 /// </summary>
 /// <param name="area">交地所在的区代码:"A", "B" or "C"</param>
 /// <param name="numb">交地序号</param>
 /// <param name="label1">标签前半部分</param>
 /// <param name="label2">标签后半部分</param>
 /// <param name="camera">相机号</param>
 /// <param name="client">opc client</param>
 /// <param name="param">opc param</param>
 public static void WriteScanOK(IOpcClient client,
                                OPCParam param,
                                string area,
                                string numb,
                                string label1,
                                string label2,
                                string camera)
 {
     // 交地
     client.TryWrite(param.ScanParam.ToLocationArea, area);
     client.TryWrite(param.ScanParam.ToLocationNo, numb);
     // 标签条码
     client.TryWrite(param.ScanParam.ScanLable1, label1);
     client.TryWrite(param.ScanParam.ScanLable2, label2);
     // 相机
     client.TryWrite(param.ScanParam.PushAside, camera);
     // 完成信号。
     WriteScanOK(client, param);
 }
Exemplo n.º 30
0
        public AddTags(IOpcClient iOpc, string groupName, string blockName)
        {
            InitializeComponent();
            this.client    = iOpc;
            this.groupName = groupName;
            this.blockName = blockName;
            lblGroup.Text  = this.groupName;
            lblBlock.Text  = this.blockName;
            imageList.Images.Add("Class_489", Properties.Resources.Class_489);
            imageList.Images.Add("ClassIcon", Properties.Resources.ClassIcon);
            imageList.Images.Add("brackets", Properties.Resources.brackets_Square_16xMD);
            imageList.Images.Add("VirtualMachine", Properties.Resources.VirtualMachine);
            imageList.Images.Add("Enum_582", Properties.Resources.Enum_582);
            imageList.Images.Add("Method_636", Properties.Resources.Method_636);
            imageList.Images.Add("Module_648", Properties.Resources.Module_648);
            imageList.Images.Add("Loading", Properties.Resources.loading);
            treeViewBranches.ImageList = imageList;

            treeViewBranches.Nodes.Add(new TreeNode("Browsering...", 7, 7));
            List <TreeNode> listNode = Task.Run(() =>
            {
                List <TreeNode> list    = new List <TreeNode>();
                List <OpcNode> opcNodes = client.ShowBranches();
                foreach (var nodeSon in opcNodes)
                {
                    TreeNode treeNode         = new TreeNode();
                    treeNode.Name             = nodeSon.NodeName;
                    treeNode.Text             = nodeSon.NodeName;
                    treeNode.Tag              = nodeSon;
                    treeNode.ImageKey         = nodeSon.Key;
                    treeNode.SelectedImageKey = nodeSon.Key;
                    if (nodeSon.IsExpand)
                    {
                        treeNode.Nodes.Add(new TreeNode());
                    }
                    list.Add(treeNode);
                }
                return(list);
            }).Result;

            treeViewBranches.Nodes.Clear();
            treeViewBranches.Nodes.AddRange(listNode.ToArray());
        }
Exemplo n.º 31
0
 public SynchronizedOpcClient(IOpcClient baseClient)
 {
     this.baseClient = baseClient;
 }