How to make a program against the libcalccables library


You will find in the test folder of the library source archive a test/example program which uses this lib.
Below is a light version (most error management has been removed) of this program to make it clearer:
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <calccables.h>

static void print_lc_error(int errnum)
{
  char *msg;

  calccables_error_get(errnum, &msg);
  fprintf(stderr, "Link cable error (code %i)...\n<<%s>>\n", 
    errnum, msg);

  g_free(msg);
}

int main(int argc, char **argv)
{
  CableHandle *handle;
  int err, i;
  uint8_t buf[4];

  // init lib
  calccables_library_init();

  // set cable
  handle = calccables_handle_new(CABLE_SLV, PORT_1);
  if(handle == NULL)
      return -1;

  calccables_options_set_timeout(handle, 15);
  calccables_options_set_delay(handle, 10);
  calccables_handle_show(handle);

  // open cable
  err = calccables_cable_open(handle);
  if(err) print_lc_error(err);
  if(err) return -1;

  // do a simple test with a TI89/92+ calculator
  buf[0] = 0x08; buf[1] = 0x68; buf[2] = 0x00; buf[3] = 0x00;    // RDY
  err = calccables_cable_send(handle, buf, 4);
  if(err) print_lc_error(err);

  // display answer
  memset(buf, 0xff, 4);
  err = calccables_cable_recv(handle, buf, 4);
  if(err) print_lc_error(err);

  for(i = 0; i < 4; i++)
    printf("%02x ", buf[i]);
  printf("\n");

  // close cable
  calccables_cable_close(handle);

  // release handle
  calccables_handle_del(handle);

  // exit lib
  calccables_library_exit();

  return 0;
}
Compile this program with:
gcc -Os -g -Wall -Wextra `pkg-config --cflags --libs calccables` example.c -o example
That's all!

Return to the main index