Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        public static Coordinate[] CoordinateArrayRead(int ArraySize, string VarAdress)
        {
            Coordinate[] ReadedCoordinate = new Coordinate[ArraySize];
            try
            {
                hVar = TwinCat3Client.CreateVariableHandle(VarAdress);
                AdsStream    ADSdataStream = new AdsStream(8 * 4 * ArraySize);
                BinaryReader binRead       = new BinaryReader(ADSdataStream);
                TwinCat3Client.Read(hVar, ADSdataStream);

                ADSdataStream.Position = 0;
                for (int i = 0; i < ArraySize; i++)
                {
                    ReadedCoordinate[i].X = binRead.ReadDouble();
                    ReadedCoordinate[i].Y = binRead.ReadDouble();
                    ReadedCoordinate[i].Z = binRead.ReadDouble();
                    ReadedCoordinate[i].C = binRead.ReadDouble();
                }

                TwinCat3Client.DeleteVariableHandle(hVar);
                hVar = 0;
                ADSdataStream.Dispose();
                binRead.Dispose();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            return(ReadedCoordinate);
        }
Ejemplo n.º 3
0
        public static Coordinate CoordinateRead(string VarAdress)
        {
            Coordinate ReadedCoordinate = new Coordinate();

            try
            {
                hVar = TwinCat3Client.CreateVariableHandle(VarAdress);
                AdsStream    ADSdataStream = new AdsStream(8 * 4);
                BinaryReader binRead       = new BinaryReader(ADSdataStream);
                TwinCat3Client.Read(hVar, ADSdataStream);

                ADSdataStream.Position = 0;

                ReadedCoordinate.X = binRead.ReadDouble();
                ReadedCoordinate.Y = binRead.ReadDouble();
                ReadedCoordinate.Z = binRead.ReadDouble();
                ReadedCoordinate.C = binRead.ReadDouble();

                TwinCat3Client.DeleteVariableHandle(hVar);
                hVar = 0;
                ADSdataStream.Dispose();
                binRead.Dispose();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            return(ReadedCoordinate);
        }
Ejemplo n.º 4
0
 public void Dispose()
 {
     foreach (var item in NotifyParameters)
     {
         try
         {
             _tcClient.DeleteDeviceNotification(item.Value.NotifyID);
         }
         catch (Exception)
         {
             // ignored
         }
     }
     try
     {
         _tcClient.AdsNotification -= tcClient_AdsNotification;
         _tcClient.Dispose();
         _dataStream.Close();
         _dataStream.Dispose();
         _binReader.Close();
     }
     catch (Exception)
     {
         // ignored
     }
 }
Ejemplo n.º 5
0
        public static string ReadString(string VarAdress)
        {
            string result = null;

            try
            {
                hVar = TwinCat3Client.CreateVariableHandle(VarAdress);
                AdsStream    ADSdataStream = new AdsStream(200);
                BinaryReader binRead       = new BinaryReader(ADSdataStream, System.Text.Encoding.ASCII);
                TwinCat3Client.Read(hVar, ADSdataStream);
                ADSdataStream.Position = 0;
                result = binRead.ReadString();
                TwinCat3Client.DeleteVariableHandle(hVar);
                hVar = 0;
                ADSdataStream.Dispose();
                binRead.Dispose();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            return(result);
        }
Ejemplo n.º 6
0
        public static double ReadLongReal(string VarAdress)
        {
            double result = 0;

            try
            {
                hVar = TwinCat3Client.CreateVariableHandle(VarAdress);
                AdsStream    ADSdataStream = new AdsStream(8);
                BinaryReader binRead       = new BinaryReader(ADSdataStream);
                TwinCat3Client.Read(hVar, ADSdataStream);
                ADSdataStream.Position = 0;
                result = binRead.ReadDouble();
                TwinCat3Client.DeleteVariableHandle(hVar);
                hVar = 0;
                ADSdataStream.Dispose();
                binRead.Dispose();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            return(result);
        }
Ejemplo n.º 7
0
        public static bool ReadBit(string VarAdress)
        {
            bool result = false;

            try
            {
                hVar = TwinCat3Client.CreateVariableHandle(VarAdress);
                AdsStream    ADSdataStream = new AdsStream(1);
                BinaryReader binRead       = new BinaryReader(ADSdataStream);
                TwinCat3Client.Read(hVar, ADSdataStream);
                ADSdataStream.Position = 0;
                result = binRead.ReadBoolean();
                TwinCat3Client.DeleteVariableHandle(hVar);
                hVar = 0;
                ADSdataStream.Dispose();
                binRead.Dispose();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            return(result);
        }
Ejemplo n.º 8
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);
                    }
                }
            }
        }