JuliaIO/LibSerialPort.jl

@async behavior

Opened this issue · 3 comments

using LibSerialPort

list_ports()
ports = get_port_list()

sp = LibSerialPort.open(ports[1], 115200)
set_flow_control(sp)
sp_flush(sp, SP_BUF_BOTH)

@async while isopen(sp)
    while bytesavailable(sp) > 0
        data = readline(sp)
        println(stdout,  "read :$data")
    end
end

I don't know why this blocked REPL. How can I keep receiving while running other codes.

LibSerialPort.jl does not currently support asynchronous I/O in the way in which Julia uses libuv for other types of I/O to enable multi-tasking within a single thread. LibSerialPort.jl calls the blocking C functions of libserialport and these will block the entire task.

By default, Julia starts up with a single thread of execution, and if libserialport blocks that thread, there is no thread left to run the REPL. Therefore you'd need at least two threads to be able to use a blocking function in libserialport and the REPL concurrently. LibSerialPort.jl is not currently using @threadcall, and neither is it trying to take care of thread safety (#69).

I have wondered whether libserialports is the right vehicle to implement proper multi-threaded serial-port communication for Julia, or whether this shouldn't be done instead on top of libuv, like all the other file IO. (After all, the operating systems present serial ports as just special files with some additional settings.)

Any update on these thoughts?