automaton
automaton

Reputation: 1121

Reading from device files

Is there a specific approach to reading device files in CL? I try the following code in SBCL but it does not appear to work:

(defparameter modem #p"/dev/ttyUSB2")
(defun read-modem()
  (with-open-file (fd modem :direction :io :if-exists :append)
          (loop while (peek-char nil fd) do
            (format t "~A" (read-line fd))
            (finish-output fd))))

I know there's output because cat /dev/ttyUSB2 shows it.

Upvotes: 1

Views: 197

Answers (2)

sds
sds

Reputation: 60062

I think your problem is with buffering. I don't think you can turn it off in CL open, so I am afraid you have to use sb-unix:unix-open and sb-unix:unix-read.

Upvotes: 2

Vsevolod Dyomkin
Vsevolod Dyomkin

Reputation: 9451

I guess, you need to read from them as from binary files. For instance, here's what I read from /dev/urandom:

> (with-open-file (fd "/dev/urandom" :direction :io :if-exists :append
                      :element-type 'unsigned-byte) 
    (read-byte fd))
161

Upvotes: 2

Related Questions