Support » Jrk G2 Motor Controller User’s Guide » 15. Writing PC software to control the Jrk »
15.1. Example code to run jrk2cmd in C
The example C code below shows how to invoke the Jrk G2 Command-line Utility (jrk2cmd) to control a Jrk G2 via USB. It demonstrates how to set the target of the Jrk using the --target
option. This code works on Windows, Linux, and macOS.
If you have multiple Jrk G2 devices connected to your computer via USB, you will need to use the -d
option to specify the serial number of the device you want to use. For example, to set the target to 2248 on a Jrk G2 with serial number 00123456, you can run the command jrk2cmd -d 00123456 --target 2248
. You can run jrk2cmd --list
in a shell to get the serial numbers of all the connected Jrk G2 devices.
In the example below, the child jrk2cmd process uses the same error pipe as the parent example program, so you will see any error messages printed by jrk2cmd if you run the example program in a terminal.
// Uses jrk2cmd to set the target of the Jrk G2 over USB. // // NOTE: The Jrk's input mode must be "Serial / I2C / USB". #include <stdint.h> #include <stdio.h> #include <stdlib.h> // Runs the given shell command. Returns 0 on success, -1 on failure. int run_command(const char * command) { int result = system(command); if (result) { fprintf(stderr, "Command failed with code %d: %s\n", result, command); return -1; } return 0; } // Sets the target, returning 0 on success and -1 on failure. int jrk_set_target(uint16_t target) { char command[1024]; snprintf(command, sizeof(command), "jrk2cmd --target %d", target); return run_command(command); } int main() { printf("Setting target to 2248.\n"); int result = jrk_set_target(2248); if (result) { return 1; } return 0; }