internal static bool PutBoolean(Plc client, int dbnum, string cycleCompletedLoc, bool val)
        {
            if (client == null)
            {
                Logger.WriteDebugLog("Client is null");
                return(false);
            }

            try
            {
                if (client.IsAvailable)
                {
                    if (!client.IsConnected)
                    {
                        ErrorCode er = client.Open();
                        if (er != ErrorCode.NoError)
                        {
                            Logger.WriteDebugLog("client open error");
                        }
                    }
                    if (client.IsConnected)
                    {
                        ErrorCode er = client.Write(string.Format("DB{0}.DBX{1}", dbnum, cycleCompletedLoc), val);
                        return(er == ErrorCode.NoError);
                    }
                }
            }
            catch (Exception ex)
            {
                client.Close();
                client.Dispose();
            }
            return(false);
        }
Beispiel #2
0
        public string Disconnect()
        {
            string status = null;

            if (plc != null && plc.IsConnected)
            {
                plc.Close();
                status = Status.Text("СТАТУС: Соединение разорвано.");
            }
            else
            {
                plc.Dispose();
                status = Status.Text("СТАТУС: Ошибка разрыва соединения.");
            }
            return(status);
        }
Beispiel #3
0
        public bool Close()
        {
            bool result = false;

            try
            {
                if (Plc != null)
                {
                    Plc.Dispose();
                }
                result = true;
            }
            catch (Exception ex)
            {
                logger.Error($"ex={ex}");
            }
            Status = DriverStatus.Disconnected;
            logger.Debug($"[{Status}] [{result}] Ip={Ip}, Port={Port}, ReceiveTimeout={ReceiveTimeout}");
            return(result);
        }
Beispiel #4
0
 /// <summary>
 /// 检测网络状态,断线重连
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void timerPrint_Tick(object sender, EventArgs e)
 {
     if (IsNetConnected)
     {
         if (plc == null)
         {
             InitPlcService();
         }
     }
     else
     {
         if (plc != null)
         {
             plc.Dispose();
             plc = null;
             OnDisplayLog("PLC[" + GlobalData.PlcIP + "]已断开连接……", false);
             log.Info("PLC[" + GlobalData.PlcIP + "]已断开连接……");
         }
     }
 }
Beispiel #5
0
 public void funClose()
 {
     try
     {
         if (sPLC != null)
         {
             sPLC.Close();
             sPLC.Dispose();
             sPLC            = null;
             bolIsConnection = false;
             InitSys.funWriteLog("SPLC_Trace", strIPAddress + "|Close!");
         }
     }
     catch (Exception ex)
     {
         MethodBase methodBase = MethodBase.GetCurrentMethod();
         InitSys.funWriteLog("Exception", methodBase.DeclaringType.FullName + "|" + methodBase.Name + "|" + ex.Message);
         bolIsConnection = false;
     }
 }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (establishingConnection)
            {
                timer.Stop();

                return;
            }

            timer.Stop();
            this.mapVariables();
            lock (plc)
            {
                short   rack = plc.Rack, slot = plc.Slot;
                string  ip  = plc.IP;
                CpuType cpu = plc.CPU;
                plc.Dispose();
                plc = new Plc(cpu, ip, rack, slot);
                this.OpenConnection();
            }
            timer.Start();
        }
        internal static float GetFloat(Plc client, int dbnum, string loc)
        {
            uint  val = 0;
            float ret = 0F;

            if (client == null)
            {
                Logger.WriteDebugLog("Client is null");
                return(ret);
            }

            try
            {
                if (client.IsAvailable)
                {
                    if (!client.IsConnected)
                    {
                        ErrorCode er = client.Open();
                        if (er != ErrorCode.NoError)
                        {
                            Logger.WriteDebugLog("client open error");
                        }
                    }
                    if (client.IsConnected)
                    {
                        var data = client.Read(string.Format("DB{0}.DBD{1}", dbnum, loc));
                        val = (uint)data;
                        ret = BitConverter.ToSingle(BitConverter.GetBytes(val), 0);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteErrorLog(ex.ToString());
                client.Close();
                client.Dispose();
            }
            return(ret);
        }
        internal static string GetString(Plc client, int dbnum, string locCycle, int nBytes)
        {
            string val      = "";
            int    location = int.Parse(locCycle) + 2;

            if (client == null)
            {
                Logger.WriteDebugLog("Client is null");
                return(val);
            }

            try
            {
                if (client.IsAvailable)
                {
                    if (!client.IsConnected)
                    {
                        ErrorCode er = client.Open();
                        if (er != ErrorCode.NoError)
                        {
                            Logger.WriteDebugLog("client open error");
                        }
                    }
                    if (client.IsConnected)
                    {
                        var data = client.Read(DataType.DataBlock, dbnum, location, VarType.String, nBytes);
                        val = (string)data;
                    }
                }
            }
            catch (Exception ex)
            {
                client.Close();
                client.Dispose();
            }
            return(val);
        }
        internal static ushort GetWord(Plc client, int dbnum, string loc)
        {
            ushort val = 0;

            if (client == null)
            {
                Logger.WriteDebugLog("Client is null");
                return(val);
            }

            try
            {
                if (client.IsAvailable)
                {
                    if (!client.IsConnected)
                    {
                        ErrorCode er = client.Open();
                        if (er != ErrorCode.NoError)
                        {
                            Logger.WriteDebugLog("client open error");
                        }
                    }
                    if (client.IsConnected)
                    {
                        var data = client.Read(string.Format("DB{0}.DBW{1}", dbnum, loc));
                        val = (ushort)data;
                    }
                }
            }
            catch (Exception ex)
            {
                client.Close();
                client.Dispose();
            }
            return(val);
        }
        internal static List <uint> GetDWords(Plc client, int dbnum, int startloc, int nValues)
        {
            List <UInt32> lst = new List <uint>();

            if (client == null)
            {
                Logger.WriteDebugLog("Client is null");
                return(lst);
            }

            try
            {
                if (client.IsAvailable)
                {
                    if (!client.IsConnected)
                    {
                        ErrorCode er = client.Open();
                        if (er != ErrorCode.NoError)
                        {
                            Logger.WriteDebugLog("client open error");
                        }
                    }
                    if (client.IsConnected)
                    {
                        var data = client.Read(DataType.DataBlock, dbnum, startloc, VarType.DWord, nValues);
                        lst = ((uint[])data).ToList();
                    }
                }
            }
            catch (Exception ex)
            {
                client.Close();
                client.Dispose();
            }
            return(lst);
        }