Polo-BOO! Halloween Sale going on now – click for details!

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;
}

Related Products

Jrk G2 18v19 USB Motor Controller with Feedback
Jrk G2 24v13 USB Motor Controller with Feedback
Jrk G2 18v27 USB Motor Controller with Feedback
Jrk G2 24v21 USB Motor Controller with Feedback
Jrk G2 21v3 USB Motor Controller with Feedback
Jrk G2 21v3 USB Motor Controller with Feedback (Connectors Soldered)
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors