Rust wrapper of the Pylon libraries for Basler cameras using a pure C ABI shim library.
The crate dynamically loads a C ABI shim library at runtime, which in turn loads the Pylon library. This allows the crate to be used without linking to the Pylon library at compile time, and also allows it to be used with different versions of the Pylon library without recompilation.
The main API entry points are:
- Top-level helpers like
create_first_device,enumerate_devices, andversion runtime::init()andruntime::RuntimeGuardfor explicit runtime lifecycle controlInstantCamerato open a device and grab imagesNodeMapand the typed node wrappers to access GenICam parametersGrabResultto inspect acquired image buffers
This crate is derived from pylon-cxx, which is a more direct wrapper of the C++ API. The API is mostly the same, but some differences exist due to the C ABI shim. Porting code from pylon-cxx should be straightforward.
Linux (x86_64) and macOS (aarch64) are tested. Windows code exists in the crate, but is not currently tested.
The smallest useful setup check is:
cargo run --example show-versionTo talk to a camera synchronously:
cargo run --example grabExamples included in this repository:
show-version: print the crate and loaded Pylon versiongrab: grab images synchronouslyasync-grab: grab images as a tokio streamshow-properties: list device propertiesshow-pixel-formats: inspect available pixel formatsfeature-persistence: save and restore node-map settingschunk-data: inspect chunk data from acquired imagesreset-all-devices: execute a reset command on discovered devices
Enable async stream support using tokio with the cargo feature stream.
The included async example also needs a multi-threaded tokio runtime:
cargo run --example async-grab --features stream,tokio/rt-multi-threadShims for different platforms and Pylon versions can be found at https://strawlab.org/assets/libpylon-cabi/precompiled/.
SHIM_FILENAME="libpylon-cabi-v1-linux-x86_64-pylon_7.3.0.27189.so"
SHIM_URL="https://strawlab.org/assets/libpylon-cabi/precompiled/${SHIM_FILENAME}"
curl --fail -L -o libpylon-cabi.so "${SHIM_URL}"
export "PYLON_CABI=$(pwd)/libpylon-cabi.so"SHIM_FILENAME="libpylon-cabi-v1-macos-aarch64-pylon_7.3.1.9.dylib"
SHIM_URL="https://strawlab.org/assets/libpylon-cabi/precompiled/${SHIM_FILENAME}"
curl --fail -L -o libpylon-cabi.dylib "${SHIM_URL}"
export "PYLON_CABI=$(pwd)/libpylon-cabi.dylib"The source code for the C ABI shim library is in the libpylon-cabi directory. It
can be built independently of this Rust crate, and the resulting shared library
can be used with this crate.
Building locally requires the Basler Pylon SDK to be installed. The default build paths are:
- Linux:
/opt/pylon - macOS:
/Library/Frameworks/pylon.framework
Build the shim with:
make -C libpylon-cabiYou can override the Linux SDK location, for example:
make -C libpylon-cabi PYLON_ROOT=/opt/pylonReproducible Linux builds of the shim can be done with Docker. See
libpylon-cabi/Dockerfile for details.
The location of the C ABI shim library can be specified to this crate using the
PYLON_CABI environment variable. If this variable is not set, the crate will
look for the shim library in the standard system library locations.
cargo run --example grabOn macOS, building works, but the Pylon framework libraries must still be found at runtime. If they are not already discoverable on your system, set:
export DYLD_LIBRARY_PATH="/Library/Frameworks/pylon.framework/Versions/A/Libraries/"
An article like
https://jorgen.tjer.no/post/2014/05/20/dt-rpath-ld-and-at-rpath-dyld/ which
describes approaches that may avoid relying on DYLD_LIBRARY_PATH.
See Basler's documentation. This can simulate different frame rates, failures, etc.
# on bash (e.g. linux)
export PYLON_CAMEMU=2
# in Windows Powershell
$Env:PYLON_CAMEMU=2
Anyone who interacts with this software in any space, including but not limited to this GitHub repository, must follow our code of conduct.
This crate is Copyright (C) 2020 Andrew Straw strawman@astraw.com.
Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or distributed except according to those terms.
Note that this license only covers this Rust crate. The underlying Pylon library has different license terms.