# 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:** 4\
**Page:** 2

<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, 9:26pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/21 "2017-09-07T21:26:51Z")

</div>

> [@Frank\_Applin](#):
>
> 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?

You originally had termios be a immutable type, so no C code can mutate the variable `tty` which is of an immutable type (see the implication of immutable type I give above). If you keep it as immutable you can embed it in a mutable type (the common one used being `Ref`) which will allow the C code to mutate it (much the same as embedding the tuple in the mutable `termios` allows C code to mutate the tuple content). You can (have) achieve(d) the same by making `termios` itself a mutable type.

> [@Frank\_Applin](#):
>
> message[end] = 0; # ensure null-termination

I think you wanted `message[buffer + 1] = 0`

> [@Frank\_Applin](#):
>
> println(unsafe\_string(pointer(message)))

This is invalid although it shouldn’t be the problem. You should resize the `message` and call `String` on it.

So you should write, roughly

```julia
message = Vector{UInt8}(bufSize)
buffer = ccall((:read, "libc"), Int32, (Int, Ptr{UInt8}, Csize_t), fd, message, sizeof(message))
resize!(message, buffer)
println(String(message))

```

---

<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:28pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/22 "2017-09-07T21:28:47Z")

</div>

So, can I ask you where you learn all of this from. I know I’m new to Julia, but you’re like amazing with your knowledge of it.

---

<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:35pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/23 "2017-09-07T21:35:59Z")

</div>

Well, that didn’t help the garbage, but thanks for helping me correct my code. I’ll go through it line by line versus the C code and see if I screwed something up.

---

<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, 10:18pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749/24 "2017-09-07T22:18:17Z")

</div>

> [@Frank\_Applin](#):
>
> So, can I ask you where you learn all of this from. I know I’m new to Julia, but you’re like amazing with your knowledge of it.

Well, not really all at the same place/through the same way and that’s a hard question in general…

> [@yuyichao](#):
>
> No it’s not a pointer in C, it’s an array.

By falling into the trap of the subtle difference between array and pointer in C mutiple time…

For most of the rest, they are from trying things myself, reading the doc and following github issues. For most of the issues mentioned here, it helps to have a clear model of the layout and the things that are allowed though.

I do agree that the doc needs improvements, not just for what each function does but also how they can be used. I myself is pretty bad at doing that though so I took the approach of answering questions and hoping one of the readers can do a better job contributing to the doc knowing what’s not obvious for them.

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