# Help with ccall to clib functions

**URL:** <https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [September 6, 2017, 7:44pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749 "2017-09-06T19:44:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 6, 2017, 7:44pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/1 "2017-09-06T19:44:53Z")

</div>

Hi,

I’m trying to implement some routines from termios in clib. I have created the constants to represent the many #defines in the c header file, but I need help with a couple items (please). So, here’s a bit of the code:

```julia
@ctypedef tcflag_t Culong
@ctypedef cc_t Cuchar
@ctypedef speed_t Clong

struct termios
c_iflag::tcflag_t #input flags
c_oflag::tcflag_t #output flags
c_cflag::tcflag_t #control flags
c_lflag::tcflag_t #local flags
c_cc::Ptr{cc_t} #control chars #this is what it looks like in C - cc_t c_cc[NCCS];
c_ispeed::speed_t #input speed
c_ospeed::speed_t #output speed
end

```

So, the first question is - am I translating an array of integers correctly in the struct?

The second question is - how do I initialize the struct (since it has an array in it)?

The C code looks like this:

```julia
struct termios tty;
memset(&tty, 0, sizeof(tty));

```

this would be followed by:

```julia
if (tcgetattrib(fd, &tty) != 0)
{
printf(“error from tcgetattrib”);
return -1;
}

```

I translated that to:

```julia
tty = termios(0,0,0,0,0,0,0) #I think the initialization is wrong

ret = ccall((:tcgetattr, “libc”), Int32, (Int32, Ref{termios}), fd, tty)

if (ret != 0)
println(“error from tcgetattr”)
return -1
end

```

Can someone help me along, here?

Frank

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 6, 2017, 7:53pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/2 "2017-09-06T19:53:15Z")

</div>

> [@Frank\_Applin](#):
>
> c\_cc::Ptr{cc\_t} #control chars #this is what it looks like in C - cc\_t c\_cc[NCCS];

No it’s not a pointer in C, it’s an array. That corresponds to a inlined tuple.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 6, 2017, 7:58pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/3 "2017-09-06T19:58:20Z")

</div>

> [@Frank\_Applin](#):
>
> ret = ccall((:tcgetattr, “libc”), Int32, (Int32, Ref{termios}), fd, tty)

Also, `tty` needs to be a `Ref` of `termios` for you to get the return value.

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 6, 2017, 8:05pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/4 "2017-09-06T20:05:56Z")

</div>

OK. What is an inlined tuple?

```julia
macro ctypedef(fake_t,real_t)
  quote
    const $(esc(fake_t)) = $(esc(real_t))
  end
end

@ctypedef tcflag_t Culong
@ctypedef cc_t Cuchar
@ctypedef speed_t Clong

```

How would I re-write cc\_t c\_cc[NCCS] as an inlined tupe?

Thanks.

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 6, 2017, 8:10pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/5 "2017-09-06T20:10:33Z")

</div>

I thought that’s what I was doing.

```julia
ret = ccall((:tcgetattr, “libc”), Int32, (Int32, Ref{termios}), fd, tty)

```

I thought the 1st Int32 represented the return value and (Int32, Ref{termios}), fd, tty represented the 2 parameters. Am I not understanding this right?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 6, 2017, 8:25pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/6 "2017-09-06T20:25:01Z")

</div>

> [@Frank\_Applin](#):
>
> How would I re-write cc\_t c\_cc[NCCS] as an inlined tupe?

Just a `NTuple{NCCS,cc_t}`

Also, please quote your code with ``` or `````

> [@Frank\_Applin](#):
>
> I thought the 1st Int32 represented the return value and (Int32, Ref{termios}), fd, tty represented the 2 parameters. Am I not understanding this right?

You are explaining the `ccall` syntax here but that’s unrelated. The calling convention is correct but the result of the `ccall` won’t be seen by the variable `tty` since the `ccall` cannot mutate it. In general, nothing other than assignments can change the binding of a variable, only mutate the object they are bound to. This is completely different from C where the two are the same thing (assignment is mutation in C/C++). What you need is a mutable object and let the `ccall` mutate that object so you need to make `tty` a `Ref` of `termios` instead of just `termios`

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 6, 2017, 8:34pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/7 "2017-09-06T20:34:03Z")

</div>

OK. I’ll give it a try. I really like Julia so far, but man the documentation needs a Microsoft touch. Yes - I was a Windows developer for years. I am a full-fledged Linux guy, now, but Microsoft did create good documentation with lots of example code.

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 12:32pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/8 "2017-09-07T12:32:43Z")

</div>

Yuyuchao - I was thinking about this all before I went to bed, last night. This is the code [not working yet].

```julia
const NCCS = 20

@ctypedef tcflag_t Culong
@ctypedef cc_t Cuchar
@ctypedef speed_t Clong

mutable struct termios
    c_iflag::tcflag_t	#input flags
	c_oflag::tcflag_t	#output flags
	c_cflag::tcflag_t	#control flags
	c_lflag::tcflag_t	#local flags
        c_cc::NTuple{NCCS,cc_t} #control chars
	c_ispeed::speed_t	#input speed
	c_ospeed::speed_t	#output speed 
end

function set_interface_attribs(fd::Int, speed::Int, parity::Int)
        tty = termios(0,0,0,0,(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),0,0) 

        ret = ccall((:tcgetattr, "libc"), Int32, (Int32, Ref{termios}), fd, tty)

        if (ret != 0)
                println("error from tcgetattr")
                return -1
        end

        ccall((:cfsetospeed, "libc"), Void, (Ref{termios}, Int32), tty, speed)
        ccall((:cfsetispeed, "libc"), Void, (Ref{termios}, Int32), tty, speed)

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8 # 8-bit chars
        # disable IGNBRK for mismatched speed tests otherwise receive break
        # as \000 chars
        tty.c_iflag &= ~IGNBRK # disable break processing
        tty.c_lflag = 0 # no signaling chars, no echo,
                                        # no canonical processing
        tty.c_oflag = 0 # no remapping, no delays
        tty.c_cc[VMIN] = 0 # read doesn't block
        tty.c_cc[VTIME] = 5 # 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY) # shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD)# ignore modem controls,
                                        # enable reading
        tty.c_cflag &= ~(PARENB | PARODD) # shut off parity
        tty.c_cflag |= parity
        tty.c_cflag &= ~CSTOPB
        tty.c_cflag &= ~CRTSCTS

        ret = ccall((:tcsetattr, "libc"), Int32, (Int32, Int32, Ref{termios}), fd, TCSANOW, tty)
        if (ret != 0) 
                println("error from tcsetattr")
                return -1
        end
        return 0
end

```

If part of struct that is getting passed in to the routines above [tcgetattrib and tcsetattrib] is an immutabe NTuple - then how does the C library calls make modifications to that part of the struct? This is all very confusing compared to making system calls from other languages. Can you explain a little better what is going on in my calls to tcgetattrib and tcsetattrib and is there a better way of doing what I’m trying to accomplish?

BTW - the goal of this code is so I can do away with two C programs that I wrote that I am calling from Julia that allow me to interact with a serial port [connected to an Arduino]. The serial libraries don’t work because I am using 32-bit versions of Julia on several development boards [RPi, Beaglebone, Udoo x86] on a Beowulf cluster.

Frank

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 7, 2017, 1:34pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/9 "2017-09-07T13:34:07Z")

</div>

> [@Frank\_Applin](#):
>
> If part of struct that is getting passed in to the routines above [tcgetattrib and tcsetattrib] is an immutabe NTuple - then how does the C library calls make modifications to that part of the struct? This is all very confusing compared to making system calls from other languages. Can you explain a little better what is going on in my calls to tcgetattrib and tcsetattrib and is there a better way of doing what I’m trying to accomplish?

In julia what you are defining is not some structure for the C interface to interpret but an type with exactly the same layout as C so as long as that’s satisfied the C code can operate on it in exactly the same way it operates on C memory (read and write for isbits type, read for others). So in short, what happens is that you defined a mutable type with C compatible layout and the function you call mutates it.

Immutable field type does not mean that you can’t change the content of the field. Immutabale type does mean (not all of them are relavant for C interop but I’m just listing them here for completion)

1. You can’t change the content without changing the object identity.

2. Since you can’t change the object identity of a variable without assignment, you can’t change the “content of a variable” (in C sense) without explicity assignment.

3. As an ABI, isbits immutable type (including `primitive type`) are stored inline in parent `struct`, similar to a normal (non-pointer) field in C. Everything else are stored as references (pointer to object in C).

4. For struct definition, mutability (and isbits) of field type is just a ABI specifier that affect the layout. it doesn’t affect whether the field can be mutated since,

> [@Frank\_Applin](#):
>
> ```julia
> tty.c_cc[VMIN] = 0 # read doesn't block
> tty.c_cc[VTIME] = 5 # 0.5 seconds read timeout
> 
> ```

There’s currently no easy way to do this in julia without unsafe code. You have to assign to the whole tuple for now.

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 1:46pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/10 "2017-09-07T13:46:11Z")

</div>

Thank you for taking the time to explain this all. It is starting to make sense. I found out already that the assignment of the NTuple didn’t work (because of it being immutable). I guess I will have to create the NTuple (like a constructor) and fill in the values there. I’ll let you know if I get it all working! 🙂

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 2:57pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/11 "2017-09-07T14:57:18Z")

</div>

OK, it seems to be working. I am communicating through the serial port in Julia to my Arduino!!! Thanks for your time helping me. One more quick question.

I have translated:

```julia
tty.c_cc[VMIN] = 0 # read doesn't block
tty.c_cc[VTIME] = 5 # 0.5 seconds read timeout

```

to:

```julia
 tty.c_cc = (0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

```

because I know what VMIN and VTIME are. How can I fill in this NTuple using VMIN and VTIME as positions into the NTuple?

Frank

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 7, 2017, 3:35pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/12 "2017-09-07T15:35:50Z")

</div>

There’s no clean way to do it currently (Ref [WIP: Make mutating immutables easier by Keno · Pull Request #21912 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/21912)). For now, and for tuples you can do `ntuple(x->x == VTIME ? cc_t(5), tty.cc[x], Val{NCCS})`.

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 3:43pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/13 "2017-09-07T15:43:48Z")

</div>

I was just reading about that in the Docs and trying to get it to work. Thanks for the example!

That’s what we need a Julia In A Nutshell book and a Julia Examples In A Nutshell companion book! 😉

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 6:23pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/14 "2017-09-07T18:23:01Z")

</div>

So, yuyichao, is there a way to convert a string like “(0,0,0,5,0,0)” to a Tuple? Your answer using ntuple() will only work for one argument. I have a function to create a string with as many arguments as I need, but then I need that string to convert to a tuple. Any ideas?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 7, 2017, 6:36pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/15 "2017-09-07T18:36:04Z")

</div>

Parsing is something you should do separately (I assume you don’t just do `int v = string;` in C either ;-p ). You generally need to write a parser yourself possibly making use of the simple parsing functions provided in Base (e.g. `parse(::Type{Int}, ::String)`).

There is a `parse(::String)` function that parses strings in julia syntax. It’s useful for hacking and local use but you shouldn’t use it like this in any “production” code (whatever tha means).

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 6:47pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/16 "2017-09-07T18:47:24Z")

</div>

Well, this seemed to work:

```julia
julia> t = eval(parse(initialize_NTuple("6,0,5,5"); raise=true))
(0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

julia> typeof(t)
NTuple{20,Int32}

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 7, 2017, 6:49pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/17 "2017-09-07T18:49:37Z")

</div>

If you are just taking well defined and checked user input this is fine. Directly passing some command line/stdin input to this code will be dangerous.

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 7:00pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/18 "2017-09-07T19:00:12Z")

</div>

Oh, no - nothing like that. I just wanted to be able to take something like:

```julia
tty.c_cc[VMIN] = should_block ? 1 : 0
tty.c_cc[VTIME] = 5 # 0.5 seconds read timeout

```

and create a string using VMIN, should\_block ? 1 : 0 and VTIME, 5. This I can do in the code and pass it to my function.

Thank you again for all of your help. This is all part of something that I would like to present at the next Julia Conference. I am doing remote calls to a Raspberry Pi, Beaglebone Black, and a Udoo X86 - each equipped with sensors wired to their GPIO pins [and running on a cluster]. I do the remote calls in a loop from my main computer (also on the cluster], so I can monitor and be told when a sensor goes off on one of the development boards. The Udoo X86 has a built in Arduino 101 on the board and I was having to run small C programs from Julia to interact with the serial port on the Udoo. This now give me a Julia version without having to drop down to C at all.

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 9:08pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/19 "2017-09-07T21:08:26Z")

</div>

> [@yuyichao](#):
>
> ret = ccall((:tcgetattr, “libc”), Int32, (Int32, Ref{termios}), fd, tty)
> 
> Also, tty needs to be a Ref of termios for you to get the return value.

Ok. writing to the serial port is going fine, but I want to make sure I’m doing this right. You said tty needs to be a Ref of termios. Isn’t it already?

---

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 7, 2017, 9:13pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/20 "2017-09-07T21:13:45Z")

</div>

And, I’m working on read from the serial port - that’s what prompted my last questions. I doing a read kind of following an example from the docs on getting a hostname.

```julia
#char buf [100];
#int n = read (fd, buf, sizeof buf); # read up to 100 characters if ready to read

message = Vector{UInt8}(bufSize)
buffer = ccall((:read, "libc"), Int32, (Int, Ptr{UInt8}, Csize_t), fd, message, sizeof(message))
message[end] = 0; # ensure null-termination
println(unsafe_string(pointer(message)))

```

I just want to make sure I’m doing this right. When I use the actual C program - I get correct values from the serial port consistently, but from Julia - I get a lot of garbage,

Frank

[Next page](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749.md?page=2)
