# 12.1. Example code to run ticcmd in C

The example C code below shows how to invoke the Tic Command-line Utility (ticcmd) to control a Tic via USB. It demonstrates how to set the target position of the Tic using the --position option. This code works on Windows, Linux, and macOS.

The Tic’s control mode should be set to “Serial / I²C / USB”.

If you have multiple Tic 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 position to 200 on a Tic with serial number 00123456, you can run the command ticcmd -d 00123456 --target 200. You can run ticcmd --list in a shell to get the serial numbers of all the connected Tic devices.

If you want to set the target velocity instead of the target position, you can change the --position argument to --velocity. For a list of other options supported by the Tic Command-line Utility, run ticcmd with no arguments.

In the example below, the child ticcmd process uses the same error pipe as the parent example program, so you will see any error messages printed by ticcmd if you run the example program in a terminal.

You might notice that the Tic only performs the desired movement for about a second before it stops moving and the red LED turns on, indicating an error. This is because of the Tic’s command timeout feature: by default, the Tic’s “Command timeout” error will happen if it does not receive certain commands periodically (see Section 5.4 for details), causing the motor to stop. You can run ticcmd --reset-command-timeout every second to get around this, or you can disable the command timeout feature using the Tic Control Center: uncheck the “Enable command timeout” checkbox in the “Serial” box.

// Uses ticcmd to control the Tic over USB.
//
// NOTE: The Tic's control 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 position, returning 0 on success and -1 on failure.
int tic_set_target_position(int32_t target)
{
char command[1024];
snprintf(command, sizeof(command), "ticcmd --exit-safe-start --position %d", target);
return run_command(command);
}

int main()
{
printf("Setting target position to 800.\n");
int result = tic_set_target_position(800);
if (result) { return 1; }
return 0;
}
(702) 262-6648
Same-day shipping, worldwide