I'm trying to call out to some library functions from libserialport 
<http://sigrok.org/wiki/Libserialport>. It is a small c library that makes 
extensive use of opaque structs in the API. In c I can do the following:

#include <libserialport.h>
#include <stdio.h>


// Compile:
// gcc -o example example.c $(pkg-config --cflags --libs libserialport)


int main(int argc, char *argv[])
{
  char *uri = argv[1];
  struct sp_port *port;


  if (sp_get_port_by_name(uri, &port) == SP_OK)
  {
    char *portname = sp_get_port_name(port);
    char *portdesc = sp_get_port_description(port);
    printf("Port name: %s\n", portname);
    printf("Description: %s\n", portdesc);
  }
  else
    printf("Could not find %s\n", uri);
  return 0;
}


As an example, if I run this with an ARM STM32 microcontroller plugged in 
to my USB port, I get the following correct output (on OS X):

$ ./example /dev/cu.usbmodem1413
Port name: /dev/cu.usbmodem1413
Description: STM32 STLink

I want to call sp_get_port_by_name from Julia. It has this signature:

enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **
port_ptr);

The ccall docs advise declaring an empty type as a placeholder for an 
opaque struct, then passing in a reference to it. I've tried many wrong 
things, such as the following:

julia> type Port end

julia> p = Ref{Port}()
Base.RefValue{Port}(#undef)

julia> ccall((:sp_get_port_by_name, "libserialport"),Cint,(AbstractString, 
Ref{Ref{Port}}),"/dev/cu.usbmodem1413",p)
0

julia> p
Base.RefValue{Port}(Segmentation fault: 11


Any tips? The double pointer is causing me some confusion.

Andrew

Reply via email to