/* Load a boolean node. */ private void LoadBooleanNode(string nodeName) { var nodeConf = Config.GetConfig(nodeName); if (nodeConf == null || string.IsNullOrWhiteSpace(nodeConf)) { return; } if (!bool.TryParse(nodeConf, out bool value)) { return; } var node = ImageProvider.GetNodeFromDevice(nodeName); if (node.IsValid) { if (GenApi.NodeIsWritable(node)) { GenApi.BooleanSetValue(node, value); } } }
/* Some features are boolean features that can be switched on and off. * This function illustrates how to access boolean features. */ private static void demonstrateBooleanFeature(PYLON_DEVICE_HANDLE hDev) { NODEMAP_HANDLE hNodeMap; NODE_HANDLE hNode; string featureName = "GammaEnable"; /* The name of the feature. */ bool value, bval; /* The value of the feature. */ EGenApiNodeType nodeType; /* Get a handle for the device's node map. */ hNodeMap = Pylon.DeviceGetNodeMap(hDev); /* Look up the feature node. */ hNode = GenApi.NodeMapGetNode(hNodeMap, featureName); if (!hNode.IsValid) { Console.WriteLine("There is no feature named '" + featureName + "'."); return; } /* We want a boolean feature node. */ nodeType = GenApi.NodeGetType(hNode); if (EGenApiNodeType.BooleanNode != nodeType) { Console.WriteLine("'" + featureName + "' is not a boolean feature."); return; } /* Check to see if the feature is readable. */ bval = GenApi.NodeIsReadable(hNode); if (bval) { /* Retrieve the current state of the feature. */ value = GenApi.BooleanGetValue(hNode); Console.WriteLine("The {0} feature is {1}.", featureName, value ? "on" : "off"); /* Set a new value. */ bval = GenApi.NodeIsWritable(hNode); if (bval) { value = (bool)!value; /* New value. */ Console.WriteLine("Switching the {0} feature {1}.", featureName, value ? "on" : "off"); GenApi.BooleanSetValue(hNode, value); } else { Console.WriteLine("Cannot set value for feature '{0}' - node not writable.", featureName); } } else { Console.WriteLine("Cannot read feature '{0}' - node not readable.", featureName); } }
public static void Write(PYLON_DEVICE_HANDLE deviceHandle, CameraProperty property) { if (!property.Supported || string.IsNullOrEmpty(property.Identifier)) { return; } NODEMAP_HANDLE nodeMapHandle = Pylon.DeviceGetNodeMap(deviceHandle); NODE_HANDLE nodeHandle = GenApi.NodeMapGetNode(nodeMapHandle, property.Identifier); if (!nodeHandle.IsValid) { return; } EGenApiAccessMode accessMode = GenApi.NodeGetAccessMode(nodeHandle); if (accessMode != EGenApiAccessMode.RW) { return; } switch (property.Type) { case CameraPropertyType.Integer: { long value = long.Parse(property.CurrentValue, CultureInfo.InvariantCulture); long step = long.Parse(property.Step, CultureInfo.InvariantCulture); long remainder = value % step; if (remainder > 0) { value = value - remainder; } GenApi.IntegerSetValue(nodeHandle, value); break; } case CameraPropertyType.Float: { double value = double.Parse(property.CurrentValue, CultureInfo.InvariantCulture); GenApi.FloatSetValue(nodeHandle, value); break; } case CameraPropertyType.Boolean: { bool value = bool.Parse(property.CurrentValue); GenApi.BooleanSetValue(nodeHandle, value); break; } default: break; } }
/// <summary> /// Takes a boolean of whether auto is ON or OFF, convert it to the correct representation and write it in the auto property. /// </summary> private static void WriteAuto(NODE_HANDLE nodeHandle, string identifier, bool isAuto) { string newValue = isAuto ? GetAutoTrue(identifier) : GetAutoFalse(identifier); switch (identifier) { case "AcquisitionFrameRateEnable": { bool newValueBool = bool.Parse(newValue); GenApi.BooleanSetValue(nodeHandle, newValueBool); break; } case "GainAuto": case "ExposureAuto": default: { PylonHelper.WriteEnum(nodeHandle, identifier, newValue); break; } } }
private static void WriteCenter(PYLON_DEVICE_HANDLE deviceHandle) { // Force write the CenterX and CenterY properties if supported. // https://docs.baslerweb.com/center-x-and-center-y.html // This is apparently required in order for the MaxWidth/MaxHeight properties to behave correctly // and independently of the offset property. // To summarize we use the following approach: // 1. If CenterX/CenterY are supported properties, we use them and OffsetX/OffsetY will be automated by Pylon. // 2. Otherwise we use manually write OffsetX/OffsetY to center the image. NODEMAP_HANDLE nodeMapHandle = Pylon.DeviceGetNodeMap(deviceHandle); NODE_HANDLE nodeHandleX = GenApi.NodeMapGetNode(nodeMapHandle, "CenterX"); NODE_HANDLE nodeHandleY = GenApi.NodeMapGetNode(nodeMapHandle, "CenterY"); if (nodeHandleX.IsValid && nodeHandleY.IsValid) { EGenApiAccessMode accessModeOffsetX = GenApi.NodeGetAccessMode(nodeHandleX); EGenApiAccessMode accessModeOffsetY = GenApi.NodeGetAccessMode(nodeHandleY); if (accessModeOffsetX == EGenApiAccessMode.RW && accessModeOffsetY == EGenApiAccessMode.RW) { GenApi.BooleanSetValue(nodeHandleX, true); GenApi.BooleanSetValue(nodeHandleY, true); } } }
public static void Write(PYLON_DEVICE_HANDLE deviceHandle, CameraProperty property) { if (!property.Supported || string.IsNullOrEmpty(property.Identifier) || !deviceHandle.IsValid) { return; } // If "auto" flag is OFF we should write it first. On some cameras the value is not writable until the corresponding auto flag is off. // If it's ON (continuous), it doesn't matter as our value will be overwritten soon anyway. if (!string.IsNullOrEmpty(property.AutomaticIdentifier)) { string enumValue = property.Automatic ? "Continuous" : "Off"; PylonHelper.WriteEnum(deviceHandle, property.AutomaticIdentifier, enumValue); } NODEMAP_HANDLE nodeMapHandle = Pylon.DeviceGetNodeMap(deviceHandle); NODE_HANDLE nodeHandle = GenApi.NodeMapGetNode(nodeMapHandle, property.Identifier); if (!nodeHandle.IsValid) { return; } EGenApiAccessMode accessMode = GenApi.NodeGetAccessMode(nodeHandle); if (accessMode != EGenApiAccessMode.RW) { if (!string.IsNullOrEmpty(property.AutomaticIdentifier) && !property.Automatic) { log.ErrorFormat("Error while writing Basler Pylon GenICam property {0}.", property.Identifier); log.ErrorFormat("The property is not writable."); } return; } try { switch (property.Type) { case CameraPropertyType.Integer: { long value = long.Parse(property.CurrentValue, CultureInfo.InvariantCulture); long step = long.Parse(property.Step, CultureInfo.InvariantCulture); long remainder = value % step; if (remainder > 0) { value = value - remainder; } GenApi.IntegerSetValue(nodeHandle, value); break; } case CameraPropertyType.Float: { double max = GenApi.FloatGetMax(nodeHandle); double min = GenApi.FloatGetMin(nodeHandle); double value = double.Parse(property.CurrentValue, CultureInfo.InvariantCulture); value = Math.Min(Math.Max(value, min), max); GenApi.FloatSetValue(nodeHandle, value); break; } case CameraPropertyType.Boolean: { bool value = bool.Parse(property.CurrentValue); GenApi.BooleanSetValue(nodeHandle, value); break; } default: break; } } catch { log.ErrorFormat("Error while writing Basler Pylon GenICam property {0}.", property.Identifier); } }
/// <summary> /// Write generic property with optional auto flag. /// </summary> private static void WriteProperty(PYLON_DEVICE_HANDLE deviceHandle, CameraProperty property) { if (property.ReadOnly) { return; } NODEMAP_HANDLE nodeMapHandle = Pylon.DeviceGetNodeMap(deviceHandle); // Switch OFF the auto flag if needed, to be able to write the main property. if (!string.IsNullOrEmpty(property.AutomaticIdentifier)) { NODE_HANDLE nodeHandleAuto = GenApi.NodeMapGetNode(nodeMapHandle, property.AutomaticIdentifier); if (nodeHandleAuto.IsValid) { bool writeable = GenApi.NodeIsWritable(nodeHandleAuto); bool currentAuto = ReadAuto(nodeHandleAuto, property.AutomaticIdentifier); if (writeable && property.CanBeAutomatic && currentAuto && !property.Automatic) { WriteAuto(nodeHandleAuto, property.AutomaticIdentifier, false); } } } // At this point the auto flag is off. Write the main property. NODE_HANDLE nodeHandle = GenApi.NodeMapGetNode(nodeMapHandle, property.Identifier); if (!nodeHandle.IsValid) { return; } EGenApiAccessMode accessMode = GenApi.NodeGetAccessMode(nodeHandle); if (accessMode != EGenApiAccessMode.RW) { return; } try { switch (property.Type) { case CameraPropertyType.Integer: { long value = long.Parse(property.CurrentValue, CultureInfo.InvariantCulture); long min = GenApi.IntegerGetMin(nodeHandle); long max = GenApi.IntegerGetMax(nodeHandle); long step = GenApi.IntegerGetInc(nodeHandle); value = FixValue(value, min, max, step); GenApi.IntegerSetValue(nodeHandle, value); break; } case CameraPropertyType.Float: { double value = double.Parse(property.CurrentValue, CultureInfo.InvariantCulture); double min = GenApi.FloatGetMin(nodeHandle); double max = GenApi.FloatGetMax(nodeHandle); value = FixValue(value, min, max); GenApi.FloatSetValue(nodeHandle, value); break; } case CameraPropertyType.Boolean: { bool value = bool.Parse(property.CurrentValue); GenApi.BooleanSetValue(nodeHandle, value); break; } default: break; } } catch { log.ErrorFormat("Error while writing Basler Pylon GenICam property {0}.", property.Identifier); } // Finally, switch ON the auto flag if needed. if (!string.IsNullOrEmpty(property.AutomaticIdentifier)) { NODE_HANDLE nodeHandleAuto = GenApi.NodeMapGetNode(nodeMapHandle, property.AutomaticIdentifier); if (nodeHandleAuto.IsValid && GenApi.NodeIsWritable(nodeHandleAuto) && property.CanBeAutomatic && property.Automatic) { WriteAuto(nodeHandleAuto, property.AutomaticIdentifier, true); } } }