Exemple #1
0
void setup_port()
{
  int result;

  result = sp_set_baudrate(piksi_port, 1000000);
  if (result != SP_OK) {
    fprintf(stderr, "Cannot set port baud rate!\n");
    exit(EXIT_FAILURE);
  }

  result = sp_set_flowcontrol(piksi_port, SP_FLOWCONTROL_NONE);
  if (result != SP_OK) {
    fprintf(stderr, "Cannot set flow control!\n");
    exit(EXIT_FAILURE);
  }

  result = sp_set_bits(piksi_port, 8);
  if (result != SP_OK) {
    fprintf(stderr, "Cannot set data bits!\n");
    exit(EXIT_FAILURE);
  }

  result = sp_set_parity(piksi_port, SP_PARITY_NONE);
  if (result != SP_OK) {
    fprintf(stderr, "Cannot set parity!\n");
    exit(EXIT_FAILURE);
  }

  result = sp_set_stopbits(piksi_port, 1);
  if (result != SP_OK) {
    fprintf(stderr, "Cannot set stop bits!\n");
    exit(EXIT_FAILURE);
  }
}
Exemple #2
0
int navilink_open_sp_port(struct sp_port* port, NavilinkDevice* device)
{
  enum sp_return result = sp_open(port, SP_MODE_READ_WRITE);
  if (result != SP_OK) {

    CATCH_LIBSERIAL_ERROR(device);
    goto error_cleanup_port;
  }

  struct sp_port_config* config = NULL;
  result = sp_new_config(&config);
  if (result != SP_OK) {
    CATCH_LIBSERIAL_ERROR(device);
    goto error_clean_config;
  }

  // Set the config
  sp_set_baudrate(port, 115200);
  sp_set_bits(port, 8);
  sp_set_parity(port, SP_PARITY_NONE);
  sp_set_stopbits(port, 1);
  sp_set_flowcontrol(port, SP_FLOWCONTROL_NONE);

  sp_set_config(port, config);

  // Allocate events buffer
  struct sp_event_set* event_set = NULL;
  sp_new_event_set(&event_set);
  sp_add_port_events(event_set, port, SP_EVENT_TX_READY);

  //Wait for the port to be ready
  result = sp_wait(event_set, 5000);
  if (result != SP_OK) {

    CATCH_LIBSERIAL_ERROR(device);

    goto error_clean_event_set;
  }

  device->serial_port = port;
  device->event_set = event_set;

  //Check that this is a Navilink device
  int res = navilink_check_protocol(device);
  if (res < 0) {
    goto error_clean_event_set;
  }

  // Retrieve the informations about the device
  res = navilink_query_information(device, &device->informations);
  if (res < 0) {
    goto error_clean_event_set;
  }
  // Retrieve the firmware version
  res = navilink_query_firmware_version(device, &device->firmware_version);

  if (res < 0) {
    goto error_clean_event_set;
  }
  return 0;
error_clean_event_set:
  sp_free_event_set(event_set);
error_clean_config:
  sp_free_config(config);
error_cleanup_port:
  sp_free_port(port);
  return -1;
}