Serial port library

29 views
Skip to first unread message

Bram Roelandts

unread,
Aug 22, 2018, 5:28:22 AM8/22/18
to luvit
Hi

I've been using Luvit for quite some time and it's a great fit for my project. Sadly though, there's no serial port library available at the moment. Since I really need the serial port, I was hoping you guys could give me a few pointers.

I've come across a thread from 2016 about serial support (https://groups.google.com/d/msg/luvit/m_bJes9J2p0/u2hi21_4CQAJ), but I was unable to run the posted gists. Someone apparently did get the serial port working, but didn't open-source it. The guy said they used "a mixture of Luvit, C and FFI. FFI for read and write, C for line monitoring threads and Luvit (uv.poll for events on the port)", which already gives a basic idea.

I'm quite comfortable working with Luvit by now, but I lack knowledge to write a library myself using libuv and FFI. How would I go about this? Should I read up on libuv and FFI and try to hack something together? What are some good resources to get started? I'm eager to learn, but I simply wouldn't know where to start on this one.

Thanks in advance!

Kind regards,
Bram

Tim Caswell

unread,
Aug 22, 2018, 12:00:16 PM8/22/18
to lu...@googlegroups.com
It's not too hard with FFI.  The closest docs are this article: http://luvit.io/blog/pty-ffi.html

At a past job, I did a lot of serial stuff with luvit.  Luckilly, it's open source and can be found at https://github.com/kubos/kubos/blob/15f8bd97c080d1f34c8b2cf93c9cfcecb7571e9a/services/communication-service/libs/termios-serial.lua

--
You received this message because you are subscribed to the Google Groups "luvit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to luvit+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Caswell

unread,
Aug 22, 2018, 12:02:13 PM8/22/18
to lu...@googlegroups.com
Using is fairly simple, you just open the serial device using the built-in fs and run the ffi functions on the FD:


    local fd = assert(fs.open(device, mode))
    set_termio(fd, baud)

Bram Roelandts

unread,
Aug 23, 2018, 3:44:40 AM8/23/18
to luvit
Thanks a lot for the open-source serial library! 

While trying to run it, however, I ran into a problem with fs. I'm unsure whether I'm supposed to use coro-fs or the standard luvit fs, but neither of them works without error. I've posted the code I'm using and the errors I'm getting on GitHub: https://gist.github.com/brammahh/2b70dfefb7728d7d9e712cdee410ba45

Your help is really appreciated!

Tim Caswell

unread,
Aug 23, 2018, 8:22:21 AM8/23/18
to lu...@googlegroups.com
The transport code is a kubos specific thing, I just meant it as an example.

The coro error you're getting is because you can't use coroutine libraries on the main thread.  Either use Lua's coroutine.create or coroutine.wrap.

Bram Roelandts

unread,
Aug 24, 2018, 4:37:35 PM8/24/18
to luvit
Coroutine.wrap indeed did the job. After some fiddling with hardware level conversion, I was able to test the example you provided and it works like a charm. Thanks for your help, and also for the wide variety of Luvit code you open-sourced on GitHub! It really helps as a reference.
Reply all
Reply to author
Forward
0 new messages