Node.js bindings for LinuxCNC HAL library
npm install @linuxcnc-node/halThis module provides Node.js bindings for the LinuxCNC Hardware Abstraction Layer (HAL). It allows you to create and interact with HAL components, pins, parameters, and signals directly from JavaScript or TypeScript.
- Create HAL components.
- Get and set values of pins and parameters.
- Monitoring: Watch pin and parameter value changes with configurable polling intervals and callback functions.
- Global HAL functions:
- Check if components exist or are ready.
- Manage RTAPI message levels.
- Create new signals.
- Connect pins to signals and disconnect them.
- Get and set values of arbitrary pins, parameters, or signals.
- Retrieve information about all pins, signals, or parameters in the system.
To install the @linuxcnc-node/hal module, use npm or yarn:
`bash`
npm install @linuxcnc-node/halor
yarn add @linuxcnc-node/hal
The module needs to compile against the LinuxCNC development headers and link against its libraries.
#### Standard LinuxCNC Installations
For standard LinuxCNC installations (e.g., from a Debian package or run-in-place after ./configure && make):
- The build script will attempt to locate the necessary files automatically.
#### Custom LinuxCNC Installations
For non-standard or custom LinuxCNC installations:
If LinuxCNC is installed in a custom location you will need to set the following environment variables before running npm install or yarn add:
- LINUXCNC_INCLUDE: Path to the directory containing LinuxCNC's include files (e.g., hal.h, rtapi.h).export LINUXCNC_INCLUDE=/path/to/linuxcnc-dev/include
Example: LINUXCNC_LIB
- : Path to the directory containing LinuxCNC's compiled libraries (e.g., liblinuxcnchal.so).export LINUXCNC_LIB=/path/to/linuxcnc-dev/lib
Example:
`typescript
import { HalComponent } from "@linuxcnc-node/hal";
import * as hal from "@linuxcnc-node/hal";
// --- Component Creation ---
const comp = new HalComponent("my-js-comp");
console.log(Component Name: ${comp.name});Component Prefix: ${comp.prefix}
console.log();
// --- Adding Pins and Parameters ---
const outFloatPin = comp.newPin("output.float", "float", "out");
const inBitPin = comp.newPin("input.bit", "bit", "in");
const rwS32Param = comp.newParam("param.s32", "s32", "rw");
// --- Making Component Ready ---
comp.ready();
console.log(Is 'my-js-comp' ready? ${HalComponent.isReady("my-js-comp")});
// --- Accessing Pin/Param Values ---
outFloatPin.setValue(123.45);
rwS32Param.setValue(-100);
console.log(Value of output.float: ${outFloatPin.getValue()});Value of input.bit (not connected): ${inBitPin.getValue()}
console.log();
// Or via component methods
comp.setValue("output.float", 456.78);
console.log(Value via component: ${comp.getValue("output.float")});
// --- Global HAL Functions ---
hal.newSignal("my-js-signal", "bit");
hal.connect("my-js-comp.output.float", "another-signal-float");
hal.setSignalValue("my-js-signal", true);
console.log(Value of 'my-js-signal': ${hal.getValue("my-js-signal")});
// --- Information Functions ---
console.log("All Pins:", JSON.stringify(hal.getInfoPins(), null, 2));
// --- Message Levels ---
hal.setMsgLevel("all");
console.log(Current message level: ${hal.getMsgLevel()});
// --- Monitoring System ---
// Set up monitoring with custom polling interval
comp.setMonitoringOptions({ pollInterval: 20 }); // Check every 20ms
// Watch for changes on pins and parameters
outFloatPin.on("change", (newValue, oldValue) => {
console.log(Output pin changed: ${oldValue} -> ${newValue});
});
rwS32Param.on("change", (newValue, oldValue) => {
console.log(Parameter changed: ${oldValue} -> ${newValue});
});
// Remove specific callbacks when no longer needed
const myCallback = (val) => console.log(val);
outFloatPin.on("change", myCallback);
outFloatPin.off("change", myCallback);
// Clean up monitoring when done
comp.dispose(); // Stops all monitoring and cleans up resources
`
For a comprehensive example of how to use this module in a real-world application, see the HAL View example:
- HAL View Example: A modern Electron-based HAL viewer application that demonstrates advanced usage of the @linuxcnc-node/hal module.
- new HalComponent(name, prefix?) - Creates a new HAL component
- newPin(), newParam() - Create pins and parametersready()
- , unready() - Control component stategetValue()
- , setValue() - Get/set values by namegetPins()
- , getParams() - Retrieve created itemsgetPin()
- , getParam() - Get specific pin/param by namesetMonitoringOptions()
- - Configure monitoringdispose()
- - Clean up resources
- HalComponent.exists(name) - Check if component existsHalComponent.isReady(name)
- - Check if component is ready
- getValue(), setValue() - Get/set valueson("change", cb)
- , off("change", cb) - Monitor value changes
- getMsgLevel(), setMsgLevel() - Message level controlconnect()
- , disconnect() - Pin/signal connectionsnewSignal()
- - Create signalsgetValue()
- , setPinParamValue(), setSignalValue() - Value operationsgetInfoPins()
- , getInfoSignals(), getInfoParams() - Information queriespinHasWriter()
- - Check pin writer status
- 64-bit Integers (s64, u64):
JavaScript's native number type is an IEEE 754 double-precision float. The C++ bindings convert 64-bit HAL integers to double for JavaScript. This means that integers larger than Number.MAX_SAFE_INTEGER (253-1) or smaller than Number.MIN_SAFE_INTEGER will lose precision. Full BigInt support for 64-bit types is not implemented.HAL_PORT
- No Support:HAL_PORT
Binding, creation, and interaction with type pins or signals are not currently implemented.
To provide comprehensive bindings that closely match the functionality available in the C API (and subsequently the Python bindings), this library includes hal_priv.h from the LinuxCNC source repository. This header allows access to internal HAL data structures and functions (like halpr_find_comp_by_name, hal_data->pin_list_ptr, etc.). This approach aims for functional parity with halcmd and Python's hal` library where possible.