LibSerialPort.jl icon indicating copy to clipboard operation
LibSerialPort.jl copied to clipboard

@async behavior

Open XinyuWuu opened this issue 4 years ago • 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.

XinyuWuu avatar Jul 19 '21 05:07 XinyuWuu

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).

mgkuhn avatar Jul 19 '21 16:07 mgkuhn

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.)

mgkuhn avatar Jul 19 '21 16:07 mgkuhn

Any update on these thoughts?

freemin7 avatar May 26 '23 14:05 freemin7