static void Main(string[] args) { string deviceDescription = "USB-4704,BID#0"; string profilePath = @"C:\Advantech\Profile\p2.xml"; tcClient = new TcAdsClient(); dataStream = new AdsStream(1); instantDoCtrl = new InstantDoCtrl(); binReader = new BinaryReader(dataStream, System.Text.Encoding.ASCII); tcClient.Connect(851); try { instantDoCtrl.SelectedDevice = new DeviceInformation(deviceDescription); errorCode = instantDoCtrl.LoadProfile(profilePath);//Loads a profile to initialize the device. if (BioFailed(errorCode)) { throw new Exception(); } hDigOutP0 = tcClient.AddDeviceNotification("A4704.byDigOutP0", dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, huValHandle); hEndA4704Program = tcClient.AddDeviceNotification("A4704.bEndA4704Program", dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, huValHandle); tcClient.AdsNotification += new AdsNotificationEventHandler(OnNotification); HeartbeatThread heartbeat = new HeartbeatThread(tcClient); heartbeatThread = new Thread(new ThreadStart(heartbeat.beat)); heartbeatThread.Start(); manualResetEvent = new ManualResetEvent(false); manualResetEvent.WaitOne(); //Console.ReadKey(false); } catch (Exception e) { // Something is wrong string errStr = BioFailed(errorCode) ? " Some error occurred. And the last error code is " + errorCode.ToString() : e.Message; //Console.WriteLine(errStr); } finally { instantDoCtrl.Dispose(); heartbeatThread.Abort(); heartbeatThread.Join(); tcClient.DeleteDeviceNotification(hDigOutP0); tcClient.Dispose(); } }
public void InitializeInterface() { dataStream = new AdsStream(255); //Encoding wird auf ASCII gesetzt, um Strings lesen zu können binRead = new BinaryReader(dataStream, System.Text.Encoding.ASCII); // Instanz der Klasse TcAdsClient erzeugen tcClient = new TcAdsClient(); // Verbindung mit Port 851 auf dem lokalen Computer herstellen tcClient.Connect(851); hConnect = new int[7]; aiData = new int[7]; try { hConnect[0] = tcClient.AddDeviceNotification(m_sVariabelName, dataStream, 0, 255, AdsTransMode.OnChange, 500, 0, aiData); //hConnect[1] = tcClient.AddDeviceNotification("MAIN.intVal", dataStream, 1, 2, AdsTransMode.OnChange, 100, 0, tbInt); } catch (Exception err) { bool b = true; while (b) { string message = "Connection to PLC-Variables failed. Please Run TwinCat or check declared variables and retry"; string caption = "Connection failed!"; } } tcClient.AdsNotification += new AdsNotificationEventHandler(OnNotification); }
/////////////////////////////////////////////////////////////////////////// public void AñadirRedireccion() { int tamaño = 0; notifications.Clear(); foreach (IO_Parameter IO in IOparameters.Out.Values) { tamaño += getTypeSize(Type.GetType(IO.TypeStr)); } stream = new AdsStream(tamaño); reader = new BinaryReader(stream, Encoding.ASCII); string asd = ""; tamaño = 0; try { tcAds.AdsNotification -= new AdsNotificationEventHandler(OnNotification); foreach (IO_Parameter IO in IOparameters.Out.Values) { asd = IO.Name; notifications.Add(tcAds.AddDeviceNotification(IO.Name, stream, tamaño, getTypeSize(Type.GetType(IO.TypeStr)), AdsTransMode.OnChange, 1, 1, IO.Value), IO.Name); tamaño += getTypeSize(Type.GetType(IO.TypeStr)); } } catch (Exception e) { parent.UBSMod.Error("Error al añadir la redirección. " + e.Message, true, false); } tcAds.AdsNotification += new AdsNotificationEventHandler(OnNotification); }
private void buttonConnect_Click(object sender, EventArgs e) { // luodaan AdsClient-olio yhteyttä varten adsClient = new TcAdsClient(); // otetaan yhteys TwinCAT PLC:hen portin 851 kautta adsClient.Connect(851); // luodaan PLC:n muuttujiin viittaavat "osoittimet" startHandle = adsClient.CreateVariableHandle("MAIN.start"); stopHandle = adsClient.CreateVariableHandle("MAIN.reset"); // Luodaan tapahtumankäsittelijä. Parametrina näyttöä päivittävän metodin nimi adsClient.AdsNotification += new AdsNotificationEventHandler(UpdateVariables); // avataan Ads-stream (luettava byte-määrä pitänee antaa parametrina this.adsStream = new AdsStream(24); binReader = new BinaryReader(this.adsStream); /* * timeHi: UDINT; // aika * timeLo: UDINT; // aika * measurement1: REAL; // mittaus 1 * measurement2: REAL; // mittaus 2 * measurement3: REAL; // mittaus 3 * counter: INT; // juokseva numero * arrayValue: INT; // jotain * */ measurementDataHandle = adsClient.AddDeviceNotification("MAIN.measurementData", this.adsStream, 0, 24, AdsTransMode.Cyclic, 100, 0, null); buttonConnect.Enabled = false; buttonSetSR1.Enabled = true; buttonResetSR1.Enabled = false; }
public void ReadLamp() { adsStreamLamp = new AdsStream(4); binReader = new BinaryReader(adsStreamLamp, ASCIIEncoding.ASCII); try { hLamp[0] = client.AddDeviceNotification("GVL_Machine.HWIO.HWOutput.Lamp01", adsStreamLamp, AdsTransMode.OnChange, 100, 0, lbLight00); hLamp[1] = client.AddDeviceNotification("GVL_Machine.HWIO.HWOutput.Lamp02", adsStreamLamp, AdsTransMode.OnChange, 100, 0, lbLight01); hLamp[2] = client.AddDeviceNotification("GVL_Machine.HWIO.HWOutput.Lamp03", adsStreamLamp, AdsTransMode.OnChange, 100, 0, lbLight02); hLamp[3] = client.AddDeviceNotification("GVL_Machine.HWIO.HWOutput.Lamp04", adsStreamLamp, AdsTransMode.OnChange, 100, 0, lbLight03); } catch (Exception err) { MessageBox.Show("Exception: ReadLamp: " + err.Message); } client.AdsNotification += Lamp_AdsNotification; }
public void AddTag(Tag tag) { if (_notificationHandlesByTag.Any(t => Equals(t.Item1, tag))) { return; } if (tag.AdsPort == 0) { tag.AdsPort = _adsPort; } tag.ValueChanged += TagValueChanged; if ((_notificationHandlesByTag.Count + 1) >= 500) { throw new PlcCommunicationException("Supported Tag limit of " + MaxTagCount + " exceeded! Beckhoff does not support more ADS handles.", _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty); } // add tag at runtime if (_twinCatClient.IsConnected) { try { int handle = _twinCatClient.AddDeviceNotification(tag.FullName(), new AdsStream(tag.BitSize == 0 ? 81 : tag.BitSize / 8), AdsTransMode.OnChange, (int)RefreshRate, 0, tag); _notificationHandlesByTag.Add(new Tuple <Tag, int>(tag, handle)); } catch (AdsErrorException e) { throw new PlcCommunicationException("Can't add Tag " + tag.FullName() + " to Listener (which is already running)", _adsAddress, _adsPort.ToString(CultureInfo.InvariantCulture), string.Empty, e); } } else { _notificationHandlesByTag.Add(new Tuple <Tag, int>(tag, 0)); } }
public void CreateNotificationHandles() { try { hConnect = ads.AddDeviceNotification("MAIN.ConnectDB.bConnect", datastream, 0, 1, AdsTransMode.OnChange, 100, 0, connect); } catch (Exception) { Console.WriteLine("not connected to tc"); throw; } hExecuteDataReturn = ads.AddDeviceNotification("MAIN.DataReturn.bExecute", datastream, 0, 1, AdsTransMode.OnChange, 100, 0, ExecuteDataReturn); hWriteExecute = ads.AddDeviceNotification("MAIN.DBWrite.bExecute", datastream, 0, 1, AdsTransMode.OnChange, 100, 0, WriteExecute); hExecuteTableCreate = ads.AddDeviceNotification("MAIN.CreateTable.bExecute", datastream, 0, 1, AdsTransMode.OnChange, 100, 0, ExecuteTableCreate); //AdsNotificationEventHandler handler = OnNotification; //ads.AdsNotification += ADSNotification; //mainbe: //ads.AdsNotification += ADSNotification; //ads.AdsNotification += new AdsNotificationEventHandler(Onnotification); }
/// <summary> /// Adds device to device list, creates read and write handlers /// </summary> /// <param name="vDeviceName">string, Name of device that matches variable name in PLC</param> /// <param name="vDataType">Type, Data Type of the variable in the PLC</param> public MX_PLCDevice AddDevice(string vDeviceName, Type vDataType) { MX_PLCDevice vNewDevice = new MX_PLCDevice(); // Create a new temporary device vNewDevice.Name = vDeviceName; // Add name to the temporary device vNewDevice.DataType = vDataType; // Add data type to the temporary device vDevices.Add(vNewDevice); // Add the new device to the device list int vIndex = vDevices.Count() - 1; // Get the index of the newly added device // Create an ADS read handler for the new device vDevices[vIndex].ReadHndl = vAdsClient.AddDeviceNotification(vDeviceName, vStream, AdsTransMode.OnChange, updateTime, 0, vDevices[vIndex].Value); // Create an ADS write handler for the new device vDevices[vIndex].WriteHndl = vAdsClient.CreateVariableHandle(vDeviceName); // Return the new device in case the calling function wants it return(vDevices[vIndex]); }
//----------------------------------------------------- //Wird als erstes beim Starten des Programms aufgerufen //Is activated first when the program is started //----------------------------------------------------- private void Load(object sender, EventArgs e) { try { // Eine neue Instanz der Klasse AdsStream erzeugen // Create a new instance of the AdsStream class dataStream = new AdsStream(31); // Eine neue Instanz der Klasse BinaryReader erzeugen // Create a new instance of the BinaryReader class binReader = new BinaryReader(dataStream); // Eine neue Instanz der Klasse TcAdsClient erzeugen // Create a new instance of the TcAdsClient class tcClient = new TcAdsClient(); // Verbinden mit lokaler SPS - Laufzeit 1 - Port 801 // Connecting to local PLC - Runtime 1 - Port 801 tcClient.Connect(801); } catch { MessageBox.Show("Fehler beim Laden"); } try { // Initialisieren der Überwachung der SPS-Variablen // Initializing the monitoring of the PLC variables hEngine = tcClient.AddDeviceNotification(".engine", dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, null); hDeviceUp = tcClient.AddDeviceNotification(".deviceUp", dataStream, 1, 1, AdsTransMode.OnChange, 10, 0, null); hDeviceDown = tcClient.AddDeviceNotification(".deviceDown", dataStream, 2, 1, AdsTransMode.OnChange, 10, 0, null); hSteps = tcClient.AddDeviceNotification(".steps", dataStream, 0, 1, AdsTransMode.OnChange, 10, 0, null); hCount = tcClient.AddDeviceNotification(".count", dataStream, 4, 2, AdsTransMode.OnChange, 10, 0, null); hSwitchNotify = tcClient.AddDeviceNotification(".switch", dataStream, 6, 1, AdsTransMode.OnChange, 10, 0, null); // Holen des Handles von "switch" - wird für das Schreiben des Wertes benötigt // Getting the handle for "switch" - needed for writing the value hSwitchWrite = tcClient.CreateVariableHandle(".switch"); // Erstellen eines Events für Änderungen an den SPS-Variablen-Werten // Creating an event for changes of the PLC variable values tcClient.AdsNotification += new AdsNotificationEventHandler(tcClient_OnNotification); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void RegisterParameter(X ParameterName, int CycleTime = 1, int MaxDelay = 0, bool CheckNotify = true) { try { var byteLen = ParameterName.GetParameterSize(); var att = ParameterName.GetAttribute <TypeAttribute>(); if (NotifyParameters.Any(a => a.Key == ParameterName)) { throw new Exception("This parameter already registered"); } var pName = $"{att.SourceFunction}.{ParameterName}"; if (CheckNotify && !att.Notify) { return; } var readId = _tcClient.AddDeviceNotification( pName, _dataStream, LastParameterIndex, byteLen, AdsTransMode.OnChange, CycleTime, MaxDelay, null); NotifyParameters.Add(new KeyValuePair <X, Parameter>(ParameterName, new Parameter() { ParameterName = ParameterName, NotifyID = readId, StreamPosition = LastParameterIndex })); LastParameterIndex += byteLen; ParametersValue.Add(ParameterName, null); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void StartMonitoring() { parameterOne = tcClient.AddDeviceNotification(ParaOne, dataStream, AdsTransMode.OnChange, 100, 0, null); tcClient.AdsNotification += new AdsNotificationEventHandler(tcClient_OnNotification); }