예제 #1
0
        public static void WriteBusQueue(List <string> list)
        {
            try
            {
                WriteTwincat("GVL.EraseTable", true);

                TcAdsClient client = new TcAdsClient();
                client.Connect(amsnetid, Convert.ToInt32(amsnetport));
                int handle = client.CreateVariableHandle("GVL.DataFromBus");

                foreach (string el in list)
                {
                    AdsStream       stream = new AdsStream(500);
                    AdsBinaryWriter writer = new AdsBinaryWriter(stream);
                    writer.WritePlcString(el, 500, Encoding.Unicode);
                    client.Write(handle, stream);
                    stream.Dispose();
                    writer.Dispose();
                    Thread.Sleep(10);
                }

                client.DeleteVariableHandle(handle);
                client.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine("BusWrite: " + ex.Message);
            }
        }
예제 #2
0
        public static void WriteTwincat(string comando, object valore)
        {
            if (!(valore is null))
            {
                using (TcAdsClient client = new TcAdsClient())
                {
                    try
                    {
                        client.Connect(amsnetid, Convert.ToInt32(amsnetport));
                        int handle = client.CreateVariableHandle(comando);

                        if (valore.GetType().FullName.Contains("String"))
                        {
                            string          el     = valore.ToString();
                            AdsStream       stream = new AdsStream(500);
                            AdsBinaryWriter writer = new AdsBinaryWriter(stream);
                            writer.WritePlcString(el, 500, Encoding.Unicode);
                            client.Write(handle, stream);

                            stream.Dispose();
                            writer.Dispose();
                        }
                        else
                        {
                            client.WriteAny(handle, valore);
                        }

                        //client.Dispose();
                        client.DeleteVariableHandle(handle);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }