Beispiel #1
0
 private void WriteSingleprivate(string PLCName, bool value)
 {
     try
     {
         int handle = Tcads.CreateVariableHandle(PLCName);
         Tcads.WriteAny(handle, value);
         Tcads.DeleteVariableHandle(handle);
     }
     catch (Exception ex)
     {
         ErrorFile.ErrorLog(ex.Message, ADS.Logfilepath);
     }
 }
Beispiel #2
0
 /// <summary>
 /// 同步CURD
 /// </summary>
 /// <param name="sqlcmd"></param>
 /// <param name="paras"></param>
 /// <param name="commandType"></param>
 /// <returns>Error return:-99</returns>
 public int ExecuteSync(string sqlcmd, SqlParameter[] paras = null, CommandType commandType = CommandType.Text)
 {
     using (var newconnection = new SqlConnection(_constring))
         using (var cmd = new SqlCommand(sqlcmd, newconnection))
         {
             try
             {
                 if (paras != null)
                 {
                     cmd.Parameters.AddRange(paras);
                 }
                 cmd.CommandType = commandType;
                 newconnection.Open();
                 return(cmd.ExecuteNonQuery());
             }
             catch (Exception ex)
             {
                 log.ErrorLog(ex.Message + "|| cmd: sqlcmd");
                 return(-99);
             }
         }
 }
Beispiel #3
0
        public T ReadSingle <T>(string PLCName)
        {
            string plcname = "." + PLCName;

            try
            {
                int handle     = Tcads.CreateVariableHandle(plcname);
                T   returnData = (T)Tcads.ReadAny(handle, typeof(T));
                Tcads.DeleteVariableHandle(handle);
                return(returnData);
            }
            catch (Exception ex)
            {
                ErrorFile.ErrorLog(ex.Message, ADS.Logfilepath);
                return(default);
Beispiel #4
0
 public bool WriteString(string PLCName, string value)
 {
     try
     {
         int handle = Tcads.CreateVariableHandle(PLCName);
         Tcads.WriteAny(handle, value, new int[] { 20 });
         Tcads.DeleteVariableHandle(handle);
         return(true);
     }
     catch (Exception ex)
     {
         ErrorFile.ErrorLog(ex.Message, ADS.Logfilepath);
         return(false);
     }
 }
Beispiel #5
0
        public async Task <T[]> ReadArray <T>(string PLCName, int size)
        {
            string plcname = "." + PLCName;

            return(await Task.Run(() =>
            {
                try
                {
                    int handle = Tcads.CreateVariableHandle(plcname);
                    T[] returnData = (T[])Tcads.ReadAny(handle, typeof(T[]), new int[] { size });
                    Tcads.DeleteVariableHandle(handle);
                    return returnData;
                }
                catch (Exception ex)
                {
                    ErrorFile.ErrorLog(ex.Message, ADS.Logfilepath);
                    return default;
                }
            }));
        }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="PLCName">用nameof(属性名),"前提是属性名和PLC变量名一致"</param>
        /// <param name="value"></param>
        /// <param name="AutoReset">是否自动复位,只对bool量有效</param>
        public bool WriteSingle <T>(string PLCName, T value, bool AutoReset = false)
        {
            string plcname = "." + PLCName;

            try
            {
                ITcAdsSymbol5 info   = (ITcAdsSymbol5)Tcads.ReadSymbolInfo(plcname);
                int           handle = Tcads.CreateVariableHandle(plcname);
                if (typeof(T) != typeof(string))
                {
                    Tcads.WriteAny(handle, value);
                }
                else
                {
                    switch (info.TypeName)
                    {
                    case "SINT":
                        sbyte sbdata = Convert.ToSByte(value);
                        Tcads.WriteAny(handle, sbdata);
                        break;

                    case "BYTE":
                        byte bydata = Convert.ToByte(value);
                        Tcads.WriteAny(handle, bydata);
                        break;

                    case "BOOL":
                        bool bdata = Convert.ToBoolean(value);
                        Tcads.WriteAny(handle, bdata);
                        if (AutoReset)
                        {
                            TobeResetList.Add(plcname);
                        }
                        break;

                    case "INT":
                        short int16data = Convert.ToInt16(value);
                        Tcads.WriteAny(handle, int16data);
                        break;

                    case "UINT":
                        ushort uint16data = Convert.ToUInt16(value);
                        Tcads.WriteAny(handle, uint16data);
                        break;

                    case "REAL":
                        float floatdata = Convert.ToSingle(value);
                        Tcads.WriteAny(handle, floatdata);
                        break;

                    case "DINT":
                        int intdata = Convert.ToInt32(value);
                        Tcads.WriteAny(handle, intdata);
                        break;

                    case "UDINT":
                        uint uintdata = Convert.ToUInt32(value);
                        Tcads.WriteAny(handle, uintdata);
                        break;

                    case "LINT":
                        long longdata = Convert.ToInt64(value);
                        Tcads.WriteAny(handle, longdata);
                        break;

                    case "ULINT":
                        ulong ulongdata = Convert.ToUInt64(value);
                        Tcads.WriteAny(handle, ulongdata);
                        break;

                    case "LREAL":
                        double doubledata = Convert.ToDouble(value);
                        Tcads.WriteAny(handle, doubledata);
                        break;
                    }
                }
                Tcads.DeleteVariableHandle(handle);
                return(true);
            }
            catch (Exception ex)
            {
                ErrorFile.ErrorLog(ex.Message, ADS.Logfilepath);
                return(false);
            }
        }
Beispiel #7
0
        public async Task <bool> WriteArrayAsync <T>(string PLCName, T[] value)
        {
            string plcname = "." + PLCName;

            return(await Task.Run(() =>
            {
                try
                {
                    ITcAdsSymbol5 info = (ITcAdsSymbol5)Tcads.ReadSymbolInfo(plcname);
                    string plctype = info.TypeName.Split(' ')[3];
                    int handle = Tcads.CreateVariableHandle(plcname);
                    int length = value.Length;
                    int i = 0;
                    if (typeof(T) != typeof(string))
                    {
                        Tcads.WriteAny(handle, value);
                    }
                    else
                    {
                        switch (plctype)
                        {
                        case "SINT":
                            sbyte[] sbdata = new sbyte[length];
                            foreach (T svalue in value)
                            {
                                sbdata[i] = Convert.ToSByte(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, sbdata);
                            break;

                        case "BYTE":
                            byte[] bydata = new byte[length];
                            foreach (T svalue in value)
                            {
                                bydata[i] = Convert.ToByte(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, bydata);
                            break;

                        case "BOOL":
                            bool[] bdata = new bool[length];
                            foreach (T svalue in value)
                            {
                                bdata[i] = Convert.ToBoolean(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, bdata);
                            break;

                        case "INT":
                            short[] int16data = new short[length];
                            foreach (T svalue in value)
                            {
                                int16data[i] = Convert.ToInt16(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, int16data);
                            break;

                        case "UINT":
                            ushort[] uint16data = new ushort[length];
                            foreach (T svalue in value)
                            {
                                uint16data[i] = Convert.ToUInt16(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, uint16data);
                            break;

                        case "REAL":
                            float[] floatdata = new float[length];
                            foreach (T svalue in value)
                            {
                                floatdata[i] = Convert.ToSingle(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, floatdata);
                            break;

                        case "DINT":
                            int[] intdata = new int[length];
                            foreach (T svalue in value)
                            {
                                intdata[i] = Convert.ToInt32(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, intdata);
                            break;

                        case "UDINT":
                            uint[] uintdata = new uint[length];
                            foreach (T svalue in value)
                            {
                                uintdata[i] = Convert.ToUInt32(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, uintdata);
                            break;

                        case "LINT":
                            long[] longdata = new long[length];
                            foreach (T svalue in value)
                            {
                                longdata[i] = Convert.ToInt64(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, longdata);
                            break;

                        case "ULINT":
                            ulong[] ulongdata = new ulong[length];
                            foreach (T svalue in value)
                            {
                                ulongdata[i] = Convert.ToUInt64(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, ulongdata);
                            break;

                        case "LREAL":
                            double[] doubledata = new double[length];
                            foreach (T svalue in value)
                            {
                                doubledata[i] = Convert.ToDouble(svalue);
                                i++;
                            }
                            Tcads.WriteAny(handle, doubledata);
                            break;
                        }
                    }

                    Tcads.DeleteVariableHandle(handle);
                    return true;
                }
                catch (Exception ex)
                {
                    ErrorFile.ErrorLog(ex.Message, ADS.Logfilepath);
                    return false;
                }
            }));
        }