private void ReadListFromPlc()
        {
            int i = 0;
            MachineParamList list = tcMachineParam1.SelectedList;

            if (adsPlcServer == null ||
                adsPlcServer.PlcIsRunning == false ||
                list == null ||
                list.Disabled == true ||
                list.PLCVar.Length == 0)
            {
                return;
            }


            try
            {
                int rows = list.Data.Count;
                TwinCAT.Ads.ITcAdsSymbol symbol = adsPlcServer.PlcClient.ReadSymbolInfo(list.PLCVar);

                if (symbol == null)
                {
                    MainApp.log.Error("Machine parameter list cannot be read from the Plc! Symbol not found! List: " + list.Name + " PlcVar: " + list.PLCVar);
                    return;
                }

                switch (symbol.Datatype)
                {
                case TwinCAT.Ads.AdsDatatypeId.ADST_BIT:
                    bool[] b = (bool[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(bool[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = b[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_INT8:
                    sbyte[] n8 = (sbyte[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(sbyte[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = n8[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_INT16:
                    Int16[] n16 = (Int16[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(Int16[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = n16[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_INT32:
                    Int32[] n32 = (Int32[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(Int32[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = n32[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_INT64:
                    Int64[] n64 = (Int64[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(Int64[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = n64[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_UINT8:
                    byte[] u8 = (byte[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(byte[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = u8[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_UINT16:
                    UInt16[] u16 = (UInt16[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(UInt16[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = u16[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_UINT32:
                    UInt32[] u32 = (UInt32[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(UInt32[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = u32[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_UINT64:
                    UInt64[] u64 = (UInt64[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(UInt64[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = u64[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_REAL32:
                    float[] f32 = (float[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(float[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = f32[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_REAL64:
                    double[] f64 = (double[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(double[]), new int[] { rows });
                    for (i = 0; i < rows; i++)
                    {
                        list.Data[i].Value = f64[i].ToString();
                    }
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_BIGTYPE:
                {
                    if (symbol.Type.Contains("STRING"))
                    {
                        int  stringLength = 0;
                        bool res          = adsPlcServer.PlcClient.GetBigTypeArgs(symbol.Type, ref stringLength);
                        if (res == true)
                        {
                            string[] fVal = (string[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(string[]), new int[] { stringLength, rows });
                            for (i = 0; i < rows; i++)
                            {
                                list.Data[i].Value = fVal[i].ToString();
                            }
                        }
                    }
                    else if (symbol.Type.Contains("TIME"))
                    {
                        UInt32[] fVal = (UInt32[])adsPlcServer.PlcClient.ReadAny(list.PLCVar, typeof(UInt32[]), new int[] { rows });
                        for (i = 0; i < rows; i++)
                        {
                            list.Data[i].Value = fVal[i].ToString();
                        }
                    }
                }
                break;

                default:
                    MainApp.log.Error("Machine parameter list cannot be read from the Plc! Datatype not supported! List: " + list.Name + ", PlcVar: " + list.PLCVar);
                    break;
                }

                // write the data back to the grid
                tcMachineParam1.FillGrid(list);
            }
            catch (Exception ex)
            {
                MainApp.log.Error("Error in FormMaschPara ReadListFromPlc()", ex);
            }
        }
        private void WriteListToPlc(int listIdx)
        {
            int i = 0;
            // get reference from parameter list object
            MachineParamList list = tcMachineParam1.GetMParList(listIdx);

            if (adsPlcServer == null ||
                adsPlcServer.PlcIsRunning == false ||
                list == null ||
                list.Disabled == true ||
                list.PLCVar.Length == 0)
            {
                return;
            }


            try
            {
                int rows = list.Data.Count;
                TwinCAT.Ads.ITcAdsSymbol symbol = adsPlcServer.PlcClient.ReadSymbolInfo(list.PLCVar);

                if (symbol == null)
                {
                    MainApp.log.Error("Machine parameter list cannot be written to the Plc! Symbol not found! List: " + list.Name + " PlcVar: " + list.PLCVar);
                    return;
                }

                switch (symbol.Datatype)
                {
                case TwinCAT.Ads.AdsDatatypeId.ADST_BIT:
                    bool[] b = new bool[rows];
                    for (i = 0; i < rows; i++)
                    {
                        b[i] = (list.Data[i].Value.Length == 0) ? false : bool.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, b);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_INT8:
                    sbyte[] n8 = new sbyte[rows];
                    for (i = 0; i < rows; i++)
                    {
                        n8[i] = (list.Data[i].Value.Length == 0) ? (sbyte)0 : sbyte.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, n8);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_INT16:
                    Int16[] n16 = new Int16[rows];
                    for (i = 0; i < rows; i++)
                    {
                        n16[i] = (list.Data[i].Value.Length == 0) ? (Int16)0 : Int16.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, n16);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_INT32:
                    Int32[] n32 = new Int32[rows];
                    for (i = 0; i < rows; i++)
                    {
                        n32[i] = (list.Data[i].Value.Length == 0) ? (Int32)0 : Int32.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, n32);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_INT64:
                    Int64[] n64 = new Int64[rows];
                    for (i = 0; i < rows; i++)
                    {
                        n64[i] = (list.Data[i].Value.Length == 0) ? (Int64)0 : Int64.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, n64);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_UINT8:
                    byte[] u8 = new byte[rows];
                    for (i = 0; i < rows; i++)
                    {
                        u8[i] = (list.Data[i].Value.Length == 0) ? (byte)0 : byte.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, u8);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_UINT16:
                    UInt16[] u16 = new UInt16[rows];
                    for (i = 0; i < rows; i++)
                    {
                        u16[i] = (list.Data[i].Value.Length == 0) ? (UInt16)0 : UInt16.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, u16);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_UINT32:
                    UInt32[] u32 = new UInt32[rows];
                    for (i = 0; i < rows; i++)
                    {
                        u32[i] = (list.Data[i].Value.Length == 0) ? (UInt32)0 : UInt32.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, u32);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_UINT64:
                    UInt64[] u64 = new UInt64[rows];
                    for (i = 0; i < rows; i++)
                    {
                        u64[i] = (list.Data[i].Value.Length == 0) ? (UInt64)0 : UInt64.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, u64);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_REAL32:
                    float[] f32 = new float[rows];
                    for (i = 0; i < rows; i++)
                    {
                        f32[i] = (list.Data[i].Value.Length == 0) ? 0 : float.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, f32);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_REAL64:
                    double[] f64 = new double[rows];
                    for (i = 0; i < rows; i++)
                    {
                        f64[i] = (list.Data[i].Value.Length == 0) ? 0 : double.Parse(list.Data[i].Value);
                    }
                    adsPlcServer.PlcClient.WriteAny(list.PLCVar, f64);
                    break;

                case TwinCAT.Ads.AdsDatatypeId.ADST_BIGTYPE:
                {
                    if (symbol.Type.Contains("STRING"))
                    {
                        string[] strVal = new string[rows];
                        for (i = 0; i < rows; i++)
                        {
                            strVal[i] = list.Data[i].Value;
                        }
                        int  stringLength = 0;
                        bool res          = adsPlcServer.PlcClient.GetBigTypeArgs(symbol.Type, ref stringLength);
                        if (res == true)
                        {
                            adsPlcServer.PlcClient.WriteAny(list.PLCVar, strVal, new int[] { stringLength, rows });
                        }
                        else
                        {
                            MainApp.log.Error("Machine parameter list cannot be written to the Plc! Write Strings! " + "List: " + list.Name + ", Row: " + (i + 1).ToString());
                        }
                    }
                    else if (symbol.Type.Contains("TIME"))
                    {
                        UInt32[] fVal = new UInt32[rows];
                        for (i = 0; i < rows; i++)
                        {
                            fVal[i] = (list.Data[i].Value.Length == 0) ? (UInt32)0 : UInt32.Parse(list.Data[i].Value);
                        }
                        adsPlcServer.PlcClient.WriteAny(list.PLCVar, fVal);
                    }
                    break;
                }

                default:
                    MainApp.log.Error("Machine parameter list cannot be written to the Plc! Datatype not supported! List: " + list.Name + ", PlcVar: " + list.PLCVar);
                    break;
                }
            }
            catch (Exception ex)
            {
                MainApp.log.Error("Machine parameter list cannot be written to the Plc! " + "List: " + list.Name + ", Row: " + (i + 1).ToString(), ex);
            }
        }
        void tcMachineParam1_ValidateValue(object sender, CancelEventArgs e, int rowNumber, MachineParamData data)
        {
            try
            {
                if (data.Value.Length == 0)
                {
                    return;
                }

                if (adsPlcServer.PlcIsRunning)
                {
                    MachineParamList         list   = tcMachineParam1.SelectedList;
                    TwinCAT.Ads.ITcAdsSymbol symbol = adsPlcServer.PlcClient.ReadSymbolInfo(list.PLCVar);
                    if (symbol != null)
                    {
                        switch (symbol.Datatype)
                        {
                        case TwinCAT.Ads.AdsDatatypeId.ADST_BIT:
                            if (data.DataType != typeof(bool))
                            {
                                e.Cancel = true;
                            }
                            break;

                        case TwinCAT.Ads.AdsDatatypeId.ADST_INT8:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_INT16:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_INT32:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_INT64:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_UINT8:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_UINT16:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_UINT32:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_UINT64:
                            if (data.DataType != typeof(int))
                            {
                                e.Cancel = true;
                            }
                            break;

                        case TwinCAT.Ads.AdsDatatypeId.ADST_REAL32:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_REAL64:
                        case TwinCAT.Ads.AdsDatatypeId.ADST_REAL80:
                            if (data.DataType != typeof(int) &&
                                data.DataType != typeof(double))
                            {
                                e.Cancel = true;
                            }
                            break;
                        }
                    }
                }

                /*
                 * double oldVal = data.val;
                 * double val = double.Parse(((TextBox)sender).Text);
                 *
                 * if (rowNumber == 10 && val > 100)
                 * {
                 *      MessageBox.Show("Out of range. Value to big!");
                 *      e.Cancel = true;
                 * }
                 */
            }
            catch
            {
                e.Cancel = true;
            }
        }