Esempio n. 1
0
        public override void Read(Plc _plc)
        {
            if (this.ReadHandler != null)
            {
                ReadEventArgs e = new ReadEventArgs();
                e.ReadID    = ID;
                e.StartTime = System.DateTime.Now;
                try
                {
                    _plc.ReadMultipleVars(Items.Values.ToList());
                    e.Result = true;
                }
                catch
                {
                    //出现异常断开连接(超时),并休眠1秒
                    _plc.Close();
                    e.Result   = false;
                    e.ErrorMsg = "由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败";
                }

                e.EndTime = System.DateTime.Now;
                e.Data    = new Dictionary <string, object>();
                foreach (var dic in Items)
                {
                    e.Data.Add(dic.Key, dic.Value.Value);
                }

                this.ReadHandler(this, e);
            }
        }
Esempio n. 2
0
        public override void Read(Plc _plc)
        {
            if (this.ReadHandler != null)
            {
                ReadEventArgs e = new ReadEventArgs();
                e.ReadID    = ID;
                e.StartTime = System.DateTime.Now;
                try
                {
                    foreach (var dic in Items)
                    {
                        var buffer = _plc.ReadBytes(dic.Value.DataType, dic.Value.DB, dic.Value.StartByteAdr, dic.Value.Count);
                        dic.Value.Value = FilterChar(System.Text.Encoding.GetEncoding("GB2312").GetString(buffer).Trim());
                        buffer          = null;
                    }
                    e.Result = true;
                }
                catch (Exception ex)
                {
                    //出现异常断开连接(超时)
                    _plc.Close();
                    e.Result   = false;
                    e.ErrorMsg = ex.Message;
                }
                e.EndTime = System.DateTime.Now;
                e.Data    = new Dictionary <string, object>();
                foreach (var dic in Items)
                {
                    e.Data.Add(dic.Key, dic.Value.Value);
                }

                this.ReadHandler(this, e);
            }
        }