/* Author: Amadej Arnus Date: 2024-04-23 */ using System; using emDrive_Configurator; using System.Threading; public class Script { // Prepare error string string err = string.Empty; // Create object that we will attempt to read from watch CO_Object HwMonitorUdc = new CO_Object(0x3071, 0x00); public void Main(CancellationToken cancellationToken) { // This will run the example for the WatchScriptObject ObjectBasedVersion(cancellationToken); // This will run the example for the API calls //ObjectBasedVersion(cancellationToken); } private void ObjectBasedVersion(CancellationToken cancellationToken) { // Create new objects for watch-script interoperability Procedure.WatchScriptObject intObject = new Procedure.WatchScriptObject("ScriptObject_Int32", 0x0004); Procedure.WatchScriptObject floatObject = new Procedure.WatchScriptObject("ScriptObject_Real32", 0x0008); // Add int object to watch if (intObject.TryToAddToWatch()) { LogResult("ScriptObject_Int32 added to watch"); } else { LogResult("ScriptObject_Int32 not added to watch"); } // Add float object to watch if (floatObject.TryToAddToWatch()) { LogResult("ScriptObject_Real32 added to watch"); } else { LogResult("ScriptObject_Real32 not added to watch"); } int i = 0; // Do the loop until cancellationToken is requested while (!cancellationToken.IsCancellationRequested) { double hwMonitorValue = 0; // Read last value of HwMonitorUdc object if (HwMonitorUdc.TryGetWatchValue(1, out hwMonitorValue)) { i++; // Set new value to int and float objects if (!intObject.TryToAddValueToWatch(i)) { LogResult("Failed to write value to ScriptObject_Int32"); } if (!floatObject.TryToAddValueToWatch(hwMonitorValue * 1.5)) { LogResult("Failed to write value to ScriptObject_Real32"); } } // If not successful, log the error else { LogResult("Failed to get HwMonitorUdc value"); } Procedure.Delay(200); } } private void ApiBasedVersion(CancellationToken cancellationToken) { // Add int object to watch if (TryAddScriptEntryToWatch("ScriptObject_Int32", 1, 0xFFFF, 0x01, datatype: 0x0004)) { LogResult("ScriptObject_Int32 added to watch"); } else { LogResult("ScriptObject_Int32 not added to watch"); } // Add float object to watch if (TryAddScriptEntryToWatch("ScriptObject_Real32", 1, 0xFFFF, 0x02, datatype: 0x0008)) { LogResult("ScriptObject_Real32 added to watch"); } else { LogResult("ScriptObject_Real32 not added to watch"); } int i = 0; // Do the loop until cancellationToken is requested while (!cancellationToken.IsCancellationRequested) { double hwMinutorValue = 0; // Read last value of HwMonitorUdc object if (TryGetWatchValue(1, 0x3071, 0x00, out hwMinutorValue)) { i++; // Set new value to int and float objects if (!TryAddScriptEntryValue(1, 0xFFFF, 0x01, i)) { LogResult("Failed to write value to ScriptObject_Int32"); } if (!TryAddScriptEntryValue(1, 0xFFFF, 0x02, hwMinutorValue * 1.5)) { LogResult("Failed to write value to ScriptObject_Real32"); } } // If not successful, log the error else { LogResult("Failed to get HwMonitorUdc value"); } Procedure.Delay(200); } } // The following methods are just wrappers to shorten the method calls bool TryAddScriptEntryToWatch(string name, int nodeId, uint index, ushort subindex, ushort datatype) { return Procedure.TryAddScriptEntryToWatch(name, nodeId, index, subindex, datatype); } bool TryAddScriptEntryValue(int nodeId, uint index, ushort subindex, dynamic value) { return Procedure.TryAddScriptEntryValue(nodeId, index, subindex, value); } bool TryGetWatchValue(int nodeId, uint index, ushort subindex, out double value) { return Procedure.TryGetWatchValue(nodeId, index, subindex, out value); } void LogResult(string LogString) { LogString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": " + LogString; Procedure.Log(LogString); } } // CO_Object for readability class CO_Object { public uint index; public ushort subindex; public dynamic value; public CO_Object(uint index, ushort subindex) { this.index = index; this.subindex = subindex; } public bool TryGetWatchValue(int nodeId, out double value) { return Procedure.TryGetWatchValue(nodeId, index, subindex, out value); } }