Re: Non-blocking read of serial device
I found a solution, thanks to this discussion about Clozure CL:
http://clozure.com/pipermail/openmcl-devel/2012-November/013904.html
The important fact seems to be:
> A FILE-STREAM is assumed to never block (e.g, reading from it will always return some data or EOF immediately.)
Assuming it also applies for LispWorks and OS X.
Based on this, instead of using OPEN, I used: (SYS:OPEN-PIPE "cat /dev/cu.usbmodem8931" :buffered nil)
And I can now safely use LISTEN within the process wait function.
Best,
Cam
(defvar *device-path* "/dev/cu.usbmodem8931")
(defun serial-loop (stream)
(mp:ensure-process-cleanup `(serial-cleanup ,stream))
(loop :do
(when (mp:wait-processing-events 100
:wait-function #'listen
:wait-args (list stream))
(with-simple-restart (abort "Return to event loop.")
(let ((byte (read-byte stream)))
(packet (funcall decoder byte)))))))
(defvar *process* nil)
(defun serial-cleanup (process stream)
(close stream)
(setf *process* nil))
(defun serial-start (&optional (device-path *device-path*))
(assert (null *process*))
(let ((stream (sys:open-pipe (format nil "cat ~A" device-path)
:buffered nil
:element-type 'unsigned-byte)))
(setf *process* (mp:process-run-function
"Serial Reader"
'(:mailbox t)
#'serial-loop
stream))))
(defun serial-stop ()
(mp:process-send *process* `(mp:process-kill ,*process*))
(mp:process-join *process*))
On 13 juin 2013, at 22:06, Camille Troillard <camille@osculator.net> wrote:
>
> Hello,
>
> Using OPEN, I can read the bytes from a serial line synchronously.
>
> I am now attempting to encapsulate this process in a separate process.
>
> I can't seem to find any solution to make my process wait as long as there is nothing to read on my opened stream. I tried PROCESS-WAIT with a wait-function that would check if there is something to read with LISTEN or READ-CHAR-NO-HANG, unfortunately both of them are blocking within the wait function (which happens I believe because the stream opened with OPEN is not set to non-blocking).
>
> Can someone help me find a solution?
> Thanks in advance!
>
>
> Best,
> Cam
>
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> lisp-hug@lispworks.com
> http://www.lispworks.com/support/lisp-hug.html
>
_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html